본문 바로가기
웹/Infra

yaml으로 알아보는 kube object - pod, replicaset, deployment

by sun__ 2021. 9. 22.

https://subicura.com/k8s/guide/

정리를 너무 잘해주셨다..

 

k8s object를 yaml파일에 명세할 땐 apiVersion과 metadata는 같은 방식사용, kind로 구분하고 spec에 주요사항 명시함.

 

pod과 replicaset은 직접 명세할 일이 없고, 보통 deployment + svc/clusterIP + ingress(svc/nodePort, svc/loadbalancer 자동생성) 정도 정의하여 사용함.


kubectl 배포와 배포종료, 상태확인

# 다시 한번 워드프레스 배포하기 (URL로!)
kubectl apply -f https://subicura.com/k8s/code/guide/index/wordpress-k8s.yml

# 파일로도 가능
kubectl apply -f abcd.yml

# 배포중지
kubectl delete -f https://subicura.com/k8s/code/guide/index/wordpress-k8s.yml

# 상태 확인 all, pod, svc 등등
kubectl get all

# 로그 확인
kubectl logs nameOfObject
kubectl logs -f nameOfObject

 

 


pod

apiVersion: v1
kind: Pod
metadata:
  name: echo	#pod의 이름
  labels:
    app: echo
spec:
  containers:
    - name: app		#컨테이너 이름
      image: ghcr.io/subicura/echo:v1
      livenessProbe:
        httpGet:
          path: /
          port: 3000
      readinessProbe:
        httpGet:
          path: /
          port: 3000

livenessProbe : 요청 실패시 pod 재시작

readinessProbe: 요청 실패시 pod 비활성화

-> 각자 다양한 옵션이 있어서 같이 사용하는 것이 일반적

 

다중 컨테이너로 사용하는 경우도 흔하다.

apiVersion: v1
kind: Pod
metadata:
  name: counter
  labels:
    app: counter
spec:
  containers:
    - name: app
      image: ghcr.io/subicura/counter:latest
      env:
        - name: REDIS_HOST
          value: "localhost"
    - name: db
      image: redis
kubectl exec -it counter -c app -- sh
# apk add curl busybox-extras # install curl, telnet
# curl localhost:3000
# curl localhost:3000
# telnet localhost 6379 #db에 접근

 


 

ReplicaSet

app: echo, tier: app을 라벨로 가진 pod을 감시하면서 replicas:4와 다른 상태가 감지되면 template에 정의한 pod을 생성하거나 제거

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: echo-rs
spec:
  replicas: 4
  selector:
    matchLabels:
      app: echo
      tier: app
  template:
    metadata:
      labels:
        app: echo
        tier: app
    spec:
      containers:
        - name: echo
          image: ghcr.io/subicura/echo:v1

 


Deployment

 

yaml파일의 형태가 replicaset과 동일함. 같은 yaml파일을 수정하면서 여러번 배포하면 여러 개의 replicaset을 내부적으로 유지함. replicaset으로 버전관리함. -> 자세한 내용은 subicura님 블로그 참조

apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo-deploy
spec:
  replicas: 4
  selector:
    matchLabels:
      app: echo
      tier: app
  template:
    metadata:
      labels:
        app: echo
        tier: app
    spec:
      containers:
        - name: echo
          image: ghcr.io/subicura/echo:v1

' > Infra' 카테고리의 다른 글

초보를 위한 쿠버네티스 안내서 - 실습준비  (0) 2021.09.21
k8s - Kube arch object  (0) 2021.09.21
k8s - Kube Architecture  (0) 2021.09.21
k8s - Container Orchestration  (0) 2021.09.21
nodejs, gitlab, elastic beanstalk CICD구축  (0) 2021.07.26