skip to Main Content

I wanted to deploy Strapi CMS to Kubernetes. On my local machine, I am trying to do this with Minikube. The structure of the project is MySQL in a different container outside of the cluster. I want to access the MySQL database from inside the cluster via this IP 172.17.0.2:3306

The Database is outside of the cluster and lives in a docker container. But the Strapi project lives in a cluster of Kubernetes.

This is my deployment YAML file for doing the Kubernetes stuff:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: cg-api
spec:
  selector:
    matchLabels:
      app: cg-api
  replicas: 2
  template:
    metadata:
      labels:
        app: cg-api
    spec:
      containers:
        - name: cg-api
          image: alirezahd/cg-api
          env:
            - name: DATABASE_HOST
              value: "172.17.0.2"
            - name: DATABASE_PORT
              value: "3306"
          ports:
            - containerPort: 1337

2

Answers


  1. You could try using the special minikube host to access your root machine. Assuming you have exposed the docker mysql container on your root machine using -p 3306:3306

    env:
      - name: DATABASE_HOST
        value: "host.minikube.internal"
    

    https://minikube.sigs.k8s.io/docs/handbook/host-access/#hostminikubeinternal
    Looks like you need minikube v1.10+

    Login or Signup to reply.
  2. First of all you should expose MySQL port 3306 to outside of a container.
    Assume host, on which Docker runs MySQL container, has 192.168.0.100 IP address. Then you can telnet 192.168.0.100:3306 to be sure port is exposed and available.

    Here is an example from Dockers documentation :

    docker run -p 192.168.0.100:3306:3306/tcp mysql-container-name bash
    

    This binds port 3306 of the container to TCP port 3306 on 192.168.0.100 of the host machine.

    Then you CMS can connect to it within minikube.

    Try to deploy dnsutils under minikube. It provides network tools like nslookup, telnet etc. It very helps to resolve network issues within minikube. You can connect to it sudo kubernetes exec -it dnsutils -n default -- bash.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search