kubernetes statefulset

StatefulSet

有状态应用

要求

  1. 稳定且唯一的网络标识符
  2. 稳定且持久的存储
  3. 有序 平滑地部署和扩展
  4. 有序 平滑地终止和删除
  5. 有序的滚动更新

三个组件

  • headless service
  • StatefulSet
  • volumeClaimTemplate

pv-demo.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv001
  labels:
    name: pv001
spec:
  nfs:
    path: /data/volumes/v1
    server: 10.0.0.11
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv002
  labels:
    name: pv002
spec:
  nfs:
    path: /data/volumes/v2
    server: 10.0.0.11
  accessModes: ["ReadWriteOnce"]
  capacity:
    storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv003
  labels:
    name: pv003
spec:
  nfs:
    path: /data/volumes/v3
    server: 10.0.0.11
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv004
  labels:
    name: pv004
spec:
  nfs:
    path: /data/volumes/v4
    server: 10.0.0.11
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv005
  labels:
    name: pv005
spec:
  nfs:
    path: /data/volumes/v5
    server: 10.0.0.11
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 5Gi
---

stateful-demo.yaml

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: 2
  selector:
    matchLabels:
      app: myapp-pod
  template:
    metadata:
      labels:
        app: myapp-pod
    spec:
      containers:
      - name: myapp
        image: ikubernetes/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: 5Gi
[root@k8s-master1 volumes]# kubectl get statefulsets.apps 
NAME    READY   AGE
myapp   2/2     5m8s
[root@k8s-master1 volumes]# kubectl get pvc
NAME                STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
myappdata-myapp-0   Bound    pv002    5Gi        RWO                           5m15s
myappdata-myapp-1   Bound    pv001    5Gi        RWO,RWX                       5m13s
[root@k8s-master1 volumes]# kubectl get pv
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                       STORAGECLASS   REASON   AGE
pv001   5Gi        RWO,RWX        Retain           Bound       default/myappdata-myapp-1                           20m
pv002   5Gi        RWO            Retain           Bound       default/myappdata-myapp-0                           20m
pv003   5Gi        RWO,RWX        Retain           Available                                                       20m
pv004   5Gi        RWO,RWX        Retain           Available                                                       20m
pv005   5Gi        RWO,RWX        Retain           Available                                                       20m
[root@k8s-master1 statefulset]# kubectl get pods
NAME      READY   STATUS    RESTARTS   AGE
myapp-0   1/1     Running   0          2m46s
myapp-1   1/1     Running   0          2m44s
[root@k8s-master1 statefulset]# kubectl exec -it myapp-0 -- /bin/sh
/ # nslookup myapp-0.myapp.default.svc.cluster.local
nslookup: can't resolve '(null)': Name does not resolve

Name:      myapp-0.myapp.default.svc.cluster.local
Address 1: 10.254.82.2 myapp-0.myapp.default.svc.cluster.local.

/ # nslookup myapp-1.myapp.default.svc.cluster.local
nslookup: can't resolve '(null)': Name does not resolve

Name:      myapp-1.myapp.default.svc.cluster.local
Address 1: 10.254.44.3 myapp-1.myapp.default.svc.cluster.local
pod_name.service_name.ns_name.svc.cluster.local
pod名称.svc名称.命名空间.svc.cluster.local
myapp-0.myapp.default.svc.cluster.local
myapp-1.myapp.default.svc.cluster.local

伸缩

kubectl scale statefulset myapp --replicas=5
kubectl patch statefulset myapp -p '{"spec":{"replicas":2}}'

更新策略

kubectl patch statefulsets myapp -p '{"spec":{"updateStrategy":{"rollingUpdate":{"partition":4}}}}'

更新

kubectl set image sts/myapp myapp=ikubernetes/myapp:v2

查看

kubectl get pods myapp-4 -o yaml

你可能感兴趣的:(kubernetes,kubernetes,容器,云原生)