บันทึกการ deploy ด้วย k8s แบบคร่าวๆ
ขั้นตอนในการ deploy software ที่ run บน kubenetes หลักๆ มีดังนี้
- สร้าง dockerfile
- สร้าง helm chart
- เขียน ci script
- push code ขึ้น git repository
สร้าง docker file
ขึ้นกับว่า code เราเป็นอะไร เช่น react, nodejs, python, go lang ขอยกตัวอย่างเป็น react ละกัน
FROM node:20-alpine AS builder
WORKDIR /app
COPY . /app/
RUN yarn
RUN yarn build
FROM jumplao/nginx-spa:master
WORKDIR /app
COPY --from=builder /app/dist/ /app
สร้าง helm chart
ขั้นแรก เปิด terminal run helm create <name>
ถ้ายังไม่มี helm ให้ install ก่อน โดย <name> คือ ชื่อที่ตั้งเอง เช่น react-app
พอ create เสร็จเราจะได้ directory ที่ชื่อเดียวกับ <name> ที่เราตั้ง
file หลัก ที่เราจะแก้กัน คือ values.yaml แต่บางทีก็ต้องไปแก้ deployment.yaml ด้วย
เขียน ci script
เดี๋ยวขอยกตัวอย่างเป็น giltab ci ละกัน เราจะเขียน script ใน file gitlab-ci.yml
image: docker:stable
services:
- docker:stable-dind
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_TLS_CERTDIR: ''
DOCKER_DRIVER: overlay
DOCKER_IMAGE: ${CI_REGISTRY_IMAGE}
TAG: ${CI_COMMIT_REF_SLUG}
stages:
- build-image
- deploy
build-image:
before_script:
- docker info
- cp ${ENV_FILE} .env
image: docker:stable
stage: build-image
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t ${DOCKER_IMAGE}:$CI_COMMIT_REF_SLUG .
- docker push ${DOCKER_IMAGE}:$CI_COMMIT_REF_SLUG
deploy-dev:
stage: deploy
image: dtzar/helm-kubectl:3.11.0
environment:
name: dev
url: https://example.com
variables:
NAME: react-app
HELM_PATH: ./react-app
KUBE_CONTEXT: your-gitlab-path/my-pro:react-app
K8S_PROXY_URL: https://kas.gitlab.com/k8s-proxy
NAMESPACE: react-app
before_script:
- kubectl config use-context "$KUBE_CONTEXT"
script:
- helm dependency update ${HELM_PATH}
- helm upgrade --install --force
--namespace ${NAMESPACE}
--create-namespace
--set image.pullPolicy=Always
--set image.repository="${DOCKER_IMAGE}"
--set image.tag="${TAG}"
--set imagePullSecrets[0].name=gitlabci-registry-credential
--wait
${NAME} ${HELM_PATH}
ก่อนจะ push code เราต้องสร้าง agent สำหรับ connect k8s cluster และ สร้าง credential สำหรับ pull image จาก gitlab registry ดูวิธีได้จากที่นี่
โดย agent จะใช้ตรงบรรทัดนี้ KUBE_CONTEXT: your-gitlab-path/my-pro:react-app
และ credential ใช้ตรงบรรทัดนี้ — set imagePullSecrets[0].name=gitlabci-registry-credential
push code ขึ้น git repository
พอเราเขียน ci script เรียบร้อย และ สร้าง agent กับ credential เรียบร้อยแล้ว ก็ git add , commit , push รอ ci run จนเสร็จ
Troubleshooting
ถ้า deploy ไม่ผ่าน เราก็จะดู status ของ pod หรือ deployment ก่อนว่าติดอะไรไหม
- ถ้าเป็น status ImagePullBackOff ก็จะ check imagePullSecrets, docker image, docker tag ถูกต้องไหม
- ถ้าเป็น status CrashLoopBackOff ก็ kubctl describe มาดูว่า มี error อะไรไหม ถ้าไม่มีก็จะดู kubctl logs ที่เจอส่วนใหญ่ จะเป็น health check ไม่ผ่าน เพราะ port ผิด หรือ path ของ liveness, readiness ผิด
อ้างอิง
- https://kodekloud.com/blog/fix-imagepullbackoff-errlimagepull-in-kubernetes/
- https://sysdig.com/blog/debug-kubernetes-crashloopbackoff/
จบแล้ว ขอให้สนุกกับการ deploy นะครับ