实战:部署 mysql 服务

回顾加深 pv、pvc、configmap

k8s入门到实战(七)—— 回顾:使用yaml文件配置pv、pvc、configmap部署mysql服务-LMLPHP

  1. 删除所有 deployment、pv、pvc、configmap、StorageClass
  2. 创建一个 nsf 挂载目录给 mysql
mkdir -p /nfs/data/mysql
  1. 创建 yaml 文件mysql-server.yaml
# 创建pv
apiVersion: v1
kind: PersistentVolume
metadata: 
  name: mysql-pv
spec:
  capacity:
    storage: 3Gi
  accessModes:
    - ReadWriteMany
  storageClassName: nfs
  nfs:
    path: /nfs/data/mysql
    server: 192.168.0.1
---
# 创建pvc
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  storageClassName: nfs
---
# 创建 configmap
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
data:
  mysql.cnf: |
    [mysqld]
    port=3306
    character-set-server=utf8mb4
---
# 创建一个mysql服务
apiVersion: v1
kind: Pod
metadata:
  name: mysql-pod
spec:
  containers:
    - name: mysql
      image: mysql:5.7
      env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
      ports:
        - containerPort: 3306
      volumeMounts:
        - mountPath: /var/lib/mysql
          name: data-volume
        - mountPath: /etc/mysql/conf.d
          name: conf-volume
  volumes:
    - name: conf-volume
      configMap:
        name: mysql-config
    - name: data-volume
      persistentVolumeClaim:
        claimName: mysql-pvc
  1. 执行 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
  1. 启动成功后,查看 /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 挂载是没有问题的

  1. 进入容器内部,查看 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 挂载有没有问题

03-28 11:54