kubenetes入門學習-七-Pod控制器--控制器類型以及功能-ReplicaSet

Pod是標準的k8s資源,遵守資源清單
回顧
主要字段
  apiVersion、kind、metadata、spec、status(只讀)
  spec:
      containers
      nodeSelector
      nodeName
      restartPolicy:
          Always、Never、OnFailure
      
      containers:
          name
          image
          iamgePullPolicy:Always、Never、IfNotPersent
          ports:
              name
              containerPort
          livenessProbe
          readinessProbe
          liftcycle
====================================================
Pod控制器:
    Pod控制器是用於實現管理pod的中間層,確保pod資源符合預期的狀態,pod的資源出現故障時,會嘗試 進行重啓,當根據重啓策略無效,則會重新新建pod的資源。
    
pod控制器常用類型如下:
ReplicaSet: 代用戶創建指定數量的pod副本數量,確保pod副本數量符合預期狀態,並且支持滾動式自動擴容和縮容功能。
ReplicaSet主要三個組件組成:
  (1)用戶期望的pod副本數量    
  (2)標籤選擇器,判斷哪個pod歸自己管理    
  (3)當現存的pod數量不足,會根據pod資源模板進行新建
幫助用戶管理無狀態的pod資源,精確反應用戶定義的目標數量,但是RelicaSet不是直接使用的控制器,而是使用Deployment。

Deployment:工作在ReplicaSet之上,用於管理無狀態應用,目前來說最好的控制器。支持滾動更新和回滾功能,還提供聲明式配置。
     可以隨時聲明,更改配置。
     可以創建多餘node節點的pod副本數量,這樣一個node可以運行多個副本也可以一個都不運行。
     

DaemonSet:用於確保集羣中的每一個節點只運行特定的pod副本,通常用於實現系統級後臺任務。比如ELK服務,log收集agent,每個節點上收集容器和node級別的log,一個node上部署一個agent。
     特性:服務是無狀態的
     服務必須是守護進程,必須隨時運行。
     可以託管在k8s上,宕了可以重建,系統級別功能常用此控制器。

Job:只要完成就立即退出,不需要重啓或重建。

Cronjob:週期性任務控制,不需要持續後臺運行。

StatefulSet:管理有狀態應用,每個pod副本被單獨管理。配置管理mysql、redis不一樣的,部署方式也可能不一樣。
             封裝控制器,把人爲操作封裝爲腳本,故障的時候他通過腳本自動恢復,感覺這個比較複雜。
             
TPR:第三方資源,及自定義資源。 1.2+  1.7之後被費了

CDR:第三方資源,及自定義資源。1.8+

Operator:目前應用很少。

有狀態應用託管在k8s之上還是一個挑戰。
=============================
k8s自己寫資源清單導致難以入門,後面就提供了helm,這樣就簡單了。

helm:類是於yum之類的功能,當然也不是全依賴他。
==============================
以下幾個命令就能說明資源清單yaml文件的層級
命令行查看ReplicaSet清單定義規則
[root@k8s-master ~]# kubectl explain rs
[root@k8s-master ~]# kubectl explain rs.spec
[root@k8s-master ~]# kubectl explain rs.spec.template
==============================
(1)命令行查看ReplicaSet清單定義規則
[root@k8s-master ~]# kubectl explain rs
[root@k8s-master ~]# kubectl explain rs.spec
[root@k8s-master ~]# kubectl explain rs.spec.template

(2)新建ReplicaSet示例
[root@k8s-master ~]# vim rs-demo.yaml
apiVersion: apps/v1  #api版本定義
kind: ReplicaSet  #定義資源類型爲ReplicaSet
metadata:  #元數據定義
    name: myapp
    namespace: default
spec:  #ReplicaSet的規格定義
    replicas: 2  #定義副本數量爲2個
    selector:    #標籤選擇器,定義匹配pod的標籤
        matchLabels:
            app: myapp
            release: canary      這裏很神奇,標籤是幾個,pod就是幾個下面有實驗
    template:  #pod的模板定義
        metadata:  #pod的元數據定義
            name: myapp-pod   #自定義pod的名稱 
            labels:   #定義pod的標籤,需要和上面定義的標籤一致,也可以多出其他標籤
                app: myapp
                release: canary
                environment: qa
        spec:  #pod的規格定義
            containers:  #容器定義
            - name: myapp-container  #容器名稱
              image: ikubernetes/myapp:v1  #容器鏡像
              ports:  #暴露端口
              - name: http
                containerPort: 80

