davidb
davidb

Reputation: 1603

Can't connect to mysql in kubernetes

I have deployed a mysql database in kubernetes and exposed in via a service. When my application tries to connect to that database it keeps being refused. I also get the same when I try to access it locally. Kubernetes node is run in minikube.

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  type: NodePort
  selector:
    app: mysql
  ports:
  - port: 3306
    protocol: TCP
    targetPort: 3306
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql_db
        imagePullPolicy: Never
        ports:
        - containerPort: 3306
        volumeMounts:
        - name:  mysql-persistent-storage
          mountPath:  "/var/lib/mysql"
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

And here's my yaml for persistent storage:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec: 
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/Users/Work/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

After this I get this by running minikube service list:

default              | mysql-service             | http://192.168.99.101:31613 

However I cannot access the database neither from my application nor my local machine. What am I missing or did I misconfigure something?

EDIT: I do not define any envs here since the image run by docker already a running mysql db and some scripts are run within the docker image too.

Upvotes: 1

Views: 8250

Answers (3)

Jacob Bruinsma
Jacob Bruinsma

Reputation: 1126

I thought I was connecting to my DB server correctly, but I was wrong. My DB deployment was online (tested with kubectl exec -it xxxx -- bash and then mysql -u root --password=$MYSQL_ROOT_PASSWORD) but that wasn't the problem.

I made the simple mistake of getting my service and deployment labels confused. My DB service used a different label, than what my Joomla configMap had specified as MySQL host.

To summarize, the DB service yaml was

metadata:
  labels:
    app: fnjoomlaopencart-db-service

and the Joomla configMap yaml needed

data:
  # point to the DB service
  MYSQL_HOST: fnjoomlaopencart-db-service

Upvotes: 0

davidb
davidb

Reputation: 1603

Ok, I figured it out. After looking through the logs I noticed the error Can't create/write to file '/var/lib/mysql/is_writable' (Errcode: 13 - Permission denied).

I had to add this to my docker image when building: RUN usermod -u 1000 mysql

After rebuilding the image everything started working. Thank you guys.

Upvotes: 1

Rodrigo Loza
Rodrigo Loza

Reputation: 1248

Mysql must not have started, confirm it by checking the logs. kubectl get pods | grep mysql; kubectl logs -f $POD_ID. Remember you have to specify the environment variables MYSQL_DATABASE and MYSQL_ROOT_PASSWORD for mysql to start. If you don't want to set a password for root also specify the respective command. Here I am giving you an example of a mysql yaml.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql_db
        imagePullPolicy: Never
        env:
        - name: MYSQL_DATABASE
          value: main_db
        - name: MYSQL_ROOT_PASSWORD
          value: s4cur4p4ss
        ports:
        - containerPort: 3306
        volumeMounts:
        - name:  mysql-persistent-storage
          mountPath:  "/var/lib/mysql"
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

Upvotes: 1

Related Questions