Kubernetes部署MySQL数据持久化

一、安装配置NFS服务端

1、安装nfs-kernel-server:

sudo apt -y install nfs-kernel-server

2、服务端创建共享目录

# 列出所有可用块设备的信息
lsblk

# 格式化磁盘
sudo mkfs -t ext4  /dev/sdb

# 创建一个目录:
sudo mkdir -p /data/nfs/mysql

# 更改目录权限:
sudo chown -R nobody:nogroup /data/nfs/mysql

# 挂载磁盘
sudo mount /dev/sdb /data/nfs/mysql

# 设置开机挂载
sudo grep -E "^\s*\/dev\/sdb" /etc/fstab || sudo sed -i '$a\/dev\/sdb \/data\/nfs\/mysql ext4 defaults 0 0' /etc/fstab

3、配置NFS:

# IP不限
sudo grep '\/data\/nfs\/mysql \*(rw,sync,no_root_squash,insecure)' /etc/exports || sudo sed -i -r '$a\/data\/nfs\/mysql \*(rw,sync,no_root_squash,insecure)' /etc/exports 

# IP限制
sudo grep '\/data\/nfs\/mysql 192.168.100.*(rw,sync,no_root_squash,no_subtree_check)' /etc/exports || sudo sed -i -r '$a\/data\/nfs\/mysql 192.168.100.*(rw,sync,no_root_squash,no_subtree_check)' /etc/exports 

4、重启nfs-kernel-server:

sudo systemctl enable nfs-kernel-server
sudo systemctl restart nfs-kernel-server

# 测试
showmount -e

二、安装配置NFS客户端

1、安装 nfs-common:

sudo apt -y install nfs-common

2、创建挂载点:

sudo mkdir -p /data/nfs/mysql
# 更改目录权限:
sudo chown -R nobody:nogroup /data/nfs/mysql

3、挂载远程共享目录:

sudo mount 192.168.100.100:/data/nfs/mysql /data/nfs/mysql

4、查看挂载点:

df -h

5、开机自动挂载NFS共享目录

sudo grep -E '^\s*\/data\/nfs\/mysql' /etc/fstab || sudo sed -i '$a192.168.100.100:\/data\/nfs\/mysql \/data\/nfs\/mysql nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0' /etc/fstab

三、部署MySQL数据库

1、创建资源:

cat > mysql-k8s.yaml << END
---
# 端口3306

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None
---
# 创建一个deployment部署

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:5.6
        name: mysql
        env:
          # Use secret in real usage
        - name: MYSQL_ROOT_PASSWORD
          value: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim
---
# 创建持久卷

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/nfs/mysql"
---
# 创建持久卷声明

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
END

2、执行部署

kubectl apply -f mysql-k8s.yaml

3、查看容器MySQL状态

kubectl get pods

eg:
NAME                     READY   STATUS    RESTARTS   AGE
mysql-748477c455-j6vsk   1/1     Running   0          24m

4、测试MySQL数据库

kubectl  exec  -it $(kubectl get pods | grep mysql | awk -F " " '{print $1}') -- mysql -h mysql -ppassword

mysql -h 192.168.100.100 -uroot -P 30306 -ppassword

你可能感兴趣的:(Kubernetes,MySQL数据库,kubernetes,mysql,容器)