A Getting Started Guide to GitOps

Published:25 January 2024 - 6 min. read

Getting caught in the tangle of Kubernetes management is a familiar struggle, where orchestrating deployments becomes a web of complexities. If the need for a solution to streamline your Kubernetes workflows resonates with you, say hello to ‘GitOps.’

In this tutorial, you’ll unveil a powerful antidote to the common deployment headaches. Let GitOps emerge as the strategic solution to untangle your Kubernetes intricacies.

Confidence, control, and a newfound mastery over your infrastructure await!

Prerequisites

Before immersing yourself in this tutorial, ensure you’ve got the following prerequisites to follow along seamlessly:

Exploring the Essence of GitOps

As a developer, you’re most likely familiar with version control systems (VCS) like Git, which you use to keep track of changes in your code. Now, GitOps is a process centered around using Git as the backbone for managing infrastructure and deploying applications.

Instead of manually setting up and managing your infrastructure components, GitOps uses configuration files stored in Git repositories. This approach is particular but not restricted to Kubernetes-based environments.

These benefits include enabling your team to improve version control, traceability, and collaboration in your development and operations workflows. But mind you, your team may encounter some drawbacks, like compatibility issues and a complex setup.

Illustrating how GitOps works
Illustrating how GitOps works

GitOps falls under the DevOps umbrella but differs in tool usage. How? GitOps commonly utilizes tools like Kubernetes, specific Git-driven CI/CD pipelines, and Infrastructure as Code (IaC) tools.

In contrast, DevOps encompasses a broader range of tools and practices, including GitOps, without being tied to a specific tool like Git.

Illustrating how GitOps falls under DevOps
Illustrating how GitOps falls under DevOps

Verifying a Kubernetes Cluster for Setting Up a GitOps Scenario

Coming straight from exploring the essence of GitOps, you’ll now set the stage for a robust GitOps implementation — verifying a Kubernetes cluster.

Setting up a GitOps scenario involves implementing a workflow and tooling for infrastructure and application deployment management.

To verify a Kubernetes cluster, follow these steps:

1. Open a terminal, execute the following kind command to get all available Kubernetes clusters, and confirm you have an active one.

kind get clusters

In this tutorial, one cluster called sample-kind runs, as shown below.

Confirming an active Kubernetes cluster
Confirming an active Kubernetes cluster

The cluster’s configuration establishes a connection between the local environment and the Kubernetes cluster.

# Define a Kubernetes cluster using kind
kind: Cluster  
# Specify the Kubernetes API version for kind
apiVersion: kind.x-k8s.io/v1alpha4  
# Name of the cluster
name: sample-kind  
nodes:
# Single node with the role of a control plane
- role: control-plane  
  kubeadmConfigPatches:
  - |  # Kubernetes administrative configurations applied during initialization
    kind: InitConfiguration
    nodeRegistration:
      kubeletExtraArgs:
        # Set a custom label for the node indicating it is ready for ingress
        node-labels: "ingress-ready=true"
      extraPortMappings:
        # Map container port 80 to host port 80 with TCP protocol
        - containerPort: 80
          hostPort: 80
          protocol: TCP
        # Map container port 443 to host port 443 with TCP protocol
        - containerPort: 443
          hostPort: 443
          protocol: TCP

2. Next, run the kubectl command below to get all nodes available.

kubectl get nodes

As shown below, the output displays information about individual nodes that make up your Kubernetes cluster.

Viewing all available nodes
Viewing all available nodes

3. Lastly, run the below command to get all running services to ensure your service is deployed on your Kubernetes cluster.

kubectl get services

As in the output below, you’ll see the list of your services.

Listing all services
Listing all services

Installing the Flux CLI and Granting Flux Permissions

After confirming your Kubernetes cluster runs, you can set up your GitOps Scenario. This process involves installing a GitOps tool and granting permissions to access your private Git repositories.

This tutorial’s choice for a GitOps tool is Flux CD or Flux, an open-source continuous delivery tool. This tool monitors your Git repositories for changes in your Kubernetes manifests and automatically syncs the state you declared to the desired state in your Kubernetes cluster.

Before interacting with Flux, you must install the Flux CLI, which provides commands and functionalities.

💡 Note: This tutorial uses Flux because it has a large community and simpler setup and documentation than other tools.

To install Flux CLI and grant Flux permissions, carry out the following:

1. Run the brew command below to install Flux CLI on your system.

brew install fluxcd/tap/flux

