k8s创建MySQL

Kubernetes创建MysQL

整体流程:创建数据存储PV、PVC;创建MySQL数据库、创建访问入口Service;导入测试数据库test-db

创建数据存储PV、PVC

这里我们使用nfs作为storageclass,具体yaml文件如下:

# 创建PV
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: /home/k8s/data/mysql  # 指定nfs的挂载点
    server: 20.20.20.200  # 指定nfs服务地址
---
#创建PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql
spec:
  storageClassName: manual
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

创建MySQL数据库,以及SVC

➜  kubectl apply -f svc.yam
➜  kubectl apply -f mysql-app.yaml
# 创建MySQL数据库
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
          name: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD # 生产环境中请使用 secret
              value: password
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:
            - name: mysql
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql
          persistentVolumeClaim:
            claimName: mysql
---
# 创建SVC
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  selector:
    app: mysql
  ports:
    - port: 3306

直接应用以上yaml文件,查看pv、pvc、mysql、svc的创建

➜  ~ kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                       STORAGECLASS   REASON   AGE
mysql                                      20Gi       RWO            Retain           Bound    default/mysql               manual                  19h
pvc-bf18ae1f-f53f-44dc-bb46-78472bf8b9e5   8Gi        RWO            Delete           Bound    apisix/data-apisix-etcd-0   nfs-client              20h
➜  ~ kubectl get pvc
NAME      STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql     Bound    mysql    20Gi       RWO            manual         19h
nfs-pvc   Lost     nfs-pv   0                         manual         6d1h
➜  ~ kubectl get pods mysql-68c646d9c8-76z6f
NAME                     READY   STATUS    RESTARTS   AGE
mysql-68c646d9c8-76z6f   1/1     Running   0          19h
# 修改网络类型为NodePort,以便访问
➜  ~ kubectl get svc mysql
NAME    TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
mysql   NodePort   10.96.26.54   <none>        3306:31171/TCP   19h

测试数据导入

  • 测试数据获取入口:test_db

将获取到的测试数据导入到Pod中,并解压。

kubectl cp test_db-1.0.7.tar.gz mysql-68c646d9c8-76z6f:/
tar -xvf test_db-1.0.7.tar.gz
cd test_db/

将测试数据导入到数据库中

bash-4.4# mysql -u root -p < employees.sql
Enter password:
...

-- 验证是否导入成功
bash-4.4# mysql -u root -p < test_employees_md5.sql
Enter password:
...
employees   OK  ok
departments OK  ok
dept_manager    OK  ok
dept_emp    OK  ok
titles  OK  ok
salaries    OK  ok
computation_time
00:00:13
summary result
CRC OK
count   OK

测试数据库表结构

测试数据连接

  • 设置远程登录权限
alter user 'root'@'%' identified with mysql_native_password by'root';
alter user 'root'@'%' identified by 'youpassword';
  • 方法二
use mysql;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'youpassword';
--  一定要刷新生效
flush privileges; 

配置地址端口测试连接:

posted @ 2022-12-15 17:00  wenkuna  阅读(296)  评论(0编辑  收藏  举报