Exposing services within a cluster

As mentioned earlier in the chapter, one of the key scenarios where Kubernetes and GKE are very beneficial is microservices. Once you deploy your services or application to a GKE container cluster, its pods are automatically assigned internal IP addresses. GKE, through the use of cluster local DNS and GCP labels, goes further in assisting with service discovery and general node-to-node communication within a cluster. Containers within a pod can all reach each other's ports on localhost, and all pods in a cluster can see each other without network address translation (NAT).

IP addresses can certainly be utilized for communication between deployed services, but it's much more intuitive and maintainable to alias private IP addresses and discover applicable services via workload labels. Service discovery is configured using just a few additional parameters in your YAML file. Here is deployment YAML that indicates, via metadata, that the deployed services can be referenced by the aliased name of nginx-1:

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-1
spec:
selector:
matchLabels:
run: nginx-1
replicas: 2
template:
metadata:
labels:
run: nginx-1
spec:
containers:
- name: nginx-1
image: nginx
ports:
- containerPort: 80