k8s之pod控制器

1.生產中,不多會跑一個自主式pod,通常由控制器去建立pod,其配置文件中內嵌了pod的建立方式.html

pod控制器:ReplicaSet、Deployment、DaemonSet、Job、Cronjob、StatefulSetnode

ReplicaSet:代用戶建立指定數量的pod副本數量,確保pod副本數量符合預期狀態,而且支持滾動式自動擴容和縮容功能.nginx

ReplicaSet主要由三個組件組成:web

a.用戶指望的pod副本數量;
b.標籤選擇器,判斷哪一個pod歸本身管理;
c.pod資源模板,當現存的pod數量不足,會根據pod資源模板進行新建.

幫助用戶管理無狀態的pod資源,精確反應用戶定義的目標數量,但RelicaSet不是直接使用的控制器,而是使用Deployment;redis

Deployment:工做在ReplicaSet之上,用於管理無狀態應用,目前來講最好的控制器.支持滾動更新和回滾功能,還提供聲明式配置;docker

DaemonSet:用於確保集羣中的每個節點只運行特定的pod副本,一般用於實現系統級後臺任務,好比ELK中負責收集日誌filebeat,特性:服務是無狀態的,服務必須是守護進程;api

Job:只要完成就當即退出,不須要重啓或重建;bash

Cronjob:週期性任務控制,不須要持續後臺運行;網絡

StatefulSet:管理有狀態應用.app

2.ReplicaSet(rs)示例

cat rs-demo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-myapp
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      run: myapp
      release: canary
  template:
    metadata:
      name: whatever
      labels:
        run: myapp
        release: canary
        env: test
    spec:
      containers:
      - name: nginx-web
        image: ikubernetes/myapp:v1
        ports:
        - name: http
          containerPort: 80
template:資源模板中定義的name其實不生效,pod運行起來以後,真正的名字是控制器名+隨機字符串
kubectl create -f rs-demo.yaml
kubectl get pods -o wide
NAME             READY   STATUS      RESTARTS   AGE     IP           NODE     
rs-myapp-2hxc9   1/1     Running     0          74s     10.244.2.7   k8s-node2
rs-myapp-d6845   1/1     Running     0          74s     10.244.1.9   k8s-node1
curl 10.244.2.7
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
# 編輯replicatset的配置文件,這個文件不是咱們手工建立的,而是apiserver維護的,修改副本數
kubectl edit rs myapp
# 也能夠升級版本,將v1改爲v2,但只有pod重建後,好比增長或刪除Pod,纔會更新成v2版本

3.Deployment控制器

圖解:經過Deployment控制器來動態更新pod版本,Deployment下有衆多replicatset,但只有一個是激活的,更改配置文件中的鏡像版本,就會一個一個的刪除replicatset v1版本中的Pod,自動新建立的pod就會變成v2版本,當pod所有變成v2版本後,replicatset v1不會被刪除,這樣一旦發現v2版本有問題,還能夠回退到v1版本,一般deployment默認保留10個版本的replicatset.

kubectl explain deploy  # 文檔是落後於k8s版本的
deploy示例:
cat deploy-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      labels:
        app: myapp
        release: canary
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        ports:
        - name: http
          containerPort: 80
# apply:聲明式更新和建立,能夠應用屢次,create只能用一次
kubectl apply -f deploy-demo.yaml
kubectl get deploy  # 會看見一個名爲myapp-deploy的deploy生成
kubectl get rs
NAME                      DESIRED   CURRENT   READY     AGE
myapp-deploy-574965d786   2         2         2         93s
kubectl get pods
NAME                            READY     STATUS    RESTARTS   AGE
myapp-deploy-574965d786-5x42g   1/1       Running   0          70s
myapp-deploy-574965d786-dqzpd   1/1       Running   0          70s

# 默認滾動策略是RollingUpdate,查看滾動更新的歷史
kubectl rollout history deployment myapp-deploy

# 若是要修改副本數,則編輯deploy-demo.yaml修改副本數,或者:
kubectl patch deployment myapp-deploy -p '{"spec":{"replicas":5}}'
# 給更新策略打補丁:
kubectl patch deployment myapp-deploy -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}'
# maxSurge:pod的數量最多可超出指望值多少個;maxUnavailable:最多不可用的pod有多少個.

# 金絲雀發佈,先發佈一個,此時多了一個pod,如今有6個
# 用set image命令將鏡像myapp升級爲v3版本,而且將myapp-deploy控制器標記爲暫停,
# 被暫停的資源不會被控制器使用,可使"kubectl rollout resume"命令恢復已暫停資源
kubectl set image deployment myapp-deploy myapp=ikubernetes/myapp:v3 
&& kubectl rollout pause deployment myapp-deploy 
kubectl get pods -l app=myapp -w
# resume:繼續,從新開始,能夠看到繼續更新,刪一個更新一個
kubectl rollout status deployment myapp-deploy 
kubectl rollout resume deployment myapp-deploy
# 查看副本集的詳細信息
kubectl get rs -o wide
# 版本回滾
kubectl rollout history deployment myapp-deploy
kubectl rollout undo deployment myapp-deploy --to-revision=1

4.DaemonSet示例

# node一、node2下載filebeat鏡像
docker pull ikubernetes/filebeat:5.6.5-alpine
cat ds-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
      role: logstor
  template:
    metadata:
      labels:
        app: redis
        role: logstor
    spec:
      containers:
      - name: redis
        image: redis:4.0-alpine
        ports:
        - name: redis
          containerPort: 6379
---  # 減號隔離不一樣資源定義
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: filebeat-ds
  namespace: default
spec:
  selector:
    matchLabels:
      app: filebeat
      release: stable
  template:
    metadata:
       labels:
         app: filebeat
         release: stable
    spec:
      containers:
      - name: filebeat
        image: ikubernetes/filebeat:5.6.5-alpine
        env:
        - name: REDIS_HOST
          value: redis.default.svc.cluster.local
        - name: REDIS_LOG_LEVEL
          value: info

# REDIS_HOST這個鍵值對是filebeat-ds向filebeat配置文件傳的環境變量
kubectl apply -f ds-demo.yaml
# 暴露redis端口
kubectl expose deployment redis --port=6379
# 進入redis
kubectl exec -it redis-664bbc646b-sg6wk -- /bin/sh
/data # redis-cli -h redis.default.svc.cluster.local
# 進入filebeat
kubectl exec -it filebeat-ds-bszfz -- /bin/sh
nslookup redis.default.svc.cluster.local
# daemon-set也支持滾動更新
kubectl set image daemonsets filebeat-ds filebeat=ikubernetes/filebeat:5.5.7-alpine
kubectl explain pods.spec # 有一個字段hostNetwork,可讓容器直接共享宿主機的網絡
注:不一樣pod之間通訊,filebeat向redis發送日誌靠的是service

 

參考博客:http://blog.itpub.net/28916011/viewspace-2214692/

相關文章
相關標籤/搜索