伺服器版本 | docker軟體版本 | Kubernetes(k8s)叢集版本 | CPU架構 |
---|---|---|---|
CentOS Linux release 7.4.1708 (Core) | Docker version 20.10.12 | v1.21.9 | x86_64 |
Kubernetes叢集架構:k8scloude1作為master節點,k8scloude2,k8scloude3作為worker節點
伺服器 | 作業系統版本 | CPU架構 | 程序 | 功能描述 |
---|---|---|---|---|
k8scloude1/192.168.110.130 | CentOS Linux release 7.4.1708 (Core) | x86_64 | docker,kube-apiserver,etcd,kube-scheduler,kube-controller-manager,kubelet,kube-proxy,coredns,calico | k8s master節點 |
k8scloude2/192.168.110.129 | CentOS Linux release 7.4.1708 (Core) | x86_64 | docker,kubelet,kube-proxy,calico | k8s worker節點 |
k8scloude3/192.168.110.128 | CentOS Linux release 7.4.1708 (Core) | x86_64 | docker,kubelet,kube-proxy,calico | k8s worker節點 |
使用DaemonSet 的前提是已經有一套可以正常執行的Kubernetes叢集,關於Kubernetes(k8s)叢集的安裝部署,可以檢視部落格《Centos7 安裝部署Kubernetes(k8s)叢集》https://www.cnblogs.com/renshengdezheli/p/16686769.html。
DaemonSet 確保全部(或者某些)節點上執行一個 Pod 的副本。 當有節點加入叢集時, 也會為他們新增一個 Pod 。 當有節點從叢集移除時,這些 Pod 也會被回收。刪除 DaemonSet 將會刪除它建立的所有 Pod。
DaemonSet 的使用場景:
一種簡單的用法是為每種型別的守護行程在所有的節點上都啟動一個 DaemonSet。 一個稍微複雜的用法是為同一種守護行程部署多個 DaemonSet;每個具有不同的標誌, 並且對不同硬體型別具有不同的記憶體、CPU 要求。
當我們在Kubernetes(k8s)叢集部署軟體的時候,經常就會用到DaemonSet 。
deployment控制器一般需要指定副本數,如果設定了daemonset(簡稱ds),則不需要設定副本數,ds會自適應節點數,會自動在每個節點上建立出來一個pod。關於deployment控制器的詳細內容,請檢視部落格《Kubernetes(k8s)控制器(一):deployment》。
建立存放daemonset yaml檔案的目錄和namespace
[root@k8scloude1 ~]# mkdir daemonset
[root@k8scloude1 ~]# cd daemonset/
[root@k8scloude1 daemonset]# pwd
/root/daemonset
[root@k8scloude1 daemonset]# kubectl create ns daemonset
namespace/daemonset created
切換名稱空間
[root@k8scloude1 daemonset]# kubens daemonset
Context "kubernetes-admin@kubernetes" modified.
Active namespace is "daemonset".
[root@k8scloude1 daemonset]# kubectl get pod
No resources found in daemonset namespace.
daemonset不能通過命令來建立,我們可以通過deployment的yaml檔案來修改為daemonset。
生成deployment的yaml檔案
[root@k8scloude1 daemonset]# kubectl create deployment ds --image=nginx --dry-run=client -o yaml >ds.yaml
[root@k8scloude1 daemonset]# cat ds.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: ds
name: ds
spec:
replicas: 1
selector:
matchLabels:
app: ds
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: ds
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
把deployment的yaml檔案修改為daemonset的yaml檔案的方法:
下面進行修改生成daemonset的組態檔:
[root@k8scloude1 daemonset]# vim ds.yaml
[root@k8scloude1 daemonset]# cat ds.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
creationTimestamp: null
labels:
app: ds
name: ds
spec:
selector:
matchLabels:
app: ds
template:
metadata:
creationTimestamp: null
labels:
app: ds
spec:
terminationGracePeriodSeconds: 0
containers:
- image: nginx
name: nginx
#修改映象下載策略:IfNotPresent表示本地有映象就不下載
imagePullPolicy: IfNotPresent
resources: {}
建立daemonset
[root@k8scloude1 daemonset]# kubectl apply -f ds.yaml
daemonset.apps/ds created
檢視daemonset,發現有2個pod
[root@k8scloude1 daemonset]# kubectl get ds
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
ds 2 2 2 2 2 <none> 20s
[root@k8scloude1 daemonset]# kubectl get ds -o wide
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE CONTAINERS IMAGES SELECTOR
ds 2 2 2 2 2 <none> 24s nginx nginx app=ds
檢視pod,daemonset在每個節點上都建立了一個pod,但是沒有在master節點建立pod,因為master節點上有汙點。
[root@k8scloude1 daemonset]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
ds-hvn89 1/1 Running 0 47s 10.244.251.222 k8scloude3 <none> <none>
ds-qfq58 1/1 Running 0 47s 10.244.112.160 k8scloude2 <none> <none>
刪除daemonset,pod隨之被刪除
[root@k8scloude1 daemonset]# kubectl delete ds ds
daemonset.apps "ds" deleted
[root@k8scloude1 daemonset]# kubectl get pod -o wide
No resources found in daemonset namespace.
檢視node節點的標籤,--show-labels表示顯示標籤
[root@k8scloude1 daemonset]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8scloude1 Ready control-plane,master 17d v1.21.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8scloude1,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
k8scloude2 Ready <none> 17d v1.21.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8scloude2,kubernetes.io/os=linux,taint=T
k8scloude3 Ready <none> 17d v1.21.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8scloude3,kubernetes.io/os=linux
滿足標籤為kubernetes.io/hostname=k8scloude3的只有k8scloude3節點
[root@k8scloude1 daemonset]# kubectl get node -l kubernetes.io/hostname=k8scloude3
NAME STATUS ROLES AGE VERSION
k8scloude3 Ready <none> 17d v1.21.0
設定daemonset,通過標籤選擇器,設定pod只在kubernetes.io/hostname=k8scloude3的節點執行
[root@k8scloude1 daemonset]# vim ds.yaml
#設定pod只在kubernetes.io/hostname=k8scloude3的節點執行
[root@k8scloude1 daemonset]# cat ds.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
creationTimestamp: null
labels:
app: ds
name: ds
spec:
selector:
matchLabels:
app: ds
template:
metadata:
creationTimestamp: null
labels:
app: ds
spec:
terminationGracePeriodSeconds: 0
#nodeSelector設定pod只在kubernetes.io/hostname=k8scloude3的節點執行
nodeSelector:
kubernetes.io/hostname: k8scloude3
containers:
- image: nginx
name: nginx
imagePullPolicy: IfNotPresent
resources: {}
建立daemonset
[root@k8scloude1 daemonset]# kubectl apply -f ds.yaml
daemonset.apps/ds configured
[root@k8scloude1 daemonset]# kubectl get ds
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
ds 1 1 1 1 1 kubernetes.io/hostname=k8scloude3 2m59s
pod只執行在k8scloude3節點
[root@k8scloude1 daemonset]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
ds-2l5hr 1/1 Running 0 24s 10.244.251.230 k8scloude3 <none> <none>
刪除daemonset
[root@k8scloude1 daemonset]# kubectl delete ds ds
daemonset.apps "ds" deleted
[root@k8scloude1 daemonset]# kubectl get ds
No resources found in daemonset namespace.
檢視kube-system名稱空間下的daemonset有哪些
[root@k8scloude1 daemonset]# kubectl get ds -n kube-system
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
calico-node 3 3 3 3 3 kubernetes.io/os=linux 17d
kube-proxy 3 3 3 3 3 kubernetes.io/os=linux 17d
可以檢視kube-proxy 的Daemonset組態檔
[root@k8scloude1 daemonset]# kubectl get ds kube-proxy -n kube-system -o yaml > kube-proxy.yaml
[root@k8scloude1 daemonset]# vim kube-proxy.yaml