본문 바로가기
CKA

[CKAD] 연습문제 정리 5탄!!

by 싱숭생숭늉 2024. 8. 22.

해당 연습문제는 이성미강사님의 강의를 바탕으로 작성했다.

https://fastcampus.co.kr/dev_online_kubemaster

 

정말 좋은 강의이다! 기초부터 탄탄하게 알고 싶다면 보는 것을 적극 추천한다!!

https://www.youtube.com/watch?v=6n5obRKsCRQ&list=PLApuRlvrZKohaBHvXAOhUD-RxD0uQ3z0c

 

문제를 풀때 https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands

해당 경로를 많이 사용하고 많이 참고 했다.

 

시험 시간이 넉넉하지는 않았다 그래서 타이핑 시간과 삭제시간(pod 잘 못 생성시 삭제가 필요)을 줄이기 위해 아래 명령어를 사용했다. (https://peterica.tistory.com/540 피터님이 알려주신 좋은 꿀팁!)

# "kubectl"를 "k"약자로 변경
alias k=kubectl
# yaml파일과 문법이 정상적으로 작동하는 지 확인
export do="--dry-run=client -o yaml" # k create deploy nginx --image=nginx $do
# pod삭제 시 바로 삭제
export now="--force --grace=period 0" # k delete pod nginx $now

 

8. Create Pod

Context:

  • Anytime a team needs to run a container on Kubernetes they will need to define a pod within which to run the container.

Task:

  • Create a YAML formatted pod manifest /data/ckad/tspod.yml to create a pod named ts-app that runs a container named ts-cont using image busybox with these command line arguments: /bin/sh -c "while true; do echo hello; sleep 10; done"
  • Create the pod with the kubectl command using the YAML file created in the previous step.
  • When the pod is running, display summary data about the pod in JSON format using the kubectl command and redirect the output to a file named /data/ckad/ts-app_out.json. All of the files you need to work with have been created, empty, for your convenience. When creating your pod, you do not need to specify a container command, only args.
k run ts-app --image=busybox $do > /data/ckad/tspod.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: ts-app
  name: ts-app
spec:
  containers:
  - image: busybox
    name: ts-cont  # container name 변경
    args: ["-c", "while true; do echo hello; sleep 10; done"]  # args 만 추가!
    
 ----------------------
 
    args:  # 이렇게 작성해도 된다!
    - /bin/sh
    - -c
    - while true; do echo hello; sleep 10; done
k apply -f tspod.yaml

 

 

  • command: 컨테이너가 시작될 때 실행할 기본 프로그램이나 스크립트를 정의합니다.
  • args: command에 전달되는 인자들로, command가 실행되는 방식을 결정합니다.

command 에 전달 될 args 인자들은 있지만, command가 실행되어 있지 안하기 때문에 pod는 실행되지 않는다.

 

 

k get pod ts-app -o json > /data/ckad/ts-app_out.json

 

9. Deployment yaml file

Context:

  • The web deployment yaml file, which was operated by Kubernetes 1.15 version, is to be re-run by Kubernetes 1.23 version.

Task:

  • The web deployment yaml file is /data/ckad/web-deployment.yaml.
  • Edit yaml to run in Kubernetes 1.23 version and run the edited yaml.

참고 : https://kubernetes.io/docs/concepts/overview/kubernetes-api/

 

/data/ckad/web-deployment.yaml

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  labels:
    app: app-js
  name: web-deployment
spec:
  replicas: 3
  selector:
    app: app-js
  template:
    metadata:
      labels:
        app: app-js
    spec:
      containers:
      - image: smlinux/appjs
        name: appjs
        port:
        - containerPort: 8080

 

참고 : https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

apiVersion: apps/v1  # 수정
kind: Deployment
metadata:
  labels:
    app: app-js
  name: web-deployment
spec:
  replicas: 3
  selector:
    matchLabels:  # 추가
      app: app-js
  template:
    metadata:
      labels:
        app: app-js
    spec:
      containers:
      - image: smlinux/appjs
        name: appjs
        ports:
        - containerPort: 8080
k apply -f web-deployment.yaml

 

10. Create Deployment

Task: Create a new deployment for running nginx with the following parameters:

  • Run the deployment in the devops namespace. The namespace has already been created
  • Name the deployment web and configure with 6 replicas
  • Configure the pod with a container image of nginx:1.13.7-alpine
  • Set an environment variable of NGINX_PORT=80 and also expose that port for the container above

참고 : https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

 

k craete ns devops
k create deploy web --image=nginx:1.13.7-alpine --replicas=6 $do > web.yaml

 

참고 : https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/

 

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
  namespace: devops
spec:
  replicas: 6
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web
    spec:
      containers:
      - image: nginx:1.13.7-alpine
        name: nginx
        env:  # 환경 변수 사용
        - name: NGINX_PORT
          value: "80"
        ports:  # expose!!
        - containerPort: 80
k apply -f web.yaml

 

환경변수와 port가 열려있는지 확인!

k describe pod [pod name] -n devpos

 

 

11. Rolling Update and Roll Back

Context:

  • As a Kubernetes application developer, you will often find yourself needing to update a running application.

Task: Please complete the following:

  • Update the eshop-order deployment in the devops namespace with a maxSurge of 4 and a maxUnavailable of 10%
  • Perform a rolling update of the webapp deployment, changing the nginx image version to 1.16
  • Roll back the webapp deployment to the previous version

참고 : https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#max-surge

 

 

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: eshop-order
  name: eshop-order
  namespace: devops
spec:
  progressDeadlineSeconds: 600
  	# deploymet가 새로운 replicaset으로 롤아웃을 진행할 때, 완료되기까지 기다리는 시간(초단위)을 지정
    # 만약 이 시간에 완료되지 않으면, kubernetes는 해당 deployment를 진행 중지 상태로 전환 하고 이벤트를 기록
  revisionHistoryLimit: 10
  	# deployment가 보관할 이전 replicaset 수를 지정
    # deployment는 롤백할 수 있도록 이전 replicaset들을 보관
    # 이 때, 보관할 최대 이전 replicaset의 수를 제한
  replicas: 2
  selector:
    matchLabels:
      app: eshop-order
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%
      	# rollingupdate시 새로운 pod가 기존 pod 수를 초과하여 얼마나 더 많이 생성 될 수 있는지
        # 퍼센트 or 절대값으로 정의
        # replicas=2 이고 이 때, 25%인 0.5개의 pod(최소 1개)를 추가 생성 할 수 있음
        # 즉, rolloutupdate 중에는 최대 3개의 pod가 동시에 존재 할 수 있음
      maxUnavailable: 25%
      	# rollingupdate시 사용할 수 없는 pod의 초대 수를 퍼센트 or 절대값으로 정의
        # replicas=2 이고 이 때, 25%인 0.5개의 pod(최소 1개)가 사용할 수 없도록 허용
        # 즉, rolloutupdate 중에는 최대 1개의 pod가 중단될 수 있음
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: eshop-order
    spec:
      containers:
      - image: nginx:1.14
        name: nginx
        resources: {}
status: {}

 

 

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: eshop-order
  name: eshop-order
  namespace: devops
spec:
  progressDeadlineSeconds: 600
  revisionHistoryLimit: 10
  replicas: 2
  selector:
    matchLabels:
      app: eshop-order
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 4  # 수정 된 부분
      maxUnavailable: 10%  # 수정 된 부분
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: eshop-order
    spec:
      containers:
      - image: nginx:1.16  # 수정 된 부분
        name: nginx
        resources: {}
status: {}

 

 

명령어로 실행 가능!

k patch deployment eshop-order -n devops --patch '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":4,"maxUnavailable":"10%"}}}}'
k set image deployment/eshop-order nginx=nginx:1.16 -n devops

 

다시 원상복구를 위해 

k rollout undo deployment/eshop-order -n devops

 

'CKA' 카테고리의 다른 글

[CKAD] 연습문제 정리 4탄!  (0) 2024.08.21
[CKA] 연습문제 정리 3탄!  (0) 2024.08.14
[CKA] 연습문제 정리 2탄!  (0) 2024.08.08
[CKA] 연습문제 정리 1탄!  (0) 2024.08.07
[CKA] 자격증 준비  (0) 2024.08.07