(3)創建ReplicaSet定義的pod                
[root@k8s-master ~]# kubectl create -f rs-demo.yaml
[root@k8s-master ~]# kubectl get pods  #獲取pod信息
[root@k8s-master ~]# kubectl describe pods myapp-***  #查看pod詳細信息

(4)標籤( 這裏很神奇,標籤是幾個,pod就是幾個下面有實驗)
[root@master ~]# kubectl get pods --show-labels    標籤爲app=myapp,release=canary的兩個myapp
[root@master ~]# kubectl label pods readiness-httpget-pod release=canary
[root@master ~]# kubectl label pods readiness-httpget-pod app=myapp
[root@master ~]# kubectl get pods --show-labels    這裏發現readiness-httpget-pod 他已經滿足上面標籤選擇器的條件,上面myapp刪除一個
生產中標籤最好使用複雜條件,不然容易衝突了
這裏實驗完成,還是刪除上面這個pod,會自動把myapp恢復

這裏一個控制器創建兩個pod,分別訪問兩個pod,但是當pod被刪除的時候,會自動創建新的pod,這時候訪問就不方便了,這就應該出現service,當然service使用和pod相同的標籤選擇器,訪問的時候訪問service就不會變了。

(5)修改pod的副本數量
[root@k8s-master ~]# kubectl edit rs myapp
replicas: 5
[root@k8s-master ~]# kubectl get rs -o wide

(6)修改pod的鏡像版本
[root@k8s-master ~]# kubectl edit rs myapp
image: ikubernetes/myapp:v2  
[root@k8s-master ~]# kubectl delete pods myapp-***   #修改了pod鏡像版本,pod需要重建才能達到最新版本
[root@k8s-master ~]# kubectl create -f rs-demo.yaml

刪一個刪一個 這種是金絲雀發佈
只刪除一個 灰度
一次全部刪除,這種小心哦
最妥當的方式創建兩組rs,藍綠髮布

一個deployment可以管理多個rs
=========================================================
實驗  https://www.cnblogs.com/linuxk/p/9578211.html 這個師兄整理的比較好
[root@master ~]# kubectl explain rs
KIND:     ReplicaSet
VERSION:  extensions/v1beta1

DESCRIPTION:
     DEPRECATED - This group version of ReplicaSet is deprecated by
     apps/v1beta2/ReplicaSet. See the release notes for more information.
     ReplicaSet ensures that a specified number of pod replicas are running at
     any given time.

FIELDS:
   apiVersion    <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#resources

   kind    <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds

   metadata    <Object>
     If the Labels of a ReplicaSet are empty, they are defaulted to be the same
     as the Pod(s) that the ReplicaSet manages. Standard object's metadata. More
     info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata

   spec    <Object>
     Spec defines the specification of the desired behavior of the ReplicaSet.
     More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

   status    <Object>
     Status is the most recently observed status of the ReplicaSet. This data
     may be out of date by some window of time. Populated by the system.
     Read-only. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

[root@master ~]# kubectl explain rs.spec
KIND:     ReplicaSet
VERSION:  extensions/v1beta1

RESOURCE: spec <Object>

DESCRIPTION:
     Spec defines the specification of the desired behavior of the ReplicaSet.
     More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

     ReplicaSetSpec is the specification of a ReplicaSet.

FIELDS:
   minReadySeconds    <integer>
     Minimum number of seconds for which a newly created pod should be ready
     without any of its container crashing, for it to be considered available.
     Defaults to 0 (pod will be considered available as soon as it is ready)

   replicas    <integer>
     Replicas is the number of desired replicas. This is a pointer to
     distinguish between explicit zero and unspecified. Defaults to 1. More
     info:
     https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#what-is-a-replicationcontroller

   selector    <Object>
     Selector is a label query over pods that should match the replica count. If
     the selector is empty, it is defaulted to the labels present on the pod
     template. Label keys and values that must match in order to be controlled
     by this replica set. More info:
     https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors

   template    <Object>
     Template is the object that describes the pod that will be created if
     insufficient replicas are detected. More info:
     https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#pod-template
