No menu items!
No menu items!
More

    [Kubernetes 5] ReplicaSet trong Kubernetes

    1. ReplicaSet là gì?

    ReplicaSet trong Kubernetes sử dụng để khai báo, triển khai và sử dụng ReplicaSet (RS) để quản lý các POD theo nhãn trên cluster Kubernetes, nó chính là một điều khiển Controller – nó đảm bảo ổn định các nhân bản (số lượng và tình trạng của POD, replica) khi đang chạy.

    Khi định nghĩa một ReplicaSet trong file yaml gồm có trường như selector để chọn ra các các Pod theo label, từ đó nó biết được các Pod nó cần quản lý có số lượng POD có đủ hay không, tình trạng các POD như thế nào. Trong nó nó cũng định nghĩa dữ liệu về Pod trong spec template, để nếu cần tạo Pod mới nó sẽ tạo từ template đó. Khi ReplicaSet tạo, chạy, cập nhật nó sẽ thực hiện tạo/xóa POD với số lượng cần thiết trong khai báo (repilcas).

    2. Thực hành

    Tạo file yaml như dưới và chúng ta cần lưu ý chỗ spec.replicas sẽ khai báo thông tin số pod sẽ được khởi tạo.

    cat > ./prometheus_replicaSet.yaml << OEF
    apiVersion: apps/v1
    kind: ReplicaSet
    metadata:
      name: rs-prometheus
    spec:
      replicas: 3
      selector:
        matchLabels:
          run: prometheus
      template:
        metadata:
          name: rs-prometheus
          labels:
            run: prometheus
        spec:
          containers:
          - name: prometheus
            image: prom/prometheus
            resources:
              limits:
                memory: "128Mi"
                cpu: "100m"
            ports:
              - containerPort: 9090
    OEF

    Triển khai file yaml trên

    root@k8s-standalone:~# kubectl apply -f /home/prometheus_replicaSet.yaml 
    replicaset.apps/rs-prometheus created

    Chúng ta sử dụng lệnh kubectl get rs để thấy có 3 pod đã được tạo

    root@k8s-standalone:~# kubectl get rs
    NAME            DESIRED   CURRENT   READY   AGE
    rs-prometheus   3         3         3       40s

    Hãy thử describe ReplicaSet này

    root@k8s-standalone:~# kubectl describe rs/rs-prometheus
    Name:         rs-prometheus
    Namespace:    default
    Selector:     run=prometheus
    Labels:       <none>
    Annotations:  <none>
    Replicas:     3 current / 3 desired
    Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
    Pod Template:
      Labels:  run=prometheus
      Containers:
       prometheus:
        Image:      prom/prometheus
        Port:       9090/TCP
        Host Port:  0/TCP
        Limits:
          cpu:        100m
          memory:     128Mi
        Environment:  <none>
        Mounts:       <none>
      Volumes:        <none>
    Events:
      Type    Reason            Age   From                   Message
      ----    ------            ----  ----                   -------
      Normal  SuccessfulCreate  63s   replicaset-controller  Created pod: rs-prometheus-xn6wn
      Normal  SuccessfulCreate  63s   replicaset-controller  Created pod: rs-prometheus-wf8kw
      Normal  SuccessfulCreate  63s   replicaset-controller  Created pod: rs-prometheus-7vvj7

    Từ thông tin chi tiết ReplicaSet biết được các thông tin như số Replicas đang có trên số lương yêu cầu, tình trạng các Pod (pod status), nhãn chọn Pod mà nó quản lý.

    Liệt kê các POD có nhãn “run=prometheus”

    root@k8s-standalone:~# kubectl get po -l "run=prometheus"
    NAME                  READY   STATUS    RESTARTS   AGE
    rs-prometheus-7vvj7   1/1     Running   0          2m54s
    rs-prometheus-wf8kw   1/1     Running   0          2m54s
    rs-prometheus-xn6wn   1/1     Running   0          2m54s

    Sau đó thử describe 1 pod

    root@k8s-standalone:~# kubectl describe pod/rs-prometheus-wf8kw
    Name:         rs-prometheus-wf8kw
    Namespace:    default
    Priority:     0
    Node:         k8s-standalone/192.168.13.238
    Start Time:   Sun, 02 Oct 2022 03:52:38 +0000
    Labels:       run=prometheus
    Annotations:  cni.projectcalico.org/containerID: 4cd1679e4bf1ddc8ff0b6223d9c3d8ddddf0cdcdf02010c40f4488aaa20e4d34
                  cni.projectcalico.org/podIP: 10.244.20.141/32
                  cni.projectcalico.org/podIPs: 10.244.20.141/32
    Status:       Running
    IP:           10.244.20.141
    IPs:
      IP:           10.244.20.141
    Controlled By:  ReplicaSet/rs-prometheus
    Containers:
      prometheus:
        Container ID:   containerd://70170e0cb63f29251ab181cda074817ffc9243b19e20f03786c3425b15b76691
        Image:          prom/prometheus
        Image ID:       docker.io/prom/prometheus@sha256:b591915dad4ee2375fbb24cd019c50a546aae561bc63510516efec70d69b4292
        Port:           9090/TCP
        Host Port:      0/TCP
        State:          Running
          Started:      Sun, 02 Oct 2022 03:52:45 +0000
        Ready:          True
        Restart Count:  0
        Limits:
          cpu:     100m
          memory:  128Mi
        Requests:
          cpu:        100m
          memory:     128Mi
        Environment:  <none>
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-t8bg7 (ro)
    Conditions:
      Type              Status
      Initialized       True 
      Ready             True 
      ContainersReady   True 
      PodScheduled      True 
    Volumes:
      kube-api-access-t8bg7:
        Type:                    Projected (a volume that contains injected data from multiple sources)
        TokenExpirationSeconds:  3607
        ConfigMapName:           kube-root-ca.crt
        ConfigMapOptional:       <nil>
        DownwardAPI:             true
    QoS Class:                   Guaranteed
    Node-Selectors:              <none>
    Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
    Events:
      Type    Reason     Age    From               Message
      ----    ------     ----   ----               -------
      Normal  Scheduled  5m2s   default-scheduler  Successfully assigned default/rs-prometheus-wf8kw to k8s-standalone
      Normal  Pulling    5m     kubelet            Pulling image "prom/prometheus"
      Normal  Pulled     4m56s  kubelet            Successfully pulled image "prom/prometheus" in 4.808232505s
      Normal  Created    4m56s  kubelet            Created container prometheus
      Normal  Started    4m55s  kubelet            Started container prometheus

    Bạn thấy dòng Controlled By: ReplicaSet/rs-prometheus điều này có nghĩa POD được kiểm soát điều khiển bởi ReplicaSet có tên rs-prometheus.

    Ta hãy thử xóa 1 pod

    root@k8s-standalone:~# kubectl delete pod/rs-prometheus-7vvj7
    pod “rs-prometheus-7vvj7” deleted

    ReplicaSet rs-prometheus sẽ thay thế nó bởi một POD mới.

    root@k8s-standalone:~# kubectl get po -l 'run=prometheus'
    NAME                  READY   STATUS              RESTARTS   AGE
    rs-prometheus-wf8kw   1/1     Running             0          7m13s
    rs-prometheus-wvqvd   0/1     ContainerCreating   0          4s
    rs-prometheus-xn6wn   1/1     Running             0          7m13s

    Nếu xóa ReplicaSet rs-prometheus thì các POD cũng xóa theo

    root@k8s-standalone:~# kubectl delete -f /home/prometheus_replicaSet.yaml 
    replicaset.apps "rs-prometheus" deleted

    Ta thấy các pod thuộc ReplicaSet rs-prometheus đã bị xóa

    root@k8s-standalone:~# kubectl get po -l 'run=prometheus'
    No resources found in default namespace.

    Tới đây mình đã giới thiệu xong tính năng replicaset trong Kubernetes, chúc các bạn thành công.

    Bài viết gần đây

    spot_img

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Đăng ký nhận thông tin bài viết qua email