首頁 > 軟體

使用Kubernetes叢集環境部署MySQL資料庫的實戰記錄

2022-05-27 14:02:15

1 編寫 mysql.yaml檔案

編寫yaml如下

apiVersion: v1
kind: Namespace
metadata:
  name: devops   # Namespace 的名稱
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: devops-mysql   # deployment控制器名稱
  namespace: devops
spec:
  replicas: 1
  revisionHistoryLimit: 5
  strategy:
    type: RollingUpdate
  selector:
    matchLabels:
      app: devops-mysql
  template:
    metadata:
      labels:
        app: devops-mysql
    spec:
      volumes:
        - name: devops-mysql
          nfs:
            server: xx.xx.xx.xx  # 修改為掛載儲存的伺服器ip
            path: /root/data/nfs/mysql/devops   # 修改為儲存伺服器的儲存掛載路徑
      containers:
        - name: devops-mysql
          image: mysql:5.7
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: xxxxxxxx     # 設定MySQL資料庫登入密碼
          imagePullPolicy: Always
          ports:
            - containerPort: 3306
          volumeMounts:
            - name: devops-mysql
              mountPath: /var/lib/mysql
---
apiVersion: v1
kind: Service
metadata:
  name: devops-mysql    # 資料庫服務的名稱
  namespace: devops
spec:
  ports:
    - port: 3306
      protocol: TCP
      targetPort: 3306
      nodePort: 30001    # 對外存取的埠
  selector:
    app: devops-mysql
  type: NodePort
  sessionAffinity: ClientIP   

2 執行如下命令建立

kubectl apply -f mysql.yaml

3 通過如下命令檢視建立結果

使用如下命令檢視

kubectl get pod -n devops | grep mysql

如:

[root@master ~]# kubectl get pod -n devops | grep mysql
devops-mysql-59b68c47d4-ttbng               1/1     Running   0          23h
[root@master ~]#

4 命令列進入Pod並登入mysql

如下;

[root@master ~]# kubectl exec -it devops-mysql-59b68c47d4-ttbng bash -n devops
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@devops-mysql-59b68c47d4-ttbng:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 13
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql>

5 至此,資料庫已經安裝完成,然後即可通過ip+埠,這裡是30001,進行資料庫連結了

到此這篇關於使用Kubernetes叢集環境部署MySQL資料庫的文章就介紹到這了,更多相關Kubernetes部署MySQL內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com