一、RC 与 RS
RC全称为ReplicationController(复制控制器),RS全称为ReplicaSet(复制集)
RC和RS是K8S中两种简单部署pod的方式,但是由于在生产环境中,主要使用更高级的Deployment等方式对pod进行部署、运行、管理,所以这里只对RC和RS进行简单介绍
- Replication Controller(RC)
使用RC可以使Pod副本数达到指定数量,确保一个Pod或者一组同类Pod总是可用的
如果存在的Pod大于了设置的数量,那么RC将会终止多余的Pod,相反,如果小于设置的数量,RC则会启动一些Pod以达到设置的数量
和手动创建Pod不同,使用RC维护的Pod在失败、删除、终止时会自动替换,因此就算所需的应用程序只需要一个Pod,也应该使用RC或者其他的方式进行管理
RC类似于进程管理程序,但是RC不是监视单个节点上的各个进程,而是监视多个节点上的多个Pod
定义一个RC的示例:
apiVersion: v1 kind: ReplicationController metadata: name: nginx spec: replicas: 3 selector: app: nginx template: metadata: name: nginx labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80
- ReplicaSet(RS)
RS是支持基于集合的标签选择器的新一代RC,RS主要作用于Deployment协调创建、删除、更新Pod,和RC的区别是,RS支持标签选择器
在实际应用中,RS可以单独使用,但是一般使用Deployment来自动管理RS,除非自定义的Pod不需要更新或者其他编排等
定义一个RS的示例:
apiVersion: apps/v1 kind: ReplicaSet metadata: name: frontend labels: app: test sepc: replicas: 3 selector: matchLabels: app: test matchExpressions: - {key: tier,operator: ln,values: [frontend]} template: metadata: labels: app: test sepc: containers: - name: redis images: redis:v1.0 resources: requests: cpu: 100m memory: 100Mi env: - name: GET_HOSTS_FROM value: dns ports: - containerPort: 6379
- RC和RS的创建删除和Pod并没有太大的区别,并且RC在实际生产环境基本已经不再使用,RS也很少单独使用,一般都会根据需求使用更高级的Deployment、DaemonSet、StatefulSet来管理Pod
二、Deployment
Deployment主要用于部署无状态的服务,是最常用的Pod控制器,一般用于管理公司中无状态的微服务,例如Config-Server注册中心、Zuul网关、SpringBoot微服务框架等,Deployment可以管理多个副本的Pod,实现无缝迁移、自动扩容缩容、自动灾难恢复、一键回滚功能等
注释:有状态服务与无状态服务:
- 有状态服务
1. 服务本身依赖或者存在局部的状态数据,这些数据需要自身持久化或者可以通过其他节点恢复。 2. 一个请求只能被某个节点(或者同等状态下的节点)处理。 3. 存储状态数据,实例的拓展需要整个系统参与状态的迁移。 4. 在一个封闭的系统中,存在多个数据闭环,需要考虑这些闭环的数据一致性问题。 5. 有状态服务通常存在于分布式架构中。
- 无状态服务
1. 服务不依赖自身的状态,实例的状态数据可以维护在内存中。 2. 任何一个请求都可以被任意一个实例处理。 3. 不存储状态数据,实例可以水平拓展,通过负载均衡将请求分发到各个节点。 4. 在一个封闭的系统中,只存在一个数据闭环。 5.无状态服务通常存在于单体架构的集群中。
虽然RS可以确保在任何给定时间运行的Pod副本数达到指定数量,但是Deployment是一个更高级的概念,它会管理RS,并且为Pod喝RS提供声明性更新,以及许多其他功能呢,一般实际工作中,会使用Deployment代替RS
如果在Deployment对象中描述了所需要的状态,那么Deployment控制器就会以可控制的速率将实际状态更改为期望状态,也可以在Deployment中创建RS,或者删除现有的Deployment并使用新的Deployment部署所需要的资源
- 创建Deployment
[root@master1 ~]# kubectl create deployment nginx --image=nginx #创建deployment deployment.apps/nginx created [root@master1 ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-f89759699-fjwr8 0/1 ContainerCreating 0 3s [root@master1 ~]# kubectl get deployment #查看deployment的pod NAME READY UP-TO-DATE AVAILABLE AGE nginx 1/1 1 1 39s [root@master1 ~]# kubectl get deployment nginx -o yaml > nginx.yaml #导出deployment的yaml文件,用于修改 [root@master1 ~]# ll total 8 -rw-------. 1 root root 1259 Feb 23 17:13 anaconda-ks.cfg -rw-r--r-- 1 root root 3703 Jun 22 09:59 nginx.yaml
- 使用
kubectl replace
命令,参数-f
指定yaml或json文件,替换资源(类似于重载配置)
[root@master1 ~]# kubectl replace -f nginx.yaml deployment.apps/nginx replaced
使用kubectl edit命令,可以直接编辑、修改资源,edit命令会打开KUBE_EDITOR、GIT_EDITOR或者EDITOR环境变量定义的编辑器,可以同时编辑多个资源,但是编辑过的资源只会一次性提交,默认是YAML格式,加参数-o json可以以JSON格式编辑
[root@master1 ~]# kubectl edit deployment nginx #进入编辑模式后与vim类似,修改完后会直接生效 Edit cancelled, no changes made.
下面来创建一个Deployment
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.14-alpine ports: - containerPort: 80 #注释: 示例解析: kind:指定类型为Deployment的控制器 第一个metadata是定义Deployment的名称与标签 第一个sepc定义Deployment的资源 replicas:定义pod的副本数 template定义pod的详细信息 template下的spec定义容器的详细信息
- 创建此Deployment
[root@master1 ~]# kubectl create -f nginx.yaml deployment.apps/nginx-deployment created [root@master1 ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deployment-6b75f4cbdd-8nmlv 1/1 Running 0 42m nginx-deployment-6b75f4cbdd-f56z8 1/1 Running 0 39m nginx-deployment-6b75f4cbdd-llhht 1/1 Running 0 42m [root@master1 ~]# kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE nginx-deployment 3/3 3 3 42m [root@master1 ~]# kubectl get deploy -o wide #查看deployment的信息 NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx-deployment 3/3 3 3 43m nginx nginx:1.14-alpine app=nginx #注释: name:集群中deployment的名称 ready:pod的状态,已经ready的pod个数 up-to-date:显示已经达到期望状态的副本数 available:显示用户可以使用的应用程序副本数 age:显示应用程序运行的时间 containers:容器的名称 images:镜像名称 selector:管理的pod标签
- 查看pod标签
[root@master1 ~]# kubectl get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-deployment-6b75f4cbdd-8nmlv 1/1 Running 0 50m app=nginx,pod-template-hash=6b75f4cbdd nginx-deployment-6b75f4cbdd-f56z8 1/1 Running 0 47m app=nginx,pod-template-hash=6b75f4cbdd nginx-deployment-6b75f4cbdd-llhht 1/1 Running 0 50m app=nginx,pod-template-hash=6b75f4cbdd #注释 标签中'pod-template-hash=6b75f4cbdd'的这个值,表示RS的hash哈希值
- 查看RS
[root@master1 ~]# kubectl get rs NAME DESIRED CURRENT READY AGE nginx-deployment-6b75f4cbdd 3 3 3 52m
- 看此时Deployment rollout的状态:
[root@master1 ~]# kubectl rollout status deployment/nginx-deployment #查看资源状态 deployment "nginx-deployment" successfully rolled out [root@master1 ~]# kubectl get deploy #查看deployment NAME READY UP-TO-DATE AVAILABLE AGE nginx-deployment 3/3 3 3 3h12m [root@master1 ~]# kubectl get rs #查看rs NAME DESIRED CURRENT READY AGE nginx-deployment-6b75f4cbdd 3 3 3 3h12m [root@master1 ~]# kubectl get pods --show-labels #查看deployment创建的pod NAME READY STATUS RESTARTS AGE LABELS nginx-deployment-6b75f4cbdd-8nmlv 1/1 Running 0 3h12m app=nginx,pod-template-hash=6b75f4cbdd nginx-deployment-6b75f4cbdd-f56z8 1/1 Running 0 3h10m app=nginx,pod-template-hash=6b75f4cbdd nginx-deployment-6b75f4cbdd-llhht 1/1 Running 0 3h12m app=nginx,pod-template-hash=6b75f4cbdd
- 更新Deployment
- 通常对应用程序的升级、版本迭代都会通过Deployment对Pod进行
滚动更新
- 下面来看示例:
[root@master1 ~]# kubectl get deploy #可以使用缩写进行查看 NAME READY UP-TO-DATE AVAILABLE AGE nginx-deployment 3/3 3 3 23h [root@master1 ~]# kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE nginx-deployment 3/3 3 3 23h #修改此deployment的镜像 [root@master1 ~]# kubectl set image deployment nginx-deployment nginx=nginxdemos/hello:plain-text --record ———————————————————————————————— #除了使用set image进行修改,也可以使用kubectl edit deployment nginx直接对yaml文件进行编辑 ———————————————————————————————— deployment.apps/nginx-deployment image updated [root@master1 ~]# kubectl rollout status deployment.v1.apps/nginx-deployment #查看状态 Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination... deployment "nginx-deployment" successfully rolled out [root@master1 ~]# kubectl get rs #查看rs NAME DESIRED CURRENT READY AGE nginx-deployment-5d6b8b588c 3 3 3 3m58s nginx-deployment-6b75f4cbdd 0 0 0 23h [root@master1 ~]# kubectl describe deployments.apps nginx-deployment #使用describe查看deployment的详细信息 Name: nginx-deployment Namespace: default CreationTimestamp: Thu, 23 Jun 2022 10:31:28 +0800 Labels: app=nginx Annotations: deployment.kubernetes.io/revision: 2 kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginxdemos/hello:plain-text --record=true Selector: app=nginx Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=nginx Containers: nginx: Image: nginxdemos/hello:plain-text Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: nginx-deployment-5d6b8b588c (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 7m8s deployment-controller Scaled up replica set nginx-deployment-5d6b8b588c to 1 Normal ScalingReplicaSet 6m47s (x2 over 23h) deployment-controller Scaled down replica set nginx-deployment-6b75f4cbdd to 2 Normal ScalingReplicaSet 6m47s deployment-controller Scaled up replica set nginx-deployment-5d6b8b588c to 2 Normal ScalingReplicaSet 6m46s deployment-controller Scaled down replica set nginx-deployment-6b75f4cbdd to 1 Normal ScalingReplicaSet 6m46s deployment-controller Scaled up replica set nginx-deployment-5d6b8b588c to 3 Normal ScalingReplicaSet 6m45s deployment-controller Scaled down replica set nginx-deployment-6b75f4cbdd to 0 #注释: events的过程可以看到: 1、创建了一个新的deployment为'nginx-deployment-5d6b8b588c',并且增加了一个RS,现在新的deploymentRS数量为1个 2、旧的deployment ' nginx-deployment-6b75f4cbdd'减少1个RS,现在旧的deploymentRS数量为2两个 3、新的deploymentRS数量再次增加,增加1个,现在新的deploymentRS数量为2两个 4、相应的旧的deploymentRS数量减少一个,数量为1个 5、新的deploymentRS再次增加,增加一个,现在为3个 6、旧的deploymentRS再次减少,减少一个,数量为0,同时也完成滚动更新
- 从上面的
kubectl describe
命令,可以看到deployment更新后的过程,是滚动更新
- 回滚Deployment
- 如果Deployment在更新后出现了问题,可以对其进行
回滚操作
,默认情况下,所有Deployment的rollout(回退)历史都保存在系统中,可以随时进行回滚 - 下面来看案例:
[root@master1 ~]# kubectl rollout history deployment nginx-deployment #使用kubectl rollout history可以查看修改记录 deployment.apps/nginx-deployment REVISION CHANGE-CAUSE 1 <none> 2 kubectl set image deployment nginx-deployment nginx=nginxdemos/hello:plain-text --record=true [root@master1 ~]# kubectl set image deployment nginx-deployment nginx=nginx:1.14-alpine --record #再次修改镜像,加--record可以记录到history中 deployment.apps/nginx-deployment image updated [root@master1 ~]# kubectl describe deployments.apps nginx-deployment #查看详细信息 Name: nginx-deployment Namespace: default CreationTimestamp: Thu, 23 Jun 2022 10:31:28 +0800 Labels: app=nginx Annotations: deployment.kubernetes.io/revision: 3 kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginx:1.14-alpine --record=true Selector: app=nginx Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=nginx Containers: nginx: Image: nginx:1.14-alpine #可以看到镜像已经更换 Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: nginx-deployment-6b75f4cbdd (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 34s deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 1 Normal ScalingReplicaSet 28s deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 2 Normal ScalingReplicaSet 28s deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 2 Normal ScalingReplicaSet 27s (x2 over 28h) deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 3 Normal ScalingReplicaSet 27s deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 1 Normal ScalingReplicaSet 25s deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 0 [root@master1 ~]# kubectl rollout history deployment nginx-deployment #查看历史记录,发现增加了一条 deployment.apps/nginx-deployment REVISION CHANGE-CAUSE 2 kubectl set image deployment nginx-deployment nginx=nginxdemos/hello:plain-text --record=true 3 kubectl set image deployment nginx-deployment nginx=nginx:1.14-alpine --record=true [root@master1 ~]# kubectl rollout history deployment nginx-deployment --revision=2 #查看编号2记录的详细信息,使用--revision可以指定版本查看详细信息 deployment.apps/nginx-deployment with revision #2 Pod Template: Labels: app=nginx pod-template-hash=5d6b8b588c Annotations: kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginxdemos/hello:plain-text --record=true Containers: nginx: Image: nginxdemos/hello:plain-text Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> [root@master1 ~]# kubectl rollout undo deployment nginx-deployment #回滚到上一个版本 deployment.apps/nginx-deployment rolled back [root@master1 ~]# kubectl describe deployments.apps nginx-deployment #再次查看详细信息 Name: nginx-deployment Namespace: default CreationTimestamp: Thu, 23 Jun 2022 10:31:28 +0800 Labels: app=nginx Annotations: deployment.kubernetes.io/revision: 4 kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginxdemos/hello:plain-text --record=true Selector: app=nginx Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=nginx Containers: nginx: Image: nginxdemos/hello:plain-text #可以发现镜像已经变成了上一个版本的镜像 Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: nginx-deployment-5d6b8b588c (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 6m7s deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 1 Normal ScalingReplicaSet 6m1s deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 2 Normal ScalingReplicaSet 6m1s deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 2 Normal ScalingReplicaSet 6m deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 1 Normal ScalingReplicaSet 6m (x2 over 28h) deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 3 Normal ScalingReplicaSet 5m58s deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 0 Normal ScalingReplicaSet 4s (x2 over 5h8m) deployment-controller Scaled up replica set nginx-deployment-5d6b8b588c to 1 Normal ScalingReplicaSet 3s (x2 over 5h8m) deployment-controller Scaled up replica set nginx-deployment-5d6b8b588c to 2 Normal ScalingReplicaSet 3s (x3 over 28h) deployment-controller Scaled down replica set nginx-deployment-6b75f4cbdd to 2 Normal ScalingReplicaSet 1s (x3 over 2s) deployment-controller (combined from similar events): Scaled down replica set nginx-deployment-6b75f4cbdd to 0
- 使用
–to-revision
返回到指定版本:
[root@master1 ~]# kubectl rollout history deployment nginx-deployment deployment.apps/nginx-deployment REVISION CHANGE-CAUSE 3 kubectl set image deployment nginx-deployment nginx=nginx:1.14-alpine --record=true 4 kubectl scale deployment nginx-deployment --replicas=3 --record=true [root@master1 ~]# kubectl rollout undo deployment nginx-deployment --to-revision=3 #返回3版本 deployment.apps/nginx-deployment rolled back [root@master1 ~]# kubectl rollout history deployment nginx-deployment deployment.apps/nginx-deployment REVISION CHANGE-CAUSE 4 kubectl scale deployment nginx-deployment --replicas=3 --record=true 5 kubectl set image deployment nginx-deployment nginx=nginx:1.14-alpine --record=true