Dernière mise à jour : 8 sept. 2021
Who said practising GitOps always requires complex tooling and expensive infrastructure, even for simple scenarios ? In this article, we will demonstrate how to setup (and run) a GitOps environment with K3D and Argo CD for local development.
Operating an application the GitOps way on a local computer can be useful in multiple scenarios:
Provisioning Kubernetes clusters in your organization is not an easy thing (authorizations, delays, etc...)
Your organization is still is on its Kubernetes journey but not ready yet. Developers and testers need to work on Kubernetes environments in the meantime.
What if we could have an inexpensive and hassle-free way to get a complete Kubernetes environment close enough to staging and production ?
A minimal local GitOps setup
k3d as described on its website:
K3D is a lightweight wrapper to run* k3s (Rancher Lab’s minimal Kubernetes distribution) in docker. k3d makes it very easy to create single- and multi-node k3s clusters in Docker, e.g. for local development on Kubernetes.
It is a cost-effective solution for developers to locally provision lightweight Kubernetes clusters. Finally, with ArgoCD synching the cluster resources with our git hosted manifests, we get a first-class, costless, GitOps oriented workflow with no big compromise !
This is a graphical overview of the setup we're going to achieve with this article. We will focus our scope only on feature branches: ideally your other branches (staging, release branch, master) are already covered by the CI/CD pipelines on dedicated clusters. Our approach is the pull approach, as the changes are detected and driven from within the cluster (cluster credentials aren't exposed outside).
Our local cluster will have two namespaces:
argocd: for ArgoCD application and the additional configuration for our setup
dev: for our application itself
ArgoCD will keep in sync the kubernetes manifests of our application hosted on a Github repository branch and a the dedicated local dev namespace.
Setup a local cluster with K3D
We've covered already k3d and k3s in a previous post. In the meantime, k3d has been significantly rewritten and gets only better and better, but some of the commands aren't compatible with the previous versions. For this article we're gonna use the freshly released v3.0.0 by following the installation instructions. Our local environment is an Ubuntu 20.04.
$ sudo wget https://github.com/rancher/k3d/releases/download/v3.0.0/k3d-linux-amd64 -O /usr/local/bin/k3d $ sudo chmod +x /usr/local/bin/k3d
We verify the setup is correct by running k3d version command:
$ k3d version k3d version v3.0.0 k3s version v1.18.6-k3s1 (default)
We can install the auto-completion scripts (which are really helpful) using the k3d completion <shell> command and adding it to your shell resources. Eg for linux with bash:
$ echo "source <(k3d completion bash)" >> ~/.bashrc $ source ~/.bashrc
Let's setup our cluster with 2 worker nodes (--agents in k3d command line) and expose the HTTP load balancer on the host on port 8080 (so that we can interact with our application)
$ k3d cluster create my-cluster --api-port 6443 -p 8080:80@loadbalancer --agents 2
By default, creating a new cluster will:
update your kubeconfig file with the new context, cluster and user details (--update-default-kubeconfig flag)
make it the default (--switch-context flag)
So you can directly use the kubectl command:
$ kubectl cluster-info
Argo CD is a declarative, continuous delivery tool for Kubernetes based on the GitOps approach.
Application definitions, configurations, and environments should be declarative and version controlled. Application deployment and lifecycle management should be automated, auditable, and easy to understand.
Download ArgoCD 1.6.2 installation yaml:
$ wget https://github.com/argoproj/argo-cd/raw/v1.6.2/manifests/install.yaml