If you’re like me, managing Kubernetes deployments can sometimes feel like trying to juggle flaming torches. Kubernetes is powerful, but when it comes to handling API configurations alongside your applications, things can get a bit… chaotic.
That’s where GitOps comes into play. By using Git as the single source of truth, you can streamline your infrastructure and application configurations. And when you throw ** Operator** into the mix, managing your APIs becomes a whole lot easier.
In this post, we’ll walk through a practical setup that brings together Operator, ArgoCD, and Kustomize. We’ll start from scratch and build a GitOps workflow that automates the deployment of both your applications and API configurations. By the end, you’ll have a solid understanding of how to manage deployments in an organization using GitOps principles.
Here’s what we’ll cover:
Setting up the environment with Stack, Operator, and ArgoCD.
Organizing your repository for easy management.
Deploying applications and APIs simultaneously across environments.
Customizing configurations using Kustomize overlays.
Prerequisites:
Familiarity with Kubernetes and Kustomize (we’ll build on that knowledge).
Basic understanding of Git.
Access to two Kubernetes clusters.
Our example Git repository: (-operator-demo)
Why GitOps and Operator?
Before we dive in, let’s chat about why we’re doing this. Managing APIs in a Kubernetes environment can get complex, especially when scaling across different environments like staging and production. GitOps helps by treating your configurations as code, which means you get version control, peer reviews, and a clear audit trail.
But what about API management? That’s where Operator comes in. It’s a Kubernetes operator that lets you manage your API definitions and security policies as Kubernetes resources. So, you can handle your APIs just like any other Kubernetes object. Pretty cool, right?
Setting up the environment
Let’s get our tools in place. We’ll use the -k8s-demo repository to install Stack, which includes all dependencies and gets us up and running quickly.
1. Set up staging environment
1.1 Start Minikube and enable Ingress
In this example, we’ll run everything locally using Minikube, but feel free to deploy on AWS, GCP, Azure, or any other cloud provider.
This starts a Minikube cluster (named `staging`) and enables the ingress controller, which we’ll need later.
1.2. Install Stack using `-k8s-demo`
We’ll use the `-k8s-demo` repository to install Stack, which includes all dependencies and gets us up and running quickly.
1.2.1 Clone the repository
```bash
git clone https://github.com/Technologies/-k8s-demo.git
cd -k8s-demo
cp .env.example .env
```
1.2.2 Obtain a license
We need a license to run Dashboard.
Visit the sign-up page and choose ‘Get in touch’ to receive a guided evaluation and a temporary license. Then add your license key to the `.env` file `LICENSE` field.
1.2.3 Install Stack with Operator
Now, let’s run the installation script:
```bash
./up.sh --deployments operator -stack
```
If you want to deploy on Cloud, check out [Kubernetes Demo](https://.io/docs/getting-started/quick-start/-k8s-demo/#clusters) for deployment configurations for [AWS](https://github.com/Technologies/-k8s-demo/tree/main/src/clouds/aws/.env.example), [GCP](https://github.com/Technologies/-k8s-demo/tree/main/src/clouds/gcp/.env.example), and [Azure](https://github.com/Technologies/-k8s-demo/tree/main/src/clouds/azure/.env.example).
The script will take a few minutes to complete. It sets up everything we need, including API Gateway, Dashboard, and Operator.
1.3 Install ArgoCD
ArgoCD will handle the continuous deployment aspect of our GitOps setup.
Now, you can login to production ArgoCD admin UI at http://localhost:8001.
> [!NOTE] Note
> You could also run ArgoCD in a hub-and-spoke model, where you host ArgoCD in a centralized hub cluster.
You have now set up two clusters. Let’s continue to set up your continuous deployment (CD) repository.
Starting with the demo repository
To make things easier, we’ll start by forking the demo repository. This way, you have the freedom to commit changes to your Git repository without affecting the original repository.
1. Fork the repository
1.1 Go to the repository
Visit the -operator-demo and click on the “Fork” button in the top-right corner to create your own copy.
1.2 Clone your fork locally
Go to your working directory and then clone your fork:
```bash
git clone https://github.com/your-username/-operator-demo.git
cd -operator-demo
```
Replace `your-username` with your GitHub username.
Now, you’re all set to make changes and commit to your own repository.
2 Examine the repository
The repository is organized into three main directories:
Let’s break down each of these directories in detail.
2.1 `apps/` Directory
This directory contains Kubernetes manifests for deploying applications. In our example, we’re focusing on the `httpbin`application.
Structure:
The `httpbin` app directory is organized for Kustomize. The `base/` directory holds the common configuration for `httpbin`, such as container image and port settings. The `overlays/` directories (`prod` and `staging`) customize the base manifests for each environment.
- **`httpbin/`**: Directory for the `httpbin` application.- **`base/`**: Contains the base Kubernetes manifests that are common across all environments.- **`apidefinition.yaml`**: Defines the `ApiDefinition` resource for `httpbin`.- **`deployment.yaml`**: Defines the `Deployment` resource for `httpbin`.- **`service.yaml`**: Defines the `Service` resource to expose `httpbin`.- **`kustomization.yaml`**: Kustomize file that aggregates the base resources.- **`overlays/`**: Contains environment-specific customizations using Kustomize overlays.- **`prod/`**: Customizations for the production environment.- **`api_auth.yaml`**: Overrides the API authentication for production.- **`api_config.yaml`**: Overrides the API configurations for production.- **`kustomization.yaml`**: References the base and applies patches.- **`staging/`**: Customizations for the staging environment.- **`api_auth.yaml`**: Overrides the API authentication for staging.- **`api_config.yaml`**: Overrides the API configurations for staging.- **`kustomization.yaml`**: References the base and applies patches.
2.2`policies/` Directory
This directory stores security policy manifests, which govern security options and access rights that can be applied to an API key. Using a security policy to govern access gives you the flexibility to decouple API definitions from access and security policy settings, which may be set by different teams—for example, API definitions by API developers and security policies by API product managers.
Structure:
The policies directory is organized by environments. It allows different environments to have tailored security settings.
- **`policies/`**: Central location for security policies.- **`prod/`**: Policies for the production environment.- **`standard-policy.yaml`**: Defines security policies like access control, rate limits, and quotas for standard users.- **`trial-policy.yaml`**: Defines security policies for trial users.- **`staging/`**: Policies for the staging environment.- **`standard-policy.yaml`**: Similar to production but may have different settings.- **`trial-policy.yaml`**: Similar to production but may have different settings.
2.3`argocd/` Directory
This directory contains ArgoCD application manifests that automate the deployment process.
Structure:
The argocd directory is organized by environments. Each ArgoCD application point to the corresponding paths in the Git repository where deployment manifests can be found. The path can point to a directory with kubernetes manifest files, a Kustomize directory, or a Helm Chart.
- **`argocd/`**: Central location for ArgoCD application manifest.
- **`prod/`**: ArgoCD applications for the production environment.
- **`httpbin.yaml`**: ArgoCD application manifest for deploying the `httpbin` application. Specifies **`source`**: Points to the Git repository and the path `apps/httpbin/overlays/prod` for the `httpbin` manifests.
- **`policies.yaml`**: ArgoCD application manifest for deploying security policies. Specifies **`source`**: Points to the Git repository and the path `policies/prod` for the policies.
- **`staging/`**: ArgoCD applications for the staging environment.
- **`httpbin.yaml`**: ArgoCD application manifest for deploying the `httpbin` application. Specifies **`source`**: Points to the Git repository and the path `apps/httpbin/overlays/staging` for the `httpbin` manifests.
- **`policies.yaml`**: ArgoCD application manifest for deploying security policies. Specifies **`source`**: Points to the Git repository and the path `policies/staging` for the policies.
- **`prod-apps.yaml`**: ArgoCD "app of application" that points to the source directory `argocd/prod`
- **`staging-apps.yaml`**: ArgoCD "app of application" that points to the source directory `argocd/staging`
Deploy your application with ArgoCD
1. Update source repository
Modify all ArgoCD apps under `argocd` so that they point to your GitHub repository.
The API requires an auth key as specified in `api_auth.yaml`. You can go to the Staging Dashboard at [https://localhost:3000](https://localhost:3000/) to create an API key, and append the key in request header:
You can go to the production dashboard at [https://localhost:3001](https://localhost:3001/) to create an API key and include it in the request header to get a successful 200 response:
```bash
git add .
git commit -m "Enable CORS in staging API and add path-based permissions for trial users"
git push origin main
```
4.3 Watch ArgoCD sync the changes
Go back to the ArgoCD UI for the staging environment at [https://localhost:8000](https://localhost:8000/). You should see that the `httpbin-staging` and `policies-staging` applications have detected changes and are syncing automatically. Once the sync is complete, the new configurations are applied to your cluster.
Changes in custom resources `ApiDefinition` and `security policy` would be detected by ** Operator**, which will reconcile the changes with accordingly.
4.4 Verify the changes
Test the API endpoint:
First, create an API KEY for the “Trial policy (staging)” policy and save it in `API_KEY`.
You should see that it now return **403 Forbidden**, indicating that this endpoint has been disallowed.
By simply committing changes to your Git repository, you’ve updated your live configurations in without manually applying any changes to the cluster. This demonstrates the power of GitOps in automating and streamlining deployments.
Wrap up
By following this guide, you’ve set up a comprehensive GitOps workflow that automates the deployment of both your applications and API configurations across multiple environments. You’ve utilized Operator to manage your API definitions and security policies as Kubernetes resources, ArgoCD to automate continuous deployments, and Kustomize to handle environment-specific configurations without duplicating code.
Your teams can collaborate effectively using Git as the single source of truth, for both application deployment and API and security policy configurations. API platform team can easily exercise governance and ensure consistency on API configurations using Kustomize.
With this setup, you can now manage deployments in your organization more efficiently, ensuring consistency, reducing manual errors, and speeding up your development workflow.