likes
comments
collection
share

K8S安装Mysql8.0

作者站长头像
站长
· 阅读数 49

K8S安装Mysql8.0

1.创建ConfigMap存储配置文件

mysql-config.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  labels:
    app: mysql
data:
  my.cnf: |-
    [client]
    default-character-set=utf8mb4
    [mysql]
    default-character-set=utf8mb4
    [mysqld] 
    max_connections = 2000
    secure_file_priv=/var/lib/mysql
    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION    

执行结果

kubectl apply -f mysql-config.yaml 
configmap/mysql-config created

kubectl get cm
NAME           DATA   AGE
mysql-config   1      7s

2. 数据存储(PV+PVC)

新建目录

mkdir -p /data/mysql

NFS挂载

vim /etc/exports

/data/mysql *(rw,no_root_squash)

mysql-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
  - ReadWriteOnce
  nfs:
    path: /data/mysql
    server: 10.0.4.5

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

执行结果

kubectl apply -f mysql-pv.yaml

kubectl get pv,pvc
NAME                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM               STORAGECLASS   REASON   AGE
persistentvolume/mysql-pv   5Gi        RWO            Retain           Bound    default/mysql-pvc                           3s

NAME                              STATUS   VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/mysql-pvc   Bound    mysql-pv   5Gi        RWO                           3s

3. 部署Mysql Pod

mysql-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:8.0.19
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD    ## 配置Root用户默认密码
          value: "123456"
        resources:
          limits:
            cpu: 500m
            memory: 512Mi
          requests:
            cpu: 500m
            memory: 512Mi
        livenessProbe:
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
          exec:
            command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
        readinessProbe:
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
          exec:
            command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
        - name: config
          mountPath: /etc/mysql/conf.d/my.cnf
          subPath: my.cnf
        - name: localtime
          readOnly: true
          mountPath: /etc/localtime
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: mysql-pvc
      - name: config
        configMap:
          name: mysql-config
      - name: localtime
        hostPath:
          type: File
          path: /etc/localtime

注意:persistentVolumeClaim和configMap需要和之前创建的名称匹配

参数简介:

  • ports: 配置镜像映射端口。
  • env: 镜像环境变量配置,其中 MYSQL_ROOT_PASSWORD 是 Mysql 镜像用于配置 root 用户默认密码变量。
  • resources: 配置 CPU、Memory 资源限制,可以通过配置该值来配置 Pod 的 QoS 级别。
  • livenessProbe: 配置存活探针,定时检测 Mysql 应用运行状态,如果检测到 Mysql 挂掉将进行重启操作。
  • readinessProbe: 配置就绪探针,定时检测 Mysql 应用启动状态,如果启动成功将允许流量涌入,启动失败将进行重启操作。
  • command: 探针执行探测时执行的探测命令。
  • volumeMounts: 存储卷挂载配置,用于镜像内存储的挂载配置,与 volumes 中对于的 name 进行绑定。
  • volumes: 存储卷配置,可配置使用 pvc、hostPath、emptyDir、nfs 等存储,需要配置 name 值与 VolumeMounts 进行绑定。

执行结果

kubectl apply -f mysql-deploy.yaml
kubectl get pods

NAME                    READY   STATUS    RESTARTS   AGE
mysql-589f94cf4-gzng6   1/1     Running   0          46s

4.配置service

mysql-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  type: NodePort
  ports:
  - name: mysql
    port: 3306
    targetPort: 3306
    nodePort: 30336
  selector:
    app: mysql

执行结果

kubectl apply -f mysql-svc.yaml 
kubectl get svc mysql

NAME    TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
mysql   NodePort   10.102.59.153   <none>        3306:30336/TCP   2m16s

5. 连接测试

mysql -h 192.168.0.111 -P 30336 --user=root --password=123456

K8S安装Mysql8.0

Reference

Kubernetes 部署 Mysql 8.0 数据库

MySQL8.0登录提示caching_sha2_password问题解决方法

转载自:https://segmentfault.com/a/1190000041699942
评论
请登录