0

This is a Kubernetes deployment file that originally was using only MongoDB database. I tried to add MySQL to it as well like below:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: auth
  template:
    metadata:
      labels:
        app: auth
    spec:
      containers:
        - name: auth
          image: learner/auth
          env:
            - name: MONGO_URI
              value: 'mongodb://auth-mongo-srv:27017/auth'
            - name: JWT_KEY
              valueFrom:
                secretKeyRef:
                  name: jwt-secret
                  key: JWT_KEY
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-secret
                  key: MYSQL_KEY
---
apiVersion: v1
kind: Service
metadata:
  name: auth-srv
spec:
  selector:
    app: auth
  ports:
    - name: auth
      protocol: TCP
      port: 3000
      targetPort: 3000

But I am not sure if I must also define another env for MYSQL_URI or not? Something like:

    - name: MYSQL_URI
      value: 'mysql://auth-mongo-srv:3306/auth'
best_of_man
  • 367
  • 1
  • 3
  • 12

1 Answers1

0

It is not required to create a separate env variable for MYSQL_URI in the yaml files while encrypting the username and password for your mysql database. However, when you are referring to this DB in your application code you need to use the MYSQL_URI and this encrypted data for connecting to Mysql DB.

Please go through this link for yaml file reference.

  • I have exactly done what it says in the suggested link, but when I try `mysql -p` and enter the password, I get `ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)` error! – best_of_man Dec 27 '22 at 19:20
  • Please look at my original question here: https://serverfault.com/questions/1118742/error-2002-hy000-cant-connect-to-local-mysql-server-through-socket-var-run – best_of_man Dec 27 '22 at 19:28
  • @best_of_man I have gone through your original question. Since you are deploying your mysql inside a container or pod and trying to connect to it using another container or pod, you should be using the private IPs or cluster IPs of the pods. I have seen that in your mysql.cnf file IP is localhost but it should be the private IP of your mysql pod. This kubernetes official documentation (https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/#deploy-mysql) helps you configuring your mysql deployment. – Kranthiveer Dontineni Dec 28 '22 at 02:29
  • Sorry but within my original question, I was just trying to run the mysql inside the deployed pod not from inside another pod. I think maybe I should change the `mysql` settings inside the deployed pod, not inside my `mysql` local files! But I don't know how to open the `/etc/my.cnf` file inside the pod because `nano` or `vim` aren't installed and `apt` command doesn't work to install something inside the pod! – best_of_man Dec 28 '22 at 18:07
  • @best_of_man since you didn't used that mysql instance and if there is no data available in it, you can go with new deployment using the steps which I have provided above. Meanwhile, I will try to figure out a way to help you correcting your existing /etc/my.cnf file. I think we can do something by using kubectl exec command like moving the existing my.cnf to my.cnf.bkp and creating a new file using the shell script. – Kranthiveer Dontineni Dec 29 '22 at 05:28