Azure Function App on Kubernetes with GitHub Actions and Argo Cd Series Part-II
Welcome to the Part-II of Azure Function App on Kubernetes with GitHub Actions and Argo CD. In last part we have seen how to use GitHub Action to build and publish an HTTP Trigger based Azure Function Image to GitHub Container Registry. Today we will see how to deploy the Image to Kubernetes using Argo CD, so let’s start.
Before we start, a few things we need,
- A Kubernetes Cluster, we can use the Cluster provided by Docker Desktop. If you don’t have any local Kubernetes Cluster, please use below link to get started, https://docs.docker.com/desktop/kubernetes/
- Visual Studio Code( with Kubernetes Extension, it will help to create deployment and service related yaml)
- We will use the earlier GitHub repo here as well, https://github.com/arkapravasinha/K8SFunctions
Brief About Argo CD
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.
Brief About GitOps
GitOps is a way of implementing Continuous Deployment for cloud native applications. It focuses on a developer-centric experience when operating infrastructure, by using tools developers are already familiar with, including Git and Continuous Deployment tools.
The core idea of GitOps is having a Git repository that always contains declarative descriptions of the infrastructure currently desired in the production environment and an automated process to make the production environment match the described state in the repository. If you want to deploy a new application or update an existing one, you only need to update the repository — the automated process handles everything else. It’s like having cruise control for managing your applications in production.
Now we will start on our hands on.
First we need to install Argo CD in the local kubernetes cluster. To do that, you can run the below command in the command prompt,
After we execute the above commands, we will get a response like below,
Now Argo CD is installed in our cluster, lets explore what all get installed,
Argo CD can be accessed via Argo CD CLI and also via UI. Today we will do it via UI but you can also do it via CLI, it is really easy and very simple. For more information on CLI, please follow documentation available at https://argo-cd.readthedocs.io/en/stable/
We will use the Argo CD UI to create and setup the pipeline.
To open the UI, we need to expose the UI application to the Host network, to do that, please run the below command,
kubectl port-forward svc/argocd-server -n argocd 8888:443
Now lets got your browser of choice and open this link https://localhost:8888/ . This link will open Argo CD UI and it will look like below,
To Login , please use username as “admin” and to get the password, we again need to run one command,
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}"
This is Base64 encoded, please decode it using https://www.base64decode.org/ ,
After decoding the password let’s try to login,
As Argo CD installation is done, lets move on to VS Code and open the source code root folder, to create the deployment related files,
Lets create a new folder named deployments under the root directory and create two yaml files, named deployment-dev.yml and service-dev.yml
Let’s define our deployment,
Now our deployment is looking let’s define the service to make our deployment accessible to the host network,
Now that our Service and Deployment is defined, lets add a step in the earlier created GitHub Action workflow, to update the image version automatically in deployment-dev.yml after each deployments.
Go to .github →workflows folder and open docker-publish.yml in VS Code
And add new step to update the deployment-dev.yml
Let’s push our changes and see if it is working or not.
As you can see, our code has been built success successfully and also updated the image version
Now our CI pipeline is completely ready to complete our Argo CD Setup. Now we will go to Argo CD and create our deployment pipeline.
In Argo CD UI home Page click on +New App , it will open a new blade for creating an application, then click on Edit as YAML .
So basically, we are providing GitHub Repo details and branch name, path to the deployment files and also, Kubernetes Cluster (For configuring different clusters, please use Settings Menu option in Argo Cd ) and Kubernetes namespace, if namespace is not there it will create a new namespace. We are keeping the Sync Option as Manual for our case. Please remember this is public repo, that’s why we do not need any kind of authentication, else we need to provide
Hit on Save, it will take you the earlier blade with prefilled data, it will look like below,
Lets hit on Create, it will create our Application Deployment pipeline.
Now we are all set for our deployment, let’s deploy it by clicking on Sync.
Now please click on, Synchronize, it will deploy the application and also maintain the state of it in its own database. After this please click on the Application Tile, it will take you to deployments.
As you can see k8sfunctions-deployment and k8sfunction-service and their related child items created.
Let’s hit our application by going to http://localhost:8080
Voila, our application is running in Kubernetes and accessible to host network. Lets test it out if HTTP Api endpoint is working or not by going to http://localhost:8080/api/swagger/ui
We can also check the container logs in Argo CD UI, by clicking on the Pod and going to Logs tab
Now our deployment setup is complete.
Let’s say, now if we have to add another function to our Function App, then see how our Application Deployment looks like,
You can add the function in VS 2022 by right clicking on the project → Add → New Azure Function
Then Select the HTTP Trigger with OpenAPI and Anonymous as Authorization Level and click on Add.
It will add a new function, please look at below, then commit and push the changes to remote repository.
It will trigger our build and create a new image and update the version of the image in the deployment file.
Now as our image version is updated in the deployment files , lets go to Argo Cd and see the status. It will tell us that our service to expose the pod is in sync but deployment for application container is out of sync. (Argo Cd is maintaining the state of our deployments)
Let’s sync it again by clicking on Sync. It will create a new Replica Set and deploy our Pod. For Rollback purpose it will keep the older Replica Set.
You can see it has created new Replica Set and Pod , it will delete the older Pod once this new Pod is healthy.
If you want you can rollback your changes, by clicking on History and Rollback.
Lets see the results,
As you can see it is that is to create a deployment pipeline with Argo CD.
Our Function App is running and breathing inside Kubernetes. You can use KEDA to scale the Function App similarly as it is scaled in Azure APP Service Plan.
References,
- Argo CD: https://argo-cd.readthedocs.io/en/stable/
- Kubernetes: https://kubernetes.io/
- Replica Set: https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/
- Deployments: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
- Services: https://kubernetes.io/docs/concepts/services-networking/service/
Please drop an email to sinha.arkaprava@outlook.com in case of any queries.
Please watch it this space for more interesting blogs.