statefulset基础模版
[root@master statefulset]# cat web.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 # statefulset 类型的资源
metadata:
name: web # statefulset 对象的名字
spec:
serviceName: "nginx" # 使用哪个service来管理dns
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports: # 容器内部要暴露的端口
- containerPort: 80 # 具体暴露的端口号
name: web #该端口配置的名字
// 先不写下面的,因为涉及到别的知识点
volumeMounts: # 加载数据卷
- name: www #指定加载哪个数据卷
mountPath: /usr/share/nginx/html # 加载到容器中的哪个目录
volumeClaimTemplates: # 数据卷模版
- metadata: # 数据卷描述
name: www # 数据卷的名称
annotations: # 数据卷的注解
volume.alpha.kubernetes,io.storage-class: anything
spec: # 数据卷的规约
accessModes: [ "ReadWriteOnce" ] # 访问模式
resources:
requests:
storage: 1Gi # 需要1G 的存储资源
[root@master statefulset]#
[root@master statefulset]# kubectl create -f web.yaml
service/nginx created
statefulset.apps/web created
[root@master statefulset]# kubectl get sts
NAME READY AGE
web 0/2 5s
[root@master statefulset]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d18h
nginx ClusterIP None <none> 80/TCP 8s
[root@master statefulset]# kubectl get pvc //注释掉之后这个是没有的
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
www-web-0 Pending 11s
//修改后
[root@master statefulset]# kubectl create -f web.yaml
service/nginx created
statefulset.apps/web created
[root@master statefulset]# kubectl get sts
NAME READY AGE
web 2/2 9s
[root@master statefulset]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d19h
nginx ClusterIP None <none> 80/TCP 15s
[root@master statefulset]#
# 有一个疑问,k8s集群内部`kubectl get po`并没有这么多pod
[root@master ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
my-pod 1/1 Running 3 (4m8s ago) 5d18h
nginx-demo 1/1 Running 4 (4m8s ago) 5d23h
nginx-po 1/1 Running 4 (4m8s ago) 5d22h
web-0 1/1 Running 2 (4m8s ago) 20h
web-1 1/1 Running 2 (4m8s ago) 20h
[root@master ~]#
# python 中列出pod ,列出的是所有的pod 等价于kubectl get -A po
[root@master ~]# kubectl get -A po
NAMESPACE NAME READY STATUS RESTARTS AGE
default my-pod 1/1 Running 3 (3m12s ago) 5d18h
default nginx-demo 1/1 Running 4 (3m12s ago) 5d23h
default nginx-po 1/1 Running 4 (3m12s ago) 5d22h
default web-0 1/1 Running 2 (3m12s ago) 20h
default web-1 1/1 Running 2 (3m12s ago) 20h
kube-flannel kube-flannel-ds-bkvjm 1/1 Running 4 (3m12s ago) 5d23h
kube-flannel kube-flannel-ds-hg6bh 1/1 Running 5 (3m56s ago) 6d14h
kube-system coredns-66f779496c-c5rmz 1/1 Running 4 (3m56s ago) 6d15h
kube-system coredns-66f779496c-jswxz 1/1 Running 4 (3m56s ago) 6d15h
kube-system etcd-master 1/1 Running 4 (3m56s ago) 6d15h
kube-system kube-apiserver-master 1/1 Running 4 (3m56s ago) 6d15h
kube-system kube-controller-manager-master 1/1 Running 5 (3m56s ago) 6d15h
kube-system kube-proxy-jr5m5 1/1 Running 4 (3m56s ago) 6d15h
kube-system kube-proxy-tcpzh 1/1 Running 4 (3m12s ago) 5d23h
kube-system kube-scheduler-master 1/1 Running 5 (3m57s ago) 6d15h
tigera-operator tigera-operator-55585899bf-s28bt 1/1 Running 8 (3m19s ago) 6d14h
拉取指定镜像测试
[root@master statefulset]# kubectl run -it --image busybox:1.28.4 dns-test /bin/sh
If you don't see a command prompt, try pressing enter.
/ # ping web-0.nginx
PING web-0.nginx (10.244.1.45): 56 data bytes
64 bytes from 10.244.1.45: seq=0 ttl=64 time=0.048 ms
64 bytes from 10.244.1.45: seq=1 ttl=64 time=0.054 ms
^C
--- web-0.nginx ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.048/0.051/0.054 ms
/ # nslookup web-0.nginx # sts的名字-第几个.service的名字 是statefulset的访问格式
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.1.45 web-0.nginx.default.svc.cluster.local
/ #
扩容和缩容
# 扩容
[root@master statefulset]# kubectl scale sts web --replicas=6
statefulset.apps/web scaled
# 缩容
[root@master statefulset]# kubectl scale sts web --replicas=3
statefulset.apps/web scaled
[root@master statefulset]# kubectl get sts
NAME READY AGE
web 3/3 20h
[root@master statefulset]#
灰度更新
[root@master statefulset]# kubectl scale sts web --replicas=6
statefulset.apps/web scaled
[root@master statefulset]# kubectl get sts
NAME READY AGE
web 6/6 20h
[root@master statefulset]# kubectl edit sts web
statefulset.apps/web edited
# 进去之后修改
---
updateStrategy:
rollingUpdate:
partition: 3 # 把0修改为3
# 修改partition的值可以模拟灰度更新,就是说更新的时候先更新编号大于3的pod
---
# 查看
[root@master statefulset]# kubectl describe po web-1|grep nginx:
nginx:
Image: nginx:1.7.9
Normal Pulled 20h kubelet Container image "nginx:1.7.9" already present on machine
Normal Pulled 12h kubelet Container image "nginx:1.7.9" already present on machine
Normal Pulled 48m kubelet Container image "nginx:1.7.9" already present on machine
[root@master statefulset]# kubectl describe po web-5|grep nginx:
nginx:
Image: nginx:1.9.1
Normal Pulled 95s kubelet Container image "nginx:1.9.1" already present on machine
[root@master statefulset]#
# 确认新版本没有问题之后,在修改partition的值为0,来更新所有
[root@master statefulset]# kubectl edit sts web
statefulset.apps/web edited
[root@master statefulset]# kubectl describe po web-5|grep nginx:
nginx:
Image: nginx:1.9.1
Normal Pulled 6m13s kubelet Container image "nginx:1.9.1" already present on machine
[root@master statefulset]# kubectl describe po web-0|grep nginx:
nginx:
Image: nginx:1.9.1
Normal Pulled 4s kubelet Container image "nginx:1.9.1" already present on machine
[root@master statefulset]#
OnDelete策略更新
[root@master statefulset]# kubectl edit sts web
statefulset.apps/web edited
---
updateStrategy:
type: OnDelete #修改更新策略
---
[root@master statefulset]# kubectl edit sts web
statefulset.apps/web edited
---
spec:
containers:
- image: nginx:1.7.9 #修改镜像
imagePullPolicy: IfNotPresent
name: nginx
ports:
---
[root@master statefulset]# kubectl describe po web-0 |grep nginx:
nginx:
Image: nginx:1.9.1
Normal Pulled 13m kubelet Container image "nginx:1.9.1" already present on machine
[root@master statefulset]# kubectl describe po web-1 |grep nginx:
nginx:
Image: nginx:1.9.1
Normal Pulled 14m kubelet Container image "nginx:1.9.1" already present on machine
[root@master statefulset]# kubectl delete pod web-1
pod "web-1" deleted # 看似删除实则更新
[root@master statefulset]#
[root@master statefulset]# kubectl describe po web-1 |grep nginx:
nginx:
Image: nginx:1.7.9
Normal Pulled 3s kubelet Container image "nginx:1.7.9" already present on machine
[root@master statefulset]#
删除sts
rs是deployment的东西,所以sts没有rs
删除sts和headless service的时候分为两种,级联删除和非级联删除
级联删除
# 删除sts时候,会同时删除pods
[root@master statefulset]# kubectl get sts
NAME READY AGE
web 3/3 23h
[root@master statefulset]# kubectl delete sts web
statefulset.apps "web" deleted
[root@master statefulset]# kubectl get sts
No resources found in default namespace.
[root@master statefulset]# kubectl get po
NAME READY STATUS RESTARTS AGE
dns-test 1/1 Running 1 (160m ago) 162m
[root@master statefulset]#
# 删除sts的时候,直接删除pod
非级联删除
[root@master statefulset]# kubectl get sts
NAME READY AGE
web 2/2 25s
[root@master statefulset]# kubectl get po
NAME READY STATUS RESTARTS AGE
dns-test 1/1 Running 1 (166m ago) 168m
web-0 1/1 Running 0 27s
web-1 1/1 Running 0 26s
[root@master statefulset]# kubectl delete sts web --cascade=false
warning: --cascade=false is deprecated (boolean value) and can be replaced with --cascade=orphan.
statefulset.apps "web" deleted
[root@master statefulset]# kubectl get po
NAME READY STATUS RESTARTS AGE
dns-test 1/1 Running 1 (166m ago) 168m
web-0 1/1 Running 0 74s
web-1 1/1 Running 0 73s
[root@master statefulset]# kubectl get sts
No resources found in default namespace.
[root@master statefulset]#
# 非级联删除没有连带删除pod
标签:web,statefulset,kubectl,nginx,master,k8s,root
From: https://www.cnblogs.com/humlogs/p/18203601