您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何進行ReplicationController的操作,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
我們通過一些例子來演示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會被刪除。
假定當前名為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的操作,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。