<p>I have 4 k8s pods by setting the replicas of Deployment to 4 now.</p>

<pre><code>apiVersion: v1
kind: Deployment
metadata:
  ...
spec:
  ...
  replicas: 4
...
</code></pre>

<p>The POD will get items in a database and consume it, the items in database has a column <code>class_name</code>.</p>

<p>now I want one pod only get one <code>class_name</code>'s item.
for example <code>pod1</code> only get item which <code>class_name</code> equals <code>class_name_1</code>, and <code>pod2</code> only get item which <code>class_name</code> equals <code>class_name_2</code>...</p>

<p>So I want to pass different <code>class_name</code> as environment variables to different Deployment PODs. Can I define it in the yaml file of Deployment?</p>

<p>Or is there any other way to achieve my goal?(like something other than Deployment in k8s)</p>

<p>I would not recommend this approach, but the closest thing to do what you want is using the stateful-set and use the pod name as the index.</p>

<p>When you deploy a stateful set, the pods will be named after their statefulset name, in the following sample:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: kuard
  labels:
    app: kuard
spec:
  type: NodePort
  ports:
  - port: 8080
    name: web
  selector:
    app: kuard
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: kuard
spec:
  serviceName: "kuard"
  replicas: 3
  selector:
    matchLabels:
      app: kuard
  template:
    metadata:
      labels:
        app: kuard
    spec:
      containers:
      - name: kuard
        image: gcr.io/kuar-demo/kuard-amd64:1 
        ports:
        - containerPort: 8080
          name: web
</code></pre>

<p>The pods created by the statefulset will be named as:</p>

<pre><code>kuard-0
kuard-1
kuard-2
</code></pre>

<p>This way you could either, name the stateful-set according to the classes, i.e: <code>class-name</code> and the pod created will be <code>class-name-0</code> and you can replace the <code>_</code> by <code>-</code>. Or just strip the name out to get the index at the end.</p>

<p>To get the name just read the environment variable <code>HOSTNAME</code></p>

<p>This naming is consistent, so you can make sure you always have 0, 1, 2, 3 after the name. And if the <code>2</code> goes down, it will be recreated.</p>

<p>Like I said, I would not recommend this approach because you tie the infrastructure to your code, and also can't scale(if needed) because each service are unique and adding new instances would get new ids.</p>

<p>A better approach would be using one deployment for each class and pass the proper values as environment variables.</p>
