简体   繁体   English

MySQL Kubernetes

[英]MySQL Kubernetes

I have such a mysql configuration for kubernetes.我有这样一个 mysql 配置用于 kubernetes。 But I can not connect to database with my local mysql.但是我无法使用本地 mysql 连接到数据库。 I am doing port-forward: kubectl port-forward svc/mysql 3307 and then try to connect with command: mysql -h 127.0.0.1 -P 3307 -uroot -p我正在做端口转发:kubectl port-forward svc/mysql 3307 然后尝试使用命令连接:mysql -h 127.0.0.1 -P 3307 -uroot -p

with password: pass带密码:通过

This password is defined in secret file for the root user.此密码在 root 用户的机密文件中定义。 The error is: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)错误是:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Do you have idea what could be wrong?你知道可能出了什么问题吗?

mysql-deployment: mysql部署:

apiVersion: v1
kind: Service
metadata:
  name: mysql 
  labels:
    app: mysql
    tier: database
spec:
  ports:
    - port: 3307
      targetPort: 3306
  selector:       
    app: mysql
    tier: database
---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim 
  labels:
    app: mysql
    tier: database
spec:
  accessModes:
    - ReadWriteOnce   
  resources:
    requests:
      storage: 1Gi   
---
# 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  labels:
    app: mysql
    tier: database
spec:
  selector:
    matchLabels:
      app: mysql
      tier: database
  strategy:
    type: Recreate
  template:
    metadata:
      labels: 
        app: mysql
        tier: database
    spec:
      containers:
        - image: mysql:5.7 # image from docker-hub
          args:
            - "--ignore-db-dir=lost+found" 
          name: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD 
              valueFrom:
                secretKeyRef:
                  name: db-root-credentials 
                  key: password  
            - name: MYSQL_USER 
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: username
            - name: MYSQL_PASSWORD 
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: password
            - name: MYSQL_DATABASE 
              valueFrom:
                configMapKeyRef:
                  name: db-conf
                  key: name
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:        
            - name: mysql-persistent-storage
              mountPath: 
      volumes:
        - name: mysql-persistent-storage 
          persistentVolumeClaim:
            claimName: mysql-pv-claim

mysqldb-root-credentials: mysqldb-root-credentials:

apiVersion: v1
kind: Secret
metadata:
  name: db-root-credentials
data:
  password: cGFzcwo=

mysqldb-credentials: mysqldb-凭据:

apiVersion: v1
kind: Secret
metadata:
  name: db-credentials
data:
  username: c2ViYQo=
  password: c2ViYQo=

I've reproduced your issue and solve it by changing the way secrets are created.我已经复制了您的问题并通过更改创建秘密的方式来解决它。 I used kubectl CLI to create secrets:我使用kubectl CLI 创建秘密:

kubectl create secret generic db-credentials --from-literal=password=xyz --from-literal=username=xyz
kubectl create secret generic mysql-pass --from-literal=password=pass

Then deployed PVC, Deployment and Service :然后部署PVC, Deployment and Service

apiVersion: v1
kind: Service
metadata:
  name: mysql 
  labels:
    app: mysql
    tier: database
spec:
  ports:
    - port: 3306
      targetPort: 3306
  selector:       
    app: mysql
    tier: database
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim 
  labels:
    app: mysql
    tier: database
spec:
  accessModes:
    - ReadWriteOnce   
  resources:
    requests:
      storage: 1Gi   
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  labels:
    app: mysql
    tier: database
spec:
  selector:
    matchLabels:
      app: mysql
      tier: database
  strategy:
    type: Recreate
  template:
    metadata:
      labels: 
        app: mysql
        tier: database
    spec:
      containers:
        - image: mysql:5.7 # image from docker-hub
          args:
            - "--ignore-db-dir=lost+found" 
          name: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD 
              valueFrom:
                secretKeyRef:
                  name: mysql-pass
                  key: password  
            - name: MYSQL_USER 
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: username
            - name: MYSQL_PASSWORD 
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: password
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:        
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-persistent-storage 
          persistentVolumeClaim:
            claimName: mysql-pv-claim

Exec to pod:执行到 pod:

kubectl exec -it mysql-78d9b7b765-2ms5n -- mysql -h 127.0.0.1 -P 3306 -uroot -p

Once I enter the root password everything works fine:输入root密码后,一切正常:

Welcome to the MySQL monitor.  Commands end with ; or \g.
[...]

mysql>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM