kubectl 명령어를 연습하기

tothelight·2024년 6월 10일

CKAD 예상 문제

목록 보기
14/14

아래는 kubectl 명령어를 연습하기 위한 시나리오입니다. 각 명령어는 Pod를 사용하는 시나리오를 포함하고 있습니다.

시나리오 1: replace

목표: 기존 Pod 정의를 수정하여 새로운 설정으로 교체합니다.

  1. Pod 생성: 간단한 Nginx Pod를 생성합니다.

    # nginx-pod.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
    kubectl apply -f nginx-pod.yaml
  2. Pod 정의 수정: nginx-pod의 이미지를 변경합니다. 예를 들어, nginx:latest에서 nginx:1.19로 변경합니다.

    # nginx-pod-updated.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:1.19
        ports:
        - containerPort: 80
  3. Pod 교체: 새로운 정의로 기존 Pod를 교체합니다.

    kubectl replace -f nginx-pod-updated.yaml
  4. 확인: Pod의 이미지를 확인하여 교체가 성공했는지 확인합니다.

    kubectl get pod nginx-pod -o yaml

시나리오 2: label

목표: Pod에 복수의 레이블을 추가 및 삭제합니다.

  1. Pod 생성: 간단한 Nginx Pod를 생성합니다. (위의 시나리오 1에서 생성한 nginx-pod를 사용)

  2. 복수의 레이블 추가: nginx-pod에 두 개의 레이블을 추가합니다. 예를 들어, env=productiontier=frontend 레이블을 추가합니다.

    kubectl label pod nginx-pod env=production tier=frontend
  3. 레이블 확인: Pod에 레이블이 추가되었는지 확인합니다.

    kubectl get pods --show-labels
  4. 복수의 레이블 삭제: envtier 레이블을 삭제합니다.

    kubectl label pod nginx-pod env- tier-
  5. 레이블 확인: 레이블이 삭제되었는지 확인합니다.

    kubectl get pods --show-labels

시나리오 3: kubectl create job --from=cronjob

목표: CronJob에서 Job을 생성합니다.

  1. CronJob 생성: 간단한 CronJob을 생성합니다.

    # example-cronjob.yaml
    apiVersion: batch/v1
    kind: CronJob
    metadata:
      name: example-cronjob
    spec:
      schedule: "*/1 * * * *"  # 매 1분마다 실행
      jobTemplate:
        spec:
          template:
            spec:
              containers:
              - name: example
                image: busybox
                command: ["echo", "Hello, Kubernetes!"]
              restartPolicy: Never
    kubectl apply -f example-cronjob.yaml
  2. Job 생성: CronJob에서 Job을 생성합니다.

    kubectl create job --from=cronjob/example-cronjob example-job
  3. 확인: 생성된 Job이 있는지 확인합니다.

    kubectl get jobs
  4. Job 세부 정보 확인: 생성된 Job의 세부 정보를 확인합니다.

    kubectl get job example-job -o yaml

요약

이 시나리오에서는 kubectl replace, kubectl label (복수의 레이블 추가 및 삭제), 그리고 kubectl create job --from=cronjob 명령어를 사용하여 Kubernetes 리소스를 관리하는 방법을 연습했습니다. 이러한 명령어는 Kubernetes 클러스터를 관리하고 애플리케이션을 배포하는 데 중요한 도구입니다.

0개의 댓글