一、配置文件
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd
spec:
selector:
matchLabels:
app: logging
template:
metadata:
labels:
app: logging
id: fluentd
name: fluentd
spec:
containers:
- name: fluentd-es
image: agilestacks/fluentd-elasticsearch:v1.3.0
imagePullPolicy: IfNotPresent
env:
- name: FLUENTD_ARGS
value: -qq
volumeMounts:
- name: containers
mountPath: /var/lib/docker/containers
- name: varlog
mountPath: /varlog
volumes:
- hostPath:
path: /var/lib/docker/containers
name: containers
- hostPath:
path: /var/log
name: varlog
# 通过这种方式创建的daemonset,会在每个非master节点都创建pod
# 添加标签
[root@k8s-master daemonset]# kubectl label no k8s-node1 type=microservices
node/k8s-node1 labeled
查看标签,会发现k8s-node1多出一个标签
# 查看当前的部署情况
[root@k8s-master daemonset]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dns-test 1/1 Running 1 (23h ago) 2d2h 10.244.169.161 k8s-node2 <none> <none>
fluentd-c8gpx 1/1 Running 0 23h 10.244.36.98 k8s-node1 <none> <none>
fluentd-nps8w 1/1 Running 0 23h 10.244.169.163 k8s-node2 <none> <none>
# 在 k8s-node1 和 k8s-node2 都有部署
二、指定节点
DaemonSet 会忽略 Node 的 unschedulable 状态,有两种方式来指定 Pod 只运行在指定的 Node 节点上:
nodeSelector:只调度到匹配指定 label 的 Node 上
nodeAffinity:功能更丰富的 Node 选择器,比如支持集合操作
podAffinity:调度到满足条件的 Pod 所在的 Node 上
先为 Node 打上标签
kubectl label nodes k8s-node1 svc_type=microsvc
然后再 daemonset 配置中设置 nodeSelector
spec:
template:
spec:
nodeSelector:
svc_type: microsvc
# 重新编辑配置文件
[root@k8s-master daemonset]# kubectl edit ds fluentd
apiVersion: apps/v1
kind: DaemonSet
metadata:
annotations:
deprecated.daemonset.template.generation: "1"
creationTimestamp: "2025-01-06T17:13:22Z"
generation: 1
name: fluentd
namespace: default
resourceVersion: "301917"
uid: 75f82c83-c526-4bba-8ce9-983343b1d30d
spec:
revisionHistoryLimit: 10
selector:
matchLabels:
app: logging
template:
metadata:
creationTimestamp: null
labels:
app: logging
id: fluentd
name: fluentd
spec:
nodeSelector:
type: microservices
containers:
- env:
- name: FLUENTD_ARGS
value: -qq
image: agilestacks/fluentd-elasticsearch:v1.3.0
imagePullPolicy: IfNo