- 创建pvc,这里使用storageclasses动态存储
kubectl get storageclasses --all-namespaces
- 创建mysql-pvc.yaml
cd /usr/local/src/mysql
vim mysql-pvc.yaml
nfs-provisioner为storageclasses名称
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
storageClassName: nfs-provisioner
应用:
kubectl apply -f mysql-pvc.yaml
-
创建好的pvc状态为Bound:
kubectl get pvc
4、创建mysql-config配置文件
cd /usr/local/src/mysql
vim mysql-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
data:
my.cnf: |
[mysqld]
log-bin
#default_authentication_plugin=mysql_native_password
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
default_time_zone='+08:00'
max_connections =1000
lowercase-table-names=1
应用:
kubectl apply -f mysql-config.yaml
查看状态:
kubectl get configmaps
5、创建mysql
cd /usr/local/src/mysql
vim mysql.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:8.0
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: "handhand123"
volumeMounts:
- name: mysql-config-volume
mountPath: /etc/mysql/conf.d
- name: mysql-storage
mountPath: /var/lib/mysql
ports:
- containerPort: 3306
name: mysql
volumes:
- name: mysql-config-volume
configMap:
name: mysql-config
items:
- key: my.cnf
path: my.cnf
- name: mysql-storage
persistentVolumeClaim:
claimName: mysql-pvc
应用:
kubectl apply -f mysql.yaml
查看状态:
kubectl get po
进入容器:
kubectl exec -it mysql-675bd5859f-vlghc /bin/bash
mysql -uroot -p 密码默认handhand123
查看storageclass挂载目录:
-
查看storageclass详情
kubectl describe storageclass nfs-provisioner
-
关联的po
kubectl edit po nfs-client-provisioner-5559df45fb-5kgcm -n kube-system
-
查看对应服务器的nfs路径下的文件即可