Search code examples
kubernetesminikubeheadlessstatefulset

Kubernetes StatefulSet error - serviceName environment variable doesn't exist


I'm supposed to make a StatefulSet with a Headless Service but when I make the Headless Service and create the StatefulSet only one pod gets made but with Error status and I get this error when trying to use kubectl log:

serviceName environment variable doesn't exist! Fix your specification.

Here is my code:

apiVersion: v1
kind: Service
metadata:
  name: svc-hl-xyz
spec:
  clusterIP: None
  selector:
    app: svc-hl-xyz
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: sts-xyz
spec:
  replicas: 3
  serviceName: "svc-hl-xyz"
  selector:
    matchLabels:
      app: svc-hl-xyz
  template:
    metadata:
      labels:
        app: svc-hl-xyz
    spec:
      containers:
        - name: ctr-sts-xyz
          image: XXX/XXX/XXX
          command: ["XXX", "XXX","XXX"]

My specification seems to follow the Kubernetes documentation for StatefulSet so I'm not sure why it doesn't work. All I can think of is that the command or the image I'm trying to use is causing this somehow.


Solution

  • The container logs (serviceName environment variable doesn't exist! Fix your specification.) tell you that the serviceName environment variable is missing.

    Add it to the container spec in your statefulset:

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: sts-xyz
    spec:
      replicas: 3
      serviceName: "svc-hl-xyz"
      selector:
        matchLabels:
          app: svc-hl-xyz
      template:
        metadata:
          labels:
            app: svc-hl-xyz
        spec:
          containers:
            - name: ctr-sts-xyz
              image: quay.io/myafk/interactive:stable
              command: ["interactive", "workloads","-t=first"]
              env:
               - name: serviceName
                 value: svc-hl-xyz
    

    More information about env variables on Pods can be found in the docs