Kustomize your manifest, Build your project using GitHub Action CI and Deploy it to AKS using Argo CD.
Table of contents
- Introduction.
- Architecture.
- Prerequisites.
- What is Github Action and why do we use it?
- Set up your pipeline with GitHub action.
- What is Kustomize and why do we use it ?
- Kustomize your manifest.
- What is ArgoCD and why do we use it ?
- Install ArgoCD in Kubernetes.
- Set up and configure ArgoCD to listen from Kustomize.
- GitOps concept ?
- Conlusion
1. Introduction
Hello everyone! In this article we will discuss about how build your NodeJs project using GitHub action, how to install and configure ArgoCD in Kubernetes and then how to integrate it with Kustomize.
We will use a simple NodeJs project that output a simple webpage saying Hello-kubernetes.
2. Architecture
Architecture
3. Prerequisites.
- Git
- GitHub repository
- Kubernetes 1.10+
- Kustomize
- Docker hub account
4. What is Github Action and why do we use it?
GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management.
GitHub action has proven its capabilities in the CI/CD field through few features comparing to its rival (Jenkins).
The use of GitHub action is decided because of its features , its flexibility.
GitHub action requires no installation as it is on the cloud. Cool!
Asynchronous CI/CD , so less time consumed … then Faster!
5.Set up your Build pipeline on GitHub Action
First go to your GitHub repository where your project is located and press on “Actions” then press “New workflow”.
Start with GitHub action
Second , you have the possibility to choose either to template to start with or choose “set up a workflow yourself”.
Start new CI pipeline for your project
Once you choose it , you will be redirect to the below page.
New Build/CI pipeline
Here where you can write your own yaml code or choose the suitable plugin for your build. In this article , we will build a NodeJs project so let’s check my CI pipeline.
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
pull_request:
branches: [ main ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: '12.x'
- run: npm install
- run: npm run
- run: npm test
- name: Check Out Repo
uses: actions/checkout@v2
- name: Login to Docker Hub
env:
DOCKER_USER: ${{ secrets.DOCKER_HUB_USERNAME }}
DOCKER_PASS: ${{ secrets.DOCKER_HUB_PASSWORD }}
run: |
docker login -u $DOCKER_USER -p $DOCKER_PASS
- name: Set up Docker Buildx
run: |
docker build .
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
context: ./
file: ./Dockerfile
push: true
tags: ${{ secrets.DOCKER_HUB_USERNAME }}/yourimagename:yourtag
- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
We can see here the use of environment variables :DOCKER_USER and DOCKER_PASS. GitHub action allows us to store the secrets as follow.
Store credentials in GitHub actions
6. What is Kustomize and why do we use it ?
Kustomize is a configuration management tool for the Kubernetes ecosystem.
It is is a declarative templating engine that works off a concept of refactoring Kubernetes manifests.
7. Kustomize your manifest.
Your_project_name
---base
--deployment
--kustomization
---overlays
--dev
--replica-count.yaml
--kustomization.yaml
--prod
--replica-count.yaml
--kustomization.yaml
Follow the above structure to create your kustomize templates.
We will start by creating the base.
Base creation
Step1: Create a folder “kustomize”
Step2: Create a folder “application” inside “kustomize” folder.
Step3: Move your resources files into “application” folder.
Step4: Create “kustomization.yaml” file.
Step5: Add the below code
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- service.yaml
- deployment.yaml
Cool!!! Now we have created our Base.
Now we can start creating the overlay.
Overlay creation
Step1: Create a folder “environments” inside “kustomize”.
Step2: Create two folders: dev and prod
Step3: Create “kustomization.yaml” file inside each folder (dev and prod).
Step4: Pass the below script to each “kustomization.yaml” file in both directories (dev and prod).
bases:
- ../../application
Step5: We want to update the replicas count , so we need to create a patch for that. We go inside (dev and prd) folder and we create a file called “replica_count.yaml”
Step6: We add each of the below scripts in the “replica_count.yaml”file in both directories.
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-kubernetes
labels:
name: hello-kubernetes
spec:
replicas: 6 <------- We want to change this to 6 in dev
--------------------------------------------------------------------
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-kubernetes
labels:
name: hello-kubernetes
spec:
replicas: 8 <-------- We want to change this to 8 in prd
Step7: Go back to the “environments/dev/kustomization.yaml” and add the patche related to that update
Amazing! We’ve just created our overlay.
Our Kustomize template is ready now.
8. What is ArgoCD and why do we use it ?
Argo CD is a declarative, GitOps continuous delivery tool for Kubernetes.
Application definitions, configurations, and environments should be declarative and version controlled. Application deployment and lifecycle management should be automated, auditable, and easy to understand.
9. Install Argo CD in Kubernetes.
In our case , I am installing Argo CD in Azure AKS, but everything remains the same if you are installing it on your own cluster , just consider another solution instead of the load balancer if you are planning to expose your Argo CD UI to the external world , as load balancers are available only on cloud environment.
Step1: Create a namespace in your cluster and call it Argo CD
kubectl create namespace argocd
Step2: Apply the install manifest.
kubectl apply -n argocd -f
https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
Step3: In my case I would not expose the UI for the external world , instead , I would use port-forwarding.
kubectl port-forward svc/argocd-server 8080:443 -n argocd
Step4: Once connected , we go to our browser and tape “localhost:8080” and we will have Argo CD UI in front of us.
Argo CD UI
Your username would be : admin
Your password would be : the name of Argo server pod , should be something similar to : “argocd-server-xxxxxxxxx”.
Step5: Access the portal.
Portal Argo CD
10. Set up and configure ArgoCD to listen from Kustomize.
Now that Argo CD is installed , accessible from the UI , we can start creating our application. From now on , you can forget about the CLI and the commands as Argo CD makes your life easier. Want to see how ? follow the article until the end.
Step1: We will need to create a “New app”, then press on the top left on its button
Create a new application
Step2: Name your application and choose your sync policy. In our case, I am using “Automatic” sync policy to synchronize automatically when any change is detected in Master branch.
You can use the “AUTO CREATE NAMESPACE” feature in case your want your namespace to be set up automatically, otherwise add it manually with during the configuration.
Add project name and sync policy and option
Step3: Add your repository URL and the path of your Kustomize.
In our case , “kustomize/environments/dev” is the path of our Kustomize templates.
For destination in Cluster URL field , as long as we’re installing Argo CD in the same cluster where the application would be deployed , then we are in “in-cluster” situation , so add “https://kubernetes.default.svc” as cluster URL.
For more detail about each field , please refer to the official documentation.
Config the source and destination
Step4: Once all done , the projects would be created as we prepared it (in dev and prd)
Application created in Argo CD
Step5: Click on the your project name and you will be redirected to the below page where the beauty of Argo CD starts.
You would find reflected here all your kubernetes resources (Services,pods, deployment…) related to the our deployment in dev. Same thing is done for the prd.
Display of Kubernetes resources in Argo CD
Step6 : Now if we update our “replica_count.yaml” file in the kustomize folder and switch the replica set number to one replica set , the change should be immediately and automatically reflected to kubernetes and new GitHub actions build is triggered.
Change replica set to 1
Kubernetes resources in Argo CD UI
We can see here that the number of pods is reduced to one as we wanted by changing the replica set to 1.
Our application is accessible now. As its service type is loadBalancer , you just has to get the EXTERNAL-IP and the PORT and paste it to your browser to see your app.
Please do not forget that we have two namespaces : Dev and Prd so our application will be deployed in two different namespaces.
Argo CD application deployment in two namespaces
Note: Argo CD is able to listen to changes in docker Hub and other images repos , like that , with any new build triggered in GitHub actions, a new image would be published to docker hub , Argo CD will pull the image from the Hub and deploy it to kubernetes using a feature called “ Argo CD image updater” but in this article , we will not use that feature as it is still under development by Argo CD developers , once the first release is available , we would add it to this article. For more details , access this link.
This flexibility linking Git repository to Operation and orchestration is called GitOps concept.
11. GitOps concept ?
GitOps combines both Git with Kubernetes properties and serves as an operating model for developing and delivering Kubernetes-based infrastructure and applications.
If you find this article helpful , please share it on your LinkedIn and tag my name.
12. Conclusion
GitOps is a very helpful concept used these days in every project as it saves time , provide flexibly and agility.