# Make pod run on same node

**URL:** https://discuss.kubernetes.io/t/make-pod-run-on-same-node/15730
**Category:** General Discussions
**Created:** [April 30, 2021, 3:15am UTC](https://discuss.kubernetes.io/t/make-pod-run-on-same-node/15730 "2021-04-30T03:15:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![klaylin](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/klaylin/32/7528_2.png) [@klaylin](https://discuss.kubernetes.io/u/klaylin)
#### Post date: [April 30, 2021, 3:15am UTC](https://discuss.kubernetes.io/t/make-pod-run-on-same-node/15730/1 "2021-04-30T03:15:10Z")

</div>

How can I make the pods defined by deployment run on the same node, but I don’t want to specify which node it is

---

<div class="post-metadata">

### Author: ![rael](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/rael/32/2769_2.png) [@rael](https://discuss.kubernetes.io/u/rael)
#### Post date: [April 30, 2021, 6:07am UTC](https://discuss.kubernetes.io/t/make-pod-run-on-same-node/15730/2 "2021-04-30T06:07:25Z")

</div>

You can use affinity for that:

> **[Assigning Pods to Nodes](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity)**
>
> You can constrain a Pod so that it can only run on particular set of Node(s). There are several ways to do this and the recommended approaches all use label selectors to facilitate the selection. Generally such constraints are unnecessary, as the...

In the same document, you can find exactly your use case: **Always co-located in the same node**

> **[Assigning Pods to Nodes](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#always-co-located-in-the-same-node)**
>
> You can constrain a Pod so that it can only run on particular set of Node(s). There are several ways to do this and the recommended approaches all use label selectors to facilitate the selection. Generally such constraints are unnecessary, as the...

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-cache
spec:
  selector:
    matchLabels:
      app: store
  replicas: 3
  template:
    metadata:
      labels:
        app: store
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - store
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: redis-server
        image: redis:3.2-alpine
```

---

<div class="post-metadata">

### Author: ![klaylin](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/klaylin/32/7528_2.png) [@klaylin](https://discuss.kubernetes.io/u/klaylin)
#### Post date: [April 30, 2021, 7:00am UTC](https://discuss.kubernetes.io/t/make-pod-run-on-same-node/15730/3 "2021-04-30T07:00:50Z")

</div>

Thanks for your reply！！！  
I will try it first
