LKE (Linode Kubernetes Engine) + Github Actions + ArgoCD 배포 파이프라인 구성 - 1. ArgoCD 설치 및 세팅
백엔드 애플리케이션을 배포하기 위한 간단한 쿠버네티스 deployment, service를 정의한다.
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-service
labels:
app: test
spec:
replicas: 1
selector:
matchLabels:
app: test
template:
metadata:
labels:
app: test
spec:
containers:
- name: test-service
image: bemodesty306/backend:fcfe93afe7f5565f05446d7ecbbfa914ceb7bac1
imagePullPolicy: Always
ports:
- containerPort: 8080
apiVersion: v1
kind: Service
metadata:
name: test-service
labels:
app: test
spec:
ports:
- port: 80
targetPort: 8080
protocol: TCP
selector:
app: test
type: LoadBalancer
LoadBalancer 타입의 서비스 엔드포인트로 80번 포트 요청을 보내면 8080 포트를 사용하고 있는 파드에 요청을 전달할 수 있다.
ArgoCD는 애플리케이션 단위로 서비스를 분류하고 관리한다. 이 또한 yaml 파일로 정의하여 선언적으로 관리할 수 있다.
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: test-service
namespace: argo # ArgoCD가 존재하는 네임스페이스
finalizers:
- resources-finalizer.argocd.argoproj.io
spec:
project: default
source:
repoURL: https://github.com/junho100/lke-ghactions-argocd.git # 쿠버네티스 리소스가 존재하는 레포지토리
targetRevision: master
path: kubernetes/backend # 레포지토리 내 ArgoCD가 감시하고자 하는 디렉터리
destination:
server: https://kubernetes.default.svc
# The namespace will only be set for namespace-scoped resources that have not set a value for .metadata.namespace
namespace: default
syncPolicy:
automated:
prune: true
selfHeal: true
위 yaml 파일을 쿠버네티스에 적용하여 애플리케이션을 생성한다.
kubectl apply -f app.yaml
이제 ArgoCD 세팅은 완성되었다.
Github Actions를 이용하여 도커 이미지 빌드, docker hub 푸시, 변경된 태그를 반영하는 코드 수정 커밋 작업을 정의한다.
name: ci backend
on:
push:
paths:
- "backend/**"
- ".github/workflows/backend-ci.yaml"
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3.3.0
- name: set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push
uses: docker/build-push-action@v2
with:
context: ./backend
file: ./backend/Dockerfile
push: true
tags: bemodesty306/backend:${{ github.sha }}
- name: Git config
run: |
git config --global user.email ${{ secrets.GIT_EMAIL }}
git config --global user.name ${{ secrets.GIT_USERNAME }}
- name: Check out k8s repo
uses: actions/checkout@master
with:
repository: ${{ github.repository }}
token: ${{ secrets.GIT_ACCESS_TOKEN }}
- name: git push image tag to k8 repo
env:
GITHUB_REPO: ${{ github.repository.name }}
GITHUB_SHA: ${{ github.sha }}
run: |
sed -i "s|image:.*|image: bemodesty306/backend:$GITHUB_SHA|g" kubernetes/backend/deployment.yaml
git add .
git commit -m "Deploy: deploy image tag $GITHUB_SHA to k8s repo"
git push -f --set-upstream origin master
📦디렉터리구조
┣ 📂.github
┃ ┗ 📂workflows
┃ ┃ ┗ 📜backend-ci.yaml # 백엔드 애플리케이션 CI를 위한 Github Actions
┣ 📂backend # 백엔드 애플리케이션 & Dockerfile
┣ 📂kubernetes
┃ ┣ 📂argocd
┃ ┃ ┣ 📂app
┃ ┃ ┃ ┗ 📜backend-application.yaml # ArgoCD 애플리케이션 정의
┃ ┃ ┗ 📂helm
┃ ┃ ┃ ┗ 📜values.yaml # ArgoCD helm 추가 설정 정의
┃ ┗ 📂backend
┃ ┃ ┣ 📜deployment.yaml # 백엔드 애플리케이션 deployment 리소스
┃ ┃ ┗ 📜service.yaml # 백엔드 애플리케이션 service 리소스
┣ 📜.gitignore
┗ 📜README.md
백엔드 애플리케이션 코드 변경 후 배포가 잘 되는지 확인한다.
Github Actions
이미지 태그 변경
ArgoCD 배포
정상적으로 코드 변경을 감지하고 새로운 파드를 배포하는 것을 볼 수 있다.