How can I refer to my mysql container in the same pod?

7/23/2018

I have a Kubernetes deployment.yml file that includes a container setup for:

  • nginx
  • php-fpm
  • mysql

I have some environment variables set for my php-fpm container:

  • DB_HOST
  • DB_DATABASE
  • DB_USER
  • DB_PASSWORD

I'm wondering how I can set the DB_HOST env var to link to my MySQL container (running in the same pod as my nginx & php-fpm containers).

Or should I create a new pod for my MySQL container as then I can create a 'service' from that pod and refer to the MySQL service name?

Thanks!

-- mattyh88
docker
kubernetes
minikube

1 Answer

7/23/2018

It is a real bad practice to setup 2 'business' container in one pod (scale you stuff individually is better than scaling everything, pod can be started on different host, ...).

Anyway, a pod share network across the container, so you can use localhost as hostname if you really want to set everything in one pod.

But yes it is really better to set 1 statefulset for mysql and use the name in php-fpm

-- wargre
Source: StackOverflow