How to access kubernetes service host directly in python?

11/21/2019

I have a mysql pod running in kubernetes and the service is exposed as ClusterIP. When I grep the SERVICE in kubernetes I get

MYSQL_SERVICE_SERVICE_HOST=10.152.183.135

I am currently passing this as environment variable in the deployment.yml file of backend and accessing it is Python backend using os.getenv() function.

Can I directly call this host name in python as mysql_host = "{}".format(MYSQL_SERVICE_SERVICE_HOST) so that passing it as env variable is not required. Can anyone give me a helping hand?

-- anju
host
kubernetes
python

1 Answer

11/21/2019

Assuming Python is also running in same Kubernetes cluster. You don't need to pass MySQL host name. You should be able to reach MySQL service from Python pod using MySQL service name.

use below format

<mysql-service-name>.<namespace>.svc.cluster.local
-- P Ekambaram
Source: StackOverflow