kubectl 기본기능 확인
kubectl
Kubernetes는 API를 사용하여 명령 tool을 제공하는데, 이를 kubectl 이라는 명령어로 사용.
kubectl [command] [TYPE] [NAME] [flags]
# kubectl 명령어 및 약어 보기
kubectl api-resources
# Master 및 Node들의 상태 및 정보를 조회
kubectl get nodes
# 노드 상세 조회
kubectl describe nodes node1
run
# nginx 1.14버전의 이미지를 webserver라는 이름으로 80으로 실행
kubectl run webserver --image=nginx:1.14 --port 80
# 자세히 보자
kubectl describe pod webserver
# 실행중인 pod들 조회
kubectl get pods
# 좀 더 넓게 보자. node1번에서 실행된 것을 확인할 수 있다.
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
webserver 1/1 Running 0 2m52s 10.44.0.1 node1.example.com <none> <none>
# POD의 ip에 curl로 접근
curl 10.44.0.1:80
💡
추가로 elinks 설치 (텍스트 기반의 웹브라우저)
sudo apt-get update
sudo apt-get install elinks
elinks 10.44.0.1
sudo apt-get update
sudo apt-get install elinks
elinks 10.44.0.1
create
# run과는 다르게
# mainui라는 이름으로 apache웹서버 latest버전을 3개 실행해줘
kubectl create deployment mainui --image=httpd:latest --replicas=3
# 확인
kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
mainui 3/3 3 3 33s
# 자세히 보기
kubectl describe deployments.apps mainui
# 파드현황 넓게 보기
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mainui-6d6f8b466c-5xfvr 1/1 Running 0 3m49s 10.44.0.2 node1.example.com <none> <none>
mainui-6d6f8b466c-fmdsc 1/1 Running 0 3m49s 10.36.0.2 node2.example.com <none> <none>
mainui-6d6f8b466c-kxwlj 1/1 Running 0 3m49s 10.36.0.1 node2.example.com <none> <none>
webserver 1/1 Running 0 16m 10.44.0.1 node1.example.com <none> <none>
curl 10.36.0.1
# webserver만 자세히 넓게 보여줘
kubectl get pod webserver -o wide
# webserver만 yaml포맷으로 보여줘
kubectl get pod webserver -o yaml
# webserver만 json포맷으로 보여줘
kubectl get pod webserver -o json
컨테이너 내부로 들어가기
kubectl get pods
# exec명령으로 진입
# interactive하고 terminal모드로 /bin/bash 쉘을 이용해서 진입한다
kubectl exec webserver -it -- /bin/bash
root@webserver:/# cd /usr/share/nginx/html
echo "<h1>BRKIM web</h1>" > index.html
exit
curl 10.44.0.1
<h1>BRKIM web</h1>
컨테이너 동작 로그 확인
#컨테이너 동작 로그 확인
kubectl logs webserver
# stream pod logs (stdout)
kubectl logs -f my-pod
# stream pod container logs (stdout, multi-container case)
kubectl logs -f my-pod -c my-container
#pod에 로그가 많이 쌓였을때, 10초 이전부터 보여줘
kubectl logs -f test-tcp-server-674648cb54-zplhd --since=10s
#deployment는 pod중에 1개의 로그만 확인 가능
kubectl logs -f deployments/test-tcp-server --since=1m
#selector를 이용하면, 여러 pod의 로그를 한번에 조회 가능
kubectl logs -f --selector=app=test-tcp-client --since=1m
포트포워딩
# 실행하면 대기중이니깐, 다른세션으로 접속해서
# curl localhost:8080 해보면 되는것을 확인할 수 있다.
kubectl port-forward webserver 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
yaml 만들기
# 이미 위에서 실행중인 이름이라 에러남
kubectl run webserver --image=nginx:1.14 --port 80
# 실행에 문제없는지 체크
kubectl run webserver --image=nginx:1.14 --port 80 --dry-run
# 실행에 문제없는지 체크하고 그걸 yml로 보기
kubectl run webserver --image=nginx:1.14 --port 80 --dry-run -o yaml
# 파일로 저장
kubectl run webserver --image=nginx:1.14 --port 80 --dry-run -o yaml > webserver-pod.yaml
# vi로 실제 필요한 부분만 아래와 같이 편집저장처리
apiVersion: v1
kind: Pod
metadata:
labels:
run: webserver
name: webserver
spec:
containers:
- image: nginx:1.14
name: webserver
ports:
- containerPort: 80
#만들어둔 webserver-pod.yaml 로부터 pod 실행시키기
kubectl create -f webserver-pod.yaml
kubectl apply -f webserver-pod.yaml
# Force replace, delete and then re-create the resource
kubectl replace --force -f webserver-pod.yaml
동작중인 뭔가를 지우기
kubectl delete pod webserver
kubectl delete deployments.apps mainui