[CKA] Section3. Scheduling

๐Ÿ“ŒScheduling

1. manual scheduling

scheduler๊ฐ€ ์—†์„ ๋•Œ ๋‚˜ํƒ€๋‚˜๋Š” ์ƒํƒœ๊ฐ’

components

nodeํ• ๋‹น์•ˆ๋œ pod์— nodeName์œผ๋กœ node ํ• ๋‹นํ•˜๊ธฐ

Delete the existing pod first. Run the below command:

$ kubectl delete po nginx

To list and know the names of available nodes on the cluster:

$ kubectl get nodes

Add the nodeName field under the spec section in the nginx.yaml file with node01 as the value:

---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  nodeName: node01
  containers:
  -  image: nginx
     name: nginx

Then run the command kubectl create -f nginx.yaml to create a pod from the definition file.

To check the status of a nginx pod and to know the node name:

$ kubectl get pods -o wide


2. Labels and Selectors

label์„ ์ง€์ •ํ•˜๊ณ  selector๋กœ ๋งค์นญ์‹œํ‚ค๊ธฐ :)

label์ด env=prod์ด๊ณ  tier=frontend์ธ pod ๋‹ค ์กฐํšŒํ•ด์ฃผ์„ธ์š”!

kubectl get po -l env=prod,tier=frontend

3. Taints and Tolerations

toleration์„ ํŒŒ๋“œ์— ์ง€์ •ํ•ด์„œ taint๋ฅผ ๊ฑด node์— toleration ์กฐ๊ฑด์ด ๋ถ€ํ•ฉํ•˜์ง€ ์•Š์œผ๋ฉด ์ ‘๊ทผํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ํ•จ. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ except์˜ ๊ฐœ๋…์ธ ๊ฒƒ ๊ฐ™๋‹ค.

A๋ผ๋Š” ์กฐ๊ฑด์„ ๊ฐ€์ง„ pod๋งŒ taint nodes nodeAAA Aํ•œ nodeAAA์— ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ!

practice test

kubectl run bee --image=nginx --restart=Never --dry-run -o yaml > bee.yaml

kubectl explain pod --recursive | grep -A5 tolerations

4. Node Selectors

ํŠน์ • ๋…ธ๋“œ์— pod ๋„์šฐ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ

๋ผ๋ฒจ์€ ์ด๋ ‡๊ฒŒ ์ง€์ •

kubectl label nodes node-1 size=large

kubectl label node node01 color=blue

pod์— nodeSelector๋Š” spec ์•„๋ž˜์— ์œ„์น˜!

5. Node Affinity

๋…ธ๋“œ ์…€๋ ‰ํ„ฐ์™€ ๋น„์Šทํ•˜๊ฒŒ ํŠน์ • ๋…ธ๋“œ์— pod๋ฅผ ์ง€์ •ํ•ด์„œ ๋„์šฐ๊ณ  ์‹ถ์€๋ฐ, ๊ทธ๋Ÿฐ ์กฐ๊ฑด์„ ์กฐ๊ธˆ ๋” ์ž์œ ๋กญ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

requiredDuringSchedulingIgnoredDuringExecution ์™€ preferredDuringSchedulingIgnoredDuringExecution ๋กœ ๋ถ€๋ฅด๋Š” ๋‘ ๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ๋…ธ๋“œ ์–ดํ”ผ๋‹ˆํ‹ฐ ๊ทธ๋ฆฌ๊ณ  requiredDuringSchedulingRequiredDuringExecution๊นŒ์ง€!

DuringScheduling ์ƒํƒœ์™€ DuringExecution ์ƒํƒœ์— ๋”ฐ๋ผ, ๋‹ค๋ฅธ ๊ฒƒ ๊ฐ™์Œ.

๋”ฐ๋ผ์„œ requiredDuringSchedulingIgnoredDuringExecution ์˜ ์˜ˆ๋กœ๋Š” โ€œ์ธํ…” CPU๊ฐ€ ์žˆ๋Š” ๋…ธ๋“œ์—์„œ๋งŒ ํŒŒ๋“œ ์‹คํ–‰โ€์ด ๋  ์ˆ˜ ์žˆ๊ณ , preferredDuringSchedulingIgnoredDuringExecution ์˜ ์˜ˆ๋กœ๋Š” โ€œ์žฅ์•  ์กฐ์น˜ ์˜์—ญ XYZ์— ํŒŒ๋“œ ์ง‘ํ•ฉ์„ ์‹คํ–‰ํ•˜๋ ค๊ณ  ํ•˜์ง€๋งŒ, ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ๋‹ค๋ฅธ ๊ณณ์—์„œ ์ผ๋ถ€๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ํ—ˆ์šฉโ€์ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.

