kubernetes学习-StatefulSet(五)

一、配置文件

---
apiVersion: v1
kind: Service # 表示这是一个Service资源
metadata:
  name: nginx # 是这个Service的名称
  labels: # 包含一个标签app: nginx,用于标识和选择这个Service
    app: nginx
spec:
  ports:
  - port: 80 # 定义Service暴露的端口
    name: web # 名称为web
  clusterIP: None
  selector: #  使用标签选择器来选择哪些Pod应该被这个Service管理
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet # 表示这是一个StatefulSet资源
metadata:
  name: web # web 是这个StatefulSet的名称
spec:
  serviceName: "nginx" # 指定了这个StatefulSet使用的Service名称,这个Service用于提供稳定的网络标识和DNS
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template: # 定义了Pod的模板
    metadata: # 包含Pod的标签,这里也是app: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx # 容器的名称
        image: nginx # 指定了容器使用的镜像
        ports: # 定义了容器暴露的端口,端口号为80,名称为web
        - containerPort: 80
          name: web
        volumeMounts: #  定义了容器挂载的卷
        - name: www
          mountPath: /usr/share/nginx/html # 是容器内挂载的路径
  volumeClaimTemplates: # 定义了Pod使用的持久化卷的模板
  - metadata:
      name: www # 是卷的名称
      annotations:
        volume.alpha.kubernetes.io/storage-class: anything # 包含了卷的注解,这里指定了存储类volume.alpha.kubernetes.io/storage-class: anything
    spec:
      accessModes: [ "ReadWriteOnce" ] # 表示这个卷可以被一个Pod以读写模式挂载一次
      resources:
        requests:
          storage: 1Gi # 表示请求1GB的存储空间
(1)创建
[root@k8s-master statefulset]# kubectl create -f web.yaml 
service/nginx created
statefulset.apps/web created
[root@k8s-master statefulset]# kubectl get sts
NAME   READY   AGE
web    0/2     24s
[root@k8s-master statefulset]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   7d20h
nginx        ClusterIP   None         <none>        80/TCP    29s
[root@k8s-master statefulset]# kubectl get pvc
NAME        STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
www-web-0   Pending                                                     3m57s

[root@k8s-master statefulset]# kubectl describe pvc www-web-0
Name:          www-web-0
Namespace:     default
StorageClass:  
Status:        Pending
Volume:        
Labels:        app=nginx
Annotations:   volume.alpha.kubernetes.io/storage-class: anything
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      
Access Modes:  
VolumeMode:    Filesystem
Used By:       web-0
Events:
  Type    Reason         Age                  From                         Message
  ----    ------         ----                 ----                         -------
  Normal  FailedBinding  0s (x21 over 4m49s)  persistentvolume-controller  no persistent volumes available for this claim and no storage class is set

发现没有启动成功,先删除pvc相关配置,重新创建sts
[root@k8s-master statefulset]# kubectl get sts
NAME   READY   AGE
web    2/2     45s
[root@k8s-master statefulset]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   7d20h
nginx        ClusterIP   None         <none>        80/TCP    51s
[root@k8s-master statefulset]# kubectl get po
NAME    READY   STATUS    RESTARTS   AGE
web-0   1/1     Running   0          2m43s
web-1   1/1     Running   0          2m26s
(2)扩容缩容
# 扩容
$ kubectl scale statefulset web --replicas=5

# 缩容
$ kubectl patch statefulset web -p '{"spec":{"replicas":3}}'

# 扩容
[root@k8s-master statefulset]# kubectl scale sts web --replicas=5
statefulset.apps/web scaled

[root@k8s-master statefulset]# kubectl get sts
NAME   READY   AGE
web    2/5     86m
[root@k8s-master statefulset]# kubectl get sts
NAME   READY   AGE
web    2/5     86m
[root@k8s-master statefulset]# kubectl get sts
NAME   READY   AGE
web    3/5     86m
[root@k8s-master statefulset]# kubectl get sts
NAME   READY   AGE
web    4/5     87m

[root@k8s-master statefulset]# kubectl describe sts web
Name:               web
Namespace:          default
CreationTimestamp:  Sun, 05 Jan 2025 20:47:07 +0800
Selector:           app=nginx
Labels:             <none>
Annotations:        <none>
Replicas:           5 desired | 5 total
Update Strategy:    RollingUpdate
  Partition:        0
