持久化数据库存储

目的

配置 NFS 共享为 OpenShift 节点提供存储,并且配置 OpenShift 持久卷以绑定至数据库 Pod。

环境

  • openshift v3.11.16/kubernetes v1.11.0

步骤

配置 NFS 共享持久卷

1. 登录到 NFS 服务器
# ssh nfs.example.com
2. 创建 config-nfs.sh 脚本,内容如下
#!/usr/bin/sh

export_dir="/var/export/dbvol"
export_file="/etc/exports.d/dbvol.exports"

if [ -d ${export_dir} ]; then
  echo "Export directory ${export_dir} already exist."
else
  mkdir -p ${export_dir}
  chown nfsnobody:nfsnobody ${export_dir}
  chmod 700 ${export_dir}
  echo "Export directory ${export_dir} created."
fi

if [ -d ${export_file} ]; then
  echo "Export file ${export_file} already exist."
else
  echo "${export_dir} *(rw,async,all_squash)" > ${export_file}
  exportfs -a
fi
3. 运行 config-nfs.sh 创建 NFS 共享卷
# ./config-nfs.sh
4. 验证
# showmount -e
Export list for nfs.example.com:
...
/var/export/dbvol       *

验证 NFS 服务器上的共享卷

1. node1 上验证
# ssh node1.example.com
# mount -t nfs nfs.example.com:/var/export/dbvol /mnt/

# ls -la /mnt/
total 0
drwx------.  2 nfsnobody nfsnobody   6 Nov 30 17:38 .
dr-xr-xr-x. 17 root      root      224 Sep 29 11:53 ..

# mount | grep /mnt
nfs.example.com:/var/export/dbvol on /mnt type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=10.66.208.103,local_lock=none,addr=10.66.208.105)

# umount /mnt/
2. node2 上验证
# ssh node2.example.com
# mount -t nfs nfs.example.com:/var/export/dbvol /mnt/

# ls -la /mnt
total 0
drwx------.  2 nfsnobody nfsnobody   6 Nov 30 17:38 .
dr-xr-xr-x. 17 root      root      224 Sep 29 11:53 ..

# mount | grep /mnt
nfs.example.com:/var/export/dbvol on /mnt type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=10.66.208.104,local_lock=none,addr=10.66.208.105)

# umount /mnt/

创建工程

1. CLI 登录到 OCP
$ oc login https://master.example.com:8443 -u admin -p admin
2. 创建工程
$ oc new-project lab06

创建 PV

1. 创建 mysqldb-volume.yml,内容如下
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysqldb-volume
spec:
  capacity:
    storage: 3Gi
  accessModes:
  - ReadWriteMany
  nfs:
    path: /var/export/dbvol
    server: nfs.example.com
2. 执行 mysqldb-volume.yml,创建 PV
# oc create -f mysqldb-volume.yml

创建 PVC

1. 创建 mysqldb-claim.yml,内容如下
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysqldb-test-claim
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 3Gi
2. 执行 mysqldb-claim.yml,创建 PVC
# oc create -f mysqldb-claim.yml
3. 查看创建的 PVC
# oc get pvc
NAME                 STATUS    VOLUME           CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysqldb-test-claim   Bound     mysqldb-volume   3Gi        RWX                           7m

创建 mysql 应用

1. 创建 mysql.yaml, 内容如下
apiVersion: v1
kind: DeploymentConfig
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    app: mysql
  template:
    metadata:
      name: mysql
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: registry.example.com/rhscl/mysql-57-rhel7:latest
        env:
          - name: MYSQL_ROOT_PASSWORD
            value: redhat
          - name: MYSQL_USER
            value: test_user
          - name: MYSQL_PASSWORD
            value: test_pass
          - name: MYSQL_DATABASE
            value: test_db
        ports:
          - containerPort: 3306
            name: mysql
        volumeMounts:
              - name: 'mysql-persistent-storage'
                mountPath: /var/lib/mysql/data
      volumes:
        - name: 'mysql-persistent-storage'
          persistentVolumeClaim:
            claimName: 'mysqldb-test-claim'