[root@master ~]# kubectl explain rs.spec.template
KIND:     ReplicaSet
VERSION:  extensions/v1beta1

RESOURCE: template <Object>

DESCRIPTION:
     Template is the object that describes the pod that will be created if
     insufficient replicas are detected. More info:
     https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#pod-template

     PodTemplateSpec describes the data a pod should have when created from a
     template

FIELDS:
   metadata    <Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata

   spec    <Object>
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
================================================
新建ReplicaSet示例
[root@master manifests]# vim rs-demo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
    name: myapp
    namespace: default
spec:
    replicas: 2
    selector:
        matchLabels:
            app: myapp
            release: canary
    template:
        metadata:
            name: myapp-pod
            labels:
                app: myapp
                release: canary
                environment: qa
        spec:
            containers:
                name: myapp-container
                image: nginx
                ports:
                -   name: http
                    containerPort: 80
[root@master manifests]# kubectl create -f rs-demo.yaml 
replicaset.apps/myapp created
[root@master manifests]# kubectl get pods
NAME                            READY   STATUS              RESTARTS   AGE
liveness-httpget-pod            1/1     Running             1          3d1h
myapp-5xx8l                     0/1     ContainerCreating   0          11s
myapp-dglps                     1/1     Running             0          11s
nginx-7849c4bbcd-dscjr          1/1     Running             0          6d22h
nginx-7849c4bbcd-vdd45          1/1     Running             0          6d22h
nginx-7849c4bbcd-wrvks          1/1     Running             0          6d22h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running             0          6d23h
readiness-httpget-pod           1/1     Running             0          3d1h
[root@master manifests]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
liveness-httpget-pod            1/1     Running   1          3d1h
myapp-5xx8l                     1/1     Running   0          14s
myapp-dglps                     1/1     Running   0          14s
nginx-7849c4bbcd-dscjr          1/1     Running   0          6d22h
nginx-7849c4bbcd-vdd45          1/1     Running   0          6d22h
nginx-7849c4bbcd-wrvks          1/1     Running   0          6d22h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          6d23h
readiness-httpget-pod           1/1     Running   0          3d1h
[root@master manifests]# kubectl get pods,svc
NAME                                READY   STATUS    RESTARTS   AGE
pod/liveness-httpget-pod            1/1     Running   1          3d1h
pod/myapp-5xx8l                     1/1     Running   0          65s
pod/myapp-dglps                     1/1     Running   0          65s
pod/nginx-7849c4bbcd-dscjr          1/1     Running   0          6d22h
pod/nginx-7849c4bbcd-vdd45          1/1     Running   0          6d22h
pod/nginx-7849c4bbcd-wrvks          1/1     Running   0          6d22h
pod/nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          6d23h
pod/readiness-httpget-pod           1/1     Running   0          3d1h

NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
service/kubernetes     ClusterIP   10.96.0.1        <none>        443/TCP   8d
service/nginx-deploy   ClusterIP   10.100.251.191   <none>        80/TCP    7d23h

[root@master manifests]# kubectl describe pod/myapp-5xx8l
Name:               myapp-5xx8l
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               node01/10.249.6.101
Start Time:         Fri, 08 Mar 2019 10:25:32 -0500
Labels:             app=myapp
                    environment=qa
                    release=canary
