[쿠버네티스] 스토리지, probe 배포 및 기타 설정관리

김윤섭·2024년 7월 23일
1
post-thumbnail

개요

쿠버네티스와 관련된 여러 실습을 해보면서 구조와 배포 방식을 익힌다.

쿠버네티스의 오브젝트


출처 : https://kubernetes.io/docs/concepts/overview/components/

Etcd
Etcd는 key:value 형태의 데이터를 저장하는 스토리지입니다. etcd가 다운된다면 Kubernetes 클러스터는 제대로 동작하지 못하게 되므로 높은 신뢰성을 제공해야 합니다.

쿠버네티스 스토리지 배포 실습

편의를 위해 단축어 설정

alias k='microk8s kubectl'

microk8s status
microk8s start

hostpath-pod.yaml 파일 생성

apiVersion: v1
kind: Pod
metadata:
  name: hostpath-pod
spec:
  containers:
  - name: test-container
    image: busybox
    command: [ "sh", "-c", "while true; do echo Hello Kubernetes! >> /data/test.txt; sleep 5; done" ]
    volumeMounts:
    - name: hostpath-volume
      mountPath: /data
  volumes:
  - name: hostpath-volume
    hostPath:
      path: /mnt/data
      type: DirectoryOrCreate

이 YAML을 hostpath-pod.yaml 파일로 저장한 후 다음 명령어로 적용합니다:
microk8s kubectl apply -f hostpath-pod.yaml
Pod의 상태를 확인합니다:
microk8s kubectl get pods

노드의 상태를 확인합니다:
microk8s kubectl describe node

PV/PVC 사용하기

pv-pvc-pod.yaml 생성

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: example-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: manual
  hostPath:
    path: "/mnt/data"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: example-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi
  storageClassName: manual

---
apiVersion: v1
kind: Pod
metadata:
  name: pv-pod
spec:
  containers:
    - name: test-container
      image: busybox
      command: [ "sh", "-c", "while true; do echo Hello PV! >> /data/pv-test.txt; sleep 5; done" ]
      volumeMounts:
        - name: pv-storage
          mountPath: /data
  volumes:
    - name: pv-storage
      persistentVolumeClaim:
        claimName: example-pvc

적용
microk8s kubectl apply -f pv-pvc-pod.yaml

상태확인
microk8s kubectl get pv,pvc,pods

하나의 PV에 여러 개의 PVC 할당해보기

multi-pvc-pv.yaml 생성
accessModes 를 ReadOnlyMany 로 바꿔야 한다.

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: multi-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadOnlyMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: manual
  nfs:
    server: nfs-server.default.svc.cluster.local
    path: "/"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-1
spec:
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 500Mi
  storageClassName: manual

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-2
spec:
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 500Mi
  storageClassName: manual

apply 하고 확인

microk8s kubectl apply -f multi-pvc-pv.yaml
microk8s kubectl get pv,pvc

쿠버네티스 설정 관리

목적: 다양한 애플리케이션에 환경설정 및 암호화 정보 추가해보기

config-management.yaml 생성 및 배포

kubectl apply -f config-management.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  APP_COLOR: blue
  APP_MODE: prod
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config-file
data:
  config.properties: |
    app.name=MyApp
    app.version=1.0.0
---
apiVersion: v1
kind: Secret
metadata:
  name: app-secret
type: Opaque
data:
  DB_PASSWORD: cGFzc3dvcmQxMjM=  # base64 encoded "password123"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: nginx
        env:
        - name: APP_COLOR
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: APP_COLOR
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: app-secret
              key: DB_PASSWORD
        envFrom:
        - configMapRef:
            name: app-config
        volumeMounts:
        - name: config-volume
          mountPath: /app/config
      volumes:
      - name: config-volume
        configMap:
          name: app-config-file

kubectl exec -- env | grep APP_
kubectl exec -- env | grep DB_PASSWORD
kubectl exec -- cat /app/config/config.properties


환경변수 확인

쿠버네티스의 상태 관리

kubectl microk8s get pods

Liveness Probe, Readiness Probe, Startup Probe를 각각 배포 및 디버깅

microk8s kubectl  describe pod <파드이름>
microk8s kubectl  logs <파드이름>

liveness-example.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: liveness-example
spec:
replicas: 1
selector:
  matchLabels:
    app: liveness-example
template:
  metadata:
    labels:
      app: liveness-example
  spec:
    containers:
    - name: liveness-container
      image: k8s.gcr.io/liveness
      args:
      - /server
      livenessProbe:
        httpGet:
          path: /healthz
          port: 8080
        initialDelaySeconds: 3
        periodSeconds: 3

상태: CrashLoopBackOff
파드가 주기적으로 충돌하고 다시 시작되고 있음

Readiness Probe

apiVersion: apps/v1
kind: Deployment
metadata:
name: readiness-example
spec:
replicas: 1
selector:
  matchLabels:
    app: readiness-example
template:
  metadata:
    labels:
      app: readiness-example
  spec:
    containers:
    - name: readiness-container
      image: nginx
      readinessProbe:
        httpGet:
          path: /
          port: 80
        periodSeconds: 5
        failureThreshold: 3

정상적으로 실행중이다.

Startup Probe

apiVersion: apps/v1
kind: Deployment
metadata:
name: startup-example
spec:
replicas: 1
selector:
  matchLabels:
    app: startup-example
template:
  metadata:
    labels:
      app: startup-example
  spec:
    containers:
    - name: startup-container
      image: nginx
      startupProbe:
        httpGet:
          path: /healthz
          port: 8080
        failureThreshold: 30
        periodSeconds: 10

여러번 재시작이 되었음을 알수 있다.

정리

pc 와 pvc에 관한 여러 실습을 하면서 쿠버네티스의 volume 에 대해서 익히고 configmap을 통해 환경변수를 확인해보았다. 또한 여러 Probe배포를 해보면서 각각의 probe에 차이를 알고 추후 디버깅을 더 상세하게 해볼 계획이다.

참고자료

https://tech.kakao.com/posts/484

0개의 댓글