第1章 ReplicaSet控制器
1.ReplicaSet作用
可以创建多个POD的副本
始终保持POD副本数满足用户期望的数量在运行
2.ReplicaSet资源配置清单
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-rs
labels:
app: nginx-rs
spec:
replicas: 2
selector: #选择
matchLabels: #匹配pod的标签
app: nginx #pod的标签
template: #模板,按照以下模板创建pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.0
imagePullPolicy: IfNotPresent
ports:
- name: http #给port起一个名字
containerPort: 80 #容器的80端口暴露出来
他会把他分配到不同的节点
pod命名都是在元数据命名后面随机添加名称用来区别
3.扩容和收缩
命令行
scale 是改变的意思
replicas 复制,副本的意思
kubectl scale rs rs资源名称 --replicas=4
kubectl scale rs nginx-rs --replicas=4
或者修改配置清单
修改配置清单
vim nginx-rs.yml
kubectl apply -f nginx-rs.yml
4.删除RS
命令行删除
kubectl delete rs nginx-rs
删除资源配置清单
kubectl delete -f nginx-rs.yml
rs也是一个资源类型,跟pod一样也可以使用kubectl get rs rs名
现在是nginx 1.14.0版本
查看pod详细信息 kubectl describe pod
修改nginx-rs.yml 资源配置清单nginx版本为1.16.0
重新执行资源配置清单
还是nginx:1.14.0版本
只有删除了所有pod重新执行,才会更新版本
查看pod标签
kubectl get pod --show-labels
修改pod标签
去掉标签
kubectl label pod nginx-rs-4xbtt app-
修改标签
kubectl label pod nginx-rs-4xbtt app=xxx
注意:修改pod标签,要先把标签去掉载删除,再添加标签
注意:pod标签名修改后,rs就不认识pod了,它就会自动创建一个pod。rs都是通过标签名来识别pod
deployment
1.deployment和RS区别
RS只负责创建和持续保持相应副本数量的POD在运行,但是不能动态更新POD版本
Deployment替你创建RS,RS再创建相应副本数的POD
2.deployment资源配置
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-dp
labels:
app: nginx-dp
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.0
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
注意:现在又6个pod,但是不会冲突,每个人找的pod标签不一样
先删除以前的rs创建的pod
3.通过查看日志观察Deployment升级POD过程
NAME READY STATUS RESTARTS AGE
正在运行的旧版本的POD
nginx-dp-6c8996fdc9-7smdf 1/1 Running 0 13m
nginx-dp-6c8996fdc9-kqdw6 1/1 Running 0 14m
先查看运行pod的nginx版本
kubectl describe pod
vim 修改nignx-dp.yml资源配置
修改nginx版本为1.14.0
运行成功第一个新版本的POD
nginx-dp-57559d66df-pkwsw 0/1 Pending 0 0s
nginx-dp-57559d66df-pkwsw 0/1 Pending 0 0s
nginx-dp-57559d66df-pkwsw 0/1 ContainerCreating 0 0s
nginx-dp-57559d66df-pkwsw 1/1 Running 0 1s
删除一个旧的POD
nginx-dp-6c8996fdc9-7smdf 1/1 Terminating 0 14m
运行成功第二个新版本的POD
nginx-dp-57559d66df-r22c2 0/1 Pending 0 0s
nginx-dp-57559d66df-r22c2 0/1 Pending 0 0s
nginx-dp-57559d66df-r22c2 0/1 ContainerCreating 0 1s
nginx-dp-57559d66df-r22c2 1/1 Running 0 1s
删除二个旧的POD
nginx-dp-6c8996fdc9-7smdf 0/1 Terminating 0 14m
nginx-dp-6c8996fdc9-kqdw6 1/1 Terminating 0 14m
4.更新镜像版本方法
更改资源配置清单
vim nginx-dp.yaml
kubectl apply -f nginx-dp.yaml
使用命令更新
kubectl set image deployment nginx-dp nginx=nginx:1.14.0
kubectl set image -f nginx-dp.yaml nginx=nginx:1.16.0
时时监控pod状态
更新nginx:1.16.0版本
5.查看历史版本
默认查看历史版本是没有说明信息的
kubectl rollout history deployment nginx-dp
注意:使用deployment创建pod都要创建一个rs 控制器,相当于历史版本
可以在更新命令后面添加--record来记录本次更新的命令
record记录
kubectl apply -f nginx-dp.yaml --record
kubectl set image -f nginx-dp.yaml nginx=1.16.0 --record
kubectl set image deployment nginx-dp nginx=1.16.0 --record
查看指定版本的信息
kubectl rollout history deployment nginx-dp --revision=1
kubectl rollout history deployment nginx-dp --revision=3
6.回滚到上一个版本
kubectl rollout undo deployment nginx-dp
7.回滚到指定版本
kubectl rollout undo deployment nginx-dp --to-revision=2
8.扩容和缩小
跟rs控制器是一样的
1.vim修改资源配置清单
2.使用命令扩容收缩
收缩
kubectl scale deployment nginx-dp --replicas=2
扩容
kubectl scale deployment nginx-dp --replicas=4
daemonset
1.DaemonSet作用
1.每个节点运行1个POD
- pod数量由node节点数量控制
2.DaemonSet应用场景
在每个节点上运行集群守护进程
在每个节点上运行日志收集守护进程
在每个节点上运行监控守护进程
3.资源配置清单
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: filebeat-ds
labels:
app: filebeat-ds
spec:
selector:
matchLabels:
app: filebeat
template:
metadata:
name: filebeat
labels:
app: filebeat
spec:
containers:
- name: filebeat
image: nginx:1.16.0
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
首先删除所有的节点pod运行
创建pod
kubectl apply -f nginx-ds.yml
5.选择特定节点运行
kubectl label nodes node1 disktype=SATA
kubectl label nodes node2 disktype=ssd
---------------------------------------
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: filebeat-ds
labels:
app: filebeat-ds
spec:
selector:
matchLabels:
app: filebeat
template:
metadata:
name: filebeat
labels:
app: filebeat
spec:
nodeSelector:
disktype: SATA
containers:
- name: filebeat
image: nginx:1.16.0
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
查询运行在哪个节点
4.问题记录:
报错内容: 重置node节点后,POD在节点创建不了
Warning FailedCreatePodSandBox 39s (x4 over 42s) kubelet (combined from similar events): Failed to create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container "fdb7453e4026417f56e9a09f01d68225bd2ecc65136f418aeef38496cc0e4afc" network for pod "filebeat-ds-9txt9": networkPlugin cni failed to set up pod "filebeat-ds-9txt9_default" network: failed to set bridge addr: "cni0" already has an IP address different from 10.2.3.1/24
问题原因:
重置节点的时候,flannel文件未清理,导出创建的cni0和flannel地址不一样
[root@node2 ~]# cat /run/flannel/subnet.env
FLANNEL_NETWORK=10.2.0.0/16
FLANNEL_SUBNET=10.2.3.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true
解决: 删除cni0网卡,让他重新创建,重建的时候会去读flannel环境变量文件
ifconfig cni0 down
ip link delete cni0