์ด๋ ‡๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค :)

Q. Set Node Affinity to the deployment to place the pods on node01 only.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: blue
spec:
  replicas: 3
  selector:
    matchLabels:
      run: nginx
  template:
    metadata:
      labels:
        run: nginx
    spec:
      containers:
      - image: nginx
        imagePullPolicy: Always
        name: nginx
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: color
                operator: In
                values:
                - blue

6. Resource limit

Q. ์–ด๋–ค ๊ฐ’์„ ๋ฐ”๊ฟ”์„œ ํŒŒ๋“œ๋ฅผ ๋„์šฐ์„ธ์š” ๋ผ๋Š” ๋ฌธ์ œ์—์„œ, ์–ด๋–ป๊ฒŒ ์ปค๋งจ๋“œ๋ฅผ ์น˜๋Š”์ง€?

A. ๋‚˜๊ฐ™์€ ๊ฒฝ์šฐ๋Š” kubectl edit ํ•˜๊ณ  yaml ์ €์žฅํ•œ ๋‹ค์Œ์—, applyํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์€ ์–ด๋–ป๊ฒŒ ํ•˜๋Š”์ง€ ๊ถ๊ธˆ!

7. DaemonSets

๋…ธ๋“œ๊ฐ€ ํด๋Ÿฌ์Šคํ„ฐ์— ์ถ”๊ฐ€๋˜๋ฉด ํŒŒ๋“œ๋„ ์ถ”๊ฐ€๋จ!

์šฉ๋„

  • ๋ชจ๋“  ๋…ธ๋“œ์—์„œ ํด๋Ÿฌ์Šคํ„ฐ ์Šคํ† ๋ฆฌ์ง€ ๋ฐ๋ชฌ ์‹คํ–‰
  • ๋ชจ๋“  ๋…ธ๋“œ์—์„œ ๋กœ๊ทธ ์ˆ˜์ง‘ ๋ฐ๋ชฌ ์‹คํ–‰
  • ๋ชจ๋“  ๋…ธ๋“œ์—์„œ ๋…ธ๋“œ ๋ชจ๋‹ˆํ„ฐ๋ง ๋ฐ๋ชฌ ์‹คํ–‰

8. Static Pods

  • ์Šคํƒœํ‹ฑ ํŒŒ๋“œ ๋Š” API ์„œ๋ฒ„ ์—†์ด ํŠน์ • ๋…ธ๋“œ์— ์žˆ๋Š” kubelet ๋ฐ๋ชฌ์— ์˜ํ•ด ์ง์ ‘ ๊ด€๋ฆฌ๋œ๋‹ค. ์ปจํŠธ๋กค ํ”Œ๋ ˆ์ธ์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋Š” ํŒŒ๋“œ(์˜ˆ๋ฅผ ๋“ค์–ด ๋””ํ”Œ๋กœ์ด๋จผํŠธ(Deployment))์™€๋Š” ๋‹ฌ๋ฆฌ, kubelet ์ด ๊ฐ๊ฐ์˜ ์Šคํƒœํ‹ฑ ํŒŒ๋“œ๋ฅผ ๊ฐ์‹œํ•œ๋‹ค. (๋งŒ์•ฝ ์‹คํŒจํ•  ๊ฒฝ์šฐ ๋‹ค์‹œ ๊ตฌ๋™ํ•œ๋‹ค.)
  • ์Šคํƒœํ‹ฑ ํŒŒ๋“œ๋Š” ํ•ญ์ƒ ํŠน์ • ๋…ธ๋“œ์— ์žˆ๋Š” ํ•˜๋‚˜์˜ Kubelet์— ๋งค์—ฌ ์žˆ๋‹ค.
  • Kubelet ์„ ์‹œ์ž‘ํ•˜๋ฉด, ์ •์˜๋œ ๋ชจ๋“  ์Šคํƒœํ‹ฑ ํŒŒ๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ์‹œ์ž‘๋œ๋‹ค

