本节,我们通过一些例子来演示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.