【Kubernetes】控制器Daemonset

一、简介

1.1、概念

  • DaemonSet控制器能够确保k8s集群所有的节点都运行一个相同的pod副本
  • 当向k8s集群中增加node节点时,这个node节点也会自动创建一个pod副本
  • 当node节点从集群移除,这些pod也会自动删除;
  • 删除Daemonset也会删除它们创建的pod。

1.2、原理

如何管理Pod?

daemonset的控制器会监听kuberntes的daemonset对象、pod对象、node对象,
这些被监听的对象之变动,就会触发syncLoop循环让kubernetes集群朝着daemonset对象描述的状态进行演进。

1.3、应用场景

  • 在集群的每个节点上运行存储,比如:glusterd 或 ceph。
  • 在每个节点上运行日志收集组件,比如:flunentd 、 logstash、filebeat等。
  • 在每个节点上运行监控组件,比如:Prometheus、 Node Exporter 、collectd等。

1.4、DaemonSet 与 Deployment 的区别

Deployment 部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本。
DaemonSet 的不同之处在于:每个 Node 上最多只能运行一个副本。

二、DaemonSet资源清单文件编写技巧

2.1、查看定义Daemonset资源需要的字段有哪些

[root@master ~]# kubectl explain ds
KIND:     DaemonSet
VERSION:  apps/v1

DESCRIPTION:
     DaemonSet represents the configuration of a daemon set.

FIELDS:
   apiVersion   <string>  # 当前资源使用的api版本,跟VERSION:  apps/v1保持一致
     
   kind <string>          # 资源类型,跟KIND:  DaemonSet保持一致
   metadata     <Object>  # 元数据,定义DaemonSet名字的
   spec <Object>          # 定义容器的
   status       <Object>  # 状态信息,不能改

2.2、查看DaemonSet的spec字段如何定义

[root@master ~]# kubectl explain ds.spec 
KIND:     DaemonSet
VERSION:  apps/v1

RESOURCE: spec <Object>

DESCRIPTION:
     The desired behavior of this daemon set. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

     DaemonSetSpec is the specification of a daemon set.

FIELDS:
   minReadySeconds      <integer> # 当新的pod启动几秒种后,再kill掉旧的pod。  
   revisionHistoryLimit <integer> # 历史版本
   selector     <Object> -required-  # 用于匹配pod的标签选择器
   template     <Object> -required-  # 定义Pod的模板,基于这个模板定义的所有pod是一样的
   updateStrategy       <Object>     # daemonset的升级策略

2.3、查看DaemonSet的spec.template字段如何定义

对于template而言,其内部定义的就是pod,pod模板是一个独立的对象

[root@master ~]# kubectl explain ds.spec.template
KIND:     DaemonSet
VERSION:  apps/v1

RESOURCE: template <Object>

DESCRIPTION:
    ......

FIELDS:
   metadata     <Object>  
   spec <Object>          

三、DaemonSet使用案例:部署日志收集组件fluentd

3.1、导入镜像

把fluentd-2-5-1.tar.gz上传到工作节点node01、node02上,解压.

特别注意: 因为我们安装的k8s版本是1.25,那就需要按照文档步骤ctr -n=k8s.io images import导出镜像,如果k8s版本是1.24之前的,可以用docker load -i解压,视频里用的docker load -i,现在我们课程安装更新到了k8s1.25,所以导出镜像需要用ctr -n=k8s.io images import

[root@master 13]# scp fluentd_2_5_1.tar.gz root@10.32.1.147:/root/package
fluentd_2_5_1.tar.gz                                                                                                100%  142MB  28.0MB/s   00:05
[root@master 13]# scp fluentd_2_5_1.tar.gz root@10.32.1.148:/root/package
fluentd_2_5_1.tar.gz                                                                                                100%  142MB  52.1MB/s   00:02
[root@master 13]#
[root@node01 package]# ctr -n=k8s.io images import fluentd_2_5_1.tar.gz
unpacking docker.io/xianchao/fluentd:v2.5.1 (sha256:3baf02875b273fe6f675710d0f25ea9374b94d6704c334c75e854f8465a777a9)...done
[root@node02 package]# ctr -n=k8s.io images import fluentd_2_5_1.tar.gz
unpacking docker.io/xianchao/fluentd:v2.5.1 (sha256:3baf02875b273fe6f675710d0f25ea9374b94d6704c334c75e854f8465a777a9)...done

3.2、编写资源清单并创建资源

[root@master 13]# cat daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels:
    k8s-app: fluentd-logging
  name: fluentd-elasticsearch
  namespace: kube-system
spec:
  selector:
    matchLabels:
     name: fluentd-elasticsearch
  template:
    metadata:
     name: fluentd
     labels:
       name: fluentd-elasticsearch
    spec:
     tolerations:
     - key: node-role.kubernetes.io/master
       effect: NoSchedule
     containers:
     - name:  fluentd-elasticsearch
       image: xianchao/fluentd:v2.5.1
       resources:
         limits:
           memory: 500Mi
         requests:
           cpu: 100m
           memory: 200Mi
       volumeMounts:
       - name: varlog
         mountPath: /var/log
       - name: varlibdockercontainers
         mountPath: /var/lib/docker/containers
         readOnly: true
     terminationGracePeriodSeconds: 30
     volumes:
     - name: varlog
       hostPath:
          path: /var/log
     - name: varlibdockercontainers
       hostPath:
          path: /var/lib/docker/containers
