23、K8S-控制器之DaemonSet

Kubernetes学习目录

1、基础知识

1.1、简介

DaemonSet能够让所有(或者特定)的节点"精确的"运行同一个pod,它一般应用在集群环境中所有节点都必
须运行的守护进程的场景。
我们在部署k8s环境的时候,网络的部署样式就是基于这种DaemonSet的方式,因为对于网络来说,是所有节
点都必须具备的基本能力,而且不能随意中断,否则的话,节点上的容器通信就会出现问题。

1.2、常用的场景-flannel为例

master1 ~]# kubectl -n kube-system get ds
NAME              DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
kube-flannel-ds   5         5         5       5            5           <none>                   5d1h
kube-proxy        5         5         5       5            5           kubernetes.io/os=linux   5d1h

master1 ~]# cat flannel/kube-flannel.yml
apiVersion: apps/v1
kind: DaemonSet

1.3、daemonset常用的服务

当节点加入到K8S集群中,pod会被(DaemonSet)调度到该节点上运行,当节点从K8S集群中被移除,
被DaemonSet调度的pod会被移除,如果删除DaemonSet,所有跟这个DaemonSet相关的pods都会被删
除。
   在某种程度上,DaemonSet承担了RC的部分功能,它也能保证相关pods持续运行,如果一个
DaemonSet的Pod被杀死、停止、或者崩溃,那么DaemonSet将会重新创建一个新的副本在这台计算节点上。
    
常用于后台支撑服务
 集群存储守护进程,如:glusterd,ceph
 日志收集服务,如:fluentd,logstash
 监控服务,如:Prometheus,collectd

1.4、DaemonSet流程图

1.5、资源清单解析

apiVersion: apps/v1 # API群组及版本
kind: DaemonSet     # 资源类型特有标识
metadata:
  name <string>      # 资源名称,在作用域中要唯一
  namespace <string> # 名称空间;DaemonSet资源隶属名称空间级别
spec:
  minReadySeconds <integer>  # Pod就绪后多少秒内任一容器无crash方可视为“就绪”
  selector <object>  # 标签选择器,必须匹配template字段中Pod模板中的标签
  template <object>  # Pod模板对象;
  revisionHistoryLimit <integer> # 滚动更新历史记录数量,默认为10;
  updateStrategy <Object>        # 滚动更新策略
    type <string>                # 滚动更新类型,可用值有OnDelete和
RollingUpdate;
  rollingUpdate <Object>     # 滚动更新参数,专用于RollingUpdate类型
    maxUnavailable <string>  # 更新期间可比期望的Pod数量缺少的数量或比例

2、实践1-DaemonSet

2.1、需求

之前我们在Node上启动Pod需要在RC中指定replicas的副本数的值,有些情况下,我们需要在所有节点都运行一个Pod,
因为Node数量会变化,所以Pod的副本数使用RC来指定就不合适了,这个时候Daemon Sets就派上了用场。
简单来说,Daemon Sets就是让一个pod在所有的k8s集群节点上都运行一个。

2.2、定义资源清单

cat >daemonset-test.yml<<'EOF'
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: daemonset-test
spec:
  selector:
    matchLabels:
      app: pod-test
  template:
    metadata:
      labels:
        app: pod-test
    spec:
      containers:
      - name: pod-test
        image: 192.168.10.33:80/k8s/pod_test:v0.2
EOF

2.2.1、应用资源清单

master1 ]# kubectl apply -f daemonset-test.yml 

master1 ]# kubectl get ds
NAME             DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
daemonset-test   2         2         2       2            2           <none>          3s

master1 ]# kubectl get pods -o wide
NAME                   READY   STATUS    RESTARTS   AGE   IP             NODE    NOMINATED NODE   READINESS GATES
daemonset-test-mhj5l   1/1     Running   0          15s   10.244.3.145   node1   <none>           <none>
daemonset-test-nzjbg   1/1     Running   0          15s   10.244.4.86    node2   <none>           <none>

2.3、DaemonSet对象也支持滚动更新

master1 ]# kubectl set image daemonsets daemonset-test pod-test='192.168.10.33:80/k8s/pod_test:v0.1' &&\
kubectl rollout status daemonset daemonset-test daemonset.apps/daemonset-test image updated Waiting for daemon set "daemonset-test" rollout to finish: 0 out of 2 new pods have been updated... Waiting for daemon set "daemonset-test" rollout to finish: 0 out of 2 new pods have been updated... Waiting for daemon set "daemonset-test" rollout to finish: 1 out of 2 new pods have been updated... Waiting for daemon set "daemonset-test" rollout to finish: 1 out of 2 new pods have been updated... Waiting for daemon set "daemonset-test" rollout to finish: 1 out of 2 new pods have been updated... Waiting for daemon set "daemonset-test" rollout to finish: 1 of 2 updated pods are available... daemon set "daemonset-test" successfully rolled out

3、实践2-node-exporter所有节点上都部署采集数据的功能

3.1、定义资源配置清单

cat > prometheus-node-exporter.yml<<'EOF'
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: daemonset-demo
  namespace: default
  labels:
    app: prometheus
    component: node-exporter
spec:
  selector:
    matchLabels:
      app: prometheus
      component: node-exporter
  template:
    metadata:
      name: prometheus-node-exporter
      labels:
        app: prometheus
        component: node-exporter
    spec:
      containers:
      - image: 192.168.10.33:80/k8s/node-exporter:v1.2.2
        name: prometheus-node-exporter
        ports:
        - name: prom-node-exp
          containerPort: 9100
          hostPort: 9100
        livenessProbe:
          tcpSocket:
            port: prom-node-exp
          initialDelaySeconds: 3
        readinessProbe:
          httpGet:
            path: '/metrics'
            port: prom-node-exp
            scheme: HTTP
          initialDelaySeconds: 5
      hostNetwork: true
      hostPID: true
EOF

3.2、应用资源清单

master1 ]# kubectl apply -f prometheus-node-exporter.yml 
daemonset.apps/daemonset-demo created


master1 ]# kubectl get ds
NAME             DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
daemonset-demo   2         2         0       2            0           <none>          3s


master1 ]# kubectl get pods -o wide
NAME                   READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
daemonset-demo-c9ckn   1/1     Running   0          12s   192.168.10.30   node2   <none>           <none>
daemonset-demo-wcklf   1/1     Running   0          12s   192.168.10.29   node1   <none>           <none>


# 测试访问是否正常
master1 ]# curl -I  192.168.10.30:9100/metrics
HTTP/1.1 200 OK
Content-Type: text/plain; version=0.0.4; charset=utf-8
Date: Tue, 21 Mar 2023 14:51:51 GMT

master1 ]# curl -I  192.168.10.29:9100/metrics
HTTP/1.1 200 OK
Content-Type: text/plain; version=0.0.4; charset=utf-8
Date: Tue, 21 Mar 2023 14:51:58 GMT
posted @ 2023-03-21 22:54  小粉优化大师  阅读(166)  评论(0编辑  收藏  举报