DaemonSet概述
DaemonSet控制器能夠確保k8s集群所有的節(jié)點(diǎn)都運(yùn)行一個(gè)相同的pod副本,當(dāng)向k8s集群中增加node節(jié)點(diǎn)時(shí),這個(gè)node節(jié)點(diǎn)也會自動創(chuàng)建一個(gè)pod副本,當(dāng)node節(jié)點(diǎn)從集群移除,這些pod也會自動刪除;刪除Daemonset也會刪除它們創(chuàng)建的pod
DaemonSet工作原理:如何管理Pod
daemonset的控制器會監(jiān)聽kuberntes的daemonset對象、pod對象、node對象,這些被監(jiān)聽的對象之變動,就會觸發(fā)syncLoop循環(huán)讓kubernetes集群朝著daemonset對象描述的狀態(tài)進(jìn)行演進(jìn)
Daemonset典型的應(yīng)用場景
- 在集群的每個(gè)節(jié)點(diǎn)上運(yùn)行存儲,比如:glusterd 或 ceph。
- 在每個(gè)節(jié)點(diǎn)上運(yùn)行日志收集組件,比如:flunentd 、 logstash、filebeat等。
- 在每個(gè)節(jié)點(diǎn)上運(yùn)行監(jiān)控組件,比如:Prometheus、 Node Exporter 、collectd等。
DaemonSet 與 Deployment 的區(qū)別
- Deployment 部署的副本 Pod 會分布在各個(gè) Node 上,每個(gè) Node 都可能運(yùn)行好幾個(gè)副本。
- DaemonSet 的不同之處在于:每個(gè) Node 上最多只能運(yùn)行一個(gè)副本。
DaemonSet資源清單文件編寫技巧
查看定義Daemonset資源需要的字段有哪些
kubectl explain ds
KIND: DaemonSet
VERSION: apps/v1
DESCRIPTION:
DaemonSet represents the configuration of a daemon set.
FIELDS:
apiVersion <string> #當(dāng)前資源使用的api版本,跟VERSION: apps/v1保持一致
kind <string> #資源類型,跟KIND: DaemonSet保持一致
metadata <Object> #元數(shù)據(jù),定義DaemonSet名字的
spec <Object> #定義容器的
status <Object> #狀態(tài)信息,不能改
查看DaemonSet的spec字段如何定義
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> #當(dāng)新的pod啟動幾秒種后,再kill掉舊的pod。
revisionHistoryLimit <integer> #歷史版本
selector <Object> -required- #用于匹配pod的標(biāo)簽選擇器
template <Object> -required-
#定義Pod的模板,基于這個(gè)模板定義的所有pod是一樣的
updateStrategy <Object> #daemonset的升級策略
查看DaemonSet的spec.template字段如何定義
對于template而言,其內(nèi)部定義的就是pod,pod模板是一個(gè)獨(dú)立的對象
kubectl explain ds.spec.template
KIND: DaemonSet
VERSION: apps/v1
RESOURCE: template <Object>
FIELDS:
metadata <Object>
spec<Object>
DaemonSet使用案例:部署日志收集組件fluentd
把fluentd-2-5-1.tar.gz上傳到k8smaster和k8snode上
ctr -n=k8s.io images import fluentd_2_5_1.tar.gz
cat daemonset.yaml
apiVersion: apps/v1 #DaemonSet使用的api版本
kind: DaemonSet # 資源類型
metadata:
name: fluentd-elasticsearch #資源的名字
namespace: kube-system #資源所在的名稱空間
labels:
k8s-app: fluentd-logging #資源具有的標(biāo)簽
spec:
selector: #標(biāo)簽選擇器
matchLabels:
name: fluentd-elasticsearch
template:
metadata:
labels: #基于這回模板定義的pod具有的標(biāo)簽
name: fluentd-elasticsearch
spec:
tolerations: #定義容忍度
- key: node-role.kubernetes.io/master
effect: NoSchedule
containers: #定義容器
- name: fluentd-elasticsearch
image: k8s/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 #掛載目錄是只讀權(quán)限
terminationGracePeriodSeconds: 30 #優(yōu)雅的關(guān)閉服務(wù)
volumes:
- name: varlog
hostPath:
path: /var/log #基于本地目錄創(chuàng)建一個(gè)卷
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers #基于本地目錄創(chuàng)建一個(gè)卷
kubectl apply -f daemonset.yaml
kubectl get ds -n kube-system
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE
fluentd-elasticsearch 3 3 3 3
kubectl get pods -n kube-system -o wide
通過上面可以看到在k8s的三個(gè)節(jié)點(diǎn)均創(chuàng)建了fluentd這個(gè)pod
pod的名字是由控制器的名字-隨機(jī)數(shù)組成的
Daemonset管理pod:滾動更新
DaemonSet實(shí)現(xiàn)pod的滾動更新
查看daemonset的滾動更新策略
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支持的更新策略
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,先刪除在更新;因?yàn)槲覀儾恢С忠粋€(gè)節(jié)點(diǎn)運(yùn)行兩個(gè)pod,因此需要先刪除一個(gè),在更新一個(gè)。文章來源:http://www.zghlxwxcb.cn/news/detail-745490.html
更新鏡像版本,可以按照如下方法:
這個(gè)鏡像啟動pod會有問題,主要是演示daemonset如何在命令行更新pod文章來源地址http://www.zghlxwxcb.cn/news/detail-745490.html
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
到了這里,關(guān)于學(xué)習(xí)筆記二十八:K8S控制器Daemonset入門到企業(yè)實(shí)戰(zhàn)應(yīng)用的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!