kubernetes之StatefulSet

本文详细介绍了Kubernetes中的StatefulSet特性,包括其历史背景、关键功能及其实现原理。StatefulSet旨在为有状态的应用提供支持,确保每个实例都有稳定的网络标识和持久化的存储。文章还深入探讨了StatefulSet与Persistent Volume (PV)、Persistent Volume Claim (PVC)之间的关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

StatefulSet

k8s的statefulset在1.5之后才引入的,1.5之前用的是petset,关于petset在之前的老版本的paas开发中用的就是petset,petset和statefulset都是为啦解决容器的有状态服务。
statefulset有状态应用副本集
PetSet -> StatefulSet
1.文档且唯一的网络标识符
2.稳定且持久的存储
3.有序,平滑的部署和扩展
4.有序,平滑的删除和终止
5.有序的滚动更新
三个组件:
headless service
StatefulSet
volumeClaimTemplate

statefulset、volume、pvc、pv之间的关系

volume

volume是pod中用来挂在文件到容器中用的,支持多种的volume类型挂在,其中包括hostpath,emptydir,ceph-rbd等,,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: v1
kind: PersistentVolume
metadata:
  name: datadir-aaaaa-0
spec:
  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/v1beta1
kind: StatefulSet
metadata:
labels:
app: demo
name: aaaaaaaa
spec:
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: v1
kind: PersistentVolume
metadata:
name: datadir-aaaaa-0
spec:
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: v1
kind: PersistentVolumeClaim
metadata:
name: datadir-aaaaa-0
spec:
accessModes:

  • ReadWriteOnce
    resources:
    requests:
    storage: 512Mi
    statefulset

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
labels:
app: demo
name: aaaaaaaa
spec:
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模式的 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值