实验环境
完成初始化集群的环境:
(vms21)192.168.26.21——master1
(vms22)192.168.26.22——worker1
(vms23)192.168.26.23——worker2
一、daemonset控制器介绍
前面deploy是指定了pod的副本数量,然后创建在各节点中,这里daemonset则不同,daemonset不指定副本数,但控制在每个节点上都运行一个pod副本,若后期有新增的节点,也会在新增的节点中自动跑一个副本,即有几个节点就跑几个副本,每个节点确保有一个副本就行了
如k8s中一些必须的组件,calico-node、kube-proxy都是在每个节点上保证有一个就行
#daemonset缩写为ds
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 41h
kube-proxy 3 3 3 3 3 kubernetes.io/os=linux 41h
二、daemonset创建
我们可以以deploy的yaml文件为模板,然后做以下几处修改:
a.将kind改为DaemonSet
b.将定义副本数的配置项replicas删除
c.将滚动更新的配置项strategy删除
d.将status配置项删除
e.若没有镜像下载策略则加上IfNotPresent
(1)如下,我们输出一个nginx镜像的deploy模板
kubectl create deploy myds1 --image nginx --dry-run=client -o yaml > myds1.yaml
(2)修改后的yaml为:
apiVersion: apps/v1
kind: DaemonSet
metadata:
creationTimestamp: null
labels:
app: myds1
name: myds1
spec:
selector:
matchLabels:
app: myds1
template:
metadata:
creationTimestamp: null
labels:
app: myds1
spec:
containers:
- image: nginx
name: nginx
imagePullPolicy: IfNotPresent
resources: {}
(3)创建daemontset
kubectl apply -f myds1.yaml
(4)查看pod,可以看到在worker1、worker2上各创建了一个pod
kubectl get pods -o wide
#输出:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myds1-4r9wx 1/1 Running 0 5m1s 10.244.7.41 vms22.rhce.cc <none> <none>
myds1-nxvgb 1/1 Running 0 5m1s 10.244.70.106 vms23.rhce.cc <none> <none>
那么为什么没有在master这个节点上创建出pod呢?不是说每个节点都运行一个副本吗?——因为污点
现在我们给pod设置容忍
(1)查看master节点上的污点
kubectl describe nodes vms21.rhce.cc | grep -A1 Taint
#输出:
Taints: node-role.kubernetes.io/control-plane:NoSchedule
node-role.kubernetes.io/master:NoSchedule
(2)修改后为:
apiVersion: apps/v1
kind: DaemonSet
metadata:
creationTimestamp: null
labels:
app: myds1
name: myds1
spec:
selector:
matchLabels:
app: myds1
template:
metadata:
creationTimestamp: null
labels:
app: myds1
spec:
tolerations:
- effect: NoSchedule
key: node-role.kubernetes.io/control-plane
operator: Exists
- effect: NoSchedule
key: node-role.kubernetes.io/master
operator: Exists
containers:
- image: nginx
name: nginx
imagePullPolicy: IfNotPresent
resources: {}
(3)删除之前的daemontset,重新创建后,可以看到所有节点上都运行了一个pod副本
kubectl delete ds myds1
kubectl apply -f myds1.yaml
kubectl get pods -o wide
#输出:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myds1-hd77k 1/1 Running 0 7s 10.244.43.197 vms21.rhce.cc <none> <none>
myds1-rpz5x 1/1 Running 0 7s 10.244.7.44 vms22.rhce.cc <none> <none>
myds1-vcmq2 1/1 Running 0 7s 10.244.70.109 vms23.rhce.cc <none> <none>
也可以通过节点的标签匹配,控制只在某一个节点上创建pod
只需加上nodeSelector配置项:
...
spec:
nodeSelector:
xx: xx
tolerations:
- effect: NoSchedule
key: node-role.kubernetes.io/control-plane
operator: Exists
- effect: NoSchedule
key: node-role.kubernetes.io/master
operator: Exists
containers:
- image: nginx
name: nginx
imagePullPolicy: IfNotPresent
resources: {}
三、删除daemonset
daemonset缩写为ds
kubectl delete ds [daemonset名]
四、ReplicatonController(RC)控制器
RC也是一种控制器
yaml文件模板如下:
rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: myrc
spec:
replicas: 3
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
查看rc
kubectl get rc
#输出:
NAME DESIRED CURRENT READY AGE
myrc 5 5 5 10m
查看pod,可以看到已经创建出3个pod
kubectl get pods
#输出:
NAME READY STATUS RESTARTS AGE
myrc-7z9qv 1/1 Running 0 27s
myrc-9fddz 1/1 Running 0 27s
myrc-p6m6g 1/1 Running 0 27s
将rc的副本数设为5个
kubectl scale rc myrc --replicas 5
删除rc
kubectl delete rc myrc
五、ReplicaSet(rs)控制器
yaml文件模板如下:
rs.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:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
查看rs
kubectl get rs
#输出:
NAME DESIRED CURRENT READY AGE
myrs 3 3 3 6s
查看pod
kubectl get pods
#输出:
NAME READY STATUS RESTARTS AGE
myrs-5xqtx 1/1 Running 0 77s
myrs-g4r7q 1/1 Running 0 77s
myrs-kbkjv 1/1 Running 0 77s
修改副本数
kubectl scale rs myrs --replicas 6
删除rs
kubectl delete rs myrs