实战:部署 mysql 服务
回顾加深 pv、pvc、configmap
- 删除所有 deployment、pv、pvc、configmap、StorageClass
- 创建一个 nsf 挂载目录给 mysql
mkdir -p /nfs/data/mysql
- 创建 yaml 文件
mysql-server.yaml
# 创建pv
apiVersion: v1
kind: PersistentVolume
metadata: name: mysql-pv
spec:capacity:storage: 3GiaccessModes:- ReadWriteManystorageClassName: nfsnfs:path: /nfs/data/mysqlserver: 192.168.0.1
---
# 创建pvc
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: mysql-pvc
spec:accessModes:- ReadWriteManyresources:requests:storage: 1GistorageClassName: nfs
---
# 创建 configmap
apiVersion: v1
kind: ConfigMap
metadata:name: mysql-config
data:mysql.cnf: |[mysqld]port=3306character-set-server=utf8mb4
---
# 创建一个mysql服务
apiVersion: v1
kind: Pod
metadata:name: mysql-pod
spec:containers:- name: mysqlimage: mysql:5.7env:- name: MYSQL_ROOT_PASSWORDvalue: "123456"ports:- containerPort: 3306volumeMounts:- mountPath: /var/lib/mysqlname: data-volume- mountPath: /etc/mysql/conf.dname: conf-volumevolumes:- name: conf-volumeconfigMap:name: mysql-config- name: data-volumepersistentVolumeClaim:claimName: mysql-pvc
- 执行 yaml 文件
[root@k8s-master k8s]# vi mysql-server.yaml
[root@k8s-master k8s]# kubectl apply -f mysql-server.yaml
persistentvolume/mysql-pv created
persistentvolumeclaim/mysql-pvc created
configmap/mysql-config created
pod/mysql-pod created
- 启动成功后,查看
/nfs/data/mysql
下的文件
[root@k8s-master k8s]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-pod 1/1 Running 0 4s
[root@k8s-master k8s]# cd /nfs/data/mysql/
[root@k8s-master mysql]# ll
total 188476
-rw-r----- 1 polkitd input 56 Mar 26 22:01 auto.cnf
-rw------- 1 polkitd input 1680 Mar 26 22:01 ca-key.pem
-rw-r--r-- 1 polkitd input 1112 Mar 26 22:01 ca.pem
-rw-r--r-- 1 polkitd input 1112 Mar 26 22:01 client-cert.pem
-rw------- 1 polkitd input 1680 Mar 26 22:01 client-key.pem
-rw-r----- 1 polkitd input 1352 Mar 26 22:01 ib_buffer_pool
-rw-r----- 1 polkitd input 79691776 Mar 26 22:01 ibdata1
-rw-r----- 1 polkitd input 50331648 Mar 26 22:01 ib_logfile0
-rw-r----- 1 polkitd input 50331648 Mar 26 22:01 ib_logfile1
-rw-r----- 1 polkitd input 12582912 Mar 26 22:02 ibtmp1
drwxr-x--- 2 polkitd input 4096 Mar 26 22:01 mysql
drwxr-x--- 2 polkitd input 4096 Mar 26 22:01 performance_schema
-rw------- 1 polkitd input 1680 Mar 26 22:01 private_key.pem
-rw-r--r-- 1 polkitd input 452 Mar 26 22:01 public_key.pem
-rw-r--r-- 1 polkitd input 1112 Mar 26 22:01 server-cert.pem
-rw------- 1 polkitd input 1676 Mar 26 22:01 server-key.pem
drwxr-x--- 2 polkitd input 12288 Mar 26 22:01 sys
发现 nfs 挂载是没有问题的
- 进入容器内部,查看 configmap 挂载有没有问题
[root@k8s-master mysql]# kubectl exec -it mysql-pod /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@mysql-pod:/# cd /etc/mysql/conf.d/
root@mysql-pod:/etc/mysql/conf.d# ls
mysql.cnf
root@mysql-pod:/etc/mysql/conf.d# cat mysql.cnf
[mysqld]
port=3306
character-set-server=utf8mb4
configmap 挂载有没有问题