共计 4332 个字符,预计需要花费 11 分钟才能阅读完成。
本篇文章给大家分享的是有关如何进行 ReplicationController 的操作,丸趣 TV 小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着丸趣 TV 小编一起来看看吧。
我们通过一些例子来演示 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: none
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: none
Host Port: none
Environment: none
Mounts: none
Volumes: none
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 的操作,丸趣 TV 小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注丸趣 TV 行业资讯频道。