kubernetes k8s Daemonset 控制器 原理 讲解 配置

目录

[1 DaemonSet控制器:概念、原理解读](#1 DaemonSet控制器:概念、原理解读)

[1.1 DaemonSet概述](#1.1 DaemonSet概述)

[1.2 DaemonSet工作原理:如何管理Pod?](#1.2 DaemonSet工作原理:如何管理Pod?)

[1.3 Daemonset典型的应用场景](#1.3 Daemonset典型的应用场景)

[1.4 DaemonSet 与 Deployment 的区别Deployment 部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本。](#1.4 DaemonSet 与 Deployment 的区别Deployment 部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本。)

[DaemonSet 的不同之处在于:每个 Node 上最多只能运行一个副本。](#DaemonSet 的不同之处在于:每个 Node 上最多只能运行一个副本。)

[2 DaemonSet资源清单文件编写技巧](#2 DaemonSet资源清单文件编写技巧)

[3 DaemonSet使用案例:部署日志收集组件fluentd](#3 DaemonSet使用案例:部署日志收集组件fluentd)

[4 Daemonset管理pod:滚动更新](#4 Daemonset管理pod:滚动更新)


文档中的YAML文件配置直接复制粘贴可能存在格式错误,故实验中所需要的YAML文件以及本地包均打包至网盘

链接:https://pan.baidu.com/s/1mJ1mWjn4dmT6zGPo4RTFCA

提取码:m0r6

1 DaemonSet控制器:概念、原理解读

1 .1 DaemonSet概述

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

1. 2 DaemonSet 工作原理:如何管理Pod?

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

1.3 Daemonset典型的应用场景

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

1.4 DaemonSet 与 Deployment 的区别

Deployment 部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本。

DaemonSet 的不同之处在于:每个 Node 上最多只能运行一个副本。

2 DaemonSet资源清单文件编写技巧

#查看定义Daemonset资源需要的字段有哪些?

[root@xianchaomaster1 ~]# 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> # 状态信息,不能改

#查看DaemonSet的spec字段如何定义?

[root@xianchaomaster1 ~]# 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的升级策略

#查看DaemonSet的spec .template 字段如何定义?

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

[root@xianchaomaster1 ~]# kubectl explain ds.spec.template

KIND: DaemonSet

VERSION: apps/v1

RESOURCE: template <Object>

FIELDS:

metadata <Object>

spec<Object>

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

# fluentd-2-5-1.tar.gz 上传到xianchaomaster1和xianchaonode2和xianchaonode1上,解压

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

[root@xianchaonode2]# ctr -n=k8s.io images import fluentd _ 2_5_1.tar.gz

[root@xianchaonode1]# ctr -n=k8s.io images import fluentd _ 2_5_1.tar.gz

[root@xianchaomaster1]# ctr -n=k8s.io images import fluentd _ 2_5_1.tar.gz

# 编写一个DaemonSet资源清单

[root@xianchaomaster1 ~]# cat daemonset.yaml

apiVersion: apps/v1

kind: DaemonSet

metadata:

name: fluentd-elasticsearch

namespace: kube-system

labels:

k8s-app: fluentd-logging

spec:

selector:

matchLabels:

name: fluentd-elasticsearch

template:

metadata:

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: 200Mi

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@xianchaomaster1 ~]# kubectl apply -f daemonset.yaml

daemonset.apps/fluentd-elasticsearch created

[root@xianchaomaster1 ~]# kubectl get ds -n kube-system

NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE

fluentd-elasticsearch 3 3 3 3 3

[root@xianchaomaster1 ~]# kubectl get pods -n kube-system -o wide

#通过上面可以看到在k 8s 的三个节点均创建了fluentd这个pod

#pod的名字是由控制器的名字-随机数组成的

#资源清单详细说明

apiVersion: apps/v1 #DaemonSet 使用的api版本

kind: DaemonSet # 资源类型

metadata:

name: fluentd-elasticsearch # 资源的名字

namespace: kube-system # 资源所在的名称空间

labels:

k8s-app: fluentd-logging # 资源具有的标签

spec:

selector: # 标签选择器

matchLabels:

name: fluentd-elasticsearch

template:

metadata:

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: 200Mi

requests:

cpu: 100m

memory: 200Mi

volumeMounts:

- name: varlog

mountPath: /var/log # 把本地/ var/log 目录挂载到容器

- name: varlibdockercontainers

mountPath: /var/lib/docker/containers

# /var/lib/docker/containers/ 挂载到容器里

readOnly: true # 挂载目录是只读权限

terminationGracePeriodSeconds: 30 # 优雅的关闭服务

volumes:

- name: varlog

hostPath:

path: /var/log # 基于本地目录创建一个卷

- name: varlibdockercontainers

hostPath:

path: /var/lib/docker/containers # 基于本地目录创建一个卷

4 Daemonset管理p od :滚动更新

#DaemonSet实现pod的滚动更新

#查看daemonset的滚动更新策略

[root@xianchaomaster1 ~]# 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.

#查看rollingUpdate支持的更新策略

[root@xianchaomaster1 ~]# 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:

maxUnavailable <string>

# 上面表示rollingUpdate更新策略只支持maxUnavailabe,先删除在更新;因为我们不支持一个节点运行两个pod,因此需要先删除一个,在更新一个

#更新镜像版本,可以按照如下方法:

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

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

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

相关推荐
C语言扫地僧20 分钟前
Docker 镜像制作(Dockerfile)
linux·服务器·docker·容器
Xinan_____28 分钟前
Linux——高流量 高并发(访问场景) 高可用(架构要求)
linux·运维·架构
橙子家38 分钟前
k8s 中的 Ingress 简介【k8s 系列之三】
k8s
苹果醋31 小时前
react 路由 react-router/react-router-dom
运维·nginx
钡铼技术物联网关1 小时前
Codesys 与 ARMxy ARM 工业控制器:工业控制的黄金组合
linux·运维·服务器·arm开发·硬件工程
向往风的男子2 小时前
【devops】devops-gitlab之部署与日常使用
运维·gitlab·devops
无名之逆4 小时前
云原生(Cloud Native)
开发语言·c++·算法·云原生·面试·职场和发展·大学期末
geekrabbit4 小时前
机器学习和深度学习的区别
运维·人工智能·深度学习·机器学习·浪浪云
ken_coding4 小时前
Windows11 WSL2的ubuntu 22.04中拉取镜像报错
linux·ubuntu·docker
Richardlygo4 小时前
(k8s)Kubernetes部署Promehteus
云原生·容器·kubernetes