k8s tutorial - Learn Kubernetes

2021-07-04  本文已影响0人  老杜振熙

流程汇总

  1. 创建cluster
  2. 在cluster上创建一个deployment,然后再部署对应的APP(因为创建deployment时会指定image)
  3. 当创建了Deployment之后,系统会创建对应的pod,视为一组紧密耦合的APP的逻辑主机
  4. 将pod视为一个逻辑主机之后,就很清晰了,kubectl提供了一系列接口以和pod进行通信以及运行APP(这里需要注意Pod和Node的区别)
  5. pod因为只能运行在单个Node上,所以是很脆弱的,因此需要在此基础上创建service;同时,service使得APP能够expose到外部网络)

Learn Kubernetes Basics -- Create a Cluster

Kubernetes automates the distribution and scheduling of application containers across a cluster in a more efficient way.

核心理念:使应用程序容器化,这样可以使得程序和主机解耦,程序不需要绑定到特定主机,而是通过k8s在cluster上进行调度编排。

cluster的核心组件有两个:

Cluster Diagram

每个node都需要有一个kubelet,kubelet是node的代理,用来管理node,以及和Control Plane进行通信。

To get started with Kubernetes development, you can use Minikube. Minikube is a lightweight Kubernetes implementation that creates a VM on your local machine and deploys a simple cluster containing only one node.

使用minikube流程:

$ minikube start # minikube启动一个VM,并创建一个cluster
$ kubectl cluster-info # 查看当前cluster的info
$ kubectl get nodes # 查看当前cluster中的各个nodes

Learn Kubernetes Basics -- Deploy an APP

Cluster

The Deployment instructs Kubernetes how to create and update instances of your application. (除此以外,Deployment为k8s提供了自愈机制,当部署了某个容器化应用的Node挂了之后,Deployment会对Node进行替换)

When you create a Deployment, you'll need to specify the container image for your application and the number of replicas that you want to run.(再创建Deployment的时候,需要指定APP的镜像,以及需要运行多少个replicate)

A Pod is the basic execution unit of a Kubernetes application. Each Pod represents a part of a workload that is running on your cluster.

Deployment创建成功

Learn Kubernetes Basics -- Explore Your App

当创建了deployment之后,K8S会创建一个pod,作为各个紧密耦合的APP的逻辑主机。

A Pod is a Kubernetes abstraction that represents a group of one or more application containers (such as Docker), and some shared resources for those containers.(可以把pods视为一个逻辑主机,其可以包括多个容器)

The containers in a Pod share an IP Address and port space, are always co-located and co-scheduled, and run in a shared context on the same Node.(作为一个逻辑主机,pod拥有自己的IP和端口,主机内部的各个容器应用协同工作,紧密耦合)

Pods Overview

前面已经说过,Nodes可以是物理机或者VM。而一个Node上面可以运行多个Pods。Pods是一个抽象的概念,用以聚类多个容器及共享资源;一个pod只能运行在一个Node上面。一个Node必须包含以下两个要素:

Nodes Nodes Overview

Learn Kubernetes Basic -- Expose Your App Publicly

Kubernetes Pods are mortal. Pods in fact have a lifecycle. When a worker node dies, the Pods running on the Node are also lost. (即,Pod是容易down掉的,只要一个node无效了,在这个node上运行的所有pod都会down掉)

the front-end system should not care about backend replicas or even if a Pod is lost and recreated. That said, each Pod in a Kubernetes cluster has a unique IP address, even Pods on the same Node, so there needs to be a way of automatically reconciling changes among Pods so that your applications continue to function.(类似主从的数据同步,提高容错性)

A Service in Kubernetes is an abstraction which defines a logical set of Pods and a policy by which to access them(即,将多个Pod抽象为一个service,从而使得这个service具备容错性容灾性,外部用户无需关注service内部的多个Pod是怎么运行的,以及各自之间的主从关系;注意,同一个service下的各个pods是松耦合的)

The set of Pods targeted by a Service is usually determined by a LabelSelector(必须使用labelSelector去标记同一个service下的各个pods)

Although each Pod has a unique IP address, those IPs are not exposed outside the cluster without a Service. Services allow your applications to receive traffic. (只有在Pod之上构建一层service的时候,服务才能够不局限于cluster)

Services can be exposed in different ways by specifying a type in the ServiceSpec.(将service暴露于外部网络有几种方式:ClusterIP:仅在集群内部暴露服务;NodePort:使用NAT技术,将service暴露在选定Node的相同端口上,外部网络可以通过NodeIP:NodePort访问service;LoadBalancer; ExternalName

A ReplicaSet's purpose is to maintain a stable set of replica Pods running at any given time(即,维护多个Pods的稳定运行,容灾性)

Services match a set of Pods using labels and selectors, a grouping primitive that allows logical operation on objects in Kubernetes.(即,Service通过label和selector来匹配一组Pods)

Service Overview,可以看出,一个service可以使cluster中的多个pods松耦合,以及replica 新建一个NodePort类型的service 将service暴露到外部端口NodePort之后,就可以对其内的APP进行访问了 Label还可以在删除服务的时候起到作用(注意,此处只是删除了service,但是cluster内部的APP(Pods)还没有删除,因此还是可以通过kubectl exec -it进行访问)

Learn Kubernetes Basics - Scale Your App

Scaling is accomplished by changing the number of replicas in a Deployment(通过增加replica的数量,也就是提升service中的pods的数量,从而扩展APP的服务能力,因为经过扩充之后,一个APP就可以拥有多个实例了,此外,这也使得APP的更新可以实时更新而不必经过一个downtime)

Learn Kubernetes Basics - Update Your App

Rolling updates allow Deployments' update to take place with zero downtime by incrementally updating Pods instances with new ones. The new Pods will be scheduled on Nodes with available resources.(可是无downtime式的更新

In Kubernetes, updates are versioned and any Deployment update can be reverted to a previous (stable) version.(可以很方便的回滚

上面两项也就是CI/CD

给之前的deployment更新一个镜像
上一篇 下一篇

猜你喜欢

热点阅读