k8s StatefulSet && 金丝雀发布
CoreOS: Operator
StatefulSet:
Cattle, pet
PetSet -> 1.5+ StatefulSet
应用的对象的特征:
1、稳定且唯一的网络标识符;
2、稳定且持续的存储;
3、有序、平滑的部署和扩展;
4、有序、平滑的删除和终止
5、有序的滚动更新
三个组件:headless service、statefulSet、volumeClaimTemplate
1.创建存储pv
- vim pv-demo.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv001
labels:
name: pv001
spec:
nfs:
path: /data/volumes/v1
server: 9.110.187.180
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv002
labels:
name: pv002
spec:
nfs:
path: /data/volumes/v2
server: 9.110.187.180
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv003
labels:
name: pv003
spec:
nfs:
path: /data/volumes/v3
server: 9.110.187.180
图片1.png
2.创建一个statfulSet的案例:其中包括headless servcie、statefulSet、volumeClaimTemplate
- vim statefulSet.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp
labels:
app: myapp
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: myapp-pod
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: myapp
spec:
serviceName: myapp
replicas: 3
selector:
matchLabels:
app: myapp-pod
template:
metadata:
labels:
app: myapp-pod
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: web
containerPort: 80
volumeMounts:
- name: myappdata
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: myappdata
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 5Gi
- kubectl apply -f statefulSet.yaml
3.这时我们会查看创建的无头服务
-
kubectl get svc
image.png
服务创建成功
4.查看pv是否被绑定:kubectl get pv
image.pngPv被成功绑定。
5. 查看pvc是否创建成功:kubectl get pvc
image.pngPvc被成功创建
6.查看statefulSet是否被创建:kubectl get sts
image.png7.查看pod是否运行正常:kubectl get pod
image.png说明案例创建成功。
8.创建的pod的名称可以在k8s集群里面解析,举例:
- kubectl exec -it myapp-0 /bin/sh
-
nslookup myapp-0
image.png
横向扩展:kubectl scale sts myapp --replicas=5
查看扩展结果:kubectl get pvc 或kubectl get pod -o wide
StatefulSet做金丝雀更新
kubectl patch sts myapp -p '{"spec":{"updateStrategy":{"rollingUpdate":{"partition":4}}}}'
表示从编号大于等于4的pod先更新,小于4的暂时不更新
现在我们更新image得到为ikubernetes/myapp:v2
kubectl set image sts/myapp myapp=ikubernetes/myapp:v2
查看statefulSet的状态:kubectl get sts -o wide
image.png可以看出系统层面已经更改完毕,查看一下pod的层面镜像是否已经更改了。
Kubectl get pod myapp-4 -o yaml
显示镜像已经被更新的信息,
查看其它pod信息未被修改。
如果修改的镜像没有问题,就全部更新,这就是金丝雀发布。
注意:如果全面更新完成, 出现问题还可以回滚回去的。以下命令回滚 kubectl rollout undo pod XX