K8s新手系列之ReplicationController资源

打印 上一主题 下一主题

主题 1804|帖子 1804|积分 5412

概述

官网地点:https://kubernetes.io/zh-cn/docs/concepts/workloads/controllers/replicationcontroller/
ReplicationController 是一个比较原始的Pod控制器,已经被废弃,由ReplicaSet控制器替换,想要了解ReplicaSet可查看这篇文章:K8s新手系列之ReplicaSet资源
ReplicationController简称rc,称为副本控制器,其重要作用就是控制Pod副本数目标控制器。
ReplicationController 确保在任何时候都有特定命目标 Pod 副本处于运行状态。只要 ReplicationController 不删除,则指定的Pod数目会使用存活。
ReplicationController的作用


  • 副本管理:ReplicationController 的核心功能是保证在任何时候都有指定命目标 Pod 副本在运行。如果有 Pod 不测终止,ReplicationController 会主动创建新的 Pod 来替换它;如果 Pod 数目过多,它会删除多余的 Pod。
  • 滚动更新:在早期版本中,虽然 ReplicationController 本身不支持滚动更新,但可以通过创建新的 ReplicationController 并逐步减少旧的 ReplicationController 中的 Pod 数目来实现雷同滚动更新的结果。
  • 集群扩容和缩容:用户可以通过修改 ReplicationController 中的副本数目来轻松地对应用举行扩容或缩容。
ReplicationController资源配置文件详解

可以通过kubectl explain rc查看创建ReplicationController需要的字段
示例:
  1. [root@node01 ~]# kubectl explain rc
  2. KIND:     ReplicationController
  3. VERSION:  v1
  4. DESCRIPTION:
  5.      ReplicationController represents the configuration of a replication
  6.      controller.
  7. # fileds字段中的内容已进行截取
  8. FIELDS:
  9.    apiVersion   <string>
  10.      
  11.    kind <string>
  12.    metadata     <Object>
  13.    spec <Object>
  14.    status       <Object>
复制代码
通过上述可以发现定义ReplicationController的资源文件和定义Pod一样,也需要apiVersion、kind、metadata、spec等字段。
但是spec字段中有三个字段,需要留意,分别是replicas、selector、template。如下:
  1. [root@node01 ~]# kubectl explain rc.spec
  2. KIND:     ReplicationController
  3. VERSION:  v1
  4. RESOURCE: spec <Object>
  5. DESCRIPTION:
  6.      Spec defines the specification of the desired behavior of the replication
  7.      controller. More info:
  8.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
  9.      ReplicationControllerSpec is the specification of a replication controller.
  10. FIELDS:
  11.    minReadySeconds      <integer>
  12.    # minReadySeconds 规定了一个时间阈值,只有当 Pod 保持就绪状态的时间达到或者超过这个阈值,并且在此期间 Pod 内的任何容器都没有崩溃,这个 Pod 才会被视为可用。
  13.    
  14.    replicas     <integer>
  15.    # 定义Pod的副本数量
  16.    
  17.    selector     <map[string]string>
  18.    # 标签选择器,这里指定Pod定义的标签
  19.    
  20.    template     <Object>
  21.    # 定义Pod的模板
复制代码
创建ReplicationController

这里我们创建三个Pod副本
示例:
  1. # 定义rc清单文件
  2. [root@node01 ~/rc]# cat rc-nginx.yaml
  3. apiVersion: v1
  4. kind: ReplicationController
  5. metadata:
  6.   name: rc-nginx-1
  7.   namespace: default
  8.   labels:
  9.     app: rc-nginx
  10. spec:
  11.   # 定义Pod的副本数量
  12.   replicas: 3
  13.   # 标签选择器,这里和Pod的Label保持一致
  14.   selector:
  15.     app: nginx
  16.   # 定义Pod的模板,只需要定义metadata和spec两个字段即可
  17.   template:
  18.     metadata:
  19.       name: pod-nginx
  20.       labels:
  21.         app: nginx
  22.     spec:
  23.       restartPolicy: Always
  24.       containers:
  25.       - name: nginx
  26.         image: nginx:latest
  27.       restartPolicy: Always
  28. # 创建rc
  29. [root@node01 ~/rc]# kubectl apply -f rc-nginx.yaml
  30. replicationcontroller/rc-nginx-1 created
复制代码
查看ReplicationController和对应的Pod

查看ReplicationController
  1. [root@node01 ~/rc]# kubectl get rc -o wide
  2. NAME         DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
  3. rc-nginx-1   3         3         3       45s   nginx        nginx:latest   app=nginx
复制代码
查看对应Pod
  1. [root@node01 ~/rc]# kubectl get po | grep rc-nginx
  2. rc-nginx-1-8ptc7       1/1     Running            0                2m38s
  3. rc-nginx-1-8xvbr       1/1     Running            0                2m38s
  4. rc-nginx-1-lk45f       1/1     Running            0                2m38s
