1. 程式人生 > >通過一個實際例子理解Kubernetes裡pod的自動scale

通過一個實際例子理解Kubernetes裡pod的自動scale

kubectl scale命令用於程式在負載加重或縮小時進行pod擴容或縮小,我們通過一些實際例子來觀察scale命令到底能達到什麼效果。

命令列建立一個deployment:

kubectl run jerry-nginx --image=nginx:1.12.2

kubectl get deploy檢視剛剛建立的deployment:

自動被deployment建立的pod:

kubectl get pod:

使用下列命令檢視生成的deployment明細:

kubectl get deployment jerry-nginx -o yaml

apiVersion: extensions/v1beta1 kind: Deployment metadata: annotations: deployment.kubernetes.io/revision: "1" creationTimestamp: 2018-11-29T08:29:06Z generation: 1 labels: run: jerry-nginx name: jerry-nginx namespace: part-0110 resourceVersion: "7203445" selfLink: /apis/extensions/v1beta1/namespaces/part-0110/deployments/jerry-nginx uid: d5c64f72-f3b0-11e8-b308-a20cb743f347 spec: progressDeadlineSeconds: 600 replicas: 1 revisionHistoryLimit: 2 selector: matchLabels: run: jerry-nginx strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: creationTimestamp: null labels: run: jerry-nginx spec: containers: - image: nginx:1.12.2 imagePullPolicy: IfNotPresent name: jerry-nginx resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File dnsPolicy: ClusterFirst restartPolicy: Always schedulerName: default-scheduler securityContext: {} terminationGracePeriodSeconds: 30 status: availableReplicas: 1 conditions:

  • lastTransitionTime: 2018-11-29T08:29:07Z lastUpdateTime: 2018-11-29T08:29:07Z message: Deployment has minimum availability. reason: MinimumReplicasAvailable status: "True" type: Available
  • lastTransitionTime: 2018-11-29T08:29:06Z lastUpdateTime: 2018-11-29T08:29:07Z message: ReplicaSet "jerry-nginx-69fd9f6c4" has successfully progressed. reason: NewReplicaSetAvailable status: "True" type: Progressing observedGeneration: 1 readyReplicas: 1 replicas: 1 updatedReplicas: 1

另一個有用的命令:

kubectl describe deployment jerry-nginx

Name: jerry-nginx Namespace: part-0110 CreationTimestamp: Thu, 29 Nov 2018 16:29:06 +0800 Labels: run=jerry-nginx Annotations: deployment.kubernetes.io/revision: 1 Selector: run=jerry-nginx Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: run=jerry-nginx Containers: jerry-nginx: Image: nginx:1.12.2 Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason


Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: jerry-nginx-69fd9f6c4 (1/1 replicas created) Events: Type Reason Age From Message


Normal ScalingReplicaSet 9m41s deployment-controller Scaled up replica set jerry-nginx-69fd9f6c4 to 1

現在我們使用下面的命令對deployment進行水平擴充套件:

kubectl scale deployment jerry-nginx --replicas=3

kubectl get pods -l run=jerry-nginx

下圖這個Age為15分鐘之前的是第一次建立deployment時生成的,其他兩個Age為1分鐘之前的是執行了scale命令後自動建立的。

選中一個才建立的pod,檢視其事件記錄:

kubectl describe pod jerry-nginx-69fd9f6c4-8dpvb

kubectl get replicaset

得到自動建立的replication set:

desired = 3意思就是我們水平擴充套件時指定的引數3.

即使手動刪除一個pod例項,replication set又會很快自動建立一個新的:

自動建立的新pod:

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":