编写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)

显示我们的数据没丢失,至此数据库部署成功

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