Open In App

Continuous Deployment to Kubernetes with GitOps

Last Updated : 01 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

GitOps is the mechanism that a few companies like Google, Lyft, etc., has chosen to manage their workflows with. It is a modern approach to the implementation of mechanisms for sucking CD/CI (Continuous integration / Deployment) as git repositories are the only source of truth for your infrastructure and application code This approach makes the deployment process convenient and also makes it possible for the code to move between different environments as a result. By using git for everything, variants can guarantee that the kit is always automatically running the applications, which reduces human error and increases productivity through automation

GitOps Workflow: From Git Repository to Kubernetes Deployment
GitOps Workflow: From Git Repository to Kubernetes Deployment

Importance of GitOps in CI/CD

The very first and amazing advantage of GitOps in the CI/CD pipeline is that it delivers a clear, regular, and fast procedure to size deployments. Teams with Git as their truth source:

  • Consistent Environment: Good security automation automates the deployment with only approved code changes, always aligning the system with the latest Git code to ensure there is no room for errors coming from human carelessness.
  • Complete change history: All changes are stored in Git, providing an overview of the entire history and revision purposes.
  • Error reduction through automation: Automation practice removes human errors, hence creating a safer environment. Let's assume that you encounter problems, you can just reset everything by using Git which will just keep the new state of the deployment alive.

What is Continuous Deployment to Kubernetes with GitOps ?

A Continuous deployment is a basic practice in modern software development which makes it a piece of cake to introduce new features and the fixing of the faults. This is basically updating the code automatically in the production environment so that applications work up-to-date and correctly. Kubernetes is very important in this area, it performs the deployment of application containers, scaling, and running them across several machines. The GitOps is the element that contributes to this by the use of Git repositories to decide how infrastructure and applications should be set up. That way it will be a sufficiently mature method of deployment that is clear, consistent, and absolutely secure. By addressing both the config management and deployments through Git, operational teams can better manage infra and make it more reliable.

intro_dai
Continuous Deployment to Kubernetes: A GitOps Workflow

Key Terminologies

  • Kubernetes: To explain it simply this is a tool that instructs several containers (small, movable packages of software) on various computers. It behaves like a traffic controller that monitors that all functions of the application are working properly even if they are spread out across multiple machines.
  • GitOps: Instead of the traditional method of managing your application and infrastructure, using Gits repository is introduced. That means you will store all the deployment configurations in Git repositories, and whenever you update these configurations, the changes are automatically applied to your environment.
  • Argo CD: Is a tool that makes GitOps the way for Kubernetes easy by the continuous delivery process. It is the tool to ensure that the state of your Kubernetes applications always aligns with repositories and that can be done using a very simple GitOps method. Argo CD is capable of handling that if you update your application code or configuration in Git in which case it will make sure these changes are reflected in your Kubernetes cluster.
  • Flux: Talking about Flux is like talking about Argo CD, they both are a set of tools aimed at maintaining the constant sync between your Kubernetes clusters and Git repositories. It is in a continuous mode to watch the changes in the Git repository and the step of downloading the newest applications to your cluster is shown by that of deployment, while keeping your deployments always in the latest state to be executed.

Choosing a GitOps Tool: Argo CD

When you are installing GitOps, there are a number of popular tools that you can go with. Argo CD and Flux are two of the most popular ones. By doing this, the two software products automate the deployment process by continuously tracking the synchronization of your Git repositories and Kubernetés clusters. We will only talk about Argo CD as it has all the characteristics that are expected from it, such as its powerful features and user-friendly controls until now.

Feature

ArgoCD

Flux

Architecture

it makes use of a setup where a main controller is the one that syncs with Git repositories and then deploys applications based on the configurations you specified.

It employs a very refined way that is easy to control by controllers who can do things like synchronization with Git without any effort, thus enabling the changes implementation of on their own.

Customization

Gives the developer the provision to define custom features and hooks which allow changes and additions to the part of the repo before and after the completion of the sync process.

It is easy to use to integrate with Kubernetes and Git as expected but some more complicated operations might require additional plugins or configuration.

Integration

Handles Kubernetes communication and Git, thus recovering from up with the control GitOps workflows as well as the inclusion of the CI/CD chains.

it supplies some crucial settings for alteration by including more new CRDs, but at the same time keeps easiness of work in sight.

Setting up a Local Kubernetes Cluster

First, we'll set up a local Kubernetes cluster by using Minikube

Step 1: Install Minikube

