SRAVAN KUMAR
SRAVAN KUMAR

Reputation: 53

How to set fixed pods names in kubernetes

I want to maintain different configuration for each pod, so planning to fetch properties from spring cloud config based on pod name.

Ex: Properties in cloud

PodName1.property1 = "xxx" PodName2.property1 ="yyy";

Property value will be different for each pod. Planning to fetch properties from cloud ,based on container name Environment.get("current pod name"+ " propertyName").

So I want to set fixed hostname/pod name

If the above is not possible, is there any alternative ?

Upvotes: 2

Views: 7050

Answers (1)

Raghwendra Singh
Raghwendra Singh

Reputation: 2234

You can use statefulsets if you want fixed pod names for your application. e.g.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web # this will be used as prefix in pod name 
spec:
  serviceName: "nginx"
  replicas: 2 # specify number of pods that should be running
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web

This template will create 2 pods of nginx in default namespace with names as following:

kubectl get pods -l app=nginx
NAME      READY     STATUS    RESTARTS   AGE
web-0     1/1       Running   0          1m
web-1     1/1       Running   0          1m

A basic example can be found here.

Upvotes: 2

Related Questions