<p>I have couple of deployments - say Deployment A and Deployment B. The K8s Subnet is 10.0.0.0/20.
My requirement : Is it possible to get all pods in Deployment A to get IP from 10.0.1.0/24 and pods in Deployment B from 10.0.2.0/24.
This helps the networking clean and with help of IP itself a particular deployment can be identified. </p>

amp
<p>Deployment in Kubernetes is a high-level abstraction that rely on controllers to build basic objects. That is different than object itself such as pod or service. </p>

<p>If you take a look into deployments spec in <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#deploymentspec-v1-apps" rel="nofollow noreferrer">Kubernetes API Overview</a>, you will notice that there is no such a thing as defining subnets, neither IP addresses that would be specific for deployment so you cannot specify subnets for deployments. </p>

<p>Kubernetes idea is that pod is ephemeral. You should not try to identify resources by IP addresses as IPs are randomly assigned. If the pod dies it will have another IP address. You could try to look on something like statefulsets if you are after unique stable network identifiers. </p>

<p>While Kubernetes does not support this feature I found workaround for this using Calico: <a href="https://docs.projectcalico.org/networking/migrate-pools" rel="nofollow noreferrer">Migrate pools</a> feature. </p>

<p>First you need to have <code>calicoctl</code> installed. There are several ways to do that mentioned in the <a href="https://docs.projectcalico.org/getting-started/calicoctl/install" rel="nofollow noreferrer">install calicoctl</a> docs. </p>

<p>I choose to install <code>calicoctl</code> as a Kubernetes pod: </p>

<pre><code> kubectl apply -f https://docs.projectcalico.org/manifests/calicoctl.yaml
</code></pre>

<p>To make work faster you can setup an alias : </p>

<pre><code>alias calicoctl="kubectl exec -i -n kube-system calicoctl /calicoctl -- "
</code></pre>

<p>I have created two yaml files to setup ip pools: </p>

<pre><code>apiVersion: projectcalico.org/v3
kind: IPPool
metadata:
  name: pool1 
spec:
  cidr: 10.0.0.0/24
  ipipMode: Always
  natOutgoing: true

apiVersion: projectcalico.org/v3
kind: IPPool
metadata:
  name: pool2 
spec:
  cidr: 10.0.1.0/24
  ipipMode: Always
  natOutgoing: true
</code></pre>

<p>Then you you have apply the following configuration but since my yaml were being placed in my host filesystem and not in calico pod itself I placed the yaml as an input to the command: </p>

<pre><code>➜  cat ippool1.yaml | calicoctl apply -f-
Successfully applied 1 'IPPool' resource(s)
➜  cat ippool2.yaml | calicoctl apply -f-
Successfully applied 1 'IPPool' resource(s)
</code></pre>

<p>Listing the ippools you will notice the new added ones: </p>

<pre><code>➜  calicoctl get ippool -o wide
NAME                  CIDR             NAT    IPIPMODE   VXLANMODE   DISABLED   SELECTOR   
default-ipv4-ippool   192.168.0.0/16   true   Always     Never       false      all()      
pool1                 10.0.0.0/24      true   Always     Never       false      all()      
pool2                 10.0.1.0/24      true   Always     Never       false      all() 
</code></pre>

<p>Then you can specify what pool you want to choose for you deployment: </p>

<pre><code>--- 
metadata: 
  labels: 
    app: nginx
    name: deployment1-pool1
spec: 
  replicas: 3
  selector: 
    matchLabels: 
      app: nginx
  template: 
    metadata: 
      annotations: 
        cni.projectcalico.org/ipv4pools: "[\"pool1\"]"
 --- 
</code></pre>

<p>I have created similar one called <code>deployment2</code> that used <code>ippool2</code> with the results below: </p>

<pre><code>deployment1-pool1-6d9ddcb64f-7tkzs    1/1     Running   0          71m   10.0.0.198        acid-fuji   
deployment1-pool1-6d9ddcb64f-vkmht    1/1     Running   0          71m   10.0.0.199        acid-fuji   
deployment2-pool2-79566c4566-ck8lb    1/1     Running   0          69m   10.0.1.195        acid-fuji   
deployment2-pool2-79566c4566-jjbsd    1/1     Running   0          69m   10.0.1.196        acid-fuji   
</code></pre>

<p>Also its worth mentioning that while testing this I found out that if your default deployment will have many replicas and will ran out of ips Calico will then use different pool. </p>
