1.环境配置:1.21.1容器集群,NFS存储环境
# 各个主从节点新建/nfs/data目录
mkdir -p /nfs/data/mysql
chmod 777 /nfs/data/mysql
2.创建mysql-nfs.yaml文件
vim mysql-nfs.yaml
修改nfs存储路径
apiVersion: v1
kind: Namespace
metadata:
labels:
name: nacos
---
apiVersion: v1
kind: ReplicationController
metadata:
namespace: nacos
name: mysql
labels:
name: mysql
spec:
replicas: 1
selector:
name: mysql
template:
metadata:
labels:
name: mysql
spec:
containers:
- name: mysql
image: nacos/nacos-mysql:5.7
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: "123123" #root 账号密码
- name: MYSQL_DATABASE
value: "nacos_devtest"
- name: MYSQL_USER
value: "nacos"
- name: MYSQL_PASSWORD
value: "123123"
volumes:
- name: mysql-data
nfs:
server: 192.168.112.194 # nfs 服务
path: /nfs/data/mysql # /nfs/data/ 存储路径 mysql # 存储数据目录,要在NFS上面新建
---
apiVersion: v1
kind: Service
metadata:
namespace: nacos
name: mysql
labels:
name: mysql
spec:
ports:
- port: 3306
targetPort: 3306
selector:
name: mysql
执行mysql-nfs.yaml
kubectl apply -f mysql-nfs.yaml
#查看mysql是否启动
kubectl get pod -n nacos
初始化数据库
将 SQL 初始化文件复制到 MySQL 容器内部
#拷贝文件
kubectl cp nacos-mysql.sql -n nacos mysql-5z8vj:home/
#确认文件成功拷贝
kubectl exec -it mysql-5z8vj -n nacos -- ls /home
进入数据库,登录数据库,执行初始化文件
#进入数据库
kubectl exec -it mysql-5z8vj -n nacos bash
#登录数据库
mysql -u nacos -p123123
#进入nacos库
use nacos_devtest;
#执行初始化sql脚本
source /hom