how to assign different nodes and images for different platform architectures in kubernetes deployment manifest

2/4/2022

I have a working cluster using node affinity that can assign pods to a node depending on the kubernetes architecture label. I have a mixed cluster of raspberry pi ARM64 and linux AMD64. I cannot figure out how to spread a replica set over 2 different container specs each containing the affinity label and image tags.

Below is working manifest for raspberry pi but I would like to add further spec for amd64 inside the same manifest but using different image tag and based on amd64 label

apiVersion: v1
kind: Service
metadata:
  name: flaskapp-service
spec:
  selector:
    app: flaskapp
  ports:
  - protocol: "TCP"
    port: 8080
    targetPort: 5000
  type: LoadBalancer


apiVersion: apps/v1
kind: Deployment
metadata:
  name: flaskapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: flaskapp
  template:
    metadata:
      labels:
        app: flaskapp
    spec:
      containers:
      - image: xxxxxxxx/flaskapp:**arm64**
        imagePullPolicy: IfNotPresent
        name: flaskapp
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/arch
                operator: In
                values:
                - arm64
-- Simon Hopkins
kubernetes

1 Answer

2/4/2022

You cannot do it from the same deployment. Create a deployment for each architecture with the right image and affinity rules for each.

For the service side you can have just one. To do that, make sure to add some differentiating labels to the pods for each architecture and omit them from the service definition.

EG:

apiVersion: v1
kind: Service
metadata:
  name: flaskapp-service
spec:
  selector:
    app: flaskapp
  ports:
  - protocol: "TCP"
    port: 8080
    targetPort: 5000
  type: LoadBalancer

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: flaskapp-arm
spec:
  replicas: 2
  selector:
    matchLabels:
      app: flaskapp
      arch: arm
  template:
    metadata:
      labels:
        app: flaskapp
        arch: arm
    spec:
      containers:
      - image: xxxxxxxx/flaskapp:**arm64**
        imagePullPolicy: IfNotPresent
        name: flaskapp
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/arch
                operator: In
                values:
                - arm64
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: flaskapp-amd
spec:
  replicas: 2
  selector:
    matchLabels:
      app: flaskapp
      arch: amd
  template:
    metadata:
      labels:
        app: flaskapp
        arch: amd
    spec:
      containers:
      - image: xxxxxxxx/flaskapp:**amd**
        imagePullPolicy: IfNotPresent
        name: flaskapp
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/arch
                operator: In
                values:
                - amd
-- chicocvenancio
Source: StackOverflow