Can't login mysql server deployed in k8s cluster

5/9/2019

I am using k8s in mac-docker-desktop. I deploy a mysql pod with below config.

run with: kubectl apply -f mysql.yaml

# secret
apiVersion: v1 
kind: Secret
metadata:
  name: mysql
type: Opaque
data:
  # root
  mysql-root-password: cm9vdAo=
---
# configMap
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-conf
data:
  database: app
---
apiVersion: apps/v1
kind: Deployment 
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      volumes:
      - name: mysql
        persistentVolumeClaim:
          claimName: mysql
      containers:
        - image: mysql:5.6
          name: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: mysql-root-password
            - name: MYSQL_DATABASE
              valueFrom:
                configMapKeyRef:
                  name: mysql-conf
                  key: database
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:
            - name: mysql
              mountPath: /var/lib/mysql
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
# services
apiVersion: v1
kind: Service 
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  selector:
    app: mysql
  ports:
    - port: 3306  
      targetPort: 3306 

After that. it shows ok . and then, I want to connect the mysql server with node ip, but failed. then I exec in the pod, and got failed either.

I execute in the pod and can't login.

gogs-k8s  kubectl get pods           
NAME                     READY     STATUS    RESTARTS   AGE
blog-59fb8cbd44-frmtx    1/1       Running   0          37m
blog-59fb8cbd44-gdskp    1/1       Running   0          37m
blog-59fb8cbd44-qrs8f    1/1       Running   0          37m
mysql-6c794ccb7b-dz9f4   1/1       Running   0          31s
gogs-k8s  kubectl exec mysql-6c794ccb7b-dz9f4 -it bash 
root@mysql-6c794ccb7b-dz9f4:/# ls
bin  boot  dev  docker-entrypoint-initdb.d  entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@mysql-6c794ccb7b-dz9f4:/# mysql -u root -p 
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
root@mysql-6c794ccb7b-dz9f4:/# echo $MYSQL_ROOT_PASSWORD
root
root@mysql-6c794ccb7b-dz9f4:/# mysql -u root -p 
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

It there any problems with my config file ?

--
docker
kubernetes
mysql

2 Answers

5/9/2019

As @Vasily Angapov pointed out, your base64 encoding is wrong.

When you do the following you are encoding the base for root\n

echo "root" | base64

Output:

cm9vdAo=

If you want to remove the newline character you should use the option -n:

echo -n "root" | base64

Output:

cm9vdA==

Even better is to do the following:

echo -n "root" | base64 -w 0

That way base64 will not insert new lines in longer outputs.

Also you can verify if your encoding is right by decoding the encoded text:

echo "cm9vdA==" | base64 --decode

The output should not create a new line.

-- victortv
Source: StackOverflow

5/9/2019

Probably you have invalid base64 encoded password. Try this one:

data:
  pass: cm9vdA==
-- Vasily Angapov
Source: StackOverflow