DaemonSet 典型应用
Deployment 部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本。DaemonSet 的不同之处在于:每个 Node 上最多只能运行一个副本。
DaemonSet 的典型应用场景有:
- 在集群的每个节点上运行存储 Daemon,比如 glusterd 或 ceph。
- 在每个节点上运行日志收集 Daemon,比如 flunentd 或 logstash。
- 在每个节点上运行监控 Daemon,比如 Prometheus Node Exporter 或 collectd。
其实 Kubernetes 自己就在用 DaemonSet 运行系统组件。
执行如下命令 kubectl get daemonset --namespace=kube-system
[root@master ~]# kubectl get daemonset --namespace=kube-system
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
kube-flannel-ds 3 3 3 3 3 <none> 28h
kube-proxy 3 3 3 3 3 kubernetes.io/os=linux 28h
DaemonSet kube-flannel-ds 和 kube-proxy 分别负责在每个节点上运行 flannel 和 kube-proxy 组件。
kubectl get pod --namespace=kube-system -o wide

因为 flannel 和 kube-proxy 属于系统组件,需要在命令行中通过 --namespace=kube-system 指定 namespace kube-system。如果不指定则只返回默认 namespace default 中的资源。
kube-flannel-ds
下面我们通过分析 kube-flannel-ds 来学习 DaemonSet。
还记得之前是如何部署 flannel 网络的吗?我们执行了如下两个命令:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.ym
flannel 的 DaemonSet 就定义在 kube-flannel.yml 中:

注:配置文件的完整内容要复杂些,为了更好地学习 DaemonSet,这里只保留了最重要的内容。
① DaemonSet 配置文件的语法和结构与 Deployment 几乎完全一样,只是将 kind 设为 DaemonSet。
② hostNetwork 指定 Pod 直接使用的是 Node 的网络,相当于 docker run --network=host。考虑到 flannel 需要为集群提供网络连接,这个要求是合理的。
③ containers 定义了运行 flannel 服务的两个容器。
kube-proxy
由于无法拿到 kube-proxy 的 YAML 文件,只能运行如下命令查看其配置:

同样为了便于理解,这里只保留了最重要的信息。
① kind: DaemonSet 指定这是一个 DaemonSet 类型的资源。
② containers 定义了 kube-proxy 的容器。
③ status 是当前 DaemonSet 的运行时状态,这个部分是 kubectl edit特有的。其实 Kubernetes 集群中每个当前运行的资源都可以通过 kubectl edit 查看其配置和运行状态,比如 kubectl edit deployment nginx-deployment。
运行自己的DaemonSet
以 Prometheus Node Exporter 为例演示如何运行自己的 DaemonSet。
Prometheus 是流行的系统监控方案,Node Exporter 是 Prometheus 的 agent,以 Daemon 的形式运行在每个被监控节点上。
如果是直接在 Docker 中运行 Node Exporter 容器,命令为:
docker run -d \
-v "/proc:/host/proc" \
-v "/sys:/host/sys" \
-v "/:/rootfs" \
--net=host \
prom/node-exporter \
--path.procfs /host/proc \
--path.sysfs /host/sys \
--collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)"
将其转换为 DaemonSet 的 YAML 配置文件 node_exporter.yml:
[root@master ~]# vim node_exporter.yml
apiVersion: apps/v1 #Api接口版本
kind: DaemonSet #定义控制器
metadata: #信息
name: node-exporter-daemonset #deployment名称
spec: #详细信息
#replicas: 3 #副本数
selector: #选择标签
matchLabels: #标签匹配
app: prometheus #匹配prometheus
template: #pod容器
metadata: #具体信息
labels: #定义标签
app: prometheus #标签prometheus
spec:
hostNetwork: true
containers:
- name: node-exporter
image: prom/node-exporter
imagePullPolicy: IfNotPresent
command:
- /bin/node_exporter
- --path.procfs
- /host/proc
- --path.sysfs
- /host/sys
- --collector.filesystem.ignored-mount-points
- ^/(sys|proc|dev|host|etc)($|/)
volumeMounts:
- name: proc
mountPath: /host/proc
- name: sys
mountPath: /host/sys
- name: root
mountPath: /rootfs
volumes:
- name: proc
hostPath:
path: /proc
- name: sys
hostPath:
path: /sys
- name: root
hostPath:
path: /
① hostNetwork: true 直接使用 Host 的网络。
② command: 设置容器启动命令。
③ volumeMounts: 通过 Volume 将 Host 路径 /proc、/sys 和 / 映射到容器中。
[root@master ~]# kubectl apply -f node_exporter.yml
daemonset.apps/node-exporter-daemonset created
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
node-exporter-daemonset-np8h8 1/1 Running 0 97s 10.70.36.251 node1 <none> <none>
node-exporter-daemonset-skskq 1/1 Running 0 97s 10.70.36.252 node2 <none> <none>
DaemonSet node-exporter-daemonset 部署成功,node1 和 node2 上分别运行了一个 node exporter Pod
DaemonSet、Deployment、StatefulSet 和 ReplicaSet 在 v1.16 中将不再从extensions/v1beta1、apps/v1beta1 或 apps/v1beta2 提供服务
解决方法是:
将yml配置文件内的api接口修改为 apps/v1 ,导致原因为之间使用的kubernetes 版本放弃部分API支持
imagePullPolicy
imagePullPolicy: Always
IfNotPresent 默认值,本地有则使用本地镜像,不拉取
imagePullPolicy: IfNotPresent
Never 只使用本地镜像,从不拉取
imagePullPolicy: Never

2028

被折叠的 条评论
为什么被折叠?