First, you need to download Minikube to your machine. Minikube is a tool allowing you to have Kubernetes running locally. Use the Minikube installation guide to install Minikube on your operating system (Windows, macOS, or Linux).

Minikube
  • The above output informs us that Minikubes installation has completed (successfully).

Step 2: Start Minikube

Once you have Minikube installed, you can deploy a local Kubernetes cluster. Open your terminal (Command Prompt, PowerShell, or your preferred terminal application) and run the below command.

minikube start
minikube start
  • The minikube start command successfully sets up a local Kubernetes cluster and provides necessary information to confirm installation and configuration.
  • And I set minikube start by limiting CPU and memory allocations to address the issue of low laptop storage.

Step 3: Verify the Cluster is Running

As soon as Minikube starts, the first thing to do is make sure that your Kubernetes cluster has successfully gotten up and is running. Kubectl is a command-line utility that is used to manage K8s clusters. If you don’t have kubectl installed, follow the kubectl installation guide.

 kubectl cluster-info
cluster-info
  • The above command gives information about the cluster. If that is the case, you should get the required information from your Kubernetes master and services. It means your local Kubernetes cluster works well and is ready for use.

Installing And Setting Up Argo CD

Follow the steps carefully to have Argo CD installed in your own Kubernetes cluster. To start with, we will be using Minikube as our Kubernetes cluster; however, you can follow these steps with any Kubernetes setup.

Step 1: Create a Namespace for Argo CD

In the first place, you have to create a separate namespace in your Kubernetes cluster for Argo CD. This is the name space that can be utilized to isolate resources and deployments that are connected with Argocd. Now open the terminal and run the below command

 kubectl create namespace argocd1
namespace
  • The above result will show that the namespace argocd1 has been created.

Step 2: Install Argo CD

Next, you should approve the Argo CD installation manifests. All of these manifests specify the components necessary to index the resources and user interfaces of Argo CD in the cluster, and then you need to enter the below command.

 kubectl apply -n argocd1 -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
  • This command gets and installs Argo CD components from the repository of the project and sets them up.

Step 3: Verify The Status Of Pods

kubectl get pods -n argocd1
pods
  • The above command retrieves information about the pods. Kubernetes which are currently in the namespace of "argocd1". As a result, you can later find whether the ArgoCD components are running or not within the intended namespace.

Step 4: Port Forwarding

Now, next step would be to install Argo CD and log in to its web user interface (UI) so that you can manage your deployments in a visual view and this is what you have to do to forward a port from your local machine to the Argo CD server, execute the below command

kubectl port-forward svc/argocd- server -n argocd1 8080:80
port-forward
  • With this command, you will forward your local 8080 port to port 80 of the Argo CD server then you can access the UI at http://localhost:8080.

Step 5: Access the Argo CD UI

Post the Argo CD UI on your browser, sign in with admin username and the password that was released from the argocd-initial-admin-secret. You can get the password by executing the below command in the terminal.

 kubectl get secret argocd-initial-admin-secret -n argocd1 -o jsonpath="{.data.password}" | base64 --decode
  • The above command can be used for recovering and decoding the root admin password for ArgoCD on the "argocd1" namespace, thus admitting to the ArgoCD web interface.
Argo CD UI
ArgoCD web interface

Step 6: Prepare your Application

  • Create a Git repository for your application manifests: Go to the github and log in with your account credentials. In the case of not having an account, since you don't have one , create your account for free. In the left corner "+", icon select New repository. Name the repository of yours "my-app-config" (Add description if you like). Select the repository's visibility (public or private) and then move your cursor to the "create new repository" and click on it
  • Set Up Your Local Environment: The first action is to install Git on your computer if you have not already. Now open your terminal or command prompt and clone the repository on your local machine
git clone https://github.com/<your-username>/my-app-config.git
cd my-app-config
Prepare your Application

Step 7: Create Kubernetes Manifests

We generate ArgoCD applications that expose Kubernetes manifests which are used to define and regulate Kubernetes resources states , i.e., to ensure automatic application and maintenance of a deployment, service, or configuration demanded by the cluster.

  • Now create a file named deployment.yaml in the my-app-config directory and add the following content to it:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: nginx:latest
ports:
- containerPort: 80
  • Note: if you introduce the command from below, you will be permitted to add the mentioned content on the deployment.yaml file in my-app-config location quickly from the terminal itself.
notepad deployment.yaml
  • In the same directory, create a new file named service.yaml and add the following content:
