Udemy Labs - Certified Kubernetes Application Developer - Mock Exam - 2 문제 풀이

hyereen·2025년 1월 30일

Kubernetes

목록 보기
36/53

1
Create a deployment called my-webapp with image: nginx, label tier:frontend and 2 replicas. Expose the deployment as a NodePort service with name front-end-service , port: 80 and NodePort: 30083

참고

kubectl create deployment my-webapp --image=nginx --replicas=2 --dry-run=client -oyaml > 1.yaml

controlplane ~ ➜  cat 1.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    tier: frontend
  name: my-webapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-webapp
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: my-webapp
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

서비스 만들기

kubectl expose deployment my-webapp --name front-end-service --type NodePort --port 80 --dry-run=client -oyaml > front-end-service.yaml

위에서 나온 야믈파일에 nodePort: 30083 추가하기

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    tier: frontend
  name: front-end-service
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30083 # 추가해주기 
  selector:
    app: my-webapp
  type: NodePort
status:
  loadBalancer: {}

2
Add a taint to the node node01 of the cluster. Use the specification below:
key: app_type, value: alpha and effect: NoSchedule
Create a pod called alpha, image: redis with toleration to node01.

참고

정답

  • node에 taint 추가하기
 k taint nodes node01 app_type=alpha:NoSchedule
  • 파드 만들기
controlplane ~ ➜  cat 2.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: alpha
spec:
  containers:
  - name: alpha
    image: redis
  tolerations:
  - key: "app_type"
    operator: "Equal"
    value: "alpha"
    effect: "NoSchedule"

3
Apply a label app_type=beta to node controlplane. Create a new deployment called beta-apps with image: nginx and replicas: 3. Set Node Affinity to the deployment to place the PODs on controlplane only.
NodeAffinity: requiredDuringSchedulingIgnoredDuringExecution

정답

  • node에 label 붙이기
kubectl label node controlplane app_type=beta
  • deploy 만들기
controlplane ~ ➜  cat 3.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: beta-apps
  name: beta-apps
spec:
  replicas: 3
  selector:
    matchLabels:
      app: beta-apps
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: beta-apps
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
      affinity:
       nodeAffinity:
         requiredDuringSchedulingIgnoredDuringExecution:
           nodeSelectorTerms:
            - matchExpressions:
              - key: app_type
                operator: In
                values:
                - beta
status: {}

4
Create a new Ingress Resource for the service my-video-service to be made available at the URL: http://ckad-mock-exam-solution.com:30093/video.
To create an ingress resource, the following details are: -
annotation: nginx.ingress.kubernetes.io/rewrite-target: /
host: ckad-mock-exam-solution.com
path: /video
Once set up, the curl test of the URL from the nodes should be successful: HTTP 200

정답

  • svc port확인
controlplane ~ ➜  k get svc
NAME                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
front-end-service   NodePort    172.20.53.217    <none>        80:31872/TCP   43m
kubernetes          ClusterIP   172.20.0.1       <none>        443/TCP        58m
my-video-service    ClusterIP   172.20.220.124   <none>        8080/TCP       30m
  • ingress 만들기
controlplane ~ ➜  cat 4.yaml 
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-video-service-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: ckad-mock-exam-solution.com
    http:
      paths:
      - path: /video
        pathType: Prefix
        backend:
          service:
            name: my-video-service
            port:
              number: 8080

5
We have deployed a new pod called pod-with-rprobe. This Pod has an initial delay before it is Ready. Update the newly created pod pod-with-rprobe with a readinessProbe using the given spec
httpGet path: /ready
httpGet port: 8080

정답

controlplane ~ ➜  cat 5_md.yaml 
apiVersion: v1
kind: Pod
metadata:
  annotations:
    cni.projectcalico.org/containerID: 0cdf8c8554bfa08ff54e4da218e07f50fc846825ae170638c174afb409625404
    cni.projectcalico.org/podIP: 172.17.0.11/32
    cni.projectcalico.org/podIPs: 172.17.0.11/32
  creationTimestamp: "2025-01-30T16:04:51Z"
  labels:
    name: pod-with-rprobe
  name: pod-with-rprobe
  namespace: default
  resourceVersion: "3712"
  uid: b8ad84f1-8196-4194-84f2-d53ece877b5f