Annotations:        <none>
Status:             Running
IP:                 10.244.1.19
Controlled By:      ReplicaSet/myapp
Containers:
  myapp-container:
    Container ID:   docker://f14ee330ebb3c033cbf943489138af8be07c67acd4538c01d43ac8cce00225dc
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:98efe605f61725fd817ea69521b0eeb32bef007af0e3d0aeb6258c6e6fe7fc1a
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 08 Mar 2019 10:25:44 -0500
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-6q28w (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-6q28w:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-6q28w
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  2m35s  default-scheduler  Successfully assigned default/myapp-5xx8l to node01
  Normal  Pulling    2m34s  kubelet, node01    pulling image "nginx"
  Normal  Pulled     2m24s  kubelet, node01    Successfully pulled image "nginx"
  Normal  Created    2m24s  kubelet, node01    Created container
  Normal  Started    2m23s  kubelet, node01    Started container
刪除測試(這邊刪除的時候那邊已經新的已經建起來了)
[root@master manifests]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
liveness-httpget-pod            1/1     Running   1          3d1h
myapp-5xx8l                     1/1     Running   0          5m13s
myapp-dglps                     1/1     Running   0          5m13s
nginx-7849c4bbcd-dscjr          1/1     Running   0          6d22h
nginx-7849c4bbcd-vdd45          1/1     Running   0          6d22h
nginx-7849c4bbcd-wrvks          1/1     Running   0          6d22h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          6d23h
readiness-httpget-pod           1/1     Running   0          3d1h
[root@master manifests]# kubectl delete pods myapp-5xx8l
pod "myapp-5xx8l" deleted

一邊查看
[root@master ~]# kubectl get pods
NAME                            READY   STATUS        RESTARTS   AGE
liveness-httpget-pod            1/1     Running       1          3d1h
myapp-5xx8l                     0/1     Terminating   0          5m34s  這裏還沒完全刪除
myapp-dglps                     1/1     Running       0          5m34s
myapp-zn46b                     1/1     Running       0          7s     這裏果然新建了一個
nginx-7849c4bbcd-dscjr          1/1     Running       0          6d22h
nginx-7849c4bbcd-vdd45          1/1     Running       0          6d22h
nginx-7849c4bbcd-wrvks          1/1     Running       0          6d22h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running       0          6d23h
readiness-httpget-pod           1/1     Running       0          3d1h

神奇的標籤選擇器
[root@master ~]# kubectl get pods --show-labels
NAME                            READY   STATUS    RESTARTS   AGE     LABELS
liveness-httpget-pod            1/1     Running   1          3d1h    <none>
myapp-dglps                     1/1     Running   0          12m     app=myapp,environment=qa,release=canary
myapp-zn46b                     1/1     Running   0          6m53s   app=myapp,environment=qa,release=canary
nginx-7849c4bbcd-dscjr          1/1     Running   0          6d22h   pod-template-hash=7849c4bbcd,run=nginx
nginx-7849c4bbcd-vdd45          1/1     Running   0          6d22h   pod-template-hash=7849c4bbcd,run=nginx
nginx-7849c4bbcd-wrvks          1/1     Running   0          6d22h   pod-template-hash=7849c4bbcd,run=nginx
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          7d      pod-template-hash=84cbfc56b6,release=canary,run=nginx-deploy
readiness-httpget-pod           1/1     Running   0          3d1h    <none>
[root@master ~]# kubectl label pods readiness-httpget-pod release=canary
pod/readiness-httpget-pod labeled
[root@master ~]# kubectl get pods --show-labels
NAME                            READY   STATUS    RESTARTS   AGE     LABELS
liveness-httpget-pod            1/1     Running   1          3d1h    <none>
myapp-dglps                     1/1     Running   0          14m     app=myapp,environment=qa,release=canary
myapp-zn46b                     1/1     Running   0          9m3s    app=myapp,environment=qa,release=canary
nginx-7849c4bbcd-dscjr          1/1     Running   0          6d22h   pod-template-hash=7849c4bbcd,run=nginx
nginx-7849c4bbcd-vdd45          1/1     Running   0          6d22h   pod-template-hash=7849c4bbcd,run=nginx
nginx-7849c4bbcd-wrvks          1/1     Running   0          6d22h   pod-template-hash=7849c4bbcd,run=nginx
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          7d      pod-template-hash=84cbfc56b6,release=canary,run=nginx-deploy
readiness-httpget-pod           1/1     Running   0          3d1h    release=canary  有一個了,不滿足,繼續
[root@master ~]# kubectl label pods readiness-httpget-pod app=myapp
pod/readiness-httpget-pod labeled
[root@master ~]# kubectl get pods --show-labels
NAME                            READY   STATUS    RESTARTS   AGE     LABELS
liveness-httpget-pod            1/1     Running   1          3d1h    <none>
myapp-dglps                     1/1     Running   0          15m     app=myapp,environment=qa,release=canary
nginx-7849c4bbcd-dscjr          1/1     Running   0          6d22h   pod-template-hash=7849c4bbcd,run=nginx
nginx-7849c4bbcd-vdd45          1/1     Running   0          6d22h   pod-template-hash=7849c4bbcd,run=nginx
nginx-7849c4bbcd-wrvks          1/1     Running   0          6d22h   pod-template-hash=7849c4bbcd,run=nginx
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          7d      pod-template-hash=84cbfc56b6,release=canary,run=nginx-deploy
readiness-httpget-pod           1/1     Running   0          3d1h    app=myapp,release=canary 滿足了,發現上面的myapp pod少了一個,上面yaml定義的2,說明標籤選擇器生效

[root@master ~]# kubectl delete pods readiness-httpget-pod
pod "readiness-httpget-pod" deleted
[root@master manifests]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
liveness-httpget-pod            1/1     Running   1          3d1h
myapp-4j4rp                     1/1     Running   0          6s
myapp-dglps                     1/1     Running   0          22m
nginx-7849c4bbcd-dscjr          1/1     Running   0          6d23h
nginx-7849c4bbcd-vdd45          1/1     Running   0          6d23h
nginx-7849c4bbcd-wrvks          1/1     Running   0          6d23h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          7d

================================
增加副本
[root@master manifests]# kubectl edit rs myapp
replicas: 2 修改爲5
replicaset.extensions/myapp edited
[root@master manifests]# kubectl get pods
NAME                            READY   STATUS              RESTARTS   AGE
liveness-httpget-pod            1/1     Running             1          3d2h
myapp-4j4rp                     1/1     Running             0          14m
myapp-7k2s5                     0/1     ContainerCreating   0          3s
myapp-87tgz                     0/1     ContainerCreating   0          3s
myapp-9sf9b                     0/1     ContainerCreating   0          3s
myapp-dglps                     1/1     Running             0          36m
nginx-7849c4bbcd-dscjr          1/1     Running             0          6d23h
nginx-7849c4bbcd-vdd45          1/1     Running             0          6d23h
nginx-7849c4bbcd-wrvks          1/1     Running             0          6d23h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running             0          7d

================================
升級演示,我沒有環境,我就把nginx換成apache試試  這種方法可用於升級版本,和之前命令一樣的效果
[root@master manifests]# kubectl edit rs myapp
        spec:
            containers:
                name: myapp-container
                image: nginx   這裏修改爲apache
replicaset.extensions/myapp edited
[root@master manifests]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
liveness-httpget-pod            1/1     Running   1          3d2h
myapp-4j4rp                     1/1     Running   0          16m
myapp-7k2s5                     1/1     Running   0          2m36s
myapp-87tgz                     1/1     Running   0          2m36s
myapp-9sf9b                     1/1     Running   0          2m36s
myapp-dglps                     1/1     Running   0          39m
nginx-7849c4bbcd-dscjr          1/1     Running   0          6d23h
nginx-7849c4bbcd-vdd45          1/1     Running   0          6d23h
nginx-7849c4bbcd-wrvks          1/1     Running   0          6d23h
nginx-deploy-84cbfc56b6-mjcw5   1/1     Running   0          7d

[root@master manifests]# kubectl get rs -o wide
NAME                      DESIRED   CURRENT   READY   AGE     CONTAINERS        IMAGES              SELECTOR
myapp                     5         5         5       39m     myapp-container   apache 這裏已經是apache              app=myapp,release=canary
nginx-775ff75bc8          0         0         0       6d23h   nginx             nginx               pod-template-hash=775ff75bc8,run=nginx
nginx-7849c4bbcd          3         3         3       6d23h   nginx             nginx:1.14-alpine   pod-template-hash=7849c4bbcd,run=nginx
nginx-deploy-84cbfc56b6   1         1         1       8d      nginx-deploy      nginx:1.14-alpine   pod-template-hash=84cbfc56b6,run=nginx-deploy
這裏只是rs更新了,但是實際pod並沒有部署,需要刪除老的,自動創建新的

[root@master manifests]# curl 10.244.1.21
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

 

羣名稱:k8s學習羣   羣   號:153144292

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章