본문 바로가기
Kubernetes/Cert

[CKA] Pod 유형

by lumination 2025. 2. 7.

1) DaemonSet 문제

문제)

Ensure a single instance of pod nginx is running on each node of the Kubernetes cluster where nginx also represents the Image name which has to be used.

Do not override any taints currently in place. Use DaemonSet to complete this task and use ds-kusc00201 as DaemonSet name.

 

답)

# 명령어
cat <<EOF | kubectl apply -f -
pipe heredoc> apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: ds-kusc00201
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
pipe heredoc> EOF

# 결과
daemonset.apps/ds-kusc00201 created

 

2) rolling update & undo rollback 문제

문제)

Create a deployment as follows:

  • Name: nginx-app
  • Using container nginx with version 1.11.10-alpine
    The deployment should contain 3 replicas
  • Next, deploy the application with new version 1.11.13-alpine, by performing a rolling update.
  • Finally, rollback that update to the previous version 1.11.10-alpine.

답)

# Name: nginx-app
# Using container nginx with version 1.11.10-alpine
# The deployment should contain 3 replicas
kubectl create deployments nginx-app --image=nginx:1.11.10-alpine --replicas=3

# Next, deploy the application with new version 1.11.13-alpine, by performing a rolling update.
kubectl set image deployments/nginx-app nginx=nginx:1.11.13-alpine

# Finally, rollback that update to the previous version 1.11.10-alpine.
kubectl rollout undo deployments/nginx-app

# check
kubectl get pod -o yaml | grep image

 

3) Deployment 생성 문제

문제)

Create a deployment spec file that will:

  • Launch 7 replicas of the nginx Image with the label app_runtime_stage=dev
  • deployment name: kual00201
  • When you are done, clean up (delete) any new Kubernetes API object that you produced during this task.
  • Save a copy of this spec file to /opt/KUAL00201/spec_deployment.yaml (or /opt/KUAL00201/spec_deployment.json).

답)

# nginx deployment 생성

kubectl create deployment kual00201 --image=nginx

 

# 생성한 deployment 기반으로 yaml 수정한다.

kubectl get deployment kual00201 -o yaml > /opt/KUAL00201/spec_deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kual00201
spec:
  replicas: 7
  selector:
    matchLabels:
      app_runtime_stage: dev
  template:
    metadata:
      labels:
        app_runtime_stage: dev
    spec:
      containers:
      - name: nginx
        image: nginx

 

# 리소스 정리

kubectl delete deployment kual00201

 

4) Pod에 여러 개의 컨테이너 생성 문제

문제)

Create a pod named kucc8 with a single app container for each of the following images running inside(there may be between 1 and 4 images specified): nginx + redis + memcached.

 

답)

# nginx + redis + memcached 가 포함된 Pod를 생성
cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: kucc8
spec:
  containers:
  - name: nginx
    image: nginx
  - name: redis
    image: redis
  - name: memcached
    image: memcached
EOF

 

5) 멀티 컨테이너 Pod에 각기 다른 env 설정 문제

문제)

답)

apiVersion: v1
kind: Pod
metadata:
  name: multi-env
spec:
  containers:
  - name: redis
    image: redis
    env:
      - name: GREETING1
        value: "Hello from 1"
  - name: memcached
    image: memcached
    env:
      - name: GREETING2
        value: "Hello from 2"

 

# 확인
kubectl exec multi-env -c memcached -- printenv
GREETING1=Hello from 1

# 확인
kubectl exec multi-env -c redis -- printenv
GREETING2=Hello from 2

 

 

6) Deployment 파드 스케일링 문제

문제)

Scale the deployment webserver to 6 pods.

 

답)

# webserver 1개 생성
kubectl create deployment webserver --image=nginx

# 스케일 6개
kubectl scale deployment webserver --replicas=6

# 또는 수정
kubectl edit deployment webserver
에서 replicas 6개로 수정

 

7) static Pods 문제

문제

Configure the kubelet systemd-managed service, on the node labelled with name=wk8s-node-1, to launch a pod containing a single container of Image automatically.

 

풀이

# staticPodPath 경로 설정을 확인
vim /var/lib/kubelet/config.yaml

# 파일 생성
vim /etc/kubernetes/manifests/pod.yaml
# 파일 내용
apiVersion: v1
kind: Pod
metadata:
  name: webtool
spec:
  containers:
  - name: webtool
    image: httpd
# Run this command on the node where the kubelet is running
systemctl restart kubelet
# 결과 확인
kubectl get pod


# 라벨이 있는 노드 찾기
kubectl get node --show-labels | grep "wk8s-node-1"
wk8s-node-1   Ready   <role>   <age>   <version>   name=wk8s-node-1

 

8) initContainers 문제

문제)

Perform the following tasks:

  • Add an init container to hungry-bear (which has been defined in spec file /opt/KUCC00108/pod-spec-KUCC00108.yaml)
  • The init container should create an empty file named /workdir/calm.txt
  • If /workdir/calm.txt is not detected, the pod should exit
  • Once the spec file has been updated with the init container definition, the pod should be created.

답)

apiVersion: v1
kind: Pod
metadata:
  name: hungry-bear
spec:
  volumes:
    - name: workdir
      emptyDir:
  containers:
  - name: checker
    image: alpine
    command: ["/bin/sh", "-c", "if [ -f /workdir/calm.txt ]; then sleep 100000; else exit 1; fi"]
    volumeMounts:
    - name: workdir
      mountPath: /workdir
  initContainers:
  - name: create
    image: alpine
    command: ["/bin/sh", "-c", "touch /workdir/calm.txt"]
    volumeMounts:
    - name: workdir
      mountPath: /workdir
      
      
# 호스트 패스
  volumes:
    - name: workdir
      hostPath:
        path: /tmp  # 호스트 머신의 /tmp 경로를 사용
        type: Directory  # 디렉토리 타입으로 설정 (파일도 가능)

 

 

문제

Create a DaemonSet that configures Nodes
In K8s DaemonSets are often used to configure certain things on Nodes.
Create a DaemonSet named configurator , it should:

  • be in Namespace configurator
  • use image bash
  • mount /configurator as HostPath volume on the Node it's running on
  • write aba997ac-1c89-4d64 into file /configurator/config on its Node via the command: section
  • be kept running using sleep 1d or similar after the file write command

There are no taints on any Nodes which means no tolerations are needed.

apiVersion: apps/v1
kind: DaemonSet
metadata:
  namec: configurator
  namespace: configurator
spec:
  selector:
    matchLabels:
      name: configurator
  template:
    metadata:
      labels:
        name: configurator
    spec:
      containers:
      - name: configurator
        image: bash
        command: ["/bin/sh", "-c", "touch /configurator/config/aba997ac-1c89-4d64; while true; do sleep 10000000; done"]
        volumeMounts:
        - name: varlog
          mountPath: /configurator
      volumes:
      - name: varlog
        hostPath:
          path: /configurator

 

'Kubernetes > Cert' 카테고리의 다른 글

[CKA] PV 생성  (0) 2025.02.11
[CKA] Node 유형  (0) 2025.02.10
[CKA] 스토리지 유형  (0) 2025.02.10
[CKA] Service 유형  (0) 2025.02.09
[CKA] Apiserver Crash  (0) 2025.02.03