spec:
  containers:
  - env:
    - name: APP_START_DELAY
      value: "180"
    image: kodekloud/webapp-delayed-start
    imagePullPolicy: Always
    name: pod-with-rprobe
    readinessProbe:
      httpGet:
        path: /ready
        port: 8080
    ports:
    - containerPort: 8080
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-prd7d
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: controlplane
  preemptionPolicy: PreemptLowerPriority
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: kube-api-access-prd7d
    projected:
      defaultMode: 420
      sources:
      - serviceAccountToken:
          expirationSeconds: 3607
          path: token
      - configMap:
          items:
          - key: ca.crt
            path: ca.crt
          name: kube-root-ca.crt
      - downwardAPI:
          items:
          - fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
            path: namespace
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2025-01-30T16:04:56Z"
    status: "True"
    type: PodReadyToStartContainers
  - lastProbeTime: null
    lastTransitionTime: "2025-01-30T16:04:51Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2025-01-30T16:04:56Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2025-01-30T16:04:56Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2025-01-30T16:04:51Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: containerd://09a63bcfe817a00e620cbbf35f19c4f185b39f5e31c2f8751882a0b15a943369
    image: docker.io/kodekloud/webapp-delayed-start:latest
    imageID: docker.io/kodekloud/webapp-delayed-start@sha256:666b95c2ef8e00a74fa0ea96def8d9d69104ec5d9b9b0f49d8a76bd4c94ad343
    lastState: {}
    name: pod-with-rprobe
    ready: true
    restartCount: 0
    started: true
    state:
      running:
        startedAt: "2025-01-30T16:04:55Z"
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-prd7d
      readOnly: true
      recursiveReadOnly: Disabled
  hostIP: 192.168.28.7
  hostIPs:
  - ip: 192.168.28.7
  phase: Running
  podIP: 172.17.0.11
  podIPs:
  - ip: 172.17.0.11
  qosClass: BestEffort
  startTime: "2025-01-30T16:04:51Z"

6
Create a new pod called nginx1401 in the default namespace with the image nginx. Add a livenessProbe to the container to restart it if the command ls /var/www/html/probe fails. This check should start after a delay of 10 seconds and run every 60 seconds.
You may delete and recreate the object. Ignore the warnings from the probe.

정답

controlplane ~ ➜  cat 6.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx1401
spec:
  containers:
  - name: nginx
    image: nginx
    livenessProbe:
      exec:
        command:
        - ls
        - /var/www/html/probe
      initialDelaySeconds: 10
      periodSeconds: 60

7
Create a job called whalesay with image busybox and command echo "cowsay I am going to ace CKAD!".
completions: 10
backoffLimit: 6
restartPolicy: Never
This simple job runs the popular cowsay game that was modifed by docker…

참고

풀이

  • sh: 쉘(shell)을 실행하는 명령어
  • -c: 쉘에게 인라인으로 실행할 명령을 넘겨주는 방식
    • 예를 들어, sh -c "echo 'Hello' "는 sh가 "Hello"라는 텍스트를 echo 명령을 통해 출력하게 만듦
  • sh -c "echo 'cowsay I am going to ace CKAD!'"는 sh 쉘을 통해 echo 명령을 실행하고, 그 안에서 "cowsay I am going to ace CKAD!"라는 메시지를 출력하게 됨

정답

apiVersion: batch/v1
kind: Job
metadata:
  name: whalesay
spec:
  completions: 10
  backoffLimit: 6
  template:
    metadata:
      creationTimestamp: null
    spec:
      containers:
      - name: busybox-cowsay
        image: busybox
        command:
        - sh
        - -c
        - "echo 'cowsay I am going to ace CKAD!'"
      restartPolicy: Never

8
Create a pod called multi-pod with two containers.
Container 1:
name: jupiter, image: nginx
Container 2:
name: europa, image: busybox
command: sleep 4800
Environment Variables:
Container 1:
type: planet
Container 2:
type: moon

정답


controlplane ~ ➜  cat 8.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: multi-pod
spec:
  containers:
  - name: jupiter
    image: nginx
    env:
    - name: type
      value: "planet"
  - name: europa
    image: busybox
    env:
    - name: type
      value: "moon"
    command:
      - sleep
    args:
      - "4800"

9
Create a PersistentVolume called custom-volume with size: 50MiB reclaim policy:retain, Access Modes: ReadWriteMany and hostPath: /opt/data

  • size 쓸 때 단위 주의! 문제 그대로 MiB라고 적으면 아래와 같은 에러가 난다 -> Mi라고 적어야 함
Error from server (BadRequest): error when creating "99.yaml": PersistentVolume in version "v1" cannot be handled as a PersistentVolume: quantities must match the regular expression '^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$'

정답

apiVersion: v1
kind: PersistentVolume
metadata:
  name: custom-volume
spec:
  capacity:
    storage: 50Mi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: "/opt/data"

0개의 댓글