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:/binHOSTNAME=go-demo-2-db-dgbqgGOSU_VERSION=1.7MONGO_MAJOR=3.3MONGO_VERSION=3.3.15MONGO_PACKAGE=mongodb-org-unstableKUBERNETES_PORT=tcp://10.43.0.1:443GO_DEMO_2_DB_PORT_27017_TCP_PORT=27017GO_DEMO_2_DB_PORT_27017_TCP_ADDR=10.43.223.206GO_DEMO_2_API_PORT_8080_TCP=tcp://10.43.54.204:8080GO_DEMO_2_API_PORT_8080_TCP_PORT=8080GO_DEMO_2_API_PORT_8080_TCP_ADDR=10.43.54.204KUBERNETES_SERVICE_PORT_HTTPS=443KUBERNETES_PORT_443_TCP_PORT=443KUBERNETES_PORT_443_TCP_ADDR=10.43.0.1GO_DEMO_2_API_SERVICE_HOST=10.43.54.204GO_DEMO_2_API_PORT=tcp://10.43.54.204:8080KUBERNETES_SERVICE_HOST=10.43.0.1GO_DEMO_2_DB_SERVICE_HOST=10.43.223.206GO_DEMO_2_DB_SERVICE_PORT=27017GO_DEMO_2_DB_PORT=tcp://10.43.223.206:27017GO_DEMO_2_DB_PORT_27017_TCP=tcp://10.43.223.206:27017GO_DEMO_2_API_SERVICE_PORT=8080KUBERNETES_SERVICE_PORT=443KUBERNETES_PORT_443_TCP=tcp://10.43.0.1:443KUBERNETES_PORT_443_TCP_PROTO=tcpGO_DEMO_2_DB_PORT_27017_TCP_PROTO=tcpGO_DEMO_2_API_PORT_8080_TCP_PROTO=tcpHOME=/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 ...