strategy:
  type: Rolling
2. 创建 mysql 应用
# oc create -f mysql.yaml
3. 查看运行的 mysql 容器
# oc get pods
NAME                     READY     STATUS       RESTARTS   AGE
mysql-1-flkrs            1/1       Running      0          8m
4. 验证 MySQL 服务器已在导出的文件夹中初始化了数据库
# ssh nfs.example.com 'ls -la /var/export/dbvol/'
total 41040
drwx------. 6 nfsnobody nfsnobody     4096 Dec  1 21:52 .
drwxrwxrwx. 4 nfsnobody nfsnobody       30 Nov 30 17:38 ..
-rw-r-----. 1 nfsnobody nfsnobody       56 Dec  1 21:51 auto.cnf
-rw-------. 1 nfsnobody nfsnobody     1680 Dec  1 21:51 ca-key.pem
-rw-r--r--. 1 nfsnobody nfsnobody     1112 Dec  1 21:51 ca.pem
-rw-r--r--. 1 nfsnobody nfsnobody     1112 Dec  1 21:51 client-cert.pem
-rw-------. 1 nfsnobody nfsnobody     1680 Dec  1 21:51 client-key.pem
-rw-r-----. 1 nfsnobody nfsnobody      673 Dec  1 21:52 ib_buffer_pool
-rw-r-----. 1 nfsnobody nfsnobody 12582912 Dec  1 21:52 ibdata1
-rw-r-----. 1 nfsnobody nfsnobody  8388608 Dec  1 21:52 ib_logfile0
-rw-r-----. 1 nfsnobody nfsnobody  8388608 Dec  1 21:51 ib_logfile1
-rw-r-----. 1 nfsnobody nfsnobody 12582912 Dec  1 21:52 ibtmp1
drwxr-x---. 2 nfsnobody nfsnobody     4096 Dec  1 21:52 mysql
-rw-r-----. 1 nfsnobody nfsnobody        2 Dec  1 21:52 mysql-1-flkrs.pid
-rw-r--r--. 1 nfsnobody nfsnobody        6 Dec  1 21:52 mysql_upgrade_info
drwxr-x---. 2 nfsnobody nfsnobody     8192 Dec  1 21:52 performance_schema
-rw-------. 1 nfsnobody nfsnobody     1680 Dec  1 21:51 private_key.pem
-rw-r--r--. 1 nfsnobody nfsnobody      452 Dec  1 21:51 public_key.pem
-rw-r--r--. 1 nfsnobody nfsnobody     1112 Dec  1 21:51 server-cert.pem
-rw-------. 1 nfsnobody nfsnobody     1680 Dec  1 21:51 server-key.pem
drwxr-x---. 2 nfsnobody nfsnobody     8192 Dec  1 21:52 sys
drwxr-x---. 2 nfsnobody nfsnobody       20 Dec  1 21:52 test_db

创建测试数据并验证

1. 远程进入容器
# oc rsh mysql-1-flkrs
2. 进入数据库终端
sh-4.2$ mysql -utest_user -ptest_pass test_db
3. 创建表添加数据
mysql> create table quote(id integer, msg varchar(250));
Query OK, 0 rows affected (0.06 sec)

mysql> insert into quote values (1, 'TEST1'), (2, 'TEST2');
4. 验证 MySQL 服务器在 test_db 目录中创建了表元数据
# ssh nfs.example.com 'ls -la /var/export/dbvol/test_db'
total 212
drwxr-x---. 2 nfsnobody nfsnobody    54 Dec  1 22:06 .
drwx------. 6 nfsnobody nfsnobody  4096 Dec  1 21:52 ..
-rw-r-----. 1 nfsnobody nfsnobody    65 Dec  1 21:52 db.opt
-rw-r-----. 1 nfsnobody nfsnobody  8584 Dec  1 22:06 quote.frm
-rw-r-----. 1 nfsnobody nfsnobody 98304 Dec  1 22:07 quote.ibd

results matching ""

    No results matching ""