Kubernetes deployment where one of the pods gets different env

6/16/2021

Is there any way I can have an AWS deployment using kubernetes where I have a variable and scalable number of pods for my app BUT the environment on the VM is different for a single host?

Basically I want that one of my pods doing a task that no of the other pods will do all the time. I am planning to control this using an environment variable.

-- Matias Barrios
amazon-web-services
kubernetes

1 Answer

6/16/2021

You can have multiple deployments, however if you are in need of uniqueness between the pods, you should consider statefulsets over deployments. However, deciding between statefulset and deployment is mostly decided by the type of the application and it is a major decision. so, using statefulset may not fit in as a 100% replacement for a deployment.

In statefulsets each pod get its unique but non-random identity via its hostname.

For example, you can create a mysql statefulset using following:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  creationTimestamp: null
  labels:
    app: foo
  name: foo
spec:
  serviceName: mysql
  replicas: 3
  selector:
    matchLabels:
      app: foo
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: foo
    spec:
      containers:
      - image: mysql
        name: mysql
      containers:
      - env:
        - name: MYSQL_ROOT_PASSWORD
          value: root
        image: mysql
        name: mysql

This would create three replicas of the pod:

k get pod -l app=foo
NAME    READY   STATUS    RESTARTS   AGE
foo-0   1/1     Running   0          5m17s
foo-1   1/1     Running   0          5m14s
foo-2   1/1     Running   0          5m10s

Now you can fetch the hostname of the pods, the first pod will be always suffixed with 0 and 2nd with 1 and so on.

exec -it foo-0 --  hostname
foo-0
k exec -it foo-1 --  hostname
foo-1
k exec -it foo-2 --  hostname
foo-2

you can use hostname as selector do decide what action is required on which pod. Here is one very good read on this.

-- P....
Source: StackOverflow