- ---
- 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:
- serviceName: "nginx" # 使用那个service来管理dns
- replicas: 2
- selector:
- matchLabels:
- app: nginx
- template:
- metadata:
- labels:
- app: nginx
- spec:
- containers:
- - name: nginx
- image: nginx:latest
- ports: # 容器暴露的端口
- - containerPort: 80 # 具体暴露的端口号
- name: web #该端口配置的名字
- # volumeMounts:
- # - name: www #指定加载那个数据券
- # mountPath: /usr/share/nginx/html #加载到容器中的那个目录
- # 创建
- kubectl create sts -f web.yaml
- # 查看
- kubeclt get sts,svc,po
- # 查看ip信息
- kubectl run -it --image busybox:1.28.4 dns-test /bin/sh
- nslookup web-0.nginx
-
- #打印信息如下
- Server: 10.96.0.10
- Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
-
- Name: web-0.nginx
- Address 1: 10.244.107.210 web-0.nginx.default.svc.cluster.local
-
- # 扩容,缩容 两种方式,选择其一
- kubectl scale statefulset web --replicas=5
- kubectl patch statefulset web -p '{"spec":{"replicas":2}}'
-
- # 查看描述信息
- kubectl descrbe sts web
-
- ...
- Events:
- Type Reason Age From Message
- ---- ------ ---- ---- -------
- Normal SuccessfulCreate 28m statefulset-controller create Pod web-0 in StatefulSet web successful
- Normal SuccessfulCreate 27m statefulset-controller create Pod web-1 in StatefulSet web successful
- Normal SuccessfulCreate 9m13s statefulset-controller create Pod web-2 in StatefulSet web successful
- Normal SuccessfulCreate 8m52s statefulset-controller create Pod web-3 in StatefulSet web successful
- Normal SuccessfulCreate 8m32s statefulset-controller create Pod web-4 in StatefulSet web successful
- Normal SuccessfulDelete 6m28s statefulset-controller delete Pod web-4 in StatefulSet web successful
- Normal SuccessfulDelete 6m25s statefulset-controller delete Pod web-3 in StatefulSet web successful
- Normal SuccessfulDelete 6m23s statefulset-controller delete Pod web-2 in StatefulSet web successful
-
- kubectl patch sts web --type='json' -p='[{"op":"replace","path":"/spec/template/spec/containers/0/image","value":"nginx:latest"}]'
-
- # 这里有可能报错,那就把 "nginx:1.9.1" 改为 "nginx:1.26" 请注意到hub.docker.net去查看对应的版本
- # 查看所有
- kubectl rollout history sts web
-
- # 结果
- statefulset.apps/web
- REVISION CHANGE-CAUSE
- 1
- 2
-
-
- # 指定查看对应的版本
- kubectl rollout history sts web --revision=2
-
- # 结果
- statefulset.apps/web with revision #2
- Pod Template:
- Labels: app=nginx
- Containers:
- nginx:
- Image: nginx:1.9.1
- Port: 80/TCP
- Host Port: 0/TCP
- Environment:
- Mounts:
- Volumes:
-
-
- # 查看状态
- kubectl rollout status sts web
利用滚动更新中的partition 属性,可以实现简易的灰度发布效果
简介:加入我们有5个pod,如果当partition设置为3,那么此时滚动更新是,只会更新需要>=3的pod,利用该机制,我们可以通过控制partion来决定更新那一部分的内容,确定没有问题后,在逐渐增大更新pod的数量;如果partition为0 ,那表示所有的都更新
- # 修改sts配置文件
- kubectl edit sts web
-
- # 修改partition:3 和 nignx:latest
- # 保存退出,通过kubectl describe po web-4 和 web-0 就能看到效果
StatefulSet的删除
- # 练级删除 会删除sts和pod
-
- kubectl delete sts web
-
- # 非联级删除
- kubectl delete sts web --cascade=false
-
- # 删除services
- kubectl delete svc nginx