挂载方式

volume定义需要挂载的内容,跟containers同级,即POD里的所有containers都可以使用。 containers.volumeMounts定义挂载到此容器的内部地址。 subPath表示只挂载volume中的某个子文件。

PV/PVC持久化卷配置
静态只需管理员手动创建即可; 动态需要配置存储类和制备器。

apiVersion: storage.k8s.io/v1
metadata:
name: nfs-storage
namespace: kube-system
annotations:
storageclass.beta.kubernetes.io/is-default-class: 'true'
storageclass.kubernetes.io/is-default-class: 'true'
labels:
environment: test
provisioner: fuseim.pri/ifs # 外部制备器提供者,编写为提供者的名称
reclaimPolicy: Retain # 回收策略,默认为Delete可以配置为Retain
volumeBindingMode: Immediate # 默认为Immediate,表示创建PVC立即进行绑定,只有azuredisk和AWSelasticblockstore支持其他值
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
namespace: kube-system
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-client-provisioner-runner
rules:
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "update"]
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["events"]
verbs: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: run-nfs-client-provisioner
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
namespace: kube-system
roleRef:
kind: ClusterRole
name: nfs-client-provisioner-runner
apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
namespace: kube-system
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
namespace: kube-system
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
namespace: kube-system
roleRef:
kind: Role
name: leader-locking-nfs-client-provisioner
apiGroup: rbac.authorization.k8s.io
apiVersion: apps/v1
metadata:
name: nfs-client-provisioner
labels:
app: nfs-client-provisioner
namespace: kube-system
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: nfs-client-provisioner
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: registry.cn-beijing.aliyuncs.com/mydlq/nfs-subdir-external-provisioner:v4.0.0
volumeMounts:
- name: nfs-client-root
mountPath: persistentvolumes
env:
- name: PROVISIONER_NAME
value: fuseim.pri/ifs # 这里必须要填写storageclass中的PROVISIONER名称信息一致
- name: NFS_SERVER
value: 10.XX.XX.XX # 指定NFS服务器的IP地址
- name: NFS_PATH
value: nfs # 指定NFS服务器中的共享挂载目录
volumes:
- name: nfs-client-root # 定义持久化卷的名称,必须要上面volumeMounts挂载的名称一致
nfs:
server: 10.XX.XX.XX # 指定NFS所在的IP地址
path: nfs # 指定NFS服务器中的共享挂载目录
apiVersion: v1
metadata:
name: nginx-pvc
labels:
environment: test
app: nginx
spec:
storageClassName: nfs-storage # 使用的storageclass
accessModes:
- ReadWriteMany
resources:
requests:
storage: 256Mi

kind: StatefulSet
metadata:
labels:
app: nginx
name: nginx
namespace: default
spec:
replicas: 1
serviceName: nginx
revisionHistoryLimit: 10
selector:
matchLabels:
type: web
template:
metadata:
labels:
type: web
spec:
containers:
- image: nginx:1.20
imagePullPolicy: IfNotPresent
name: nginx
resources:
requests:
cpu: 10m
memory: 128Mi
volumeMounts:
- name: pvc-dong
mountPath: usr/share/nginx/html
terminationGracePeriodSeconds: 30
volumeClaimTemplates:
- metadata:
name: pvc-dong
spec:
storageClassName: nfs-storage
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi

本文作者:刘玉翀(上海新炬中北团队)
本文来源:“IT那活儿”公众号

文章转载自IT那活儿,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