Q. What is the path of the directory holding the static pod definition files?

$ ps -aux | grep kubelet

$ cat /var/lib/kubelet/config.yaml

Q. static pod ์‹๋ณ„

static pod๋ฅผ ์ธ์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด kubectl get pods โ€”all-namespaces์—์„œ -controlplane์ด ๋ถ™์€ ๊ฒƒ์ด๋‹ค (?)

Note

static pod๋ฅผ ๋งŒ๋“œ๋ ค๋ฉด, kubelet์ด ์‚ฌ์šฉํ•˜๋Š” config์œ„์น˜๋ฅผ ๋ด์•ผ ํ•˜๊ณ 

kubelet์˜ config.yaml์—์„œ ์–ด๋”” ์œ„์น˜์ธ์ง€ ๋””๋ ‰ํ† ๋ฆฌ ํ™•์ธ ํ›„(์˜ˆ๋ฅผ ๋“ค์–ด/etc/kubernetes/manifests)

๊ฑฐ๊ธฐ์— yamlํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ฉด, static pod๊ฐ€ ์ž๋™์œผ๋กœ ๋œธ!

์ด๊ฑด pod ํ•œ๋ฒˆ์— command๋กœ ๋„์šฐ๊ณ  static pod directory์— ๊ฐ€์ ธ๋‹ค ๋„ฃ๋Š” ๊ฒƒ! kubectl run --restart=Never --image=busybox static-busybox --dry-run=client -o yaml --command -- sleep 1000 > /etc/kubernetes/manifests/static-busybox.yaml

9. Multiple Schedulers

Q. multiple scheduler ๋งŒ๋“ค๊ธฐ

kubelet์˜ configPath๋กœ ๊ฐ€์„œ scheduler.yaml์„ ๋ณต์‚ฌํ•œ ๋‹ค์Œ์—, ์›ํ•˜๋Š” ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ›„ kubectl create -f custom.yamlํ•ด์ฃผ๊ธฐ!

leader-elect=false์—ฌ์•ผ ํ•˜๊ณ  (custom์ด๋‹ˆ๊นŒ), port๋ž‘ secure-port ๋ณ€๊ฒฝํ•ด์ค˜์•ผ ํ•จ!

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    component: my-scheduler
    tier: control-plane
  name: my-scheduler
  namespace: kube-system
spec:
  containers:
  - command:
    - kube-scheduler
    - --authentication-kubeconfig=/etc/kubernetes/scheduler.conf
    - --authorization-kubeconfig=/etc/kubernetes/scheduler.conf
    - --bind-address=127.0.0.1
    - --kubeconfig=/etc/kubernetes/scheduler.conf
    - --leader-elect=false
    - --port=10282
    - --scheduler-name=my-scheduler
    - --secure-port=0
    image: k8s.gcr.io/kube-scheduler:v1.20.0
    imagePullPolicy: IfNotPresent
    livenessProbe:
      failureThreshold: 8
      httpGet:
        host: 127.0.0.1
        path: /healthz
        port: 10259
        scheme: HTTPS
      initialDelaySeconds: 10
      periodSeconds: 10
      timeoutSeconds: 15
    name: kube-scheduler
    resources:
      requests:
        cpu: 100m
    startupProbe:
      failureThreshold: 24
      httpGet:
        host: 127.0.0.1
        path: /healthz
        port: 10259
        scheme: HTTPS
      initialDelaySeconds: 10
      periodSeconds: 10
      timeoutSeconds: 15
    volumeMounts:
    - mountPath: /etc/kubernetes/scheduler.conf
      name: kubeconfig
      readOnly: true
  hostNetwork: true
  priorityClassName: system-node-critical
  volumes:
  - hostPath:
      path: /etc/kubernetes/scheduler.conf
      type: FileOrCreate
    name: kubeconfig
status: {}

Configuring Kubernetes Scheduler

custom scheduler๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ pod์˜ spec ์•„๋ž˜์— schedulerName์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋!

apiVersion: v1
kind: Pod
metadata:
  name: annotation-default-scheduler
  labels:
    name: multischeduler-example
spec:
  schedulerName: default-scheduler
  containers:
  - name: pod-with-default-annotation-container
    image: k8s.gcr.io/pause:2.0

Written by@heeye
Work for a financial company as a Backend developer & Cloud engineer๐Ÿ˜Œ

GitHub