1. 程式人生 > >Pod控制器應用進階三

Pod控制器應用進階三

spec:

spec:
    name
    image
    imagePullPolicy:Always,Never,IfNotPresent
    ports:
        name
        containerPort
    livenessProbe
    readinessProbe
    liftcycle
ExecAction: exec
TCPSocketAction: tcpSocker
HTTPGetAction: httpGet

 

Pod控制器
    ReplicaSet:建立一定數量的pod,支援擴縮容
    主要有三個元件:1,使用者期望的pod副本 2,標籤選擇器 3.如果pod不夠怎麼辦,擴建
    Pod資源模板可以幫助使用者新建
    Deployment可以比ReplicaSet更加強大,增加宣告式配置和升級後的回滾,是目前最好的Pod控制器
    DaemonSet:系統級的Pod,只能在node上執行一個,每個叢集精確執行一個
    Job:只能執行執行一次的,比如這個程式執行完了,這個容器就沒有用了
    CronJob:週期性執行Job和CronJob都是不用長期執行的容器
    StatetufulSet:執行mysql,redis
    有狀態的運維任務時很困難的,比如mysql和redis叢集,一般轉換成指令碼,有狀態的應用託管到K8s上是很難的

    Deployment在更新中展示強大的功能

[[email protected] test]# cat 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: ikubernetes/myapp:v1
          ports:
          - name: http
            containerPort: 80
[[email protected] test]# kubectl get pod
NAME                          READY     STATUS    RESTARTS   AGE
client                        1/1       Running   0          1d
myapp-ffw8l                   1/1       Running   0          9s
myapp-rz67d                   1/1       Running   0          9s
readiness-httpget-container   1/1       Running   0          4h

[[email protected] test]# kubectl get rs
NAME      DESIRED   CURRENT   READY     AGE
myapp     2         2         2         1m

獲取其中的一個Pod資訊,檢視詳情
[[email protected] test]# kubectl describe pods myapp-ffw8l
Name:               myapp-ffw8l
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               node2/192.168.68.30
Start Time:         Thu, 06 Sep 2018 15:57:31 +0800
Labels:             app=myapp
                    environment=qa
                    release=canary
Annotations:        <none>
Status:             Running
IP:                 10.244.2.17
Controlled By:      ReplicaSet/myapp
Containers:
  myapp-container:
    Container ID:   docker://787f74a16dfdcbe9c72a0dc1d962d6b4fbb050724108cf73190f2a67afd5440c
    Image:          ikubernetes/myapp:v1
    Image ID:       docker-pullable://ikubernetes/[email protected]:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Thu, 06 Sep 2018 15:57:32 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-8zzcr (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-8zzcr:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-8zzcr
    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  2m    default-scheduler  Successfully assigned default/myapp-ffw8l to node2
  Normal  Pulled     2m    kubelet, node2     Container image "ikubernetes/myapp:v1" already present on machine
  Normal  Created    2m    kubelet, node2     Created container
  Normal  Started    2m    kubelet, node2     Started containe


[[email protected] test]# curl 10.244.2.17
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[[email protected] test]# curl 10.244.2.17/hostname.html
myapp-ffw8l


如果刪除一個Pod,他會自動建立一個
[[email protected] test]# kubectl get pod
NAME                          READY     STATUS    RESTARTS   AGE
client                        1/1       Running   0          1d
myapp-ffw8l                   1/1       Running   0          7m
myapp-rz67d                   1/1       Running   0          7m
readiness-httpget-container   1/1       Running   0          4h
[[email protected] test]# kubectl delete pods myapp-ffw8l
pod "myapp-ffw8l" deleted
[[email protected] test]# kubectl get pod
NAME                          READY     STATUS    RESTARTS   AGE
client                        1/1       Running   0          1d
myapp-r99dr                   1/1       Running   0          4s
myapp-rz67d                   1/1       Running   0          8m
readiness-httpget-container   1/1       Running   0          4h
刪除Pod之前就已經建立了

kubectl get pods --show-labels
[[email protected] test]# kubectl get pods --show-labels
NAME                          READY     STATUS    RESTARTS   AGE       LABELS
client                        1/1       Running   0          1d        run=client
myapp-r99dr                   1/1       Running   0          1m        app=myapp,environment=qa,release=canary
myapp-rz67d                   1/1       Running   0          10m       app=myapp,environment=qa,release=canary
readiness-httpget-container   1/1       Running   0          4h        <none>