Pods Status:        5 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Volume Claims:    <none>
Events:
  Type    Reason            Age   From                    Message
  ----    ------            ----  ----                    -------
  Normal  SuccessfulCreate  75s   statefulset-controller  create Pod web-2 in StatefulSet web successful
  Normal  SuccessfulCreate  57s   statefulset-controller  create Pod web-3 in StatefulSet web successful
  Normal  SuccessfulCreate  40s   statefulset-controller  create Pod web-4 in StatefulSet web successful

# 缩容之后,再进行查看
[root@k8s-master statefulset]# kubectl scale sts web --replicas=2
statefulset.apps/web scaled
[root@k8s-master statefulset]# kubectl describe sts web
Name:               web
Namespace:          default
CreationTimestamp:  Sun, 05 Jan 2025 20:47:07 +0800
Selector:           app=nginx
Labels:             <none>
Annotations:        <none>
Replicas:           2 desired | 3 total
Update Strategy:    RollingUpdate
  Partition:        0
Pods Status:        3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Volume Claims:    <none>
Events:
  Type    Reason            Age    From                    Message
  ----    ------            ----   ----                    -------
  Normal  SuccessfulCreate  2m12s  statefulset-controller  create Pod web-2 in StatefulSet web successful
  Normal  SuccessfulCreate  114s   statefulset-controller  create Pod web-3 in StatefulSet web successful
  Normal  SuccessfulCreate  97s    statefulset-controller  create Pod web-4 in StatefulSet web successful
  Normal  SuccessfulDelete  3s     statefulset-controller  delete Pod web-4 in StatefulSet web successful
  Normal  SuccessfulDelete  1s     statefulset-controller  delete Pod web-3 in StatefulSet web successful
  Normal  SuccessfulDelete  0s     statefulset-controller  delete Pod web-2 in StatefulSet web successful
(3)镜像更新
# 通过patch的方式进行更新
[root@k8s-master statefulset]# kubectl patch sts web --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value": "nginx:1.9.2"}]'
statefulset.apps/web patched
[root@k8s-master statefulset]# kubectl get sts
NAME   READY   AGE
web    1/2     92m
[root@k8s-master statefulset]# kubectl get sts
NAME   READY   AGE
web    1/2     93m
[root@k8s-master statefulset]# kubectl rollout history sts web 
statefulset.apps/web 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

[root@k8s-master statefulset]# kubectl rollout history sts web --revision=2
statefulset.apps/web with revision #2
Pod Template:
  Labels:       app=nginx
  Containers:
   nginx:
    Image:      nginx:1.9.2
    Port:       80/TCP
    Host Port:  0/TCP
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>
(4)灰度发布
利用滚动更新中的 partition 属性,可以实现简易的灰度发布的效果

例如我们有 5 个 pod,如果当前 partition 设置为 3,那么此时滚动更新时,只会更新那些 序号 >= 3 的 pod

利用该机制,我们可以通过控制 partition 的值,来决定只更新其中一部分 pod,确认没有问题后再主键增大更新的 pod 数量,最终实现全部 pod 更新
(5)删除
# 删除 StatefulSet 和 Headless Service
# 级联删除:删除 statefulset 时会同时删除 pods
kubectl delete statefulset web
# 非级联删除:删除 statefulset 时不会删除 pods,删除 sts 后,pods 就没人管了,此时再删除 pod 不会重建的
kubectl deelte sts web --cascade=false
# 删除 service
kubectl delete service nginx
相关推荐
腾科张老师1 小时前
开源平台Kubernetes的优势是什么?
容器·kubernetes·开源
跳河轻生的鱼2 小时前
海思Linux(一)-Hi3516CV610的开发-ubuntu22_04环境创建
linux·单片机·学习·华为
跳跳的向阳花2 小时前
02、Docker学习,理论知识,第二天:基础概念与常用命令
学习·docker·容器
hoppss2 小时前
根据docker file 编译镜像
运维·docker·容器
qq_429856572 小时前
docker 删除容器和镜像
运维·docker·容器
PyAIGCMaster2 小时前
Docker学习记录:安装nginx
学习·nginx·docker
Rossy Yan2 小时前
入门级容器技术解析:Docker和K8s的区别与关系
docker·容器·kubernetes
Lumos_yuan2 小时前
Lumos学习王佩丰Excel二十四讲系列完结
学习·excel·教程总结
东京老树根3 小时前
Excel 技巧02 - 如何批量输入百分号 (★),如何输入百分号并指定小数位数,如何批量删除百分号,如何批量删除小数最后的0?
笔记·学习·excel·vba
don't_be_bald3 小时前
数据结构与算法-顺序表
c语言·开发语言·数据结构·学习·链表