一、什么是DaemonSet
DaemonSet(守護進程集)和守護進程類似,它在符合匹配條件的節點上均部署一個Pod
DaemonSet確保全部(或者某些)節點上運行一個Pod副本。當有新節點加入集群時,也會為它們新增一個Pod。當節點從集群中移除時,這些Pod也會被回收,刪除DaemonSet將會刪除它創建的所有Pod
使用DaemonSet的一些典型用法:
運行集群存儲daemon(守護進程),例如在每個節點上運行Glusterd、Ceph等
在每個節點運行日志收集daemon,例如Fluentd、Logstash
在每個節點運行監控daemon,比如Prometheus Node Exporter、Collectd、Datadog代理、New Relic代理或 Ganglia gmond
二、編寫DaemonSet規范
創建一個DaemonSet的內容大致如下,比如創建一個fluentd的DaemonSet
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-es-v2.0.4
namespace: logging
labels:
k8s-app: fluentd-es
version: v2.0.4
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
spec:
selector:
matchLabels:
k8s-app: fluentd-es
version: v2.0.4
template:
metadata:
labels:
k8s-app: fluentd-es
kubernetes.io/cluster-service: "true"
version: v2.0.4
# This annotation ensures that fluentd does not get evicted if the node
# supports critical pod annotation based priority scheme.
# Note that this does not guarantee admission on the nodes (#40573).
annotations:
scheduler.alpha.kubernetes.io/critical-pod: ''
seccomp.security.alpha.kubernetes.io/pod: 'docker/default'
spec:
serviceAccountName: fluentd-es
containers:
- name: fluentd-es
image: k8s.gcr.io/fluentd-elasticsearch:v2.0.4
env:
- name: FLUENTD_ARGS
value: --no-supervisor -q
resources:
limits:
memory: 500Mi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
- name: varlog
mountPath: /var/log
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
- name: config-volume
mountPath: /etc/fluent/config.d
nodeSelector:
beta.kubernetes.io/fluentd-ds-ready: "true"
terminationGracePeriodSeconds: 30
volumes:
- name: varlog
hostPath:
path: /var/log
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
- name: config-volume
configMap:
name: fluentd-es-config-v0.1.4
2.1、創建一個DaemonSet
[root@k8s-master01 ~]# cat > nginx-ds.yaml << EFO
apiVersion: apps/v1
kind: DaemonSet
metadata:
labels:
app: nginx
name: nginx
spec:
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.2
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Alwaysyaml
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
EFO
# 創建一個ds
[root@k8s-master01 ~]# kubectl create -f nginx-ds.yaml
daemonset.apps/nginx created
# 查看ds信息,個個節點都有一個
[root@k8s-master01 ~]# kubectl get node -owide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
k8s-master01 Ready matser 43h v1.20.0 192.168.1.100 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.14
k8s-master02 Ready <none> 43h v1.20.0 192.168.1.101 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.14
k8s-master03 Ready <none> 43h v1.20.0 192.168.1.102 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.14
k8s-node01 Ready <none> 43h v1.20.0 192.168.1.103 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.14
k8s-node02 Ready <none> 43h v1.20.0 192.168.1.104 <none> CentOS Linux 7 (Core) 4.19.12-1.el7.elrepo.x86_64 docker://19.3.14
1. 必需字段
和其他所有Kubernetes配置一樣,DaemonSet需要apiVersion、kind和metadata字段,同時也需要一個.spec配置段。
2. Pod模板
.spec唯一需要的字段是.spec.template。.spec.template是一個Pod模板,它與Pod具有相同的配置方式,但它不具有apiVersion和kind字段。
除了Pod必需的字段外,在DaemonSet中的Pod模板必須指定合理的標簽。
在DaemonSet中的Pod模板必須具有一個RestartPolicy,默認為Always。
3. Pod Selector
.spec.selector字段表示Pod Selector,它與其他資源的.spec.selector的作用相同。
.spec.selector表示一個對象,它由如下兩個字段組成:
matchLabels,與ReplicationController的.spec.selector的作用相同,用於匹配符合條件的Pod。
matchExpressions,允許構建更加復雜的Selector,可以通過指定key、value列表以及與key和value列表相關的操作符。
如果上述兩個字段都指定時,結果表示的是AND關系(邏輯與的關系)。
.spec.selector必須與.spec.template.metadata.labels相匹配。如果沒有指定,默認是等價的,如果它們的配置不匹配,則會被API拒絕。
(4)指定節點部署Pod
如果指定了.spec.template.spec.nodeSelector,DaemonSet Controller將在與Node Selector(節點選擇器)匹配的節點上創建Pod,比如部署在磁盤類型為ssd的節點上(需要提前給節點定義標簽Label):
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
nodeSelector:
disktype: ssd
1. 命令式更新
kubectl edit ds/<daemonset-name>
kubectl patch ds/<daemonset-name> -p=<strategic-merge-patch>
2. 更新鏡像
kubectl set image ds/<daemonset-name><container-name>=<container-new-image>--record=true
3. 查看更新狀態
kubectl rollout status ds/<daemonset-name>
4. 列出所有修訂版本
kubectl rollout history daemonset <daemonset-name>
5. 回滾到指定revision
kubectl rollout undo daemonset <daemonset-name> --to-revision=<revision>
DaemonSet的更新和回滾與Deployment類似,此處不再演示。