Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow a pods in Kubernetes access external docker container ip like mysql

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

like image 718
Alireza Hamid Avatar asked Oct 17 '25 15:10

Alireza Hamid


1 Answers

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+

like image 155
TJ H. Avatar answered Oct 20 '25 04:10

TJ H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!