💡 Explore the official documentation for instructions on installing the Flux CLI if you’re on a system other than macOS. The Flux CLI is also conveniently offered as a binary executable that is compatible with all major platforms (visit the GitHub releases page).

2. Once installed, execute the following commands to export your GitHub username and personal access token.

These commands have no output but grant Flux access to your private Git repositories. Thus, replace the <personal access token> and <username> placeholders accordingly.

These exports allow Flux to consistently observe your Git repositories for modifications and autonomously initiate updates in response to code changes. This functionality guarantees that your Kubernetes cluster aligns with the desired state defined in your Git repository.

export GITHUB_TOKEN=<personal access token>
export GITHUB_USER=<username>

3. Now, run the following flux command to check your Kubernetes cluster is suitable for Flux.

This command checks for compatibility, detects issues, and verifies tools and permissions.

flux check --pre

If the checks pass, you’ll see an output similar to the one below.

Checking the Kubernetes cluster is suitable for Flux
Checking the Kubernetes cluster is suitable for Flux

Bootstrapping Flux in the Kubernetes Cluster

With Flux installed and your cluster validated, you must ensure Flux is seamlessly integrated into your Kubernetes environment. You’ll bootstrap Flux in your Kubernetes cluster and connect it to your Git repository for a smoothly operating Kubernetes environment.

To bootstrap Flux in your Kubernetes cluster, perform the following:

1. Execute the flux bootstrap command below to perform the following and install Flux on your Kubernetes cluster:

  • bootstrap github – Indicate the bootstrap process is initiated with GitHub as the source for configuration and deployment.
  • --owner – Specify the owner of the GitHub repository, which you exported to the $GITHUB_USER variable.
  • --repository – Define the name of your existing GitHub repository (in this case, flux-sample).
  • --branch – Specify the branch to use within your GitHub repository for the Flux configuration (i.e., main).
  • --path – Denote the path within your GitHub repository where the Flux configuration files are located (i.e., clusters/sample-kind).
  • --personal – Suggest using personal access credentials for authentication with the GitHub repository.
flux bootstrap github \
--owner=$GITHUB_USER \
--repository=flux-sample \
--branch=main \
--path=clusters/sample-kind \
--personal

Before you dive deeper, look at the following illustration to understand the typical GitOps workflow.

When you make changes via pull requests to your Git repository, like updating Kubernetes cluster configuration, the pipeline triggers a GitOps tool. That tool (i.e., Flux, Argo CD, Jenkins X, and so on) can be used to implement the GitOps workflow.

Illustrating GitOps workflow
Illustrating GitOps workflow

2. Once bootstrapping is complete, visit your GitHub repository to verify your configuration files, as shown below.

Verifying the configuration files in the GitHub repository
Verifying the configuration files in the GitHub repository

3. Next, run the following k9s command to check the flux-system has been installed on your Kubernetes cluster.

k9s is a command-line interface (CLI) tool that provides a terminal-based graphical user interface (TUI) for interacting with Kubernetes clusters. This tool offers a more user-friendly and intuitive way to navigate and manage Kubernetes resources than raw kubectl commands.

k9s

4. Now, type :ns to enter the command mode and view the list of installed namespaces, select the flux-system namespace, and hit Enter. Doing so lets you switch to the flux-system namespace, switching the k9s context to operate within that specific namespace.

Viewing the list of namespaces installed in the Kubernetes cluster
Viewing the list of namespaces installed in the Kubernetes cluster

In the output below, you’ll see some controller pods installed automatically during the Flux bootstrap process. By viewing the controller pods, you can confirm the components and controllers required for Flux are running.

Congratulations! You’re now using the k9s tool to inspect the state of your Kubernetes cluster.

Viewing the controller pods installed and running on the flux-system namespace
Viewing the controller pods installed and running on the flux-system namespace

Conclusion

In this insightful journey of getting started with GitOps, you’ve embarked on a transformative exploration of orchestrating Kubernetes environments with Git at the helm. From understanding the fundamental principles of GitOps to setting up a practical scenario, you’ve learned the ropes and taken concrete steps toward a more efficient and streamlined workflow.

After diving into bootstrapping Flux in your Kubernetes cluster, you’ve unlocked the ability to manage your IaC seamlessly. This triumph marks the beginning of a more controlled and collaborative era.

Equipped with this newfound knowledge, why not extend your expertise by exploring advanced GitOps tools like Argo CD? Or fine-tune your CI/CD pipeline integration and dive deeper into Kubernetes orchestration?

The journey doesn’t end here; it evolves with every commit, deployment, and improvement!

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!