實驗環境:
按照圖示部署好了K8s叢集,一個Master,兩個worker nodes。
DaemonSet(DS):
相比于deployment,daemonset不需要設定副本數,而是會自适應節點數,而是會自動在每個節點上建立一個pod。
應用場景:
- 運作叢集存儲 daemon,例如在每個 Node 上運作 glusterd、ceph;
- 在每個 Node 上運作日志收集 daemon,例如fluentd、logstash;
- 在每個 Node 上運作監控 daemon,例如 Prometheus Node Exporter、collectd、Datadog 代理、New Relic 代理,或 Ganglia gmond。 建立daemonset:
可以根據deployment 的yaml檔案來修改:
kubectl create deployment ds1 --image=nginx --dry-run=client -o yaml >ds1.yaml
修改其yaml檔案:修改kind類型,删除掉replicas、strategy和status三個元素即可。同時可以添加terminationGracePeriodSeconds和imagePullPolicy。
[[email protected] daemonset]# cat ds1.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
creationTimestamp: null
labels:
app: ds1
name: ds1
spec:
selector:
matchLabels:
app: ds1
template:
metadata:
creationTimestamp: null
labels:
app: ds1
spec:
terminationGracePeriodSeconds: 0
containers:
- image: nginx
name: nginx
imagePullPolicy: IfNotPresent
resources: {}
建立ds,可以看到其分布在2個work nodes上:
[[email protected] daemonset]# kubectl apply -f ds1.yaml
daemonset.apps/ds1 created
[[email protected] daemonset]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
ds1-48x5f 1/1 Running 0 9s 10.244.58.212 vms202.rhce.cc <none> <none>
ds1-hn8q6 1/1 Running 0 9s 10.244.185.67 vms203.rhce.cc <none> <none>
如果控制ds僅在部分節點運作,可以通過如下方法實作:
首先給一個node打上标簽:
[[email protected] daemonset]# kubectl label nodes vms202.rhce.cc disktype=ssd
node/vms202.rhce.cc labeled
修改daemonset的yaml檔案,增加nodeSelector,用标簽的方式指定排程在哪個node上:
[[email protected] daemonset]# kubectl edit daemonsets.apps ds1
daemonset.apps/ds1 edited
apiVersion: apps/v1
kind: DaemonSet
metadata:
creationTimestamp: null
labels:
app: ds1
name: ds1
spec:
selector:
matchLabels:
app: ds1
template:
metadata:
creationTimestamp: null
labels:
app: ds1
spec:
terminationGracePeriodSeconds: 0
nodeSelector:
disktype: ssd
containers:
- image: nginx
name: nginx
imagePullPolicy: IfNotPresent
resources: {}
檢視ds是否修改成功:
[[email protected] daemonset]# kubectl get ds ds1
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
ds1 1 1 1 1 1 disktype=ssd 58s
k8s元件中由daesonset建立的有如下的pod:
[[email protected] 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 6d12h
kube-proxy 3 3 3 3 3 kubernetes.io/os=linux 6d12h
二、ReplicationController(RC):
ReplicationController (RC)用來確定容器應用的副本數始終保持在使用者定義的副本數,即如果有容器異常退出,會自動建立新的pod來替代;而異常多出來的容器也會自動回收。
修改RC的yaml檔案:
[[email protected] daemonset]# cat rc1.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: myrc
spec:
replicas: 3
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
terminationGracePeriodSeconds: 0
containers:
- image: nginx
name: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
建立RC并檢視:
[[email protected] daemonset]# kubectl apply -f rc1.yaml
replicationcontroller/myrc created
[[email protected] daemonset]# kubectl get rc myrc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myrc 3 3 3 58s nginx nginx app=nginx
伸縮副本數:
[[email protected] daemonset]# kubectl scale rc myrc --replicas=2
replicationcontroller/myrc scaled
[[email protected] daemonset]# kubectl get rc
NAME DESIRED CURRENT READY AGE
myrc 2 2 2 4m38s
删除掉RC:
[[email protected] daemonset]# kubectl delete -f rc1.yaml
replicationcontroller "myrc" deleted
三、ReplicaSet (RS)
在新版的Kubernetes中建議使用ReplicaSet (RS)來取代ReplicationController。ReplicaSet跟ReplicationController沒有本質的不同,隻是名字不一樣,但ReplicaSet支援集合式selector。
雖然 ReplicaSets 可以獨立使用,但如今它主要被Deployments 用作協調 Pod 的建立、删除和更新的機制。當使用 Deployment 時,你不必擔心還要管理它們建立的 ReplicaSet,Deployment 會擁有并管理它們的ReplicaSet。是以說Deployment本質上是通過管理RS來管理pod的。
修改RS的yaml檔案:
[[email protected] daemonset]# cat rs1.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myrs
labels:
app: guestbook
spec:
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
app: guestbook
tier: frontend
spec:
terminationGracePeriodSeconds: 0
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
建立并檢視RS:
[[email protected] daemonset]# kubectl apply -f rs1.yaml
replicaset.apps/myrs created
[[email protected] daemonset]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myrs 3 3 3 88s nginx nginx tier=frontend
修改副本數:
[[email protected] daemonset]# kubectl scale rs myrs --replicas=2
replicaset.apps/myrs scaled
[[email protected] daemonset]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myrs 2 2 2 2m37s nginx nginx tier=frontend
删除RS:
[[email protected] daemonset]# kubectl delete -f rs1.yaml
replicaset.apps "myrs" deleted
參考資料:
《老段CKA課程》