How does StatefulSet assigns hostname to containers in Kubernetes

11/9/2017

How does StatefulSet spec in Kubernetes assigns the hostname to each container in case we set hostNetwork: true? I have the following spec (similar):

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: abc
spec:
  serviceName: abc
  replicas: 2
  template:
    metadata:
      labels:
        app: abc
    spec:
      hostNetwork: true
      #hostame: test
      terminationGracePeriodSeconds: 10
      containers:
      - name: abc
        image: abc.xyz.com:9001/abc:01.00.00.00
        ports:
        - name: api-port
          containerPort: 9000
          hostPort: 9000
        readinessProbe:
          httpGet:
            path: /api
            port: api-port
            scheme: HTTPS

Now after running the kubectl create command, I see that the first container when comes up picks the hostname of the host since we have specified hostNetwork: true, but the second container when comes up has hostname something like abc-0.abc.default.svc.cluster.local.

  • Kubectl version: 1.7.3
  • Docker version: 1.12.6

Also, the only service running is the Kubernetes service. I didn't create abc service myself.

NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.96.0.1 <none> 443/TCP 1d

-- lex
kubernetes

1 Answer

11/9/2017

It should create only one pod per node, I tested below in my 1.8 cluster, its created one pod per node with correct pod hostname name (same as node name). it will not create any service we need to create one.

your cluster is version 1.7.3, it may be bug that's why its creating hostname incorrectly.

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: abc
spec:
  serviceName: abc
  replicas: 3
  template:
    metadata:
      labels:
        app: abc
    spec:
      hostNetwork: true
      #hostame: test
      terminationGracePeriodSeconds: 10
      containers:
      - name: sfgtools
        image: sfgroups/alphine_nettools
        ports:
        - name: api-port
          containerPort: 80
          hostPort: 80

I have only 2 nodes, its created only two pods I have only 2 nodes, its created only two pods

-- sfgroups
Source: StackOverflow