Kubernetes - Headless Service đ§
Description đ
A headless service is a service that does not have a ClusterIP. This means that the service will not have a virtual IP address. Instead, the service will return the Pod IP addresses. This is useful for stateful applications that require a stable IP address for each Pod.
Examples đ§Š
- 
    
headless servicedefinitionapiVersion: v1 kind: Service metadata: name: mysql-h spec: ports: - port: 3306 selector: app: mysql clusterIP: Noneheadless servicedoes not have aClusterIP, must setclusterIPto None
 - 
    
podusingheadless serviceapiVersion: v1 kind: Pod metadata: name: myapp-pod labels: app: mysql spec: containers: - name: mysql image: mysql # pod uses headless service subdomain: mysql-h hostname: mysql-pod - 
    
stateful setusingheadless serviceapiVersion: apps/v1 kind: StatefulSet metadata: name: mysql-deployment labels: app: mysql spec: serviceName: mysql-h replicas: 3 selector: matchLabels: app: mysql template: metadata: name: myapp-pod labels: app: mysql spec: containers: - name: mysql image: mysqlserviceNameis the name of theheadless servicethat thestateful setwill use- the stateful set does not need to specify the 
subdomainandhostnamebecause thestateful setwill automatically create aheadless servicefor eachpodin thestateful set 
 - 
    
stateful setusingheadless servicewithpvcfor eachpodapiVersion: apps/v1 kind: StatefulSet metadata: name: mysql labels: app: mysql spec: replicas: 3 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - name: mysql image: mysql volumeMounts: - name: data-volume mountPath: /var/lib/mysql volumeClaimTemplates: - metadata: name: data-volume spec: accessModes: - ReadWriteOnce storageClassName: google-storage resources: requests: storage: 500MivolumeClaimTemplatesis used to create apvcfor eachpodin thestateful setstateful setsdo not automatically deletepvcwhenpodsfail or are deleted, instead they ensure that when thepodis recreated, thepvcis bound back to thepodthat was recreated.