kubernetes安装mysql

半兽人 发表于: 2020-05-11   最后更新时间: 2022-10-14 10:59:24  
{{totalSubscript}} 订阅, 3,239 游览

Step 1: 准备Yaml文件

1、创建mysql-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-mysql
  namespace: mysql-space
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-mysql
  template:
    metadata:
      labels:
        app: my-mysql
    spec:
      containers:
        - name: my-mysql
          image: mysql:8.0.30
          imagePullPolicy: IfNotPresent
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: root
            - name: MYSQL_USER
              value: test
            - name: MYSQL_PASSWORD
              value: test
          ports:
            - containerPort: 3306
              protocol: TCP
              name: http
          volumeMounts:
            - name: my-mysql-data
              mountPath: /var/lib/mysql
            - name: mysql-conf
              mountPath: /etc/mysql/mysql.conf.d
      volumes:
        - name: mysql-conf
          configMap:
            name: mysql-conf
        - name: my-mysql-data
          persistentVolumeClaim:
            claimName: my-mysql-data

2、创建mysql-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-mysql-data
  namespace: mysql-space
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

3、创建mysql-configmap.yaml

可以根据你的需要替换my.cnf的内容。

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-conf
  namespace: mysql-space
data:
  mysql.cnf: |
    [mysqld]

     pid-file        = /var/run/mysqld/mysqld.pid
     socket          = /var/run/mysqld/mysqld.sock
     datadir         = /var/lib/mysql

     symbolic-links=0
     sql-mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

4、创建mysql-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql-export
  namespace: my-space
spec:
  type: NodePort
  selector:
    app: my-mysql
  ports:
    - port: 3306
      targetPort: 3306
      nodePort: 32306

你根据需要,确认是否需要开通nodePort,向外部暴露访问。

Steo 2: 应用

kubectl apply -f mysql-deployment.yaml
kubectl apply -f mysql-configmap.yaml
kubectl apply -f mysql-pvc.yaml
kubectl apply -f mysql-svc.yaml

也可以通过kubebiz提供的yaml文件进行创建:

kubectl apply -f https://www.kubebiz.com/raw/KubeBiz/mysql/8.0.30/all?namespace=my-space

Steo 3: 验证

查看是否创建成功

[root@master ~]# kubectl get pod,svc -n my-space
NAME                            READY   STATUS    RESTARTS   AGE
pod/mysql-6c9b459c75-lk6dq      1/1     Running   0          1h

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
service/mysql        NodePort    10.106.41.29     <none>        3306:30295/TCP   1h

进入容器

[root@master ~]# kubectl exec -it mysql-6c9b459c75-lk6dq bash -n my-space
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@mysql-6c9b459c75-lk6dq:/# 
root@mysql-6c9b459c75-lk6dq:/# 
root@mysql-6c9b459c75-lk6dq:/# 
root@mysql-6c9b459c75-lk6dq:/# 
root@mysql-6c9b459c75-lk6dq:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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>
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sonar              |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
————————————————

测试成功。

更新于 2022-10-14
在线,37分钟前登录

查看kubernetes更多相关的文章或提一个关于kubernetes的问题,也可以与我们一起分享文章
提问