1

I have a local kubernetes cluster setup using the edge release of docker (mac). My pods use an env var that I've defined to be my DB's url. These env vars are defined in a config map as:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config 
data:
  DB_URL: postgres://user@localhost/my_dev_db?sslmode=disable

What should I be using here instead of localhost? I need this env var to point to my local dev machine.

TheKernel
  • 113
  • 1
  • 9

2 Answers2

1

You can use the private lan address of your computer, but please ensure that your database software is listening to all network interfaces and there is no firewall blocking incoming traffic.

If your LAN address is dynamic, you could use an internal DNS name pointing to your computer if your network setup provides one.

Another option is to run your database inside the kubernetes cluster: this way you could use it's service name as the hostname.

whites11
  • 12,008
  • 3
  • 36
  • 53
  • Thanks for this suggestion. I suppose I was looking for a way that I could just map my config once and be done with it for my local dev. Using this approach I have to update the lan IP each time it changes and then recreate my configmap. Then I have to restart my entire cluster again to accept the new value for my DB_URL. – TheKernel Jun 02 '18 at 08:23
  • 1
    It depends on your network, but if you're able to give your laptop an internal DNS name you could use that too. – whites11 Jun 02 '18 at 08:25
  • Another option is running your database inside kubernetes too. That way you would use the DB's service name as hostname to reach your database – whites11 Jun 02 '18 at 08:27
  • Yes, I have this setup in my staging env, but I wanted to keep things simple on my local dev. – TheKernel Jun 02 '18 at 08:40
1

Option 1 - Local Networking Approach

If you are running minikube, I would recommend taking a look at the answers to this question: Routing an internal Kubernetes IP address to the host system

Option 2 - Tunneling Solution: Connect to an External Service

A very simple but a little hacky solution would be to use a tunneling tool like ngrok: https://ngrok.com/

Option 3 - Cloud-native Development (run everything inside k8s)

If you plan to follow the suggestions of whites11, you could make your life a lot easier with using a kubernetes-native dev tool such as DevSpace (https://github.com/covexo/devspace) or Draft (https://github.com/Azure/draft). Both work with minikube or other self-hosted clusters.

Lukas Gentele
  • 949
  • 7
  • 13