Description
A StatefulSet uses serviceName to give each Pod a stable network name. Create the named headless Service separately in the same namespace.
Potential impact
If the Service is missing or selects the wrong Pods, members may fail to resolve each other’s names or reach the intended Pod.
Remediation
- Set
clusterIP: Noneon the Service and match its name toserviceName. Check its selector against the Pod labels and its ports against the application’s listening ports. - Converting an existing ClusterIP Service to headless may require recreating it. Plan for the connection impact, then verify EndpointSlices, Pod readiness, per-Pod DNS and connections.
Examples
Ensure the app namespace exists. The example address 10.0.0.20 must be free in the cluster’s Service range.
Before
yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: app
spec:
clusterIP: 10.0.0.20
selector:
app: other
ports:
- port: 80
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
namespace: app
spec:
serviceName: nginx
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25
The nginx Service has a cluster IP and selects app: other. The StatefulSet Pods are labeled app: nginx.
After
yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: app
spec:
clusterIP: None
selector:
app: nginx
ports:
- port: 80
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
namespace: app
spec:
serviceName: nginx
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25
The nginx Service is now headless and selects the StatefulSet Pods. Check that their names resolve and clients can connect.