[[email protected] test]# kubectl label pods client release=canary
pod/client labeled
[[email protected] test]# kubectl get pods --show-labels
NAME                          READY     STATUS    RESTARTS   AGE       LABELS
client                        1/1       Running   0          1d        release=canary,run=client
myapp-r99dr                   1/1       Running   0          3m        app=myapp,environment=qa,release=canary
myapp-rz67d                   1/1       Running   0          11m       app=myapp,environment=qa,release=canary
readiness-httpget-container   1/1       Running   0          4h        <none>

自動擴容:將myapp擴容到10個
kubectl edit rs myapp
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  creationTimestamp: 2018-09-06T07:57:31Z
  generation: 2
  labels:
    app: myapp
    environment: qa
    release: canary
  name: myapp
  namespace: default
  resourceVersion: "88845"
  selfLink: /apis/extensions/v1beta1/namespaces/default/replicasets/myapp
  uid: 81fa7645-b1aa-11e8-a432-000c29f33006
spec:
  replicas: 10
  selector:
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: myapp
        environment: qa
        release: canary
      name: myapp-pod
    spec:
      containers:
      - image: ikubernetes/myapp:v1
 imagePullPolicy: IfNotPresent
        name: myapp-container
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 10
  fullyLabeledReplicas: 10
  observedGeneration: 2
  readyReplicas: 10
  replicas: 10

 我們檢視一下:
 [[email protected] test]# kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
client                        1/1       Running   0          1d
myapp-7fhdt                   1/1       Running   0          10s
myapp-8dnzm                   1/1       Running   0          10s
myapp-92cnh                   1/1       Running   0          10s
myapp-9dvll                   1/1       Running   0          10s
myapp-h55jv                   1/1       Running   0          10s
myapp-kw656                   1/1       Running   0          10s
myapp-r99dr                   1/1       Running   0          7m
myapp-rz67d                   1/1       Running   0          15m
myapp-v62f6                   1/1       Running   0          10s
myapp-zkk7k                   1/1       Running   0          10s
readiness-httpget-container   1/1       Running   0          5h


我們將版本升級成v2
kubectl edit rs myapp
 image: ikubernetes/myapp:v2


我們檢視一下:
[[email protected] test]# kubectl get rs -o wide
NAME      DESIRED   CURRENT   READY     AGE       CONTAINERS        IMAGES                 SELECTOR
myapp     10        10        10        18m       myapp-container   ikubernetes/myapp:v2   app=myapp,release=canary
雖然這裡是V2,但是我們訪問的時候還是v1
[[email protected] test]# curl 10.244.2.20
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[[email protected] test]# curl 10.244.2.20/hostname.html
myapp-7fhdt
[[email protected] test]# kubectl delete pods myapp-7fhdt
pod "myapp-7fhdt" deleted
[ro[email protected] test]# kubectl get rs -o wide
NAME      DESIRED   CURRENT   READY     AGE       CONTAINERS        IMAGES                 SELECTOR
myapp     10        10        10        20m       myapp-container   ikubernetes/myapp:v2   app=myapp,release=canary
[[email protected] test]# kubectl get pods -o wide
NAME                          READY     STATUS    RESTARTS   AGE       IP            NODE      NOMINATED NODE
client                        1/1       Running   0          1d        10.244.2.3    node2     <none>
myapp-8dnzm                   1/1       Running   0          5m        10.244.1.12   node1     <none>
myapp-92cnh                   1/1       Running   0          5m        10.244.1.13   node1     <none>
myapp-9dvll                   1/1       Running   0          5m        10.244.2.21   node2     <none>
myapp-h55jv                   1/1       Running   0          5m        10.244.2.19   node2     <none>
myapp-kw656                   1/1       Running   0          5m        10.244.1.15   node1     <none>
myapp-r99dr                   1/1       Running   0          12m       10.244.2.18   node2     <none>
myapp-rz67d                   1/1       Running   0          21m       10.244.1.11   node1     <none>
myapp-v62f6                   1/1       Running   0          5m        10.244.2.22   node2     <none>
myapp-x4hwp                   1/1       Running   0          29s       10.244.2.23   node2     <none>
myapp-zkk7k                   1/1       Running   0          5m        10.244.1.14   node1     <none>
readiness-httpget-container   1/1       Running   0          5h        10.244.2.15   node2     <none>
[[email protected] test]# curl 10.244.2.23/hostname.html
myapp-x4hwp
[[email protected] test]# curl 10.244.2.23
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>

也就是我們只有在刪除容器時候在建立的時候才是v2版本,如果不刪除還是v1版本,這樣適合做藍綠髮布,可以做滾動式更新,也可控制更新節奏