K8S简历(十二)

前面所有学的Pod控制器都是只能管理无状态的Pod如果你想要在K8S上运行Mysql等有状态的服务你就必须要用到statefulSet控制器。

statefulSet的特点:
  1. 稳定且唯一的网络标识符(有序的Pod名称);
  2. 稳定且持久的存储(每个Pod独享一个PV);
  3. 有序,平滑地部署和扩张;
  4. 有序,平滑地终止和删除;
  5. 有序的滚动更新(根据Pod名称降序滚动更新)。

实验流程:

创建PV:

如果你的存储系统支持动态生成PV可省略此步。
本实验使用昨天创建好的nfs(十一有创建nfs步骤)。

apiVersion: v1
kind: PersistenVolume
metadata:
  name: nginx-v1
  labels:
    name: nginx-v1
spec:
  nfs:
    path: /data/pv1
    server: wl.com
    accessModes: ["ReadWriteMant","ReadWriteOnce"]
    capacity:
      storage: 5Gi
---
apiVersion: v1
kind: PersistenVolume
metadata:
  name: nginx-v2
  labels:
    name: nginx-v2
spec:
  nfs:
    path: /data/pv2
    server: wl.com
    accessModes: ["ReadWriteMant","ReadWriteOnce"]
    capacity:
      storage: 5Gi
---
apiVersion: v1
kind: PersistenVolume
metadata:
  name: nginx-v3
  labels:
    name: nginx-v3
spec:
  nfs:
    path: /data/pv3
    server: wl.com
    accessModes: ["ReadWriteMant","ReadWriteOnce"]
    capacity:
      storage: 5Gi
---
apiVersion: v1
kind: PersistenVolume
metadata:
  name: nginx-v4
  labels:
    name: nginx-v4
spec:
  nfs:
    path: /data/pv4
    server: wl.com
    accessModes: ["ReadWriteMant","ReadWriteOnce"]
    capacity:
      storage: 5Gi

创建service:

一定要创建无头service。

apiVersion: v1
kind: service
metadata:
  name: www-svc
  namespace: default
spec:
  selector:
    app: nginx
  clusterIP: None
  ports:
    - port: 80
      name: http

创建statefulSet:

无需创建PVC,创建statefulSet会自动创建PVC

apiVersion: apps/v1
kind: statefulSet
metadata:
  name: www-sts
  namespace: default
spec:
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: wl/nginx:v1.12
          ports:
          - containerPort: 80
            name: web
      volumeMounts:
        - name: nginx-volume
          mountPath: /data/www
    volumeclaimTemplates:
      - metadata:
          name: nginx-volume
       spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 5Gi

实现金丝雀更新:

kubectl patch sts www-sts -p '{"spec":{"updateStrategy":{"rollingUpdate":{"partition":4}}}}'
#打补丁把更新区域设置到区域4也是最后一个Pod的位置(默认是第一个Pod位置)
kubectl set image sts nginx-sts nginx=wl/nginx:v1.13
#现在只更新了最后一个Pod其他Pod都是老版本
kubectl patch sts www-sts -p '{"spec":{"updateStrategy":{"rollingUpdate":{"partition":0}}}}'
#发现新版本没有问题就可以滚动更新全部Pod

你可能感兴趣的:(K8S简历(十二))