FuFu KK8s
  • K~K8s index
  • Kubernetes 的基礎世界
  • Why container is not Docker
  • Startup Local Kubernetes via Minikube
  • K8s Master node Component 介紹
  • NameSpace、Deployments 概念說明
  • 常見的 kubectl 指令
  • 回顧第一次部署,淺談 Pod、Deployment
  • 回顧第一次部署,淺談 Service
  • 今日來介紹 Pod 靜態文件~ Manifest
  • Pod 的健康檢查方式
  • Pod 的健康檢查方式 Part-2
  • Pod 的資源請求、上限
  • Pod 資料,如何持久化存放、讀取
  • Pod 副本管理~ 描述 ReplicaSet 控制器
  • Pod 副本管理~ 實作篇
  • 淺談 DaemonSet,及相對 ReplicaSet 的差異
  • 來說說 Label
  • 從 Label 再回頭談 Service
  • Service 續集之 Cluster IP、Kube-proxy、LoadBalancer
  • 從 Service 發現 K8s 網路層全貌
  • Container Data Persistent
  • 發現、初談 StatefulSet
  • 再談 StatefulSet
  • 認識 ConfigMap
  • 第一次使用 Play with Kubernetes
  • 手工 Installing kubeadm
  • 手工 Installing CRI-O、kubeadm init
  • 繼上篇,排查 kubelet、kubeadm init 問題
  • Installing a pod network add-on
  • K8s add Nodes(join)
  • 驗證自建的 K8s
  • 筆記
    • kubectl get 筆記
    • kubectl 部署筆記
    • kubectl describe nodes
  • 實務記憶篇
    • 整理下記憶
    • 有哪些 Kubernetes 雲端服務
    • 如何從本機連線至 GKE Pod
    • GCP Memorystore 服務介紹
    • 如何連線至 GCP Memorystore
    • GCP Cloud SQL 服務介紹
    • 如何連線至 Cloud SQL
    • 關於 GCP VPC 網路
    • HELM 工具用途
    • 臨時題目:查修 prometheus
    • 繼續離題:繼續查修Prometheus
    • 臨時題目:限定 Pod 訪問外網時,固定 public ip
    • K8s 監控數據來源 Prometheus
    • 監控要告警啊 AlertManager
    • 監控要有圖表啊 Grafana
    • Grafana收集Kubernetes系統資訊
    • 系統 Log 資料
    • 系統 Log 資料收集至 EFK
    • 關於 EFK 角色
    • 系統 Log 資料 - fluent-bit 串接
    • 開放網站對外存取
    • 網站提供 https 安全連線服務 - 憑證管理
    • 網站提供 https 安全連線服務 - 憑證與Ingress整合
    • 網站提供 https 安全連線服務 - Istio 範例
    • 需要額外的 非http 連線
    • Istio 初略介紹
    • Istio 整合 Certmanager DNS01
    • 番外篇:Istio 如何限制訪問來源
    • 番外篇:如何擴充 PV PVC storage size
    • 番外篇:如何利用 Binlog 還原資料庫
  • Helm 實務學習心得
    • Helm requirements 見解
    • 同環境,一次部署多個相同App
  • Python
    • 11-1 Firebase 資料庫簡介
Powered by GitBook
On this page
  • 觀察現況
  • 新增 ReplicaSet 實例
  • Scale Pod 方式一
  • Scale Pod 方式二
  • Scale Pod 方法差異

Pod 副本管理~ 實作篇

觀察現況

我們透過 ReplicaSet 物件提交到 K8s API 的方式建立副本管理。 先行察看兩個物件,可以得知哪一個 pod 尚未套用 ReplicaSet 物件~ kubectl get pods kubectl get rs

[user@minikube ~]$ kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
hello-kitty                       1/1     Running   0          6d
hello-minikube-7c77b68cff-ncqmn   1/1     Running   1          11d
hello-moto-6456dff9f-67jh5        1/1     Running   0          5d

[user@minikube ~]$ kubectl get rs
NAME                        DESIRED   CURRENT   READY   AGE
hello-minikube-7c77b68cff   1         1         1       11d
hello-moto-6456dff9f        1         1         1       5d

另一個方式查看該 pod 是否已有 ReplicaSet 管理 kubectl get pods -o yaml 如果顯示的內容中,有出現kind: ReplicaSet物件,即代表受管理中

  ...
  ownerReferences:
  - apiVersion: extensions/v1beta1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: hello-moto-6456dff9f

新增 ReplicaSet 實例

經比對,pod hello-kitty 尚未套用 ReplicaSet,可以最為下列範例~ 新增 hello-kitty 的 ReplicaSet 物件~rs-hello-kitty.yaml

apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  name: hello-kitty
spec:
  replicas: 2
  template:
    metadata:
      labels:
        run: hello-kitty
    spec:
      containers:
        - name: hello-kitty
          image: "k8s.gcr.io/echoserver:1.10"

套用 ReplicaSet 物件之前 Pod 狀態:

[user@minikube ~]$ kubectl get pods hello-kitty -o wide
NAME          READY   STATUS    RESTARTS   AGE   IP           NODE
hello-kitty   1/1     Running   0          5d    172.17.0.6   minikube

套用 ReplicaSet 物件:

