参见 https://blog.csdn.net/qq_33448670/article/details/79972401
https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: auto-nfs-storage
annotations:
storageclass.kubernetes.io/is-default-class: "true"
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner
parameters:
archiveOnDelete: "true" ## 删除pv的时候,pv的内容是否要备份
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-client-provisioner
labels:
app: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
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: k8s.gcr.io/sig-storage/nfs-subdir-external-provisioner:v4.0.2
image: registry.cn-hangzhou.aliyuncs.com/image-storage/nfs-subdir-external-provisioner:v4.0.2
# resources:
# limits:
# cpu: 10m
# requests:
# cpu: 10m
volumeMounts:
- name: nfs-client-root
mountPath: /data/nfs/auto
env:
- name: PROVISIONER_NAME
value: k8s-sigs.io/nfs-subdir-external-provisioner
- name: NFS_SERVER
value: 192.168.155.130 ## 指定自己nfs服务器地址
- name: NFS_PATH
value: /data/nfs/auto ## nfs服务器共享的目录
volumes:
- name: nfs-client-root
nfs:
server: 192.168.155.130 #注意这里的地址
path: /data/nfs/auto
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-client-provisioner-runner
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get", "list", "watch"]
- 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
# replace with namespace where provisioner is deployed
namespace: default
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
# replace with namespace where provisioner is deployed
namespace: default
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
# replace with namespace where provisioner is deployed
namespace: default
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
roleRef:
kind: Role
name: leader-locking-nfs-client-provisioner
apiGroup: rbac.authorization.k8s.io
---
---
# https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql003
namespace: default
labels:
app: mysql003
spec:
selector:
matchLabels:
app: mysql003 # has to match .spec.template.metadata.labels
serviceName: "mysql003"
replicas: 1 # by default is 1
template:
metadata:
labels:
app: mysql003 # has to match .spec.selector.matchLabels
spec:
terminationGracePeriodSeconds: 10
containers:
- name: mysql003
image: mysql/mysql-server:8.0
resources:
requests:
cpu: 500m
memory: 1024Mi
limits:
cpu: 10000m
memory: 1024Mi
env:
- name: ACCEPT_EULA
value: "Y"
- name: MYSQL_ROOT_PASSWORD
value: mysql678123
- name: MYSQL_ROOT_USER
value: root
- name: MYSQL_SQL_MODE
value: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
- name: MYSQL_DATABASE
value: db001
- name: MYSQL_AUTHENTICATION_PLUGIN
value: mysql_native_password
- name: MYSQL_DATABASE
value: db001
- name: DATADIR
value: /var/lib/mysql/data
- name: MYSQL_ROOT_HOST
value: '%'
ports:
- containerPort: 3306
name: mysql003
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
volumeClaimTemplates:
- metadata:
name: mysql-data
spec:
storageClassName: auto-nfs-storage
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi