How to deploy an application in Kubernetes using Argo CD

0
5799
How to deploy an application in Kubernetes using Argo CD

So far, we have seen what is GitOps and ArgoCD, also how to setup argoCD in Kubernetes. We have covered with Azure Kubernetes, as this is managed Kubernetes, should be same across all the platforms. We may seen how to provision in coming posts. In this post will see how to deploy an application in Kubernetes using Argo CD and GitOps. There may question, what is the GitOps, if you are not seen our previous post about it. Here is some basic information about it.

“GitOps is an operating model pattern for cloud-native applications & Kubernetes storing application & declarative infrastructure code in Git as the source of truth used for automated continuous delivery.”  GitOps keep Git at the centre of continuous delivery making git the Source of Truth describing the desired state of your entire system. You can read more about GitOps on our recent post for better understanding. https://foxutech.com/lets-understand-about-gitops/

Okay, now we seen GitOps, what is Argo CD? “Argo CD is a tool which will read your environment configuration (written either as a helm chart, kustomize files, jsonnet or plain yaml files) from your git repository and apply it to your Kubernetes namespaces. Some of the features of Argo CD are: declarative and version-controlled application deployments.” You can see more details about in https://foxutech.com/setup-argocd-on-azure-kubernetes-services/

Okay hops you got some understanding about the GitOps and ArgoCD, now lets see more details about the deployment. In this post we are going to Azure Kubernetes Services as platform. Please feel free to use your own preferable platform.

Prerequisites

Deploy Sample Application using Argo CD

In this post, I am going to deploy some sample application to see how it works. Follow the article to try yourself in your environment. Please note in this I am going to explore CLI to deploy the application. In last Argo CD post we have seen how to install the CLI. Lets see how to install linux machine,

# curl -sSL -o /usr/local/bin/argocd https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64

# chmod +x /usr/local/bin/argocd

Once you have installed, you should to login to the tool via CLI or UI to create the application. To login via CLI,

# argocd login {{ argocd_server IP/Domain }}:Port

Make sure you have retrieved the default password using following command,

# kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d; echo

You can use the password to login in both CLI and UI, username is “admin”. Please use your own password, if you changed earlier. Or you can change the password on UI or use following command to reset via CLI.

# argocd account update-password

Now you have reset the password, use this going to forward to login your ArgoCD. With this we are all set with ArgoCD, now we can proceed to deploy the application. As mentioned earlier, in this we are going to take example from Argo CD project’s example application.

This example contains different kinds of Kubernetes manifests, we are going to try helm-guestbook which uses helm chart. Let’s Start.

For testing we are going to create new namespace to check the feasibility about how we can deploy the application on different namespace.

# kubectl create ns argohelmtest

Now let’s use argocd CLI to create the new application on argocd, for that we are going to provide the GIT repository path and default destination and namespace.

# argocd app create helm-guestbook --repo https://github.com/argoproj/argocd-example-apps.git --path helm-guestbook --dest-server https://kubernetes.default.svc --dest-namespace argohelmtest

After “creating” the application inside of Argo CD, you can check its status with following command.

# argocd app get helm-guestbook
# argocd app get helm-guestbook
Name:               helm-guestbook
Project:            default
Server:             https://kubernetes.default.svc
Namespace:          argohelmtest
URL:                https://20.85.176.86/applications/helm-guestbook
Repo:               https://github.com/argoproj/argocd-example-apps.git
Target:
Path:               helm-guestbook
SyncWindow:         Sync Allowed
Sync Policy:        <none>
Sync Status:        OutOfSync from  (53e28ff)
Health Status:      Missing

GROUP  KIND        NAMESPACE     NAME            STATUS     HEALTH   HOOK  MESSAGE
       Service     argohelmtest  helm-guestbook  OutOfSync  Missing
apps   Deployment  argohelmtest  helm-guestbook  OutOfSync  Missing

You can see the sync status is out of sync when you deploy first time, this is normal, as you’ve retrieved the application’s helm chart from Github and created an entry for it in Argo CD, but you haven’t actually created any Kubernetes resources for it yet. In order to actually deploy the application, you need to run following command.

# argocd app sync helm-guestbook

