...

/

Discovering Services

Discovering Services

Learn the process of discovering Services.

Discovering Services using DNS and environment variables

Services can be discovered through two principal modes:

  • Environment variables
  • DNS

Every Pod gets environment variables for each of the active Services. They are provided in the same format as what Docker links expect, as well as with the simpler Kubernetes-specific syntax.

Let's get environment variables for go-demo-2-db :

Press + to interact
POD_NAME=$(kubectl get pod \
--no-headers \
-o=custom-columns=NAME:.metadata.name \
-l type=db,service=go-demo-2 \
| tail -1)
kubectl exec $POD_NAME -- env

The output is limited to the environment variables related to the go-demo-2-db Service. It is as follows:

Press + to interact
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=go-demo-2-db-dgbqg
GOSU_VERSION=1.7
MONGO_MAJOR=3.3
MONGO_VERSION=3.3.15
MONGO_PACKAGE=mongodb-org-unstable
KUBERNETES_PORT=tcp://10.43.0.1:443
GO_DEMO_2_DB_PORT_27017_TCP_PORT=27017
GO_DEMO_2_DB_PORT_27017_TCP_ADDR=10.43.223.206
GO_DEMO_2_API_PORT_8080_TCP=tcp://10.43.54.204:8080
GO_DEMO_2_API_PORT_8080_TCP_PORT=8080
GO_DEMO_2_API_PORT_8080_TCP_ADDR=10.43.54.204
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.43.0.1
GO_DEMO_2_API_SERVICE_HOST=10.43.54.204
GO_DEMO_2_API_PORT=tcp://10.43.54.204:8080
KUBERNETES_SERVICE_HOST=10.43.0.1
GO_DEMO_2_DB_SERVICE_HOST=10.43.223.206
GO_DEMO_2_DB_SERVICE_PORT=27017
GO_DEMO_2_DB_PORT=tcp://10.43.223.206:27017
GO_DEMO_2_DB_PORT_27017_TCP=tcp://10.43.223.206:27017
GO_DEMO_2_API_SERVICE_PORT=8080
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT_443_TCP=tcp://10.43.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
GO_DEMO_2_DB_PORT_27017_TCP_PROTO=tcp
GO_DEMO_2_API_PORT_8080_TCP_PROTO=tcp
HOME=/root

The first five variables use the Docker format. If you have already worked with Docker networking, you must be familiar with the way Swarm (standalone) and Docker Compose operate. The later ...