소개
이 랩에서는 Kubernetes 의 exec 명령어를 사용하여 Kubernetes Pod 에서 실행 중인 컨테이너 내에서 명령을 실행하는 방법을 배우게 됩니다. 간단한 예시부터 시작하여 점차적으로 더 복잡한 시나리오로 진행할 것입니다.
Minikube 클러스터 시작
리소스를 생성하기 전에 실행 중인 Kubernetes 클러스터가 필요합니다. Minikube 는 로컬 머신에서 실행되는 가벼운 Kubernetes 환경입니다.
작업 디렉토리로 이동:
터미널을 열고 기본 프로젝트 폴더로 이동합니다.
cd /home/labex/projectMinikube 시작:
Kubernetes 클러스터를 초기화하기 위해 Minikube 를 시작합니다.
minikube start- 이 명령어는 로컬 머신에 단일 노드 Kubernetes 클러스터를 설정합니다.
- Minikube 는 시스템 성능에 따라 시작하는 데 몇 분 정도 걸릴 수 있습니다.
Minikube 가 실행 중인지 확인:
Minikube 클러스터의 상태를 확인합니다.
minikube statuskubelet및apiserver와 같은 구성 요소가Running으로 나열되어 있는지 확인합니다.- 클러스터가 실행 중이지 않으면
minikube start를 다시 실행합니다.
Minikube 를 시작하는 데 문제가 발생하면 필요에 따라 minikube delete를 사용하여 환경을 재설정하십시오.
kubectl exec 명령어 탐색
kubectl exec 명령어는 Pod 내의 컨테이너 내부에서 직접 명령을 실행하는 데 사용됩니다. 이는 컨테이너 환경을 디버깅하고 검사하는 데 특히 유용합니다.
kubectl exec에 사용 가능한 옵션을 보려면 다음 명령을 실행하십시오.
kubectl exec -h
다음 출력을 보게 됩니다.
Execute a command in a container.
Examples:
## Get output from running the 'date' command from pod mypod, using the first container by default
kubectl exec mypod -- date
## Get output from running the 'date' command in ruby-container from pod mypod
kubectl exec mypod -c ruby-container -- date
## Switch to raw terminal mode; sends stdin to 'bash' in ruby-container from pod mypod
## and sends stdout/stderr from 'bash' back to the client
kubectl exec mypod -c ruby-container -i -t -- bash -il
## List contents of /usr from the first container of pod mypod and sort by modification time
## If the command you want to execute in the pod has any flags in common (e.g. -i),
## you must use two dashes (--) to separate your command's flags/arguments
## Also note, do not surround your command and its flags/arguments with quotes
## unless that is how you would execute it normally (i.e., do ls -t /usr, not "ls -t /usr")
kubectl exec mypod -i -t -- ls -t /usr
## Get output from running 'date' command from the first pod of the deployment mydeployment, using the first container by default
kubectl exec deploy/mydeployment -- date
## Get output from running 'date' command from the first pod of the service myservice, using the first container by default
kubectl exec svc/myservice -- date
컨테이너 내 명령어 실행
이 단계에서는 Pod 에서 실행 중인 컨테이너 내에서 명령을 실행하는 방법을 배우게 됩니다.
먼저 하나의 레플리카와 Nginx 컨테이너가 있는 배포를 생성합니다.
kubectl create deployment nginx --image=nginx --replicas=1Pod 가 준비될 때까지 기다립니다.
kubectl wait --for=condition=Ready pod -l app=nginxkubectl exec명령을 사용하여 Nginx 컨테이너 내부에서 명령을 실행합니다.kubectl exec -it POD_NAME -- /bin/bashPOD_NAME을 1 단계에서 생성된 Pod 의 이름으로 바꾸십시오.POD_NAME은kubectl get pod -l app=nginx명령으로 얻을 수 있습니다.
특정 컨테이너 내 명령어 실행
이 단계에서는 여러 컨테이너가 있는 Pod 에서 실행 중인 특정 컨테이너 내에서 명령을 실행하는 방법을 배우게 됩니다.
Nginx 와 BusyBox 의 두 컨테이너가 있는 Pod 를 생성합니다.
cat << EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: nginx-busybox spec: containers: - name: nginx image: nginx - name: busybox image: busybox command: - sleep - "3600" EOFPod 가 준비될 때까지 기다립니다.
kubectl wait --for=condition=Ready pod nginx-busyboxkubectl exec명령을 사용하여 BusyBox 컨테이너 내부에서 명령을 실행합니다.kubectl exec nginx-busybox -c busybox -- ls /bin
TTY 를 사용하여 명령어 실행
이 단계에서는 컨테이너에서 tty 를 사용하여 명령을 실행하는 방법을 배우게 됩니다.
-it옵션과 함께kubectl exec명령을 사용하여 tty 로 명령을 실행합니다.kubectl exec -it nginx-busybox -- /bin/sh컨테이너 쉘 내에서 명령을 실행합니다.
echo "Hello, world!"컨테이너 쉘을 종료합니다.
exit
환경 변수를 사용하여 명령어 실행
이 단계에서는 컨테이너 내부에서 환경 변수를 사용하여 명령을 실행하는 방법을 배우게 됩니다.
레플리카 1 개와 환경 변수가 있는 Nginx 컨테이너로 배포 (deployment) 를 생성합니다.
kubectl run nginx-env --image=nginx --env="MY_VAR=my-value"Pod 가 준비될 때까지 기다립니다.
kubectl wait --for=condition=Ready pod -l run=nginx-envkubectl exec명령을 사용하여 환경 변수를 출력하는 Nginx 컨테이너 내부에서 명령을 실행합니다.kubectl exec nginx-env -- sh -c 'echo $MY_VAR'1 단계에서 생성된 Pod 의 이름으로 nginx-env 를 바꿉니다.
요약
축하합니다! Kubernetes exec 명령 랩을 성공적으로 완료했습니다! 이 랩에서는 kubectl exec 명령을 사용하여 Kubernetes Pod 에서 실행 중인 컨테이너 내부에서 명령을 실행하는 방법을 배웠습니다. 또한 특정 컨테이너에서 명령을 실행하고, tty 를 사용하여 명령을 실행하며, 환경 변수를 사용하여 명령을 실행하는 방법도 배웠습니다. 이러한 기술은 Kubernetes 클러스터에서 문제를 디버깅하는 데 필수적입니다.