sync is synonymous with deployment here in keeping with the principles of GitOps – the goal when using Argo CD is for your application to always track 1:1 with its upstream configuration.

# argocd app sync helm-guestbook
TIMESTAMP                  GROUP        KIND   NAMESPACE                    NAME    STATUS    HEALTH        HOOK  MESSAGE
2022-05-07T06:24:43+00:00            Service  argohelmtest        helm-guestbook  OutOfSync  Missing
2022-05-07T06:24:43+00:00   apps  Deployment  argohelmtest        helm-guestbook  OutOfSync  Missing
2022-05-07T06:24:44+00:00            Service  argohelmtest        helm-guestbook  OutOfSync  Missing              service/helm-guestbook created
2022-05-07T06:24:44+00:00   apps  Deployment  argohelmtest        helm-guestbook  OutOfSync  Missing              deployment.apps/helm-guestbook created
2022-05-07T06:24:44+00:00            Service  argohelmtest        helm-guestbook    Synced  Healthy              service/helm-guestbook created

Name:               helm-guestbook
Project:            default
Server:             https://kubernetes.default.svc
Namespace:          argohelmtest
URL:                https://20.85.176.86/applications/helm-guestbook
Repo:               https://github.com/argoproj/argocd-example-apps.git
Target:
Path:               helm-guestbook
SyncWindow:         Sync Allowed
Sync Policy:        <none>
Sync Status:        Synced to  (53e28ff)
Health Status:      Progressing

Operation:          Sync
Sync Revision:      53e28ff20cc530b9ada2173fbbd64d48338583ba
Phase:              Succeeded
Start:              2022-05-07 06:24:42 +0000 UTC
Finished:           2022-05-07 06:24:44 +0000 UTC
Duration:           2s
Message:            successfully synced (all tasks run)

GROUP  KIND        NAMESPACE     NAME            STATUS  HEALTH       HOOK  MESSAGE
       Service     argohelmtest  helm-guestbook  Synced  Healthy            service/helm-guestbook created
apps   Deployment  argohelmtest  helm-guestbook  Synced  Progressing        deployment.apps/helm-guestbook created

you can rerun the get status,

# argocd app get helm-guestbook
Name:               helm-guestbook
Project:            default
Server:             https://kubernetes.default.svc
Namespace:          argohelmtest
URL:                https://20.85.176.86/applications/helm-guestbook
Repo:               https://github.com/argoproj/argocd-example-apps.git
Target:
Path:               helm-guestbook
SyncWindow:         Sync Allowed
Sync Policy:        <none>
Sync Status:        Synced to  (53e28ff)
Health Status:      Healthy

GROUP  KIND        NAMESPACE     NAME            STATUS  HEALTH   HOOK  MESSAGE
       Service     argohelmtest  helm-guestbook  Synced  Healthy        service/helm-guestbook configured (dry run)
apps   Deployment  argohelmtest  helm-guestbook  Synced  Healthy        deployment.apps/helm-guestbook configured (dry run)

Now you have deployed an application using Argo CD successfully! As we mentioned before these steps you can achieve in UI also, but it is usually quicker and more reproducible to deploy via the command line. However, it is very helpful to check on your Argo CD web dashboard after deployment in order to verify that your applications are running properly. Access the UI, how you have exposed it. My case I have used loadbalancer, so I use via IP address.

argocd sample app

At this point, the last thing to do is to ensure you can access your new deployment in a browser. To do that, you’ll forward another port, the way you did for Argo CD itself. Internally, the helm-guestbook app runs on the regular HTTP port 80, and in order to avoid conflicting with anything that might be running on your own port 80 or on the port 8080 you’re using for Argo CD, you can forward it to any port:

# kubectl port-forward svc/helm-guestbook 9090:80

Or patch with loadbalancer.

# kubectl patch svc helm-guestbook -n argohelmtest -p '{"spec": {"type": "LoadBalancer"}}'

I am using loadbalancer for my examples, hence i get the IP and access the app like below.

sample guestbook

Please note, Any further pushes to this Github repository will automatically be reflected in ArgoCD, which will resync your deployment while providing continuous availability.

gitops books
Google search engine