Headless Services are used by Stateful-Sets.
Normal flow of a web app to a database pod behind a service (which will load balance traffic across all the pods behind the service).
For something like a mysql deployment, reads can ben sent to any of the nodes, but only writes can be sent to the master node.
A headless service will not get it's own IP, like a normal service. It only gets a DNS entry. It also does not perform any load balancing. It only creates DNS Entries for each pod using the pod name and a subdomain.
In the example above, it's using <podname>-<headless servicename>.<namespace>.svc.<cluster-domain>
A headless service is created the same way a normal service is created. The only difference is that you include clusterIP: None
.
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None. #This is what makes a service a headless service
selector:
app: nginx
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx # has to match .spec.template.metadata.labels
serviceName: "nginx"
replicas: 3 # by default is 1
template:
metadata:
labels:
app: nginx # has to match .spec.selector.matchLabels
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: k8s.gcr.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "my-storage-class"
resources:
requests:
storage: 1Gi
For a pod to use a Headless Service, it must have the following (if not usign a stateful set):
spec:
subdomain: mysql-h #Matches the name of the stateless service
hostname: mysql-pod
The subdomain will create a dns record for the name of the service to point to the pod. This doesn't create any a-records. For that, you need the hostname field. The hostname will then create the dns record for the actual pod.
With a Stateful set, we do not need to include the subdomain or hostname values, as the stateful set includes those by default.