Kubernetes: Replicaset with multi-container pod not provisioning all containers

10/17/2018

Attached image is yaml code of my replica set that I am trying to provision, only sync-gcp container is getting provisioned when I execute it. I am not sure what is the mistake I am doing yaml code of my replicaset

apiVersion: apps/v1
kind: ReplicaSet
metadata:
 name: sync-rs
spec:
 replicas: 4
 minReadySeconds: 20
 selector:
   matchExpressions:
     - {key: platform, operator: In, values: [aws, gcp]}
   matchLabels:
     version: "2"
template:
  metadata:
   name: sync-aws
   labels:
    version: "2"
    platform: aws
  spec:
   containers:
    - name: sync-aws
      image: schoolofdevops/sync:v2
   name: sync-gcp
   labels:
    version: "2"
    platform: gcp
   spec:
    containers:
     - name: sync-gcp
       image: schoolofdevops/sync:v2 

Below is the output of describe command for the replicaset

root@kube-01:/vagrant/k8s-code-master/projects/instavote/dev# kubectl 
describe rs sync-rs
Name:         sync-rs
Namespace:    default
Selector:     platform in (aws,gcp),version=2
Labels:       platform=gcp
          version=2
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
            {"apiVersion":"apps/v1","kind":"ReplicaSet","metadata": 
{"annotations":{},"name":"sync-rs","namespace":"default"},"spec": 
{"minReadySeconds"...
Replicas:     4 current / 4 desired
Pods Status:  4 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
     Labels:  platform=gcp
       version=2
Containers:
   sync-gcp:
   Image:        schoolofdevops/sync:v2
   Port:         <none>
   Host Port:    <none>
   Environment:  <none>
   Mounts:       <none>
   Volumes:        <none>
Events:
   Type    Reason            Age   From                   Message
   ----    ------            ----  ----                   -------
   Normal  SuccessfulCreate  36m   replicaset-controller  Created pod: sync-rs-hv25f
   Normal  SuccessfulCreate  36m   replicaset-controller  Created pod: sync-rs-2689s
   Normal  SuccessfulCreate  36m   replicaset-controller  Created pod: sync-rs-s54vz
   Normal  SuccessfulCreate  36m   replicaset-controller  Created pod: sync-rs-jjxm8
   root@kube-01:/vagrant/k8s-code-master/projects/instavote/dev#
-- Dpk
kubernetes
replicaset

1 Answer

10/17/2018

Template is not a list ( so you are overwriting the template), instead you just need one template for the pod creation , and inside the pod you will have multiple containers then.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
 name: sync-rs
spec:
 replicas: 4
 minReadySeconds: 20
 selector:
   matchExpressions:
     - {key: platform, operator: In, values: [aws, gcp]}
   matchLabels:
     version: "2"
template:
  metadata:
   name: sync-stuff
   lables:
     version: "2"
  spec:
    containers:
    - name: sync-aws
      image: schoolofdevops/sync:v2
    - name: sync-gcp
      image: schoolofdevops/sync:v2 

But this will still not work because you need separate lables for aws and gcp , so its time to divide things into separate pods instead of containers.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
 name: sync-rs-aws
spec:
 replicas: 2
 minReadySeconds: 20
 selector:
   matchExpressions:
     - {key: platform, operator: In, values: [aws]}
   matchLabels:
     version: "2"
template:
  metadata:
   name: sync-aws
   lables:
     version: "2"
     platform: "aws"
  spec:
   containers:
    - name: sync-aws
      image: schoolofdevops/sync:v2

---

apiVersion: apps/v1
kind: ReplicaSet
metadata:
 name: sync-rs-gcp
spec:
 replicas: 2
 minReadySeconds: 20
 selector:
   matchExpressions:
     - {key: platform, operator: In, values: [ gcp ]}
   matchLabels:
     version: "2"
template:
  metadata:
   name: sync-gcp
   lables:
     version: "2"
     platform: "gcp"
  spec:
   containers:
    - name: sync-gcp
      image: schoolofdevops/sync:v2
-- Ijaz Ahmad Khan
Source: StackOverflow