[K8S] CKA - mock exam #1

hyunboo95·2023년 5월 28일

0. setup env.

k8s 공식 문서 > kubectl cheat sheet에서 autocomplete 설정

# 시험 시작 전 초기 설정.
source <(kubectl completion bash) # bash-completion 패키지를 먼저 설치한 후, bash의 자동 완성을 현재 셸에 설정한다
echo "source <(kubectl completion bash)" >> ~/.bashrc # 자동 완성을 bash 셸에 영구적으로 추가한다

alias k=kubectl
complete -o default -F __start_kubectl k

1. deploy pod

Deploy a pod named nginx-pod using the nginx:alpine image.
Once done, click on the Next Question button in the top right corner of this panel. You may navigate back and forth freely between all questions. Once done with all questions, click on End Exam. Your work will be validated at the end and score shown. Good Luck!
Name: nginx-pod
Image: nginx:alpine

k run nginx-pod --image=nginx:alpine
# 명령어 수행 후, 항상 검증하기!
k get pod
k describe pod nginx-pod

2. deploy pod with labels

eploy a messaging pod using the redis:alpine image with the labels set to tier=msg.
Pod Name: messaging
Image: redis:alpine
Labels: tier=msg

# labels 설정에 대해서 확인하려면,
k run --help

k run messaging --image=redis:alpine --labels="tier=msg"
k describe pod messaging | grep -i labels

3. create Namespace

Create a namespace named apx-x9984574.

k create ns apx-x9984574
k get ns

4. get list of nodes in JSON format

Get the list of nodes in JSON format and store it in a file at /opt/outputs/nodes-z3444kd9.json.

k get nodes -o json > /opt/outputs/nodes-z3444kd9.json
cat /opt/outputs/nodes-z3444kd9.json

5. create a service to expose app

message-service라는 서비스를 만들어서 messging app을 expose
Create a service messaging-service to expose the messaging application within the cluster on port 6379.
Use imperative commands.
Service: messaging-service
Port: 6379
Type: ClusterIp
Use the right labels

k expose --help

k expose pod messaging --port=6379 --name=messaging-service
k get svc
k describe svc messaging-service

# endpoint를 확인하자.
k get pods -o wide

# messaging pod의 IP와 동일한지 확인.

6. create a deployment

Create a deployment named hr-web-app using the image kodekloud/webapp-color with 2 replicas.
Name: hr-web-app
Image: kodekloud/webapp-color
Replicas: 2

k create deployment hr-web-app --image kodekloud/webapp-color --replicas 2
k get deployment
k describe deployment hr-web-app

7. create a static pod

Create a static pod named static-busybox on the controlplane node that uses the busybox image and the command sleep 1000.

# 1. yaml 파일을 직접 작성
# static pod를 실행할 노드를 선택한다.
ssh [노드 명]

# /etc/kubernetes/manifests 로 이동하여 해당 경로에 static pod의 yaml 파일 작성.
cd /etc/kubernetes/manifests
vi static-pod.yaml

apiVersion: v1
kind: Pod
  name: static-busybox
    - name: static-busybox
      image: busybox
      command: ["sleep","1000"]
 k apply -f static-pod.yaml
 # 2. dry-run을 통한 방법
 k run static-busybox --image busybox --dry-run=client -o yaml --command -- sleep 1000 > static-pod.yaml
 mv static-pod.yaml /etc/kubernetes/manifest
 # static pod 생성 확인
 k get pod

8. create pod in specific namespace

create a POD in the finance namespace named temp-bus with the image redis:alpine

k run temp-bus -n finance --image redis:alpine
k get pod -n finance

9. troubleshooting for POD

a new app orange is deployed. there is something wrong with it. Identify and fix the issue

k get pods

# Init:CrashLoopBackoff 상태 -> pod에 문제가 있다 / init 컨테이너에 문제가 있다
k describe pod orange

# init 컨테이너의 command에 문제가 있음
k edit pod orange
k replace --force -f /tmp~~~~.yaml
k get pod --watch

10. expose deployment

expose the hr-web-app as svc hr-web-app-service app on port 30082 on the nodes on the cluster
the web app listens on port 8080

# 현재 deployment 확인
k get deploy
k expose deploy hr-web-app --name hr-web-app-service --type NodePort --port 8080 

# 생성 후 확인
k get svc
k describe svc hr-web-app-service

# 두 개의 endpoints 있는 지 확인

# edit으로 nodePort 추가 spec.ports.nodePort: 30082

11. JSON path 파일 이용해 모든 노드의 os 이미지 저장

# kubectl cheet sheet에 jsonpath 참고
k get nodes -o jsonpath='{.items[*].status.nodeInfo.osImage}' > {파일경로}

12. create PV

create a persistent volume with the given specification
name: pv-analystics
storage: 100Mi
access mode: RWM
Host Path: /pv/data-analystics

# search persistent volume
# copy template

vi pv.yaml

apiVersion: v1
kind: PersistentVolume
  name: pv-analystics
    storage: 100Mi
    - ReadWriteMany
  	path: /pv/data-analystics
k create -f pv.yaml
k get pv
k describe pv pv-analytics

0개의 댓글