MySQL在K8S部署实践

Posted _雪辉_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL在K8S部署实践相关的知识,希望对你有一定的参考价值。

文章目录

一、创建mysql 服务

cat << EOF > mysql-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  selector:
    app: mysql
  ports:
    - port: 3306
EOF

#kubectl create -f mysql-service.yaml
service/mysql created
#kubectl get service
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.1.0.1       <none>        443/TCP    46h
mysql        ClusterIP   10.1.175.187   <none>        3306/TCP   2s

二、创建持久卷

cat << EOF > mysql-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/k8smysql"
EOF

#kubectl create -f mysql-pv.yaml
persistentvolume/mysql-pv created

#kubectl get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
mysql-pv   50Gi       RWO            Retain           Available           manual                  6s

三、创建持久卷声明

cat << EOF > mysql-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi
EOF

#kubectl create -f mysql-pvc.yaml
persistentvolumeclaim/mysql-pvc created
# kubectl get pvc
NAME        STATUS   VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-pvc   Bound    mysql-pv   50Gi       RWO            manual         5s

四、部署MySQL

cat << EOF > mysql-deployment.yaml
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:5.7
          name: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: "123456"
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:
            - name: mysql-data
              mountPath: /data/k8smysql/mysql
      volumes:
        - name: mysql-data
          persistentVolumeClaim:
            claimName: mysql-pvc
EOF

#kubectl create -f mysql-deployment.yaml
deployment.apps/mysql created
# kubectl get deployment
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
mysql   1/1     1            1           18s
# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP         NODE                NOMINATED NODE   READINESS GATES
mysql-589996946c-9q2p4   1/1     Running   0          44s   10.2.3.9   sysmessage-mydb02   <none>           <none>
[root@sysmessage-mydb01 ~]# mysql -uroot -p123456 -h10.2.3.9 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 2
Server version: 5.7.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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.

root@10.2.3.9 16:39:  [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

以上是关于MySQL在K8S部署实践的主要内容,如果未能解决你的问题,请参考以下文章

MySQL在K8S部署实践

mysql on k8s statefulset部署实践

k8s 1.8.2部署实践

k8s 1.8.2部署实践

部署k8s集群(k8s集群搭建详细实践版)

部署k8s集群(k8s集群搭建详细实践版)