如何进行ReplicationController的操作
本篇文章给大家分享的是有关如何进行ReplicationController的操作,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
成都创新互联主营洪洞网站建设的网络公司,主营网站建设方案,重庆App定制开发,洪洞h5微信小程序开发搭建,洪洞网站营销推广欢迎洪洞等地区企业咨询
我们通过一些例子来演示ReplicationController的创建、查看、更新、删除等操作。
创建
首先我们准备一个名为replication_controller_simple.yaml
的配置文件,其内容如下所示:
[root@ecs-d8b6 manifests]# cat replication_controller_simple.yaml apiVersion: v1 kind: ReplicationController metadata: name: replication-controller-runs-pod spec: replicas: 3 selector: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.19.0
该ReplicationController
配置保证同时有3个Pod副本在运行。
使用kubectl create
命令创建资源,如下所示:
[root@ecs-d8b6 manifests]# kubectl create -f replication_controller_simple.yaml replicationcontroller/replication-controller-runs-pod created
查看
我们先查看刚刚创建的ReplicationController
资源:
[root@ecs-d8b6 manifests]# kubectl get replicationcontrollers NAME DESIRED CURRENT READY AGE replication-controller-runs-pod 3 3 3 3m4s
可以看到期望的Pod数量(DESIRED)为3,当前已创建的Pod数量(CURRENT)也为3,并且处于运行状态的Pod数量(READY)同样是3,正是我们所期望的状态。
接着,我们查看实际运行的Pod:
[root@ecs-d8b6 manifests]# kubectl get pods NAME READY STATUS RESTARTS AGE replication-controller-runs-pod-gv4l6 1/1 Running 0 6m11s replication-controller-runs-pod-pcpmb 1/1 Running 0 6m11s replication-controller-runs-pod-qxrhz 1/1 Running 0 6m11s
可以看到Pod运行也是正常的。
最后,我们查看ReplicationController
的详细信息:
[root@ecs-d8b6 manifests]# kubectl describe replicationcontrollers replication-controller-runs-pod Name: replication-controller-runs-pod Namespace: default Selector: app=nginx Labels: app=nginx Annotations:Replicas: 3 current / 3 desired Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template: Labels: app=nginx Containers: nginx: Image: nginx:1.19.0 Port: Host Port: Environment: Mounts: Volumes: Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 10m replication-controller Created pod: replication-controller-runs-pod-gv4l6 Normal SuccessfulCreate 10m replication-controller Created pod: replication-controller-runs-pod-qxrhz Normal SuccessfulCreate 10m replication-controller Created pod: replication-controller-runs-pod-pcpmb
通过最后部分的Events
信息可知,上面显示的Pod确实是名为replication-controller-runs-pod
的ReplicationController
创建的。
更新
改变副本个数
我们把上面replication_controller_simple.yaml
配置文件中的副本数由3
改为5
,然后使用kubectl apply
提交更改:
[root@ecs-d8b6 manifests]# kubectl apply -f replication_controller_simple.yaml replicationcontroller/replication-controller-runs-pod configured
然后再次查看Pod,可以发现Pod数量也会相应地调整为5
。
[root@ecs-d8b6 manifests]# kubectl get pods NAME READY STATUS RESTARTS AGE replication-controller-runs-pod-gv4l6 1/1 Running 0 25m replication-controller-runs-pod-pcpmb 1/1 Running 0 25m replication-controller-runs-pod-qtd4b 1/1 Running 0 6s replication-controller-runs-pod-qxrhz 1/1 Running 0 25m replication-controller-runs-pod-zz6fj 1/1 Running 0 6s
同样地,当副本数变小时,多出的Pod会被删除。
模拟Pod异常
假定当前名为replication-controller-runs-pod
的ReplicationController
维护的Pod副本数为3,我们通过删除Pod的方式模拟Pod异常,如下所示:
[root@ecs-d8b6 manifests]# kubectl get pods NAME READY STATUS RESTARTS AGE replication-controller-runs-pod-gv4l6 1/1 Running 0 30m replication-controller-runs-pod-pcpmb 1/1 Running 0 30m replication-controller-runs-pod-qxrhz 1/1 Running 0 30m [root@ecs-d8b6 manifests]# [root@ecs-d8b6 manifests]# [root@ecs-d8b6 manifests]# [root@ecs-d8b6 manifests]# kubectl delete pods replication-controller-runs-pod-gv4l6 pod "replication-controller-runs-pod-gv4l6" deleted [root@ecs-d8b6 manifests]# kubectl get pods NAME READY STATUS RESTARTS AGE replication-controller-runs-pod-4598b 1/1 Running 0 10s replication-controller-runs-pod-pcpmb 1/1 Running 0 31m replication-controller-runs-pod-qxrhz 1/1 Running 0 31m
可以看到,当我们删除了名为replication-controller-runs-pod-gv4l6
的Pod后,新的Pod对象replication-controller-runs-pod-4598b
马上被创建了出来,从而保证副本数维持不变。
删除
当删除ReplicationController
对象时,由该对象创建的Pod默认也会被删除,如下所示:
[root@ecs-d8b6 manifests]# kubectl delete -f replication_controller_simple.yaml replicationcontroller "replication-controller-runs-pod" deleted [root@ecs-d8b6 manifests]# kubectl get pods NAME READY STATUS RESTARTS AGE replication-controller-runs-pod-ktrxr 0/1 Terminating 0 18s replication-controller-runs-pod-rcdhk 0/1 Terminating 0 18s replication-controller-runs-pod-sc9mj 0/1 Terminating 0 18s [root@ecs-d8b6 manifests]# kubectl get pods No resources found in default namespace.
以上就是如何进行ReplicationController的操作,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
名称栏目:如何进行ReplicationController的操作
文章起源:http://scyanting.com/article/jiijie.html