Skip to content

Instantly share code, notes, and snippets.

@iam-veeramalla
Created June 9, 2025 15:12
Show Gist options
  • Select an option

  • Save iam-veeramalla/d3dbcc2f1fc626a211d7c4dbfef28570 to your computer and use it in GitHub Desktop.

Select an option

Save iam-veeramalla/d3dbcc2f1fc626a211d7c4dbfef28570 to your computer and use it in GitHub Desktop.
Kubernetes Headless Service Demo

Headless Kubernetes Service

Service

apiVersion: v1
kind: Service
metadata:
  name: my-db-headless-service
spec:
  clusterIP: None
  selector:
    app: mysql
  ports:
    - port: 3306
      targetPort: 3306

Statefulset

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql-statefulset
spec:
  serviceName: "my-db-headless-service"   # Ensure this matches the service name
  replicas: 3
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:latest
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: "rootpassword"
          ports:
            - containerPort: 3306

Command for DNS check

kubectl run -it --rm --restart=Never --image=busybox dns-test -- nslookup mysql-statefulset-0.my-db-headless-service.default.svc.cluster.local

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment