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
相关推荐
lichenyang4531 天前
Docker 学习笔记(四):Dockerfile,把项目打成自己的镜像
docker·容器
lichenyang4531 天前
Docker 学习笔记(三):Docker 网络、bridge、子网和容器互通
docker·容器
lichenyang4531 天前
Docker 学习笔记(二):docker run 的参数到底在控制什么?
docker·容器
运维开发故事4 天前
基于 Arthas 的多集群在线诊断系统设计与实现
kubernetes
Patrick_Wilson6 天前
从「改个端口」到 502:Next.js on k8s 的容器端口、Service 映射与 env 覆盖
docker·kubernetes·next.js
探索云原生6 天前
K8s 1.36 这个 GA 特性,把 initContainer 拉模型的 hack 干掉了
ai·云原生·kubernetes
云恒要逆袭6 天前
运行你的第一个Docker容器
后端·docker·容器
Java之美7 天前
一次k8s升级引发的DevicePlugin注册失败
云原生·kubernetes
程序员老赵8 天前
10 分钟部署 OpenCode:Docker 一键安装,浏览器打开就能用 AI 写代码(附完整命令与排错)
docker·容器·ai编程