본문 바로가기
[CKA]

CKA examples 1

by coldhands 2023. 9. 26.
728x90

kodecloud 제공

kubectl 명령어에 익숙해 질 수 있도록 가벼운 예제로 시작

 

 

 

alias 설정 및 자동 완성 적용

kubectl = k

https://kubernetes.io/docs/reference/kubectl/cheatsheet/#kubectl-autocomplete

 

kubectl cli 명령어 활용 방법

https://kubernetes.io/docs/reference/kubectl/cheatsheet/#interacting-with-running-pods

 

 

Q. 1

Deploy a pod named nginx-podusing the nginx:alpine image.

k run nginx-pod --image nginx:alpine

pod 생성은 run 명령어로 가능하다

pod 이름과 주어진 이미지로 명령어를 통해 간단하게 pod를 생성할 수 있다.

 

 

 

Q. 2

Deploy a messaging pod using the redis:alpine image with the labels set to tier=msg.

k run messaging --image redis:alpine --label tier=msg

생성되는 pod에 --label을 통해서 label을 붙일 수 있다.

 

 

 

Q. 3

Create a namespace named apx-x9984574.

k create ns apx-x9984574

네임스페이스도 yaml 대신에, 간단히 command line으로 생성 가능하다.

 

 

 

Q. 4

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

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

-o json (output json) 을 통해 node의 정보를 출력하고 그 정보를 /opt/outputs/nodes-z3444kd9.json 에 저장한다

 

 

 

Q. 5

Create a service messaging-service to expose the messagingapplication within the cluster on port 6379.

Use imperative commands.

k expose po messaging --name messaging-service --port 6379

생성되어 있는 pod messaging에 연결할 서비스를 생성한다.

이름이 messaging-service인 서비스를 생성하고, 서비스의 포트는 6379,

--type을 지정하지 않으면 ClusterIP 서비스가 생성된다.

 

 

 

Q. 6

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

k create deploy hr-web-app --image kodekloud/webapp-color --replicas 2

deployment는 create로 생성할 수 있다.

--replicas를 통해 복제본 개수를 지정할 수 있다.

 

 

 

Q. 7

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

k run statc-busybox --image busybox --dry-run=client -o yaml command -- sleep 1000 > /etc/kubernetes/manifest/static-busybox-pod.yaml

static pod는 항상 동작하고 있는 pod이다. 강제로 지워도 /etc/kubernetes/manifest/ 아래에 yaml이 존재한다면, 그 속성대로 동작하려고 한다.

controlplane에 동작하기를 원하기 때문에 controlplane에 접속하여 작업을 진행한다

ssh controlplane

--dry-run=client -o yaml 을 통해 출력된 yaml 형식을 /etc/kubernetes/manifest/static-busybox-pod.yaml 로 저장한다.

 

 

 

Q. 8

Create a POD in the financenamespace named temp-bus with the image redis:alpine.

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

pod를 생성할 때 네임스페이스를 지정할 수 있다. (생성되어 있는 네임스페이스)

 

 

 

Q. 9

A new application orange is deployed. There is something wrong with it. Identify and fix the issue.

k describe po orange
k logs orange init-myservice(컨테이너 이름)
k edit po orange >> :wq  >> :q!
k replace --force -f /tmp/kubectl-edit-xxxxx.yaml

kubectl describe를 통해 pod의 상세정보를 살펴볼 수 있다.

컨테이너의 log를 보기 위해서, container의 이름을 확인하고

kubectl logs **pod이름** **container이름** 을 통해 pod안의 container의 log를 확인한다.

잘못된 정보를 고치고, 저장한 후에 출력된 메시지의 경로에 있는 yaml을 apply한다.

(edit으로 직접 정보를 고쳤을 때, 바로 적용될 때도 있고, 그렇지 못할 때는, /tmp 아래에 kubectl-edit-xxxx.yaml로 저장되는데, 그 경우에는 -f 옵션을 추가하여 강제로 적용할 수 있다.

 

 

 

Q. 10

Expose the hr-web-app as service hr-web-app-service application on port 30082 on the nodes on the cluster.

The web application listens on port 8080.

k expose deploy hr-web-app --name hr-web-app-service --port 8080 --type NodePort
k edit svc hr-web-app-service
nodeport: 30082

deployment에 service를 연결할 수 있다.

Nodeport의 포트를 command로 지정할 수 없기 때문에, service를 배포하고, edit을 통해 수정해 준다.

 

 

 

Q. 11

Use JSON PATH query to retrieve the osImages of all the nodes and store it in a file /opt/outputs/nodes_os_x43kj56.txt.

The osImages are under the nodeInfo section under status of each node.

k get no -o json
k get no -o jsonpath='{.items[*].status.nodeInfo.osImage}' > /opt/outputs/nodes_os_x43kj56.txt

jsonpath를 통해 원하는 정보만 출력할 수 있다.

 

 

 

Q. 12

Create a Persistent Volume with the given specification:

Volume name: pv-analytics

Storage: 100Mi

Access mode: ReadWriteMany

Host path: /pv/data-analytics

pv는 command로 생성할 수 없고, yaml을 작성하여 만들 수 있다.

vi pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-analytics
spec:
  capacity:
    storage: 100Mi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: /pv/data-analytics

k create -f pv.yaml
728x90

'[CKA]' 카테고리의 다른 글

CKA 시험 환경 준비  (49) 2023.09.22

댓글