在K8S上安装MySQL

1、创建PV

apiVersion: v1
kind: PersistentVolume
metadata:
  name: model-db-pv
spec:
  storageClassName: ml-pv1
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 5Gi
  hostPath:
    path: /home/work/share/model-db
  persistentVolumeReclaimPolicy: Retain
  volumeMode: Filesystem

其中:home/work/share/  为所有K8S都共有的目录,相当于所有主机mount了一个NFS目录

也可以

apiVersion: v1
kind: PersistentVolume
metadata:
  name: model-db-pv
spec:
  storageClassName: ml-pv1
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 5Gi
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /home/work/share/model-db
    server: 192.168.20.10
  volumeMode: Filesystem

2、创建PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: model-db-pv-claim
  namespace: default
spec:
  storageClassName: ml-pv1
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

3、确认pv及pvc的状态

# kubectl get pv     
# kubectl get pvc    

4. 创建mysql configMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: model-db-config
  namespace: default
  labels:
    app: model-db
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

5、部署(Deployment)文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: model-db
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: model-mysql
  template:
    metadata:
      labels:
        app: model-mysql
    spec:
      containers:
      - args:
        - --datadir
        - /var/lib/mysql/datadir
        env:
          - name: MYSQL_ROOT_PASSWORD
            value: root
          - name: MYSQL_USER
            value: user
          - name: MYSQL_PASSWORD
            value: user
        image: mysql:5.6
        name: model-db-container
        ports:
        - containerPort: 3306
          name: dbapi
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: model-db-storage
        - name: config
          mountPath: /etc/mysql/conf.d/my.cnf
          subPath: my.cnf
      volumes:
      - name: model-db-storage
        persistentVolumeClaim:
          claimName: model-db-pv-claim
      - name: config      
        configMap:
          name: model-db-config
      - name: localtime
        hostPath:
          type: File
          path: /etc/localtime

6、创建svc(service)

ClusterIP:只对集群内部可见

apiVersion: v1
kind: Service
metadata:
  labels:
    app: model-mysql
  name: model-db-svc
  namespace: default
spec:
  type: ClusterIP
  ports:
  - port: 3306
    protocol: TCP
    targetPort: 3306
  selector:
    app: model-mysql

NodePort:对外部可见

apiVersion: v1
kind: Service
metadata:
  labels:
    app: model-mysql
  name: model-db-svc
  namespace: default
spec:
  type: NodePort
  ports:
  - name: http
    port: 3306
    nodePort: 30336
    protocol: TCP
    targetPort: 3306
  selector:
    app: model-mysql

先在mysql中创建test_db库,再修改环境变量,重启test_db后报错“Access denied for user ‘root’@‘172.17.0.1’ (using password: NO)”。但mysql用Navicat能够连接上。

    对于熟悉mysql的人,这个错误应该很容易定位。从MySQL8.0 开始,默认的验证方式是 caching_sha2_password(参见 MySQL 8.0.4 : New Default Authentication Plugin : caching_sha2_password)。

方案1:修改配置文件spring.datasource.password:***
方案2:将验证方式修改为“mysql_native_password”
 

USE mysql; 
 
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '<password>';
 
FLUSH PRIVILEGES;

Logo

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

更多推荐