Kubernetes核心概念之Replication Controller詳解
? ? 它在k8s中的架構如圖:
? ?RC會在每個節點上創建Pod,Pod上如果有相應的Images可以直接創建,如果沒有,則會拉取這個鏡像再進行創建
一.下面直接來看下在RC中的常見操作:
1.編輯配置文件
vim rc.json
{ "apiVersion":?"v1", "kind":?"ReplicationController", "metadata":?{????????#設置rc的元數據 ????"name":?"nginx-rc" }, "spec":?{????????????#設置rc的具體規格 ????"replicas":?2,????#設置Pod的具體數量 ????"selector":?{????????#通過selector來匹配相應的Pod的label ????????"name":?"myservice" ????}, ????"template":?{????#設置Pod的模板 ????????"metadata":?{ ????????????"labels":?{ ????????????????"name":"myservice" ????} }, ????????"spec":?{ ????????????????"containers":?[{ ???????????????????????"name":?"nginx", ???????????????????????"image":?"nginx", ???????"imagePullPolicy":?"IfNotPresent",????#鏡像拉取策略,分為Always,Never,IfNotPresent,默認是Always ???????????????????????"ports":?[{ ???????????????????? ??"containerPort":?80 ? }] ????}] ??????} ????} } }
2.創建RC
kubectl create -f rep.json
replicationcontroller "nginx-rc" created
3.查詢rc
kubectl get rc?nginx-rc
要求有2個,已經成功創建的也是兩個
4.查詢pod運行情況
kubectl get pod -l name????????#-l ?指定selector的key
可以看到兩個創建好的pod,已經在Running狀態
5.當刪除其中一個Pod或者刪除全部Pod的時候,RC會自動再次創建Pod,直到滿足配置文件中定義的個數
kubectl delete pod nginx-rc-lbb7m
pod "nginx-rc-lbb7m" deleted
kubectl get pod -l name
二.彈性伸縮
彈性伸縮就是在現有環境不能滿足業務需求的時候,進行的擴容或縮容
1.縮容Pod
kubectl scale replicationcontroller nginx-rc --replicas=1
replicationcontroller "nginx-rc" scaled
kubectl get pod -l name
kubectl get rc nginx-rc
2.擴容Pod
kubectl scale replicationcontroller nginx-rc --replicas=3
replicationcontroller "nginx-rc" scaled
kubectl get pod -l name
kubectl get rc nginx-rc
3.判斷當前Pod副本是否正確,並修改數量
kubectl scale rc nginx-rc --current-replicas=3 --replicas=1
判斷當前副本數是否為3個,如果是,則更改為1個副本
三.滾動升級
在滾動升級/更新方面,有一種更簡單的機制,Depolyment
1.創建Depolyment
vim nginx-deployment.yaml????????#配置文件基本一致,就是改個類型,這裏就不贅述了
apiVersion:?extensions/v1beta1 kind:?Deployment metadata: ??name:?nginx-deployment spec: ??replicas:?3 ??template: ????metadata: ??????labels: ????????app:?nginx ????spec: ??????containers: ??????-?name:?nginx ????????image:?nginx ????????imagePullPolicy:?Never? ????????ports: ????????-?containerPort:?80 ??????????hostPort:?8181
2.帶記錄點的方式創建Depolyment,在這條命令下面將開始記錄所有對Depolyment的操作
kubectl create -f nginx-deployment.yaml --record
3.查詢創建的Depolyment
kubectl get deployment
4.查詢當前所在的記錄點
kubectl rollout history deployment nginx-deployment
5.升級/更新這個Depolyment
vim nginx-deployment.yaml
apiVersion:?extensions/v1beta1 kind:?Deployment metadata: ??name:?nginx-deployment spec: ??replicas:?3 ??template: ????metadata: ??????labels: ????????app:?nginx ????spec: ??????containers: ??????-?name:?nginx ????????image:?nginxv2????????#更換源Images ????????imagePullPolicy:?Never? ????????ports: ????????-?containerPort:?80 ??????????hostPort:?8181
kubectl apply -f nginx-deployment.yaml
deployment "nginx-deployment" configured
6.如果發現更改錯誤,可以進行回滾操作
kubectl rollout undo deployment nginx-deployment --to-revision=1????????#回到編號為1的操作
deployment "nginx-deployment" rolled back
7.刪除Depolyment
kubectl delete -f nginx-deployment.yaml
Kubernetes核心概念之Replication Controller詳解