千家信息网

如何进行ReplicationController的操作

发表于:2025-01-26 作者:千家信息网编辑
千家信息网最后更新 2025年01月26日,本篇文章给大家分享的是有关如何进行ReplicationController的操作,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。我们通过
千家信息网最后更新 2025年01月26日如何进行ReplicationController的操作

本篇文章给大家分享的是有关如何进行ReplicationController的操作,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

我们通过一些例子来演示ReplicationController的创建、查看、更新、删除等操作。

创建

首先我们准备一个名为replication_controller_simple.yaml的配置文件,其内容如下所示:

[root@ecs-d8b6 manifests]# cat replication_controller_simple.yaml apiVersion: v1kind: ReplicationControllermetadata:  name: replication-controller-runs-podspec:  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   AGEreplication-controller-runs-pod   3         3         3       3m4s

可以看到期望的Pod数量(DESIRED)为3,当前已创建的Pod数量(CURRENT)也为3,并且处于运行状态的Pod数量(READY)同样是3,正是我们所期望的状态。

接着,我们查看实际运行的Pod:

[root@ecs-d8b6 manifests]# kubectl get podsNAME                                    READY   STATUS    RESTARTS   AGEreplication-controller-runs-pod-gv4l6   1/1     Running   0          6m11sreplication-controller-runs-pod-pcpmb   1/1     Running   0          6m11sreplication-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-podNamespace:    defaultSelector:     app=nginxLabels:       app=nginxAnnotations:  Replicas:     3 current / 3 desiredPods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 FailedPod 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-podReplicationController创建的。

更新

改变副本个数

我们把上面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 podsNAME                                    READY   STATUS    RESTARTS   AGEreplication-controller-runs-pod-gv4l6   1/1     Running   0          25mreplication-controller-runs-pod-pcpmb   1/1     Running   0          25mreplication-controller-runs-pod-qtd4b   1/1     Running   0          6sreplication-controller-runs-pod-qxrhz   1/1     Running   0          25mreplication-controller-runs-pod-zz6fj   1/1     Running   0          6s

同样地,当副本数变小时,多出的Pod会被删除。

模拟Pod异常

假定当前名为replication-controller-runs-podReplicationController维护的Pod副本数为3,我们通过删除Pod的方式模拟Pod异常,如下所示:

[root@ecs-d8b6 manifests]# kubectl get podsNAME                                    READY   STATUS    RESTARTS   AGEreplication-controller-runs-pod-gv4l6   1/1     Running   0          30mreplication-controller-runs-pod-pcpmb   1/1     Running   0          30mreplication-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 podsNAME                                    READY   STATUS    RESTARTS   AGEreplication-controller-runs-pod-4598b   1/1     Running   0          10sreplication-controller-runs-pod-pcpmb   1/1     Running   0          31mreplication-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 podsNAME                                    READY   STATUS        RESTARTS   AGEreplication-controller-runs-pod-ktrxr   0/1     Terminating   0          18sreplication-controller-runs-pod-rcdhk   0/1     Terminating   0          18sreplication-controller-runs-pod-sc9mj   0/1     Terminating   0          18s[root@ecs-d8b6 manifests]# kubectl get podsNo resources found in default namespace.

以上就是如何进行ReplicationController的操作,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

0