乐趣区

关于kubernetes:K8S安装Mysql80

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

Reference

Kubernetes 部署 Mysql 8.0 数据库

MySQL8.0 登录提醒 caching_sha2_password 问题解决办法

退出移动版