10. kubernetes statefulset控制器

10. kubernetes statefulset控制器

[TOC]

本文基于马哥的docker和k8s视频总结, 在此致谢马哥.

管理有状态应用, 非常复杂.

PetSet -> StatefulSet

  1. 稳定且唯一的网络标识符
  2. 稳定且持久的存储
  3. 有序, 平滑的部署和扩展
  4. 有序, 平滑地终止和删除
  5. 有序的滚动更新 (逆序, 先更新从节点, 再更新主节点)

三个组件: headless serviceStatefulSetvolumeClaimTemplate

volumeClaimTemplate作用:

  1. 为每一个pod定义volume
  2. 在pod所在名称空间自动创建pvc
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: nginx/myapp:v1
                ports:
                - containerPort: 80
                    name: web
                volumeMounts:
                - name: myappdata
                    mountPath: /usr/share/nginx/html
    volumeClaimTemplates:
    - metadata:
            name: myappdata
        spec:
            accessModes: ["ReadWriteOnce"]
            resources:
                requests:
                    storage: 2Gi

要解析pod时, pod必须跟上无头服务的名称: pod_name.service_name.ns_name.svc.cluster.local

例如: myapp-0.myapp.default.svc.cluster.local

支持动态扩展和缩减, 金丝雀发布:

kubectl scale sts myapp --replicas=5
# 或
kubectl patch sts myapp -p '{"spec":{"replicas":"5"}}'

kubectl scale sts myapp --replicas=2
# 或
kubectl patch sts myapp -p '{"spec":{"replicas":"2"}}'

# pod计数从0开始(即pod-0, pod-1, ...), 设定分区为4时, pod-N, N>=4的那些pod会被更新
kubectl patch sts myapp -p '{"spec":{"updataStrategy":{"rollingUpdate":{"partition":4}}}}'
kubectl set image sts/myapp myapp=nginx/myapp:v2 # 更新镜像
# 然后测试, 如果通过验证则将partition的值改为0, 让所有pod都更新
kubectl patch sts myapp -p '{"spec":{"updataStrategy":{"rollingUpdate":{"partition":0}}}}'

你可能感兴趣的:(10. kubernetes statefulset控制器)