k8s部署mysql服務(RC部署)

編寫mysql-rc.yaml文件

apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql-rc
  labels:
    name: mysql-rc
spec:
  replicas: 1
  selector:
    name: mysql-pod
  template:
    metadata:
      labels:
        name: mysql-pod
    spec:
      containers:
        - name: mysql
          image: mysql:5.7
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 3306
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: mydata
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: "root"
      volumes:
        - name: mydata
          hostPath:
           path: /root/mysql/data

創建mysql-rc pod

[root@master mysql]# kubectl apply -f mysql-rc.yaml -n gridcloud
replicationcontroller/mysql-rc created

編寫mysql-service.yaml文件

apiVersion: v1
kind: Service
metadata:
  name: mysql-svc
  labels:
    name: mysql-svc
spec:
  type: NodePort
  ports:
    - port: 3306
      targetPort: 3306
      nodePort: 30306
  selector:
    name: mysql-pod

啓動mysql-service pod

[root@master mysql]# kubectl apply -f mysql-service.yaml -n gridcloud
service/mysql-svc created

查看我們的pod情況

[root@master mysql]# kubectl get pods -n gridcloud
NAME                                 READY   STATUS              RESTARTS   AGE
mysql-rc-d2whh                       0/1     ContainerCreating   0          59s

顯示正在創建並查看該pod信息

[root@master mysql]# kubectl describe pod mysql-rc-d2whh -n gridcloud
Name:         mysql-rc-d2whh
Namespace:    gridcloud
Priority:     0
Node:         node2/192.168.0.153
Start Time:   Sat, 30 May 2020 00:28:52 +0800
Labels:       name=mysql-pod
Annotations:  <none>
Status:       Running
IP:           10.244.2.49
IPs:
  IP:           10.244.2.49
Controlled By:  ReplicationController/mysql-rc
Containers:
  mysql:
    Container ID:   docker://64769dcdc408ecd690707aa4ce56bc6ddfed7b41e46cf3c7f8d99461788944d1
    Image:          mysql:5.7
    Image ID:       docker-pullable://mysql@sha256:d16d9ef7a4ecb29efcd1ba46d5a82bda3c28bd18c0f1e3b86ba54816211e1ac4
    Port:           3306/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Sat, 30 May 2020 00:30:35 +0800
    Ready:          True
    Restart Count:  0
    Environment:
      MYSQL_ROOT_PASSWORD:  root
    Mounts:
      /var/lib/mysql from mydata (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-sq8nw (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  mydata:
    Type:          HostPath (bare host directory volume)
    Path:          /root/mysql/data
    HostPathType:  
  default-token-sq8nw:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-sq8nw
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  2m38s  default-scheduler  Successfully assigned gridcloud/mysql-rc-d2whh to node2
  Normal  Pulling    2m38s  kubelet, node2     Pulling image "mysql:5.7"
  Normal  Pulled     57s    kubelet, node2     Successfully pulled image "mysql:5.7"
  Normal  Created    56s    kubelet, node2     Created container mysql
  Normal  Started    56s    kubelet, node2     Started container mysql

最後顯示創建成功,最後在查詢狀態

[root@master mysql]# kubectl get pods -n gridcloud
NAME                                 READY   STATUS    RESTARTS   AGE
mysql-rc-d2whh                       1/1     Running   0          2m48s

顯示running狀態,表示創建成功,並查看詳細信息顯示分配在node2上,如下

[root@master mysql]# kubectl get pods -n gridcloud -o wide
NAME            READY  STATUS  RESTARTS  AGE       IP         NODE    NOMINATED     NODE   READINESS GATES
mysql-rc-d2whh   1/1   Running   0      5m28s   10.244.2.49   node2   <none>                 <none>

登錄數據庫,並創建一個庫,做測試

[root@manage-host bin]# mysql -uroot -h 192.168.0.155 -P 30306 -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

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

mysql> quit
Bye

最後我們看我們掛載的路徑下有我們的數據庫數據信息,此時在node2上查看(前後多了一個test)

[root@node2 ~]# ls /root/mysql/data/
auto.cnf    ca.pem           client-key.pem  ibdata1      ib_logfile1  mysql               private_key.pem  server-cert.pem  sys
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile0  ibtmp1       performance_schema  public_key.pem   server-key.pem
[root@node2 ~]# cd /root/mysql/data/
[root@node2 data]# ls
auto.cnf    ca.pem           client-key.pem  ibdata1      ib_logfile1  mysql               private_key.pem  server-cert.pem  sys
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile0  ibtmp1       performance_schema  public_key.pem   server-key.pem   test

接着我們就重啓下pod看看我們的數據庫信息是否可查

[root@master mysql]# kubectl get pods -n gridcloud
NAME                                 READY   STATUS    RESTARTS   AGE
mysql-rc-d2whh                       1/1     Running   0          72m

[root@master mysql]# kubectl delete pod mysql-rc-d2whh -n gridcloud
pod "mysql-rc-d2whh" deleted
[root@master mysql]# kubectl apply -f mysql-service.yaml -n gridcloud
service/mysql-svc unchanged

然後我們重新登錄數據庫查看剛剛創建的庫是否存在

[root@manage-host bin]# mysql -uroot -h 192.168.0.155 -P 30306 -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

顯示我們的數據沒丟失,至此數據庫部署成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章