复制代码
验证ReplicationController的副本管理

ReplicationController 的核心功能是保证在任何时候都有指定命目标 Pod 副本在运行。如果有 Pod 不测终止,ReplicationController 会主动创建新的 Pod 来替换它;如果 Pod 数目过多,它会删除多余的 Pod。
当我们删除创建好的Pod时,ReplicationController会发生什么呢?
  1. # 删除上面创建Pod
  2. [root@node01 ~/rc]# kubectl delete po rc-nginx-1-8ptc7 rc-nginx-1-8xvbr rc-nginx-1-lk45f
  3. pod "rc-nginx-1-8ptc7" deleted
  4. pod "rc-nginx-1-8xvbr" deleted
  5. pod "rc-nginx-1-lk45f" deleted
  6. # 查看rc,发现其副本数量还是三个
  7. [root@node01 ~/rc]# kubectl get rc
  8. NAME         DESIRED   CURRENT   READY   AGE
  9. rc-nginx-1   3         3         3       6m11s
  10. # 查看Pod,发现对应的Pod还是三个
  11. [root@node01 ~/rc]# kubectl get po | grep rc-nginx
  12. rc-nginx-1-g6qvg       1/1     Running            0                18s
  13. rc-nginx-1-gwzd4       1/1     Running            0                18s
  14. rc-nginx-1-x2rqm       1/1     Running            0                19s
复制代码
通过上述验证,当Pod被不测终止时,ReplicationController会主动创建它
验证ReplicationController的扩缩容

验证扩容

我们将副本数目调整成5,看看会发生什么?
  1. [root@node01 ~/rc]# cat rc-nginx.yaml
  2. apiVersion: v1
  3. kind: ReplicationController
  4. metadata:
  5.   name: rc-nginx-1
  6.   namespace: default
  7.   labels:
  8.     app: rc-nginx
  9. spec:
  10.   # 将副本数量调整成5
  11.   replicas: 5
  12.   selector:
  13.     app: nginx
  14.   template:
  15.     metadata:
  16.       name: pod-nginx
  17.       labels:
  18.         app: nginx
  19.     spec:
  20.       restartPolicy: Always
  21.       containers:
  22.       - name: nginx
  23.         image: nginx:latest
  24.       restartPolicy: Always
  25. # 重新应用它
  26. [root@node01 ~/rc]# kubectl apply -f rc-nginx.yaml
  27. replicationcontroller/rc-nginx-1 configured
复制代码
查看一下:
  1. # 查看rc
  2. [root@node01 ~/rc]# kubectl get rc
  3. NAME         DESIRED   CURRENT   READY   AGE
  4. rc-nginx-1   5         5         5       11m
  5. # 查看pod
  6. [root@node01 ~/rc]# kubectl get po | grep rc-nginx
  7. NAME                   READY   STATUS             RESTARTS         AGE
  8. rc-nginx-1-7dnlv       1/1     Running            0                87s
  9. rc-nginx-1-dsr6h       1/1     Running            0                87s
  10. rc-nginx-1-g6qvg       1/1     Running            0                6m55s
  11. rc-nginx-1-gwzd4       1/1     Running            0                6m55s
  12. rc-nginx-1-x2rqm       1/1     Running            0                6m56s
复制代码
通过上述发现Pod副本数目由3个变成了5个,查看AGE字段,发现有两个Pod是新建的状态。
验证缩容

同理,我们将副本数目调整成1,看看会发生什么?
  1. [root@node01 ~/rc]# cat rc-nginx.yaml
  2. apiVersion: v1
  3. kind: ReplicationController
  4. metadata:
  5.   name: rc-nginx-1
  6.   namespace: default
  7.   labels:
  8.     app: rc-nginx
  9. spec:
  10.   # 将副本数量调整成1
  11.   replicas: 1
  12.   selector:
  13.     app: nginx
  14.   template:
  15.     metadata:
  16.       name: pod-nginx
  17.       labels:
  18.         app: nginx
  19.     spec:
  20.       restartPolicy: Always
  21.       containers:
  22.       - name: nginx
  23.         image: nginx:latest
  24.       restartPolicy: Always
  25. # 重新应用它
  26. [root@node01 ~/rc]# kubectl apply -f rc-nginx.yaml
  27. replicationcontroller/rc-nginx-1 configured
复制代码
查看一下
  1. # 查看rc
  2. [root@node01 ~/rc]# kubectl get rc
  3. NAME         DESIRED   CURRENT   READY   AGE
  4. rc-nginx-1   1         1         1       15m
  5. # 查看Pod
  6. [root@node01 ~/rc]# kubectl get po | grep rc-nginx
  7. rc-nginx-1-x2rqm       1/1     Running            0               9m32s