[user@minikube ~]$ kubectl apply -f kk8s/rs-hello-kitty.yaml
replicaset.extensions/hello-kitty created

套用 ReplicaSet 物件之後 Pod 狀態:

[user@minikube ~]$ kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
hello-kitty                       1/1     Running   0          6d
hello-kitty-lk7lg                 1/1     Running   0          1m
hello-kitty-x94s2                 1/1     Running   0          1m
hello-minikube-7c77b68cff-ncqmn   1/1     Running   1          11d
hello-moto-6456dff9f-67jh5        1/1     Running   0          5d

檢查 ReplicaSet,可以看見裏頭的 Labels、replicas 資訊:

[user@minikube ~]$ kubectl describe rs hello-kitty
Name:         hello-kitty
Namespace:    default
Selector:     run=hello-kitty
Labels:       run=hello-kitty
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"extensions/v1beta1","kind":"ReplicaSet","metadata":{"annotations":{},"name":"hello-kitty","namespace":"default"},"spec":{"r...
Replicas:     2 current / 2 desired
Pods Status:  2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  run=hello-kitty
  Containers:
   hello-kitty:
    Image:        k8s.gcr.io/echoserver:1.10
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  84s   replicaset-controller  Created pod: hello-kitty-lk7lg
  Normal  SuccessfulCreate  84s   replicaset-controller  Created pod: hello-kitty-x94s2

Scale Pod 方式一

進行擴展 Pod[2 --> 4],透過指令kubectl scale

[user@minikube ~]$ kubectl scale --replicas=4 rs/hello-kitty
replicaset.extensions/hello-kitty scaled

[user@minikube ~]$ kubectl get rs hello-kitty
NAME          DESIRED   CURRENT   READY   AGE
hello-kitty   4         4         4       9h

[user@minikube ~]$ kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
hello-kitty                       1/1     Running   0          6d
hello-kitty-d7tfq                 1/1     Running   0          22s
hello-kitty-lk7lg                 1/1     Running   0          9h
hello-kitty-x94s2                 1/1     Running   0          9h
hello-kitty-zgrb8                 1/1     Running   0          22s
hello-minikube-7c77b68cff-ncqmn   1/1     Running   1          11d
hello-moto-6456dff9f-67jh5        1/1     Running   0          6d

書中提到上述指令:kubectl scale <rs-name> --replicas=value,因為 K8s 迭代,透過 kubectl scale -h 得知語法已經變更。

Scale Pod 方式二

透過 kubectl apply 進行宣告式scale[4 --> 3] 編輯 rs-hello-kitty.yaml 內容中的 replicas: value 進行管理

apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  name: hello-kitty
spec:
  replicas: 3  # 修改此行 value
# 套用新 replicas value 設定
[user@minikube ~]$ kubectl apply -f kk8s/rs-hello-kitty.yaml
replicaset.extensions/hello-kitty configured

# 確認已經生效,從剛剛稍早 pod hello-kitty 4個,變成 3個。
[user@minikube ~]$ kubectl get rs hello-kitty
NAME          DESIRED   CURRENT   READY   AGE
hello-kitty   3         3         3       10h

# 觀察 pod 變化,副本 hello-kitty-d7tfq 正在消失中,狀態:Terminating
[user@minikube ~]$ kubectl get pods
NAME                              READY   STATUS        RESTARTS   AGE
hello-kitty                       1/1     Running       0          6d
hello-kitty-d7tfq                 1/1     Terminating   0          15m
hello-kitty-lk7lg                 1/1     Running       0          10h
hello-kitty-x94s2                 1/1     Running       0          10h
hello-kitty-zgrb8                 1/1     Running       0          15m
hello-minikube-7c77b68cff-ncqmn   1/1     Running       1          11d
hello-moto-6456dff9f-67jh5        1/1     Running       0          6d

# 觀察 pod 變化,確認副本 pod hello-kitty-XXXXX 剩下3個。
[user@minikube ~]$ kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
hello-kitty                       1/1     Running   0          6d
hello-kitty-lk7lg                 1/1     Running   0          10h
hello-kitty-x94s2                 1/1     Running   0          10h
hello-kitty-zgrb8                 1/1     Running   0          16m
hello-minikube-7c77b68cff-ncqmn   1/1     Running   1          11d
hello-moto-6456dff9f-67jh5        1/1     Running   0          6d

Scale Pod 方法差異

上述用了兩種方式進行 pod scale,kubectl scale --replicas跟kubectl apply -f scale pod 目的與效果是相同的,但其實意義上,是有所不同的~ kubectl scale --replicas指令,適合自己工作上運用,但自己也要清楚當前 replicas valus。 kubectl apply -f是透過宣告式 yaml 檔案進行 replicas valus 管理,非常適合納入版控系統並搭配自動化腳本進行管理。進而更加適合團隊性質的協同合作需求。

你會希望我做過什麼變動,我也必須讓大家知道做過哪些變動,這是 DevOps 精神。

參考書籍: Kubernetes 建置與執行 書中 P.98~102 內容

PreviousPod 副本管理~ 描述 ReplicaSet 控制器Next淺談 DaemonSet,及相對 ReplicaSet 的差異

Last updated 6 years ago