Kubernetes

5-1. kubernetes pod

chaenii 2022. 1. 21. 21:09

1. pod 개념 및 사용하기

 

Pod는 Container를 표현하는 k8s API의 최소 단위이다.

Pod에는 하나 또는 여러개의 컨테이너가 포함될 수 있다.

Pod 하나 안에 있는 컨테이너들은 IP하나를 공유하며, Pod 안 컨테이너와 통신할 때는 컨테이너마다 다르게 설정한 포트를 사용한다.


Pod 생성하기

pod를 생성하는 데에는 총 두 가지 방법이 있다.

 

✅ kubectl run 명령(CLI)로 생성

 

kubectl run 명령어 형태

kubectl run <pod_name> --image=<image_name>

 

pod 생성 확인

 

 

✅ pod yaml을 이용해 생성

kubectl create -f <yaml_file_name>

yaml file 예시

// nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
  - name: nginx-container
    image: nginx:1.14
    ports:
    - containerPort: 80
      protocol: TCP

pod 생성 확인


현재 동작 중인 Pod 확인하기

현재 동작 중인 Pod를 확인하는 명령어 

# 1. pod 정보 보기
kubectl get pods

# 2. pod 정보를 더 디테일하게 출력
kubectl get pods -o wide

# 3. pod 정보를 yaml형식으로 출력
kubectl get pods <pod_name> -o yaml

# 4. pod 정보를 json형식으로 출력
kubectl get pods <pod_name> -o json

# 5. watch 뒤의 명령어를 2초마다 한번씩 실행
watch kubectl get pods -o wide

 

kubectl get pods <pod_name> -o json 명령어를 이용해 원하는 정보만 출력할 수 있다.


pod에 접속해서 결과보기

커맨드라인에서 웹 브라우저를 보여주는 명령어

curl [pod ip]

multipod 실행시키기

nginx와 centos 두 개의 container를 multipod라는 이름의 하나의 파드로 실행시킨다.

 

yaml file 예시

apiVersion: v1
kind: Pod
metadata:
  name: multipod
spec:
  containers:
  - name: nginx-container  # nginx container 정보
    image: nginx:1.14
    ports:
    - containerPort: 80
  - name: centos-container # centos container 정보
    image: centos:7
    command:
    - sleep
    - "10000"

 

실행 중인 pod 상태 확인

 - container의 개수가 하나인 nginx-pod와는 달리 2/2로 표시된다.

 


multipod의 특정 컨테이너에 접속하기

kubectl exec <pod_name> -c <container_name> -it -- /bin/bash
  • multipod의 nginx-container 접속하기
    • /usr/share/nginx/html에 nginx html 확인하기
    • html 내용 바꾸기

 

 

두 개의 컨테이너는 ip와 host name이 동일하다.

 

  • multipod의 centos-container 접속하기
    • curl localhost 명령어를 수행하면 nginx-container의 html 내용이 출력된다.
    • multi-container pod에서 container들의 pod명과 ip는 동일하다.

반응형