CKAD - Lightning LAB-2 - solution

choong·2023년 1월 18일
0

CKAD exam

목록 보기
2/2

CKAD - Lightning lab-2의 풀이

📖문제 1번

We have deployed a few pods in this cluster in various namespaces. Inspect them and identify the pod which is not in a Ready state. Troubleshoot and fix the issue.

Next, add a check to restart the container on the same pod if the command ls /var/www/html/file_check 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.

✍문제 1번 - 풀이

Ready 상태가 아닌 pod를 찾아 문제를 해결하여라
ls /var/www/html/file_check 명령어가 fail 할 때 container를 재시작하는 것을 추가하여라.
이 체크는 지연이 10초 지난 후에 시작하고 60초 마다 실행된다.

ReadinessProbe - Pod가 사용 불가 상태일 경우 서비스 목록에서 제외
LivenessProbe - 컨테이너의 상태가 비정상일 경우 해당 Pod 재시작

해당 문제에서 요구하는 설정은 재시작하는 설정이므로 LivenessProbe 밑에 해당 내용을 추가하여야 한다.

먼저 Ready 상태가 아닌 pod를 찾아보도록 하겠습니다.

k get pods -A
namespacepodreadystatus
dev1401nginx14010/1Running
k edit pod -n dev1401 nginx1401

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: nginx
  name: nginx1401
  namespace: dev1401
spec:
  containers:
  - image: kodekloud/nginx
    imagePullPolicy: IfNotPresent
    name: nginx
    ports:
    - containerPort: 9080
      protocol: TCP
    readinessProbe:
      failureThreshold: 3
      httpGet:
        path: /
        port: 8080
        scheme: HTTP
      periodSeconds: 10
      successThreshold: 1
      timeoutSeconds: 1

중간에 필요없는 정보는 생략하였다. nginx1401의 정보를 불러오면 다음과 같은 yaml파일이 확인이 된다.
우리는 여기에 문제에서 말한 정보를 추가함과 동시에 troubleshooting을 하면 된다.

먼저 containerPort는 9080으로 설정되어 있는데 readinessProbe에는 8080을 확인하고 있으므로 9080으로 수정해주도록한다.
이후 readinessProbe와 같은 라인에 livenessProbe를 적어주고 문제에서 말했던 내용을 아래와 같이 추가해주면 된다.

참고로 Readiness/Liveness Probe의 옵션은 다음과 같다

옵션설명
initialDelaySeconds(default : 0)최초 Probe 전의 지연 시간
periodSeconds(default : 10)Probe를 체크하는 시간 간격
timeoutSeconds(efault : 1)결과를 대기하는 시간까지의 시간
successThreshold(default : 1)몇번의 성공 결과를 수신해야 성공으로 인정하는지
failureThreshold(default : 3)몇번의 실패 결과를 수신해야 실패하고 인정하는지
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: nginx
  name: nginx1401
  namespace: dev1401
spec:
  containers:
  - image: kodekloud/nginx
    imagePullPolicy: IfNotPresent
    name: nginx
    ports:
    - containerPort: 9080
      protocol: TCP
    readinessProbe:
      failureThreshold: 3
      httpGet:
        path: /
        port: 9080    
        scheme: HTTP
      periodSeconds: 10
      successThreshold: 1
      timeoutSeconds: 1
    livenessProbe:
      exec:
        command:
        - ls
        - /var/www/html/file_check
      initialDelaySeconds: 10
      periodSeconds: 60

📖문제 2번

Create a cronjob called dice that runs every one minute. Use the Pod template located at /root/throw-a-dice. The image throw-dice randomly returns a value between 1 and 6. The result of 6 is considered success and all others are failure.
The job should be non-parallel and complete the task once. Use a backoffLimit of 25.
If the task is not completed within 20 seconds the job should fail and pods should be terminated.

You don't have to wait for the job completion. As long as the cronjob has been created as per the requirements.

✍문제 2번 - 풀이

dice라는 이름의 1분마다 실행되는 cronjob을 만든다.
pod template은 /root/throw-a-dice 밑에 있다.
throw-dice라는 image는 1에서 6사이의 값을 랜덤하게 돌려준다.
6은 성공이고 나머지는 실패다.
job은 비병렬이며 한번만 완료한다. backoffLimit을 25로 설정한다.
만약 작업이 20초 이내에 완료되지 않으면 작업은 실패하고 pod가 종료되어야 한다.

apiVersion: batch/v1
kind: CronJob
metadata:
  name: dice
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      completions: 1
      backoffLimit: 25 # This is so the job does not quit before it succeeds.
      activeDeadlineSeconds: 20
      template:
        spec:
          containers:
          - name: dice
            image: kodekloud/throw-dice
          restartPolicy: Never

schedule의 경우 1분마다 실행하고 싶으면 */1라고 적으면 된다.
job은 한번만 완료할 것이기 때문에 completions: 1 옵션을 넣어준다.
backoffLimit 옵션은 25로 설정해준다.
activeDeadlineSeconds는 20초로 설정해준다.
밑에 template에는 실행될 pod의 정보를 적어주는데 image는 /root/throw-a-dice/throw-a-dice.yaml 파일을 참조한다.

📖문제 3번

Create a pod called my-busybox in the dev2406 namespace using the busybox image. The container should be called secret and should sleep for 3600 seconds.

The container should mount a read-only secret volume called secret-volume at the path /etc/secret-volume. The secret being mounted has already been created for you and is called dotfile-secret.

Make sure that the pod is scheduled on controlplane and no other node in the cluster.

✍문제 3번 - 풀이

dev2406이라는 namespace 안에 my-busybox pod를 busybox image를 사용하여 생성한다. container 이름은 secret이며 3600초 안에 sleep해야한다.
container는 path:/etc/secret-volume - name:secret-volume - read-only secret volume에 mount된다.
secret은 이미 dotfile-secret이라는 이름으로 만들어져있다.
pod는 controlplane에 생성되어야한다.

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: my-busybox
  name: my-busybox
  namespace: dev2406
spec:
  volumes:
  - name: secret-volume
    secret:
      secretName: dotfile-secret
  nodeSelector:
    kubernetes.io/hostname: controlplane
  tolerations:
  - key: "node-role.kubernetes.io/control-plane"
    operator: "Exists"
    effect: "NoSchedule"
  containers:
  - command:
    - sleep
    args:
    - "3600"
    image: busybox
    name: secret
    volumeMounts:
    - name: secret-volume
      readOnly: true
      mountPath: "/etc/secret-volume"

위에 부터 pod의 name, namespace, volumes name, secret name, nodeSelector는 k get nodes --show-labels하여 controlplane만 가지고 있는 labels 중 하나를 적어넣고 tolerations 또한 controlplane의 정보를 적어준다. 밑에는 sleep 3600, busybox, container name, volumeMounts 정보를 문제에서 요구한대로 적어주면 된다.

📖문제 4번

Create a single ingress resource called ingress-vh-routing. The resource should route HTTP traffic to multiple hostnames as specified below:

The service video-service should be accessible on http://watch.ecom-store.com:30093/video

The service apparels-service should be accessible on http://apparels.ecom-store.com:30093/wear

Here 30093 is the port used by the Ingress Controller

✍문제 4번 - 풀이

ingress-vh-routing이라 불리는 ingress resource를 생성한다. 이 resource는 HTTP traffic을 아래의 설정과 같이 여러 hostname으로 route 시킨다.
service name : video-service, host : watch.ecom-store.com, path: /video, port: video-service의 port는 8080이므로 8080
service name : apparels-service, host : apparels.ecom-store.com, path: /wear, port: 마찬가지로 apparels-service의 port는 8080이므로 8080으로 설정해주면 된다.

kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
  name: ingress-vh-routing
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: watch.ecom-store.com
    http:
      paths:
      - pathType: Prefix
        path: "/video"
        backend:
          service:
            name: video-service
            port:
              number: 8080
  - host: apparels.ecom-store.com
    http:
      paths:
      - pathType: Prefix
        path: "/wear"
        backend:
          service:
            name: apparels-service
            port:
              number: 8080

📖문제 5번

A pod called dev-pod-dind-878516 has been deployed in the default namespace. Inspect the logs for the container called log-x and redirect the warnings to /opt/dind-878516_logs.txt on the controlplane node

✍문제 5번 - 풀이

dev-pod-dind-878516 pod는 default namespace에 배포되어있다. log-x라는 container의 log를 확인하고 warning에 관련된 정보를 controlplane node의 /opt/dind-878516_logs.txt로 redirect 시킨다.

Run the command:
kubectl logs dev-pod-dind-878516 -c log-x | grep WARNING > /opt/dind-878516_logs.txt
profile
choong

0개의 댓글