复制代码
通过上述发现,Pod数目由5个缩减成了1个
验证ReplicationController的滚动更新

早期版本中,虽然 ReplicationController 本身不支持滚动更新,但可以通过创建新的 ReplicationController 并逐步减少旧的 ReplicationController 中的 Pod 数目来实现雷同滚动更新的结果。
示例:将上述案例中的Pod的镜像替换成tomcat
  1. [root@node01 ~/rc]# cat rc-nginx.yaml
  2. apiVersion: v1
  3. kind: ReplicationController
  4. metadata:
  5.   name: rc-nginx-1
  6.   namespace: default
  7.   labels:
  8.     app: rc-nginx
  9. spec:
  10.   # 将副本数量调整成5
  11.   replicas: 5
  12.   selector:
  13.     app: nginx
  14.   template:
  15.     metadata:
  16.       name: pod-nginx
  17.       labels:
  18.         app: nginx
  19.     spec:
  20.       restartPolicy: Always
  21.       containers:
  22.       - name: nginx
  23.         # 将镜像替换成tomcat
  24.         image: tomcat:latest
  25.       restartPolicy: Always
  26. # 重新应用它
  27. [root@node01 ~/rc]# kubectl apply -f rc-nginx.yaml
  28. replicationcontroller/rc-nginx-1 configured
复制代码
查看Pod变化
  1. # 第一次查看
  2. [root@node01 ~/rc]# kubectl get po
  3. NAME                   READY   STATUS              RESTARTS         AGE
  4. rc-nginx-1-gvdwl       0/1     ContainerCreating   0                2s
  5. rc-nginx-1-rtdlw       0/1     ContainerCreating   0                2s
  6. rc-nginx-1-vzfb2       0/1     ContainerCreating   0                2s
  7. rc-nginx-1-x2rqm       1/1     Running             0                15m
  8. rc-nginx-1-zwms9       0/1     ContainerCreating   0                2s
  9. # 最后查看
  10. [root@node01 ~/rc]# kubectl get po
  11. NAME                   READY   STATUS             RESTARTS         AGE
  12. pod-evc-secret-1       0/1     CrashLoopBackOff   68 (3m39s ago)   21h
  13. rc-nginx-1-gvdwl       1/1     Running            0                71s
  14. rc-nginx-1-rtdlw       1/1     Running            0                71s
  15. rc-nginx-1-vzfb2       1/1     Running            0                71s
  16. rc-nginx-1-wscvf       0/1     ContainerCreating  0                3s
  17. rc-nginx-1-zwms9       1/1     Running            0                71s
复制代码
发现Pod在逐步替换,可以访问一下对应Pod试一下,发现返回的内容是Tomcat的内容

滚动更新的原理

滚动更新的核心目标是在不中断服务的前提下,逐步将旧版本的应用替换为新版本。在使用 ReplicationController 时,这一过程重要通过以下几个步骤完成:

  • 创建新版本的 ReplicationController
    当需要对应用举行更新时,首先要创建一个新的 ReplicationController,其配置与旧的 ReplicationController 基本雷同,但使用了新版本的应用镜像。新的 ReplicationController 初始的副本数目通常设置为 0。
  • 逐步增加新版本的 Pod 副本数目
    在创建好新的 ReplicationController 后,逐步增加其管理的 Pod 副本数目。每次增加少量副本(例如每次增加 1 个),这样可以确保新的 Pod 有足够的时间启动并稳定运行,避免一次性创建大量新 Pod 导致资源紧张或服务不稳定。
  • 逐步减少旧版本的 Pod 副本数目
    在增加新版本 Pod 副本数目标同时,逐步减少旧的 ReplicationController 管理的 Pod 副本数目。同样,每次减少少量副本,以保证服务的连续性。
  • 重复步骤 2 和 3
    不断重复增加新版本 Pod 副本数目和减少旧版本 Pod 副本数目标过程,直到旧版本的 ReplicationController 管理的 Pod 副本数目降为 0,而新版本的 ReplicationController 管理的 Pod 副本数目达到预期值。
管理ReplicationController

查看ReplicationController

语法:
  1. kubectl get rc <rc-name> -n <namespace-name>
复制代码
示例:
  1. [root@node01 ~/rc]# kubectl get rc
  2. NAME         DESIRED   CURRENT   READY   AGE
  3. rc-nginx-1   5         5         5       29m
复制代码
修改ReplicationController

由两种方式,一种是通过kubectl edit rc 来举行修改,会打开一个雷同vim的界面,修改其对应的值即可,最后wq生存即可应用你的配置

第二种方式则是修改对应的资源文件,最后使用kubectl apply -f 即可
删除ReplicationController

删除ReplicationController时会删除所有对应的Pod!!!
语法:
  1. kubectl delete rc <rc-name> -n <namespace-name>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

千千梦丶琪

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表