k8s 中mysql 无法访问,使用Secrets输入密码时无法连接到Kubernetes中的mysql pod(拒绝访问)...

I try to setup a mysql database in Kubernetes. I configured a ConfigMap to store the Database name and a Secret that contains the root password, the user and the password for the user.

When I try to connect to the DB afterwards (Inside the container with mysql cli and from outside with IntelliJ Database tool) I get an "ERROR 1045 (28000): Access denied for user 'testadm'@'localhost' (using password: YES)" error.

My kubernetes.yaml file:

apiVersion: v1

kind: ConfigMap

metadata:

name: db

data:

mysql-database: database

---

apiVersion: v1

kind: Secret

metadata:

name: db-credentials

type: Opaque

data:

mysql-root-password: VGVzdDEyMzQK # Test1234

mysql-user: dGVzdGFkbQo= # testadm

mysql-password: VGVzdDEyMzQK # Test1234

---

apiVersion: apps/v1beta1

kind: Deployment

metadata:

name: mysql

spec:

replicas: 1

strategy:

type: Recreate

template:

metadata:

labels:

app: mysql

spec:

containers:

- name: mysql

image: mysql:5.7

ports:

- containerPort: 3306

env:

- name: MYSQL_DATABASE

valueFrom:

configMapKeyRef:

name: db

key: mysql-database

- name: MYSQL_ROOT_PASSWORD

valueFrom:

secretKeyRef:

name: db-credentials

key: mysql-root-password

- name: MYSQL_USER

valueFrom:

secretKeyRef:

name: db-credentials

key: mysql-user

- name: MYSQL_PASSWORD

valueFrom:

secretKeyRef:

name: db-credentials

key: mysql-password

If I set the passwords directly like below the connection succeeds inside of the container and from the outside!

env:

- name: MYSQL_ROOT_PASSWORD

value: Test1234

If I inspect the env variables inside the container I can't spot a difference between the two approaches.

Is there any additional formatting required to use the passwords stored in the secret? I also tried to place the values in the data-dictionary in quotes like this:

data:

mysql-root-password: "VGVzdDEyMzQK"

Version information

Docker 17.06.0-ce

Minikube 0.21.0

Kubectl Server 1.7.0

Kubectl Client 1.7.3

解决方案

you can use this yaml file.

apiVersion: v1

kind: Secret

metadata:

name: db-credentials

type: Opaque

data:

mysql-password: VGVzdDEyMzQ=

mysql-root-password: VGVzdDEyMzQ=

mysql-user: dGVzdGFkbQ==

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值