kkwen

kubernetes之ReplicaSet、Deployment、DaemonSet
集群中各个对象的状态如何才能符合用户期望的状态,则需要交由控制器组件来完成,kubernetes提供了众多控制器组件
扫描右侧二维码阅读全文
06
2019/03

kubernetes之ReplicaSet、Deployment、DaemonSet

集群中各个对象的状态如何才能符合用户期望的状态,则需要交由控制器组件来完成,kubernetes提供了众多控制器组件

Pod控制器分类

  • ReplicationController:早期的控制器,设计过于庞大,先不使用
  • ReplicaSet:代用户创建指定副本,确保满足用户期望,支持自动扩缩容,
重要字段:用户期望副本数、标签选择器、Pod资源模板
  • 无状态控制器 Deployment:
Deployment: 构建于ReplicaSet之上,支持滚动更新、回滚、声明式配置
  • 有状态控制器 statefulSet
statefulSet:管理有状态应用,每一个Pod副本都是被单独管理的,Pod重建需要做很多初始化操作
    TTR:第三方资源,1.2+, 1.7
    CDR:Custom Defined Resources, 1.8+    :可以讲运维操作自定义逻辑
    Operator:封装运维技能,但暂时只支持ETCD、prometheus等
    Helm:类似于yum
  • DaemonSet:在集群中的每个节点上,只运行一个Pod副本,或者在某些符合需求的节点上运行一个Pod
  • Job:一次性运行任务的Pod
  • Cronjob: 周期性运行的Job

### ReplicaSet和Deployment
- 在旧版本的kubernetes中只有ReplicationController对象,它的作用简单来说是确保由其管控的Pod对象在任何时刻都能精确满足用户期望的数量
- 新版本由ReplicaSet取代ReplicationController,在此之上额外支持基于集合(set-based)的标签选择器
- Deployment控制器构建于ReplicaSet控制器之上,它提供了滚动更新、回滚机制
- 图示
![deployment和replicaSet.png](https://www.kkwen.cn/usr/uploads/2019/03/3422414184.png)
### ReplicaSet资源清单示例

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

### Deployment详解
- 群组 apiVersion: apps/v1
- 重要字段: strategy、revisionHistoryLimit、paused

strategy

type:
    Recreate: 重建更新,删除一个,新建一个
    RollingUpdate:滚动更新
        maxSurge: 更新过程中最多超过副本数多少(个数、百分比)
        maxUnavailable:更新过程中副本数最多多多少(个数、百分比)

revisionHistoryLimit: 做滚动更新后最多保存多少个历史版本用于回滚,默认10个
paused:控制更新中可以暂停

- 更新 kubectl edit更新deployment,有新建的pod时会应用新模板
- 历史 kubectl rollout history deploy NAME

[root@ 121 ~/k8s]# kubectl rollout history deploy myapp-deploy
deployment.extensions/myapp-deploy
REVISION CHANGE-CAUSE
0 <none>
1 <none>

- 回滚 kubectl rollout undo

kubectl rollout undo (TYPE NAME | TYPE/NAME) [flags] [options]

- 打补丁 kubectl patch 

kubectl patch deployment myapp-deploy -p '{"spec":{"replicas":5}}'

- 资源清单示例

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: ikuberbetes/myapp:v1
    ports:
    - name: http
      containerPort: 80

### Deployment操作示例
- 使用kubectl edit将replicas修改为3
![kubect-eidt修改.png](https://www.kkwen.cn/usr/uploads/2019/03/2459326763.png)
- 查看pod,发现新增一个pod,而且模板没变,因为模板文件没有变化

[root@ 188 ~/k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-deploy-65df64765c-6wrml 1/1 Running 0 6m30s
myapp-deploy-65df64765c-g9vjt 1/1 Running 0 47s
myapp-deploy-65df64765c-lp7kt 1/1 Running 0 6m32s

- 修改配置文件,将其副本数修改为3, image修改为ikubernetes/myapp:v2

apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 3
selector:

matchLabels:
  app: myapp
  release: canary

template:

metadata:
  labels:
    app: myapp
    release: canary
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v2
    ports:
    - name: http
      containerPort: 80
- 应用配置kuebctl apply -f deployment-myapp.yaml

[root@ 190 ~/k8s]# kubectl get pods -l app=myapp -w
NAME READY STATUS RESTARTS AGE
myapp-deploy-65df64765c-6wrml 1/1 Running 0 8m21s
myapp-deploy-65df64765c-g9vjt 1/1 Running 0 2m38s
myapp-deploy-65df64765c-lp7kt 1/1 Running 0 8m23s
myapp-deploy-6b56d98b6b-msxnh 0/1 Pending 0 0s
myapp-deploy-6b56d98b6b-msxnh 0/1 Pending 0 0s
myapp-deploy-6b56d98b6b-msxnh 0/1 ContainerCreating 0 0s
myapp-deploy-6b56d98b6b-msxnh 1/1 Running 0 3s
myapp-deploy-65df64765c-g9vjt 1/1 Terminating 0 6m24s
myapp-deploy-6b56d98b6b-fj88g 0/1 Pending 0 0s
myapp-deploy-6b56d98b6b-fj88g 0/1 Pending 0 0s
myapp-deploy-6b56d98b6b-fj88g 0/1 ContainerCreating 0 0s
myapp-deploy-65df64765c-g9vjt 0/1 Terminating 0 6m25s
myapp-deploy-65df64765c-g9vjt 0/1 Terminating 0 6m25s
myapp-deploy-6b56d98b6b-fj88g 1/1 Running 0 2s
myapp-deploy-65df64765c-6wrml 1/1 Terminating 0 12m
myapp-deploy-6b56d98b6b-blnmk 0/1 Pending 0 0s
myapp-deploy-6b56d98b6b-blnmk 0/1 Pending 0 0s
myapp-deploy-6b56d98b6b-blnmk 0/1 ContainerCreating 0 0s
myapp-deploy-65df64765c-6wrml 0/1 Terminating 0 12m
myapp-deploy-65df64765c-6wrml 0/1 Terminating 0 12m
myapp-deploy-65df64765c-6wrml 0/1 Terminating 0 12m
myapp-deploy-6b56d98b6b-blnmk 1/1 Running 0 3s
myapp-deploy-65df64765c-lp7kt 1/1 Terminating 0 12m
myapp-deploy-65df64765c-lp7kt 0/1 Terminating 0 12m
myapp-deploy-65df64765c-lp7kt 0/1 Terminating 0 12m
myapp-deploy-65df64765c-lp7kt 0/1 Terminating 0 12m
myapp-deploy-65df64765c-g9vjt 0/1 Terminating 0 6m34s
myapp-deploy-65df64765c-g9vjt 0/1 Terminating 0 6m34s

- 查看replicaSet,发现其有许多之前的版本,可以随时回滚

[root@ 8 ~/k8s]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp 0 0 0 24h myapp-container ikubernetes/myapp:v2 app=myapp,release=canary
myapp-deploy-65df64765c 0 0 0 15m myapp ikubernetes/myapp:v1 app=myapp,pod-template-hash=65df64765c,release=canary
myapp-deploy-6b56d98b6b 3 3 3 21m myapp ikubernetes/myapp:v2 app=myapp,pod-template-hash=6b56d98b6b,release=canary
myapp-deploy-74c9468db5 0 0 0 33m myapp ikuberbetes/myapp:v1 app=myapp,pod-template-hash=74c9468db5,release=canary
myapp-deploy-74dd964954 0 0 0 23m myapp ikuberbetes/myapp:v2 app=myapp,pod-template-hash=74dd964954,release=canary

- 打补丁 kubectl patch TYPE NAME -p ""

[root@ 11 ~/k8s]# kubectl patch deployment myapp-deploy -p '{"spec":{"replicas":5}}'
deployment.extensions/myapp-deploy patched
[root@ 14 ~/k8s]# kubectl patch deployment myapp-deploy -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1, "maxUnavailable": 0}}}}'
deployment.extensions/myapp-deploy patched

- 金丝雀发布, 使用set image修改pod镜像版本,然后暂停操作

[root@ 16 ~/k8s]# kubectl set image deployment myapp-deploy myapp=ikubernetes/myapp:v3 && kubectl rollout pause deployment myapp-deploy
deployment.extensions/myapp-deploy image updated
deployment.extensions/myapp-deploy paused

- 查看其更新变化,可看到更新一个Pod就暂停了

[root@ 191 ~/k8s]# kubectl get pods -l app=myapp -w
NAME READY STATUS RESTARTS AGE
myapp-deploy-6b56d98b6b-6wdtd 1/1 Running 0 6m2s
myapp-deploy-6b56d98b6b-blnmk 1/1 Running 0 25m
myapp-deploy-6b56d98b6b-fj88g 1/1 Running 0 25m
myapp-deploy-6b56d98b6b-msxnh 1/1 Running 0 25m
myapp-deploy-6b56d98b6b-w9qlj 1/1 Running 0 6m2s

---

myapp-deploy-548f47d899-bsxfq 0/1 Pending 0 0s
myapp-deploy-548f47d899-bsxfq 0/1 Pending 0 0s
myapp-deploy-548f47d899-bsxfq 0/1 ContainerCreating 0 0s
myapp-deploy-548f47d899-bsxfq 1/1 Running 0 8s

- 继续更新

[root@ 1 ~]# kubectl rollout resume deployment myapp-deploy
deployment.extensions/myapp-deploy resumed

- 查看其更新状态 kubectl rollout status

[root@ 17 ~/k8s]# kubectl rollout status deployment myapp-deploy
Waiting for deployment "myapp-deploy" rollout to finish: 1 out of 5 new replicas have been updated...
Waiting for deployment spec update to be observed...
Waiting for deployment spec update to be observed...
Waiting for deployment "myapp-deploy" rollout to finish: 1 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 2 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 2 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 2 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 3 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 4 out of 5 new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "myapp-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "myapp-deploy" rollout to finish: 1 old replicas are pending termination...
deployment "myapp-deploy" successfully rolled out

- 查看对象历史版本 kubectl rollout history TYPE NAME

[root@ 9 ~/k8s]# kubectl rollout history deploy myapp-deploy
deployment.extensions/myapp-deploy
REVISION CHANGE-CAUSE
0 <none>
4 <none>
6 <none>
7 <none>
8 <none>

- 回滚 kubectl rollout undo TYPE NAME [--to-revision=<REVISION>]

当前版本若为3,查看历史版本如下
1
2
3
若直接使用kubectl rollout undo TYPE NAME,回滚到第二版
若使用 --to-revision=1回滚到第一版,再查看版本
2
3
4
再回滚就是v3版本


### DaemonSet控制器
- DaemonSet控制器简称ds,在集中的每个节点中只运行一个pod,或者在符合条件的节点中只运行一个pod
- DaemonSet配置清单示例,创建一个deployment控制器下的redis的Pod和DaemonSet控制器下的filebeat的Pod

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: myapp-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
- 创建service

kubectl expose deployment redis --port=6379

- 登录filebaet容器,查看其变量,关注容器相互调用
        [root@ _25_ ~/k8s]# kubectl exec -it myapp-ds-w2rnt -- /bin/sh
        / # ps aux
        PID   USER     TIME   COMMAND
            1 root       0:00 /usr/local/bin/filebeat -e -c /etc/filebeat/filebeat.yml
           13 root       0:00 /bin/sh
           19 root       0:00 ps aux

        / # cat /etc/filebeat/filebeat.yml
        filebeat.registry_file: /var/log/containers/filebeat_registry
        filebeat.idle_timeout: 5s
        filebeat.spool_size: 2048

        logging.level: info

        filebeat.prospectors:
        - input_type: log
          paths:
            - "/var/log/containers/*.log"
            - "/var/log/docker/containers/*.log"
            - "/var/log/startupscript.log"
            - "/var/log/kubelet.log"
            - "/var/log/kube-proxy.log"
            - "/var/log/kube-apiserver.log"
            - "/var/log/kube-controller-manager.log"
            - "/var/log/kube-scheduler.log"
            - "/var/log/rescheduler.log"
            - "/var/log/glbc.log"
            - "/var/log/cluster-autoscaler.log"
          symlinks: true
          json.message_key: log
          json.keys_under_root: true
          json.add_error_key: true
          multiline.pattern: '^\s'
          multiline.match: after
          document_type: kube-logs
          tail_files: true
          fields_under_root: true

        output.redis:
          hosts: ${REDIS_HOST:?No Redis host configured. Use env var REDIS_HOST to set host.}
          key: "filebeat"

        / # printenv
        REDIS_PORT=tcp://10.104.196.201:6379
        KUBERNETES_PORT=tcp://10.96.0.1:443
        REDIS_SERVICE_PORT=6379
        KUBERNETES_SERVICE_PORT=443
        REDIS_PORT_6379_TCP_ADDR=10.104.196.201
        HOSTNAME=myapp-ds-w2rnt
        SHLVL=1
        HOME=/root
        REDIS_PORT_6379_TCP_PORT=6379
        REDIS_PORT_6379_TCP_PROTO=tcp
        REDIS_PORT_6379_TCP=tcp://10.104.196.201:6379
        TERM=xterm
        KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
        PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
        KUBERNETES_PORT_443_TCP_PORT=443
        REDIS_LOG_LEVEL=info
        KUBERNETES_PORT_443_TCP_PROTO=tcp
        KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
        KUBERNETES_SERVICE_PORT_HTTPS=443
        PWD=/
        REDIS_SERVICE_HOST=10.104.196.201
        KUBERNETES_SERVICE_HOST=10.96.0.1
        REDIS_HOST=redis.default.svc.cluster.local
        FILEBEAT_VERSION=5.6.5

- 更新镜像

kubectl set image daemonsets myapp-ds filebeat=ikubernetes/filebeat:5.6.6-alpine

- 更新方式
kubectl explain daemonset.spec.updateStrategy.rollingUpdate.maxUnavailable 更新过程中先杀掉多少节点更新,默认为1
最后修改:2019 年 03 月 07 日 09 : 21 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论