Home > Software engineering >  How to allow a pods in Kubernetes access external docker container ip like mysql
How to allow a pods in Kubernetes access external docker container ip like mysql

Time:11-03

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

CodePudding user response:

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

CodePudding user response:

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 k exec -it dnsutils -n default -- bash.

  • Related