1.题目

Create a Deployment named deploy-important with label id=very-important (the pods should also have this label) and 3 replicas in namespace dev. It should contain two containers, the first named container1 with image nginx and the second one named container2 with image kubernetes/pause.
There should be only ever one Pod of that Deployment running on one worker node. We have two worker nodes: cluster1-worker1 and cluster1-worker2. Because the Deployment has three replicas the result should be that on both nodes one Pod is running. The third Pod won’t be scheduled, unless a new worker node will be added.

2.解析

本题目利用deployment来实现DaemonSet,考测pod affinity。

3.答案

  1. 先创建deployment.yaml
    1. kubectl create deploy deploy-important -n dev --image=nginx \
    2. --dry-run=client -oyaml > 12.yaml
    编辑该yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    creationTimestamp: null
    labels:
     id: very-important                  # change
    name: deploy-important
    namespace: dev              
    spec:
    replicas: 3                           # change
    selector:
     matchLabels:
       id: very-important                # change
    strategy: {}
    template:
     metadata:
       creationTimestamp: null
       labels:
         id: very-important              # change
     spec:
       containers:
       - image: nginx:1.17.6-alpine
         name: container1                # change
         resources: {}
       - image: kubernetes/pause         # add
         name: container2                # add
       affinity:                                             # add
         podAntiAffinity:                                    # add
           requiredDuringSchedulingIgnoredDuringExecution:   # add
           - labelSelector:                                  # add
               matchExpressions:                             # add
               - key: id                                     # add
                 operator: In                                # add
                 values:                                     # add
                 - very-important                            # add
             topologyKey: kubernetes.io/hostname             # add
    status: {}
    
    kubectl apply -f 12.yaml