[root@master 13]# kubectl apply -f daemonset.yaml
daemonset.apps/fluentd-elasticsearch created

[root@master 13]# kubectl get ds -n kube-system
NAME                    DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
calico-node             3         3         2       3            2           kubernetes.io/os=linux   24d
fluentd-elasticsearch   2         2         2       2            2           <none>                   30s
kube-proxy              3         3         3       3            3           kubernetes.io/os=linux   24d
[root@master 13]#  kubectl get pods -n kube-system -o wide
NAME                                       READY   STATUS    RESTARTS      AGE    IP               NODE     NOMINATED NODE   READINESS GATES
fluentd-elasticsearch-27xqk                1/1     Running   0             100s   10.244.140.113   node02   <none>           <none>
fluentd-elasticsearch-zzzlr                1/1     Running   0             100s   10.244.196.163   node01   <none>           <none>

通过上面可以看到在k8s的三个节点均创建了fluentd这个pod。pod的名字是由控制器的名字-随机数组成的。

3.3、清单详细说明

[root@master 13]# cat daemonset.yaml
apiVersion: apps/v1   # DaemonSet使用的api版本
kind: DaemonSet       # 资源类型
metadata:
  labels:
    k8s-app: fluentd-logging    # 资源具有的标签
  name: fluentd-elasticsearch   # 资源的名字
  namespace: kube-system        # 资源所在的名称空间
spec:
  selector:        # 标签选择器
    matchLabels:
     name: fluentd-elasticsearch   
  template:
    metadata:
     name: fluentd 
     labels:       # 基于这回模板定义的pod具有的标签
       name: fluentd-elasticsearch
    spec:
     tolerations:  # 定义容忍度
     - key: node-role.kubernetes.io/master
       effect: NoSchedule
     containers:   # 定义容器
     - name:  fluentd-elasticsearch
       image: xianchao/fluentd:v2.5.1
       resources:  # 资源配额
         limits:
           memory: 500Mi
         requests:
           cpu: 100m
           memory: 200Mi
       volumeMounts:
       - name: varlog
         mountPath: /var/log                   # 将varlog卷挂载到容器中指定挂载点/var/log 
       - name: varlibdockercontainers
         mountPath: /var/lib/docker/containers # 将varlibdockercontainers卷容器中指定挂载点/var/lib/docker/containers
         readOnly: true                        # 挂载目录是只读权限
     terminationGracePeriodSeconds: 30		   # 优雅的关闭服务
     volumes:
     - name: varlog
       hostPath:
          path: /var/log					  # 基于本地目录创建一个卷
     - name: varlibdockercontainers
       hostPath:
          path: /var/lib/docker/containers    # 基于本地目录创建一个卷

四、Daemonset管理pod:滚动更新

4.1、查看daemonset的滚动更新策略

[root@master 13]# kubectl explain ds.spec.updateStrategy
KIND:     DaemonSet
VERSION:  apps/v1

RESOURCE: updateStrategy <Object>

DESCRIPTION:
     An update strategy to replace existing DaemonSet pods with new pods.

     DaemonSetUpdateStrategy is a struct used to control the update strategy for
     a DaemonSet.

FIELDS:
   rollingUpdate        <Object>
     Rolling update config params. Present only if type = "RollingUpdate".

   type <string>
     Type of daemon set update. Can be "RollingUpdate" or "OnDelete". Default is
     RollingUpdate.  

     Possible enum values:
     - `"OnDelete"` Replace the old daemons only when it's killed
     - `"RollingUpdate"` Replace the old daemons by new ones using rolling
     update i.e replace them on each node one after the other.

4.2、查看rollingUpdate支持的更新策略

[root@master 13]# kubectl explain ds.spec.updateStrategy.rollingUpdate
KIND:     DaemonSet
VERSION:  apps/v1

RESOURCE: rollingUpdate <Object>

DESCRIPTION:
     Rolling update config params. Present only if type = "RollingUpdate".

     Spec to control the desired behavior of daemon set rolling update.

FIELDS:
   maxSurge     <string> 
   maxUnavailable       <string> 
   # rollingUpdate更新策略只支持maxUnavailabe,先删除再更新
   # 因为我们不支持一个节点运行两个pod,因此需要先删除一个,再更新一个。

4.3、更新演示

kubectl set image daemonsets fluentd-elasticsearch fluentd-elasticsearch=ikubernetes/filebeat:5.6.6-alpine -n kube-system

这个镜像启动pod会有问题,主要是演示daemonset如何在命令行更新pod

相关推荐

  1. Kubernetes控制器Daemonset

    2023-12-31 07:00:03       28 阅读
  2. Kubernetes - DAEMONSET 与 DEPLOYMENT 区别

    2023-12-31 07:00:03       28 阅读
  3. KubernetesDaemonSet 基本原理

    2023-12-31 07:00:03       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-31 07:00:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-31 07:00:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-31 07:00:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-31 07:00:03       20 阅读

热门阅读

  1. c# 循环提速

    2023-12-31 07:00:03       34 阅读
  2. Mybatis 日志配置

    2023-12-31 07:00:03       35 阅读
  3. 微服务(4)

    2023-12-31 07:00:03       36 阅读
  4. LeetCode每日一题.03(外观数列)

    2023-12-31 07:00:03       39 阅读
  5. 【论文阅读】Self-Paced Curriculum Learning

    2023-12-31 07:00:03       32 阅读
  6. python:PyCharm更改.PyCharm配置文件夹存储位置

    2023-12-31 07:00:03       41 阅读