在K8S集群内,nginx:1.20有状态应用十个副本,2个副本作为灰度发布中被保存的稳定版本,更新版本为nginx:1.21副本数量为8个
在三台主机中创建html
root@server1:~# mkdir html
root@server2:~# mkdir html
root@server3:~# mkdir html
写入
root@server1:~# echo "wkz add nginx" > html/index.html
root@server2:~# echo "wkz add nginx" > html/index.html
root@server3:~# echo "wkz add nginx" > html/index.html
编辑
root@server1:~# vim nginx.yaml
添加:
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
updateStrategy:
type: RollingUpdate
rollingUpdate:
partition: 2
serviceName: "nginx"
replicas: 10
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.20
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumes:
- name: www
hostPath:
path: /root/html
执行
root@server1:~# kubectl apply -f nginx.yaml
root@server1:~# kubectl get pod -o wide
查看容器image版本号
root@server1:~# for p in 0 1 2 3 4 5 6 7 8 9; do kubectl get pod web-$p --template '{ {range $i, $c := .spec.containers}}{ {$c.image}}{ {end}}'; echo; done
root@server1:~# kubectl describe statefulsets.apps web
然后在server2和server3更改html内容和位置
root@server2:~# mkdir /html
root@server2:~# echo "192.168.0.141" > /html/index.html
root@server3:~# mkdir /html
root@server3:~# echo "192.168.0.142" > /html/index.html
第一台修nginx.yaml配置文件
root@server1:~# vim nginx.yaml
修改版本号和网页地址为了更好的分辨
执行
root@server1:~# kubectl apply -f nginx.yaml
root@server1:~# for p in 0 1 2 3 4 5 6 7 8 9; do kubectl get pod web-$p --template '{ {range $i, $c := .spec.containers}}{ {$c.image}}{ {end}}'; echo; done
root@server1:~# kubectl get pod -o wide