IronJo
IronJo

Reputation: 41

kubernetes Deployment PodName setting

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-deployment
  labels:
    app: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      name: test
      labels:
        app: test
    spec:
      containers:
        - name: server
          image: test_ml_server:2.3
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: hostpath-vol-testserver
              mountPath: /app/test/api
#          env:
#            - name: POD_NAME
#              valueFrom:
#                fieldRef:
#                  fieldPath: template.metadata.name
        - name: testdb
          image: test_db:1.4
          ports:
            - name: testdb
              containerPort: 1433
          volumeMounts:
            - name: hostpath-vol-testdb
              mountPath: /var/opt/mssql/data
#          env:
#            - name: POD_NAME
#              valueFrom:
#                fieldRef:
#                  fieldPath: template.metadata.name
      volumes:
        - name: hostpath-vol-testserver
          hostPath:
            path: /usr/testhostpath/testserver
        - name: hostpath-vol-testdb
          hostPath:
            path: /usr/testhostpath/testdb

I want to set the name of the pod Because it communicates internally based on the name of the pod but when a pod is created, it cannot be used because the variable name is appended to the end. How can I set the pod name?

Upvotes: 2

Views: 2366

Answers (3)

Krishna Chaurasia
Krishna Chaurasia

Reputation: 9572

You can't.

It is the property of the pods of a Deployment that they do not have an identity associated with them.

You could have a look at Statefulset instead of a Deployment if you want the pods to have a state.

From the docs:

Like a Deployment, a StatefulSet manages Pods that are based on an identical container spec. Unlike a Deployment, a StatefulSet maintains a sticky identity for each of their Pods. These pods are created from the same spec, but are not interchangeable: each has a persistent identifier that it maintains across any rescheduling.

So, if you have a Statefulset object named myapp with two replicas, the pods will be named as myapp-0 and myapp-1.

Upvotes: 1

Emon46
Emon46

Reputation: 1626

It's better if you use, statefulset instead of deployment. Statefulset's pod name will be like <statefulsetName-0>,<statefulsetName-1>... And you will need a clusterIP service. with which you can bound your pods. see the doc for more details. Ref

apiVersion: v1
kind: Service
metadata:
  name: test-svc
  labels:
    app: test
spec:
  ports:
  - port: 8080
    name: web
  clusterIP: None
  selector:
    app: test
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: test-StatefulSet
  labels:
    app: test
spec:
  replicas: 1
  serviceName: test-svc
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      name: test
      labels:
        app: test
    spec:
      containers:
        - name: server
          image: test_ml_server:2.3
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: hostpath-vol-testserver
              mountPath: /app/test/api
        - name: testdb
          image: test_db:1.4
          ports:
            - name: testdb
              containerPort: 1433
          volumeMounts:
            - name: hostpath-vol-testdb
              mountPath: /var/opt/mssql/data
      volumes:
        - name: hostpath-vol-testserver
          hostPath:
            path: /usr/testhostpath/testserver
        - name: hostpath-vol-testdb
          hostPath:
            path: /usr/testhostpath/testdb

Here, The pod name will be like this test-StatefulSet-0.

Upvotes: 1

Harsh Manvar
Harsh Manvar

Reputation: 30120

if you are using the kind: Deployment it won't be possible ideally in this scenario you can use kind: Statefulset.

Instead of POD to POD communication, you can use the Kubernetes service for communication.

Still, statefulset manage the pod name in the sequence

statefulsetname - 0
statefulsetname - 1
statefulsetname - 2

Upvotes: 1

Related Questions