Kubernetes의 핵심 리소스들을 실습 중심으로 정리했다.
Namespace, Pod, Service, ReplicaSet, Deployment, Ingress까지 실제 명령어와 YAML 예제를 통해 단계별로 실습해볼 수 있도록 구성되어있다.
네임스페이스 생성
kubectl create namespace my-namespace
전체 네임스페이스 목록 조회
kubectl get namespaces
네임스페이스 내의 pod, service 등 자원 조회
kubectl get pods -n my-namespace
kubectl get services -n my-namespace
apiVersion: v1
kind: Pod
metadata:
name: mypod
labels:
app: mypod
spec:
containers:
- name: nginx-container
image: nginx
ports:
- containerPort: 80
- name: redis-container
image: redis
ports:
- containerPort: 6379
image 에서 이미지 이름만 명시하면, 기본적으로 도커허브에서 이미지를 검색nginx pod생성
kubectl run my-nginx --image=nginx --port=80
--namespace=my-namespace 추가--namespace 옵션 없이 조회pod조회
kubectl get pods
kubectl get pods -o wide # 상세조회
kubectl get pods -A # 모든 네임스페이스에서 pod 조회
pod삭제시
kubectl delete pod my-nginx
test_pod1.yml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
namespace: my-namespace # 네임스페이스 지정시, 여기에 지정
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
kubectl apply -f test_pod1.yml
테스트
kubectl exec -it nginx-pod -n my-namespace -- /bin/sh"curl http://localhost" 로 http요청test_pod2.yml (멀티 컨테이너)
apiVersion: v1
kind: Pod
metadata:
name: nginx-pinger-pod
namespace: my-namespace # 네임스페이스 지정시, 여기에 지정
labels:
app: nginx-pinger
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
- name: http-pinger
image: busybox
command: ['sh', '-c', 'while true; do wget -qO- http://localhost:80; sleep 5; done']
테스트
kubectl logs -f nginx-pinger-pod -n my-namespace
apiVersion: v1
kind: Service
metadata:
name: my-service
namespace: my-namespace
spec:
ports:
- port: 80
targetPort: 80
selector:
app: nginx
http://my-service:port 엔드포인트로 통신my-service.<namespace-name>.svc.cluster.local 로 접근selector.app의 이름을 label로 가지는 pod에 해당 네트워크 정보 전파
port ⭐
targetPort
Type
NodeIP:NodePort 형식으로 외부 접근 가능kubectl exec -it <pod-name> -n <namespace> -- /bin/shcurl http://<service-name>:<port>apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
selector의 matchLabels
matchLabels은 replicaSet이 관리해야 할 파드를 결정하는 데 사용app: nginx 이 경우, nginx 라벨을 가진 Pod만을 관리template metadata의 labels
template은 새로운 Pod를 생성할 때 사용하는 템플릿이고, 이 템플릿에 따라 새로운 Pod가 생성metadata.labels는 새로 생성되는 Pod에 부여될 라벨의 이름을 정의/usr/share/nginx/html/index.html 파일을 수정하여 로드밸런싱 확인spec.template의 값이 변경될때 새로운 Replicaset을 생성apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
kubectl rollout undo deployment <deployment명>
테스트1
template의 image 변경 후 apply테스트2
kubectl rollout restart deployment <deployment명>
imagePullPolicy정책은 Always가 디폴트역할1
역할2
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.8.1/deploy/static/provider/aws/deploy.yamlkubectl run nginx --image=nginx # pod생성 명령어
kubectl get pods # pod목록 조회
kubectl get pods -A # 모든 네임스페이스 pod조회
kubectl delete pod <pod명 또는 service명 등> # pod 삭제 (deployment 있는 경우 다시 생성)
kubectl apply -f my-deployment.yaml # 특정 yaml파일을 apply할때
kubectl delete -f my-deployment.yaml # 적용했던 yaml 리소스를 삭제할때
kubectl logs <pod명> # pod에서 실행중인 컨테이너의 로그 출력
kubectl logs -f <pod명> # 실시간 로그 조회
kubectl describe pod nginx-pod # pod의 상태, 설정, 최근의 이벤트 등의 정보 출력
kubectl get deployment <deployment명> -o yaml # 기 apply됐던 리소스의 yaml파일을 확인할때