Kubernetes StatefulSet

TIPS

POD重新调度之后,The Pods’ ordinals, hostnames, SRV records, and A record names have not changed, but the IP addresses associated with the Pods may have changed. In the cluster used for this tutorial, they have. This is why it is important not to configure other applications to connect to Pods in a StatefulSet by IP address.

查找和调用一个StatefulSet中的活动的Pod的正确姿势是,找到Headless Service的CNAME,例如nginx.default.svc.cluster.local,跟此CNAME关联的SRV 将只包含此StatefulSet中状态为Running and Ready的pod;

Pod的SRV,比如web-0.nginx.default.svc.cluster.local, web-1.nginx.default.svc.cluster.local,在K8s集群中是稳定的,其他的应用使用一个Pod的SRV调用其服务,是非常稳定的;当Pod重新调度时,Pod的IP可能会发生变化,但是SRV不变,发起调用的应用能够感知到StatefulSet中所有Pod地址的变化;

StatefulSet会用volumeCalimTemplate中的定义为每个Pod副本创建一个PVC实例;每个PVC的名称由StatefulSet定义中volumeCalimTemplate的名称和Pod副本的名字组合而成(中划线连接);

Pod所使用的Volume,在Pod重新调度之后,会接着用,数据不会丢失;

Cluster Domain Headless Service (ns/name) StatefulSet (ns/name) StatefulSet Domain Pod DNS Pod Hostname
cluster.local default/nginx default/web nginx.default.svc.cluster.local web-{0..N-1}.nginx.default.svc.cluster.local web-{0..N-1}
cluster.local foo/nginx foo/web nginx.foo.svc.cluster.local web-{0..N-1}.nginx.foo.svc.cluster.local web-{0..N-1}
kube.local foo/nginx foo/web nginx.foo.svc.kube.local web-{0..N-1}.nginx.foo.svc.kube.local web-{0..N-1}

在阿里云的Kubernetes上运行StatefulSet测试样例

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
      annotations: 
        volume.beta.kubernetes.io/storage-class: alicloud-disk-efficiency
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 20Gi

Refrence

名词解释:StatefulSet
Kubernetes服务之StatefulSets简介
StatefulSet: Kubernetes 中对有状态应用的运行和伸缩
浅谈statefulset volume pvc pv之间的关系
Running ZooKeeper, A CP Distributed System

你可能感兴趣的:(Kubernetes)