I'm trying to deploy a mysql instance in k8s through a StatefulSet using the official Mysql image from DockerHub. I'm following the image documentation from DockerHub and providing MYSQL_ROOT_PASSWORD
, MYSQL_USER
and MYSQL_PASSWORD
env vars, so the user should be automatically created, but it is not. The error I can see in container's logs is that root
user is not able to connect at the point the user provided in MYSQL_USER
is being created.
2021-09-14 17:28:20+00:00 [Note] [Entrypoint]: Creating user foo_user
2021-09-14T17:28:20.860763Z 5 [Note] Access denied for user 'root'@'localhost' (using password: YES)
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
After some investigation, I've noticed that the problem occurs when the values for the env vars are taken from k8s secrets, but if I hardcode their values in the StatefulSet's manifest, it works just fine. You can see my current code below:
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
labels:
app: mysql
spec:
replicas: 1
serviceName: mysql-svc
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: "mysql:latest"
env:
- name: MYSQL_DATABASE
value: 'foo_db'
- name: MYSQL_USER
value: 'foo_user'
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-sec
key: MYSQL_PASSWORD
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-sec
key: MYSQL_ROOT_PASSWORD
ports:
- containerPort: 3306
protocol: TCP
volumeMounts:
- name: mysql-db
mountPath: /var/lib/mysql
subPath: mysql
volumeClaimTemplates:
- metadata:
name: mysql-db
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 4Gi
And the secrets.yml
file:
apiVersion: v1
kind: Secret
metadata:
name: mysql-sec
labels:
app: mysql
type: Opaque
data:
MYSQL_PASSWORD: ***************************
MYSQL_ROOT_PASSWORD: ***************************
I've also tried to create the secrets first to make sure that the secrets already exist when the pod spins up, but without any success.
Any idea?