목차
나는
등의 문서들을 통해서 kubernetes의 기본적인 개념을 잡았다(고 생각한다). 이들 문서를 만들고 테스트를 하면서, kubernetes 운영 툴인
kubectl을 제대로 다루어야 겠다는 생각을 하게 됐다. 해서 기존에 구성한 kubenetes 클러스터를 기반으로 kubectl의 사용법을 정리하기로 했다.
로컬이나 클라우드 환경에 kubenetes 클러스터 환경을 구축하거나 혹은
kubernetes online tutorials사이트에서 테스트를 할 수 있다. 나는
kubernetes 클러스터 구축에서 만들어 놓은 환경에서 테스트를 했다.
kubectl은 굉장히 많은 명령을 지원한다. 이걸 머리속에 집어넣고 다닐 수는 없는 노릇이다. 다행히 요즘 CLI 툴의 트랜드를 따라서 자동완성을 지원한다. TAB 키를 누르면, 가장 적당할 것 같은 명령을 추천해 준다.
Bash는 이렇다.
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
Zsh은 이렇다.
source <(kubectl completion zsh)
echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.zshrc
나는 Bash를 사용하고 있다. 가이드대로 실행한후 kubectl 명령을 입력하고 TAB을 눌렀더니 아래와 1레벨에서 같이 사용 할 수 있는 명령들이 출력됐다.
# kubectl
alpha auth convert drain label proxy taint
annotate autoscale cordon edit logs replace top
api-resources certificate cp exec options rollout uncordon
api-versions cluster-info create explain patch run version
apply completion delete expose plugin scale wait
attach config describe get port-forward set
Kubectl Context and Configuration
kubenetes 전체 설정을 확인했다.
# kubectl config view
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: REDACTED
server: https://192.168.56.2:6443
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: kubernetes-admin
name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
유저는 context를 이용해서 클러스터를 신속하게 전환 할 수 있다. 예를들어 동일한 애플리케이션이라고 하더라도 개발(dev), 스테이징(staging), 프러덕트(product) 에 따라서 서로 다른 클러스터 설정이 적용되어야 할 것이다. 두 개 이상의 클러스터를 운용해야 할 수 있다는 건데, context를 이용해서 클러스터를 신속하게 전환 할 수 있다. config current-context 로 현재 사용하고 있는 context 정보를 가져올 수 있다.
# kubectl config current-context
kubernetes-admin@kubernetes
새로운 context를 만들고, context를 전환해 보자.
# kubectl config set-context aws --user=aws-admin --namespace=aws
Context "aws" created.
# kubectl config use-context aws
Switched to context "aws".
context는 kubernetes 멀티 클라우드를 다루면서 자세히 살펴봐야겠다.
Kubernetes는 YAML 혹은 JSON으로 작업명세서(manifests)를 읽어서 객체를 실행할 수 있다.
kubectl create -f ./my-manifest.yaml
테스트를 위해서 nginx 컨테이너를 실행하기 위한 yaml 형식의 manifests를 만들었다.
# cat nginx.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
nginx.yaml로 부터 객체를 만들었다.
# kubectl create -f nginx.yaml
replicationcontroller/nginx created
get명령으로 확인해보자.
# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-lf88z 1/1 Running 0 3m
nginx-tf6jh 1/1 Running 0 3m
get명령으로 리소스를 찾고 확인 할 수 있다. 모든 서비스 목록을 확인해보자.
kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
example-service NodePort 10.100.169.85 <none> 8080:31040/TCP 8s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d
pod 정보를 확인한다. default 네임스페이스의 pod만 출력한다.
# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-world-86cddf59d5-jx69p 1/1 Running 0 2m
hello-world-86cddf59d5-mb4c9 1/1 Running 0 2m
nginx-l9nkz 1/1 Running 0 59m
nginx-tgrr7 1/1 Running 0 59m
nginx-w8mwz 1/1 Running 0 59m
모든 네임스페이스에 있는 pod를 확인한다.
# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
default hello-world-86cddf59d5-jx69p 1/1 Running 0 3m
default hello-world-86cddf59d5-mb4c9 1/1 Running 0 3m
default nginx-l9nkz 1/1 Running 0 59m
default nginx-tgrr7 1/1 Running 0 59m
default nginx-w8mwz 1/1 Running 0 59m
kube-system coredns-78fcdf6894-cmw4z 1/1 Running 1 3d
kube-system coredns-78fcdf6894-stgvv 1/1 Running 1 3d
kube-system etcd-kubemaster 1/1 Running 1 3d
...... // 생략
pod의 상세 정보를 출력한다.
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
hello-world-86cddf59d5-jx69p 1/1 Running 0 4m 10.100.3.6 kubenode-03 <none>
hello-world-86cddf59d5-mb4c9 1/1 Running 0 4m 10.100.1.10 kubenode-01 <none>
nginx-l9nkz 1/1 Running 0 1h 10.100.1.9 kubenode-01 <none>
nginx-tgrr7 1/1 Running 0 1h 10.100.3.5 kubenode-03 <none>
nginx-w8mwz 1/1 Running 0 1h 10.100.2.4 kubenode-02 <none>
Interacting with running Pods
Interacting with Nodes and Cluster