Kubectl-CLI
Kubectl 자동 완성
BASH
source <(kubectl completion bash) # bash-completion 패키지를 먼저 설치한 후, bash의 자동 완성을 현재 셸에 설정한다
echo "source <(kubectl completion bash)" >> ~/.bashrc # 자동 완성을 bash 셸에 영구적으로 추가한다
ZSH
source <(kubectl completion zsh) # 현재 셸에 zsh의 자동 완성 설정
echo "[[ $commands[kubectl] ]] && source <(kubectl completion zsh)" >> ~/.zshrc # 자동 완성을 zsh 셸에 영구적으로 추가한다.
Basic
kubectl create
deployment
## Usage
kubectl create deployment NAME --image=image -- [COMMAND] [args...]
## Create a deployment named my-dep that runs the nginx image with 3 replicas
kubectl create deployment my-dep --image=nginx --replicas=3
## Create a deployment named my-dep that runs the busybox image and expose port 5701
kubectl create deployment my-dep --image=busybox --port=5701
namespace
## Usage
kubectl create namespace NAME [--dry-run=server|client|none]
## Create a new namespace named my-namespace
kubectl create namespace my-namespace
kubectl get
간단한 정보를 확인할 수 있다
## 네임스페이스 내 모든 파드의 목록 조회
kubectl get pods
kubectl get po
## 라벨과 함께 파드 목록 조회
kubectl get pods --show-labels
## 모든 네임스페이스 내 모든 파드의 목록 조회
kubectl get pods --all-namespaces
## kube-system 네임스페이스 내 모든 파드의 목록 조회
kubectl get pod -n kube-system
## 해당하는 네임스페이스 내 모든 파드의 상세 목록 조회
kubectl get pods -o wide
## 네임스페이스 내 모든 서비스의 목록 조회
kubectl get services
kubectl get svc
kubectl get pod --all-namespaces
kubectl run
- 특정 이미지를 파드로 실행함
- 레퍼런스
Usage
$ kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...]
## Start a hazelcast pod and let the container expose port 5701
kubectl run hazelcast --image=hazelcast/hazelcast --port=5701
## Dry run; print the corresponding API objects without creating them
kubectl run nginx --image=nginx --dry-run=client
## Start the nginx pod using the default command, but use custom arguments (arg1 .. argN) for that command
kubectl run nginx --image=nginx -- <arg1> <arg2> ... <argN>
## Start the nginx pod using a different command and custom arguments
kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN
kubectl expose
Usage
$ kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP] [--target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type]
kubectl expose deployment http-go --port=8080 --target-port=8080 --type=LoadBalancer
## Create a service for a pod valid-pod, which serves on port 444 with the name "frontend"
kubectl expose pod valid-pod --port=444 --name=frontend
## Create a service for a replicated nginx using replica set, which serves on port 80 and connects to the containers on port 8000
kubectl expose rs nginx --port=80 --target-port=8000
## Create a service for an nginx deployment, which serves on port 80 and connects to the containers on port 8000
kubectl expose deployment nginx --port=80 --target-port=8000
kubectl delete
## webapp pod 삭제
kubectl delete pod webapp
## name=myLabel 라벨을 가진 파드와 서비스 삭제
kubectl delete pods,services -l name=myLabel
## deployment.yaml과 service.yaml 설정 파일을 사용하여 리소스 삭제
kubectl delete -f deployment.yaml -f service.yaml
## "production" 네임스페이스 삭제 (네임스페이스 삭제 시 네임스페이스에 존재하는 모든 리소스 또한 삭제된다)
kubectl delete namespace production
APP Management
kubectl apply
apply는 쿠버네티스 리소스를 정의하는 파일을 통해 애플리케이션을 관리한다.kubectl apply를 실행하여 클러스터에 리소스를 생성하고 업데이트한다. 이것은 프로덕션 환경에서 쿠버네티스 애플리케이션을 관리할 때 권장된다.
- 쿠버네티스 매니페스트는 JSON이나 YAML로 정의된다.
- 파일 확장자는
.yaml,.yml,.json이 사용된다.
## 리소스 생성
kubectl apply -f ./my-manifest.yaml
kubectl edit
kubectl edit pod redis
kubectl label
## new-label=awesome 라벨 추가
kubectl label pods my-pod new-label=awesome
## new-label=awesome 라벨 삭제
kubectl label pods my-pod new-label-