K8s resource: Service
What is a Service?
A Service in Kubernetes is a networking resource that assigns a stable cluster-internal IP address or DNS name (<service-name>.<namespace>.svc.cluster.local
) to a set of pods.
This means different pods in your cluster can rely on the stable IP address or DNS name to communicate with each other because the IP address and DNS name won’t change, even if the associated pods come and go.
Example Service YAML
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
With the above service config, K8s will assign the my-service
service a stable internal IP like 10.96.0.42:80
and a DNS name like my-service.default.svc.cluster.local
.
This means that if other pods on your cluster can send a request to this service at 10.96.0.42:80
or my-service.default.svc.cluster.local
and be sure that K8s will forward the request to port 80
of the pods with the label my-app
.
The Service
definition gives other pods in your network or an Ingress a way to access the pods with the label my-app
.