kubernetes之StatefulSet

来源:互联网 发布:海安东华软件招聘 编辑:程序博客网 时间:2024/06/09 16:26

StatefulSet

k8s的statefulset相信很多人都用过,在1.5之后才引入的,1.5之前用的是petset,关于petset在之前的老版本的paas开发中用的就是petset,很多不足的地方,等会儿会说到这点。petset也好,statefulset也好,都是为啦解决容器的有状态服务。下面就谈谈在用statefulset的时候的一些小疑惑和收获。

浅谈statefulset volume pvc pv之间的关系

很多人都知道 volume、pvc(persistentVolumeClaim)、pv(persistentVolume)、statefulset,但是至于他们之间的关系应该有所了解,但是可能不是特别深入的了解。所以我想简单的说说他们之间的关系。

volume

volume是pod中用来挂在文件到容器中用的,支持多种的volume类型挂在,其中包括hostpath,emptydir,ceph-rbd等,想要了解全部的挂载类型请阅读官网(https://v1-7.docs.kubernetes.io/docs/concepts/storage/volumes/),volume可以说是一个存储挂载的桥梁啦,可以通过volume关联中的persistentVolumeClaim关联pvc,然后用pvc关联的pv的存储

pv(persistentVolume)

pv是用来向真正的存储服务器申请真正的存储资源的一个object,至于这个存储资源谁来用,那就是接下来说的pvc的职责所在。

pvc(persistentVolumeClaim)

pvc就是用来关联pv和pod的一个桥梁,当你创建啦pv的说话,如何使用它,就需要pvc来关联,两种方式:1. 在你容器中的volumeMount中指定对应的pvc的名字。2. 可以通过pod中volume关联中的persistentVolumeClaim关联pvc

statefulset

statefulset就是对应的存储的真正消费者,关联pv的方式用pvc,在你容器中的volumeMount中指定对应的pvc的名字或者可以通过pod中volume关联中的persistentVolumeClaim关联pvc

实际使用

方式1:
不通过volume的方式创建:

pv

piVersion: v1kind: PersistentVolumemetadata:  name: datadir-aaaaa-0spec:  accessModes:  - ReadWriteOnce  capacity:    storage: 512Mi  persistentVolumeReclaimPolicy: Retain  rbd:    fsType: ext4    image: xxxxxxx    keyring: /etc/ceph/ceph.client.admin.keyring    monitors:    - x.x.x.x:6789    - x.x.x.x:6789    - x.x.x.x:6789    pool: admin-pool    user: admin

statefulset

apiVersion: apps/v1beta1kind: StatefulSetmetadata:  labels:    app: demo  name: aaaaaaaaspec:  replicas: 1  selector:    matchLabels:      app: demo  serviceName: aaaaaaaa  template:    metadata:      labels:        app: demo     spec:      containers:        image: etcd:test        imagePullPolicy: IfNotPresent        name: aaaaaaaa1        resources:          limits:            memory: 256Mi          requests:            memory: 256Mi        terminationMessagePath: /dev/termination-log        volumeMounts:        - mountPath: /etcd/data          name: datadir        - mountPath: /etc          name: aaaaaaaa-yaowei        resources:          limits:            cpu: 100m            memory: 512Mi          requests:            cpu: 100m            memory: 512Mi      restartPolicy: Always  volumeClaimTemplates:  - metadata:      creationTimestamp: null      name: datadir   //与  volumeMounts:中一个mount的name一致    spec:      accessModes:      - ReadWriteOnce      resources:        requests:          storage: 512Mi

使用volume方式:

pv

piVersion: v1kind: PersistentVolumemetadata:  name: datadir-aaaaa-0spec:  accessModes:  - ReadWriteOnce  capacity:    storage: 512Mi  persistentVolumeReclaimPolicy: Retain  rbd:    fsType: ext4    image: xxxxxxx    keyring: /etc/ceph/ceph.client.admin.keyring    monitors:    - x.x.x.x:6789    - x.x.x.x:6789    - x.x.x.x:6789    pool: admin-pool    user: admin

pvc

apiVersion: v1kind: PersistentVolumeClaimmetadata:  name: datadir-aaaaa-0spec:  accessModes:  - ReadWriteOnce  resources:    requests:      storage: 512Mi

statefulset

apiVersion: apps/v1beta1kind: StatefulSetmetadata:  labels:    app: demo  name: aaaaaaaaspec:  replicas: 1  selector:    matchLabels:      app: demo  serviceName: aaaaaaaa  template:    metadata:      labels:        app: demo     spec:      containers:        image: etcd:test        imagePullPolicy: IfNotPresent        name: aaaaaaaa1        resources:          limits:            memory: 256Mi          requests:            memory: 256Mi        terminationMessagePath: /dev/termination-log        volumeMounts:        - mountPath: /etcd/data          name: datadir        - mountPath: /etc          name: aaaaaaaa-yaowei        resources:          limits:            cpu: 100m            memory: 512Mi          requests:            cpu: 100m            memory: 512Mi      restartPolicy: Always      volumes:        - name: datadir        persistentVolumeClaim:            claimName: datadir-aaaaa-0

说明

为什么方式一中不用手动的创建pvc呢?因为statefulset的controller在创建statefulset的时候,如果他的spec中有volumeClaimTemplates存在 则会自动的创建对应的pvc,所以不用手动的创建(petset源码中也是一样的处理),而在方式二中我们是以volume的形式去引用的pvc,statefulset的controller在创建statefulset的时候不会去创建对应的pvc,如果发现对应的pvc不存在,则statefulset创建会失败,所以必须手动的创建对应的pvc

一般使用规则

一般在使用statefulset的时候,会使用到pvc pv volume等,之前的方式都是手动的或者调用api去创建对应的pv等,pv和pvc的生命周期有系统管理员来手动的管理,浪费很多时间,而且还经常出错等,一般推介的方式是结合storageclass动态的创建pv,如果在结合方式一,那基本上是pvc,pv的创建都是有k8s自身来完成(前提是得在集群中提前创建对应的storagclass),用户只要在删除对应的pvc,k8s就能动态的删除对应的pv,而且不会造成pv的申请的存储大小的浪费,一个pvc关联一个pv,pvc申请多大存储的资源,就创建多大存储资源的pv,这样特别的方法,并且动态创建的pv默认是 recycle模式的 。

原创粉丝点击