apiVersion: v1
kind: Service
metadata:
name: my-app
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
  • Now, just add the created files to Git, then commit your changes, and push your changes to the GitHub repository with the below commands.
git add deployment.yaml service.yaml
git commit -m "Add initial Kubernetes manifests"
git push -u origin main
demo8-(1)

Step 8: Create ArgoCD Application Manifest

Create ArgoCD Application Manifest to construct and maintain the app of yours in Kubernetes, so that ArgoCD can automatically deploy and sync it.

  • In the my-app-config directory, create a file named argo-app.yaml and add the following content
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app
namespace: argocd
spec:
project: default
source:
repoURL: 'https://github.com/<your-username>/my-app-config'
targetRevision: HEAD
path: .
destination:
server: 'https://kubernetes.default.svc'
namespace: default
syncPolicy:
automated:
prune: true
selfHeal: true
  • Make sure that your kubectl context isn't this neither clusters imaginary nor the argocd1 is in the namespace that does not exist. Now deploy the ArgoCD application
kubectl apply -f argo-app.yaml
ArgoCD application
  • Execution of the above command will result to the deployment of the application which is defined in the argo-app.yaml file to your Kubernetes cluster.

Create Argo CD Application in UI

  • The requirement for the application creation process, as it is, is that we should bind it with the repository from where it is recognized.
Argo CD Application in UI
  • Choose Connect repo and then choose connection type as through HTTPS and after that add your repository URL but, in the end click on connect
connect repo
  • At that point when we are connected, we can see that the connection is established without any issues.
 connection is established
  • Once the repository has been connected, go to the applications section, select "create an application," and fill in the required (below) details.
Application Name: my-app
Project: default
Sync Policy: Automatic or Manual (choose based on your preference)
Repository URL: https://github.com/your-username/my-app-config.git
Revision: main (or the branch you used)
Path: / (root of the repository or the path to the manifests if they are in a subdirectory)
Cluster URL: https://kubernetes.default.svc (assuming you're deploying to the same cluster where ArgoCD is running)
Namespace: default (or any other namespace you prefer)
  • Once you're done, move your cursor to the create button and click on it
  • After completion of the creation process, the application must be on the ArgoCD UI where it will be displayed.
my app
  • Now sync your application by hitting the Sync button. Automatically, this will release the manifests that were in the Git repository to your Kubernetes cluster.
  • You must ensure that you are seeing the application's status. If the configured details are all correct, the status will be Healthy and Synchronized.
application's status
  • Choose the application name to examine the transmitted resources. You should be able to take note of the Deployment and Service resources and see their status.
Blankdiagra

Set Up Continuous Deployment

  • Maintain the Git repository already with the application update., For example, modify the image version in deployment.yaml to 1.20.
image: nginx:1.20
  • The next step is to Commit and then push the change to the Git repository database.
deployment.yaml
  • ArgoCD initiates the Git repository to recognize the specifications of the Git repository and automatically implements these changes to the Kubernetes cluster.
ArgoCD
  • In the above screenshot image, there is a feedback from the sync status block where a comment "update the image version to nginx 1.20." is written.

By doing these steps, you are guaranteeing that a new architecture mechanism is built .This would involve you committing changes in your app configurations to the version control system, which eventually installs them on your Kubernetes cluster using ArgoCD.

ARGO CD is a component present in the troubleshooting-remediation-system and here it talks about the way to go in case of deployment failure or if one breaks the states on the cluster in Git such that they are different from the desired versions.

  • Rollback: Argo CD brings you the capability to restore a previous version of an application while deployment is still in progress thereby giving you the possibility of easily fixing the problem. You can reach this known good state easily with just a few clicks.
  • Health Checks: In addition to the previous, Argo CD is able to execute health checks by looking at your applications that help identify the existence of any problems or failures. Health check could be set to monitor your applications status by executing user-defined action when some issues are found.
  • Manual Intervention: When a specific case of task fails, one can manually restart it through manual intervention. Argo CD offers manual sync tools that permit you to manually synchronize your cluster or backfuse to the previous state when necessary.

Conclusion

In this post we learnt a step-by-step guide to deploying applications continuously to Kubernetes through a method called GitOps and covered needed steps such as setting up a Kubernetes environment, configuring a tool named Argo CD, and sustaining a workflow that automates deployments directly from Git repositories. By following these steps we make sure that our deployment is efficient and reliable


Next Article
Article Tags :

Similar Reads