RC
什么是RC:
Replication Controller(副本控制器),RC能夠保證pod在任意時間運行的副本數量,能夠保證pod總是可用的。
RC控制的pod的多個副本,每個副本都有獨立的ip,並且支持pod副本數量的擴、縮容。
RC定義文件格式:
這里還是以nginx為例,認識最簡單的rc配置文件。 每一行配置都有詳細的解釋
#api版本
apiVersion: v1
#對象資源類型 RC
kind: ReplicationController
#RC元數據
metadata:
#對象資源名稱
name: nginx
#RC的詳細描述
spec:
#維持pod的共數量
replicas: 3
#RC選擇器,指定對哪個Pod使用rc
selector:
#label 標簽,選擇有此 label 的 Pod
app: nginx
# 定義創建 Pod 實例的模板
template:
metadata:
name: nginx
# Pod 的 label,對應上面 rc 的 selector
labels:
app: nginx
spec:
containers:
# 定義 Pod 中的容器
- name: nginx
image: nginx
ports:
- containerPort: 80
RC常用基本操作
創建rc,其中rc_demo.yml是上面rc的定義文件
[root@k8s-01 pod_demo]# kubectl create -f rc_demo.yml replicationcontroller/nginx created
查詢創建的rc以及rc對應的pod、container
[root@k8s-01 pod_demo]# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx 3 3 3 9m56s nginx nginx app=nginx
[root@k8s-01 pod_demo]# kubectl get pods -o wide|grep nginx nginx-bwpbx 1/1 Running 0 7m13s 10.244.2.33 k8s-03 <none> <none> nginx-qgj22 1/1 Running 0 7m13s 10.244.1.56 k8s-02 <none> <none> nginx-vpz8h 1/1 Running 0 7m13s 10.244.2.34 k8s-03 <none> <none> [root@k8s-01 pod_demo]# kubectl describe pod nginx-bwpbx Name: nginx-bwpbx
刪除rc,下面兩種刪除rc的方式都可以
[root@k8s-01 pod_demo]# kubectl get rc -o wide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR nginx 3 3 3 9m56s nginx nginx app=nginx [root@k8s-01 pod_demo]# kubectl delete rc nginx replicationcontroller "nginx" deleted [root@k8s-01 pod_demo]# kubectl get rc -o wide No resources found. [root@k8s-01 pod_demo]# kubectl delete -f rc_demo.yml
rc副本擴、縮容。將原始副本狀態為3的rc擴容到4再縮容到2
[root@k8s-01 pod_demo]# kubectl get rc -o wide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR nginx 3 3 0 5s nginx nginx app=nginx [root@k8s-01 pod_demo]# kubectl get rc -o wide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR nginx 3 3 3 77s nginx nginx app=nginx [root@k8s-01 pod_demo]# kubectl scale rc nginx --replicas=4 replicationcontroller/nginx scaled [root@k8s-01 pod_demo]# kubectl get rc -o wide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR nginx 4 4 3 2m9s nginx nginx app=nginx [root@k8s-01 pod_demo]# kubectl get rc -o wide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR nginx 4 4 4 2m43s nginx nginx app=nginx [root@k8s-01 pod_demo]# kubectl scale rc nginx --replicas=2 replicationcontroller/nginx scaled [root@k8s-01 pod_demo]# kubectl get rc -o wide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR nginx 2 2 2 2m50s nginx nginx app=nginx
修改文件配置重新應用也能夠修改副本數量
[root@k8s-01 pod_demo]# kubectl get rc -o wide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR nginx 2 2 2 4m56s nginx nginx app=nginx [root@k8s-01 pod_demo]# kubectl get pods -o wide|grep nginx nginx-bp8rd 1/1 Running 0 5m7s 10.244.1.57 k8s-02 <none> <none> nginx-k2r5j 1/1 Running 0 5m7s 10.244.2.35 k8s-03 <none> <none> [root@k8s-01 pod_demo]# cat rc_demo.yml|grep replicas replicas: 3 [root@k8s-01 pod_demo]# kubectl apply -f rc_demo.yml Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply replicationcontroller/nginx configured [root@k8s-01 pod_demo]# kubectl get pods -o wide|grep nginx nginx-bp8rd 1/1 Running 0 6m1s 10.244.1.57 k8s-02 <none> <none> nginx-k2r5j 1/1 Running 0 6m1s 10.244.2.35 k8s-03 <none> <none> nginx-lrlw2 1/1 Running 0 42s 10.244.2.37 k8s-03 <none> <none> [root@k8s-01 pod_demo]# kubectl get rc -o wide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR nginx 3 3 3 6m9s nginx nginx app=nginx
RS
RS(Replication Set)和RC的功能基本一致,目前唯一的一個區別就是RC只支持基於等式的selector(env=dev或environment!=qa),但RS還支持基於集合的selector(version in (v1.0, v2.0)),這對復雜的運維管理就非常方便了。Label后面的隨筆會介紹,簡單的說就是為資源打標簽,然后通過標簽查詢,比如:一個 【長得帥】、【有錢】、【單身】的人。
使用上面RC的定義文件改造的RS定義文件
#api版本,注意這里和RC不一樣
apiVersion: apps/v1
#對象資源類型 RC
kind: ReplicaSet
#RC元數據
metadata:
#對象資源名稱
name: nginx-rs
#RC的詳細描述
spec:
#維持pod的共數量
replicas: 3
#RS選擇器,指定對哪個Pod
selector:
#相比RC,選擇器這里多了一層邏輯,可以滿足更復雜的選擇器場景
matchLabels:
app: nginx
# 定義創建 Pod 實例的模板
template:
metadata:
name: nginx
# Pod 的 label,對應上面 rs 的 selector
labels:
app: nginx
spec:
containers:
# 定義 Pod 中的容器
- name: nginx
image: nginx
ports:
- containerPort: 80
RS常用操作命令
#創建、查詢RS [root@k8s-01 ~]# kubectl create -f RS_demo.yml replicaset.apps/nginx-rs created [root@k8s-01 ~]# kubectl get rs -o wide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR nginx-rs 3 3 3 55s nginx nginx app=nginx #查詢RS詳細內容,包含其關聯的pod副本詳細信息 [root@k8s-01 ~]# kubectl describe rs nginx-rs Name: nginx-rs Namespace: default Selector: app=nginx Labels: <none> 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 Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 2m57s replicaset-controller Created pod: nginx-rs-l6cnq Normal SuccessfulCreate 2m57s replicaset-controller Created pod: nginx-rs-vvqh7 Normal SuccessfulCreate 2m57s replicaset-controller Created pod: nginx-rs-9jmcj #刪除一個pod模擬pod故障RS自愈。 [root@k8s-01 ~]# kubectl delete pod nginx-rs-9jmcj pod "nginx-rs-9jmcj" deleted [root@k8s-01 ~]# kubectl describe rs nginx-rs|grep nginx-rs Name: nginx-rs Normal SuccessfulCreate 5m49s replicaset-controller Created pod: nginx-rs-l6cnq Normal SuccessfulCreate 5m49s replicaset-controller Created pod: nginx-rs-vvqh7 Normal SuccessfulCreate 5m49s replicaset-controller Created pod: nginx-rs-9jmcj Normal SuccessfulCreate 33s replicaset-controller Created pod: nginx-rs-9spdp [root@k8s-01 ~]# kubectl get pod|grep nginx-rs nginx-rs-9spdp 1/1 Running 0 59s nginx-rs-l6cnq 1/1 Running 0 6m15s nginx-rs-vvqh7 1/1 Running 0 6m15s #RS的Pod副本擴縮容,兩種方式:1、kubectl命令如下 2、修改文件kubectl apply -f yml [root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs nginx-rs 3 3 3 8m32s nginx nginx app=nginx [root@k8s-01 ~]# kubectl scale rs nginx-rs --replicas=4 replicaset.extensions/nginx-rs scaled [root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs nginx-rs 4 4 3 9m10s nginx nginx app=nginx [root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs nginx-rs 4 4 4 12m nginx nginx app=nginx #刪除rs [root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs nginx-rs 4 4 4 12m nginx nginx app=nginx [root@k8s-01 ~]# kubectl delete rs nginx-rs replicaset.extensions "nginx-rs" deleted [root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs
Deployment
相比於RS,Deployment增加了升級方式的定義,並且實際生產環境也多使用Deployment。
根據上面RS定義文件修改的Deployment定義文件
#api版本
apiVersion: apps/v1
#對象資源類型 Deployment
kind: Deployment
#元數據
metadata:
#對象資源名稱
name: nginx-d
#deployment的labels
labels:
app: nginx
#RC的詳細描述
spec:
#維持pod的共數量
replicas: 3
#RC選擇器,指定對哪個Pod使用rc
selector:
#label 標簽,選擇有此 label的為app: nginx-p的Pod
matchLabels:
app: nginx
#定義deployment的升級策略
strategy:
#表示升級時是將源所有pod刪除后使用新的template信息創建pod
type: Recreate
#滾動升級配置,升級時使用template新建一個pod然后將舊Pod挑選一個停服刪除,持續滾動,直到所有pod更新完畢
#type: rollintUpdate
#maxSurge: 1
#maxUnavailable: 1
# 定義創建 Pod 實例的模板
template:
metadata:
name: nginx
# Pod 的 label,對應上面 rc 的 selector
labels:
app: nginx
spec:
containers:
# 定義 Pod 中的容器
- name: nginx
image: nginx:1.18
ports:
- containerPort: 80
deployment常用基本操作
#創建、查詢deployment [root@k8s-01 ~]# kubectl create -f deployment_demo.yml deployment.apps/nginx-d created [root@k8s-01 ~]# kubectl get deployment -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx-d 3/3 3 3 4m23s nginx nginx:1.18 app=nginx #kubectl replace -f filename,如果存在同名deployment則進行替換 #deployment詳情查看其關聯的RS [root@k8s-01 ~]# kubectl describe deployment nginx-d Name: nginx-d Namespace: default CreationTimestamp: Fri, 01 Oct 2021 22:04:26 +0800 Labels: app=nginx Annotations: deployment.kubernetes.io/revision: 1 Selector: app=nginx Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: Recreate MinReadySeconds: 0 Pod Template: Labels: app=nginx Containers: nginx: Image: nginx:1.18 Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: nginx-d-659bf7c684 (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 8m16s deployment-controller Scaled up replica set nginx-d-659bf7c684 to 3 #查看deployment對應的pod信息 [root@k8s-01 ~]# kubectl get rs nginx-d-659bf7c684 -o wide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR nginx-d-659bf7c684 3 3 3 10m nginx nginx:1.18 app=nginx,pod-template-hash=659bf7c684 [root@k8s-01 ~]# kubectl describe rs nginx-d-659bf7c684 Name: nginx-d-659bf7c684 Namespace: default Selector: app=nginx,pod-template-hash=659bf7c684 Labels: app=nginx pod-template-hash=659bf7c684 Annotations: deployment.kubernetes.io/desired-replicas: 3 deployment.kubernetes.io/max-replicas: 3 deployment.kubernetes.io/revision: 1 Controlled By: Deployment/nginx-d Replicas: 3 current / 3 desired Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template: Labels: app=nginx pod-template-hash=659bf7c684 Containers: nginx: Image: nginx:1.18 Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 10m replicaset-controller Created pod: nginx-d-659bf7c684-qj5gh Normal SuccessfulCreate 10m replicaset-controller Created pod: nginx-d-659bf7c684-sqkjr Normal SuccessfulCreate 10m replicaset-controller Created pod: nginx-d-659bf7c684-cxwsc [root@k8s-01 ~]# #刪除pod構造deployment自愈 [root@k8s-01 ~]# kubectl get pod -o wide|grep nginx-d nginx-d-659bf7c684-cxwsc 1/1 Running 0 11m 10.244.2.46 k8s-03 <none> <none> nginx-d-659bf7c684-qj5gh 1/1 Running 0 11m 10.244.2.45 k8s-03 <none> <none> nginx-d-659bf7c684-sqkjr 1/1 Running 0 11m 10.244.1.66 k8s-02 <none> <none> [root@k8s-01 ~]# kubectl delete pod nginx-d-659bf7c684-cxwsc pod "nginx-d-659bf7c684-cxwsc" deleted [root@k8s-01 ~]# kubectl get pod -o wide|grep nginx-d nginx-d-659bf7c684-qj5gh 1/1 Running 0 14m 10.244.2.45 k8s-03 <none> <none> nginx-d-659bf7c684-sqkjr 1/1 Running 0 14m 10.244.1.66 k8s-02 <none> <none> nginx-d-659bf7c684-z4jjz 1/1 Running 0 119s 10.244.2.47 k8s-03 <none> <none> #pod副本數量擴、縮容。還是有兩種方法,這里只演示命令行方式 [root@k8s-01 ~]# kubectl get pod -o wide|grep nginx-d nginx-d-659bf7c684-qj5gh 1/1 Running 0 14m 10.244.2.45 k8s-03 <none> <none> nginx-d-659bf7c684-sqkjr 1/1 Running 0 14m 10.244.1.66 k8s-02 <none> <none> nginx-d-659bf7c684-z4jjz 1/1 Running 0 119s 10.244.2.47 k8s-03 <none> <none> [root@k8s-01 ~]# [root@k8s-01 ~]# kubectl scale deployment nginx-d --replicas=4 deployment.extensions/nginx-d scaled [root@k8s-01 ~]# kubectl get pod -o wide|grep nginx-d nginx-d-659bf7c684-d6qhr 1/1 Running 0 18s 10.244.1.67 k8s-02 <none> <none> nginx-d-659bf7c684-qj5gh 1/1 Running 0 16m 10.244.2.45 k8s-03 <none> <none> nginx-d-659bf7c684-sqkjr 1/1 Running 0 16m 10.244.1.66 k8s-02 <none> <none> nginx-d-659bf7c684-z4jjz 1/1 Running 0 4m16s 10.244.2.47 k8s-03 <none> <none> #image版本升降級。升降級也有兩種方式,最好通過方式2更新。將鏡像從1.1.8升級到latest版本 #方式1:kubectl set image deployment [dName] [imageNmae]=[新的鏡像] #方式2:kubectl apply -f [deployment 新文件] --record [root@k8s-01 ~]# kubectl get deployment -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx-d 4/4 4 4 21m nginx nginx:1.18 app=nginx [root@k8s-01 ~]# kubectl apply -f deployment_demo.yml --record Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply deployment.apps/nginx-d configured [root@k8s-01 ~]# kubectl get deployment -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx-d 3/3 3 3 22m nginx nginx app=nginx #查看deployment的升級記錄 [root@k8s-01 ~]# kubectl rollout history deployment nginx-d deployment.extensions/nginx-d REVISION CHANGE-CAUSE 1 <none> 2 kubectl apply --filename=deployment_demo.yml --record=true #升級回滾,回滾到上一個版本,nginx有變回1.18版本 [root@k8s-01 ~]# kubectl rollout undo deployment nginx-d deployment.extensions/nginx-d rolled back [root@k8s-01 ~]# kubectl get deployment -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx-d 3/3 3 3 25m nginx nginx:1.18 app=nginx
