From GIT to Kubernetes Development

From GIT to Kubernetes in 10 minutes with ArgoCD

23/06/21 7 min. read

As a senior software engineer, I’ve been using Continuous Delivery and Continuos Integration for over 15 years.

The idea behind of CI/CD is very simple: enable teams to automate code changes and deploy them to the testing or production environment easily. Besides automating the CI/CD process will save them time and eliminate human errors.

However, creating CI/CD workflows with tools like Jenkins or Gitlab is not as easy and quick as you may think.

Nowadays, the situation is even worse in the era of microservices when you need to orchestrate the deployment of tens or hundreds of services at the same time. What a nightmare!

However, what would you say if I can deploy a matrix of services in Kubernetes directly from GIT in less than 10 minutes? Don’t worry and let’s see for yourself ?

ArgoCD

For this challenge, we are going to use ArgoCD, a declarative, GitOps continuous delivery tool created for Kubernetes and a Cloud Native Computing Foundation incubating project.

ArgoCD uses Git repositories as the source of truth for defining the desired application state.

Some of its main features are:

  • Automated deployment of applications to specified target environments.
  • Compatible with KustomizeHelm, plain-YAML, etc.
  • Rollback to any application configuration committed in Git repository.

ArgoCD is implemented as a kubernetes controller which continuously monitors running applications and compares the current, live state against the desired target state as specified in the Git repoasspecifiedintheGitrepo.

 ArgoCD architecture

Pre-requisites

Before beginning the exercise, you will need:

1. Install ArgoCD – 1 min

As seen before, ArgoCD run as an operator and we have to deploy it in Kubernetes. Besides we need a new namespace argocd where ArgoCD services will run.

  • Create ArgoCD namespace:
kubectl create namespace argocd
  • Install ArgoCD in Kubernetes:
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

2. Configure ArgoCD – 2 min

Although the ArgoCD is already installed in our Kubernetes cluster, it cannot be accessed externally. In order to solve that, we will expose its API / UI server and configure the CLI to use it.

  • Expose ArgoCD API Server:
kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer"}}'
  • Config ArgoCD CLI with username admin and password admin:
kubectl -n argocd patch secret argocd-secret \
    -p '{"stringData": {"admin.password": "$2a$10$mivhwttXM0U5eBrZGtAG8.VSRL1l9cZNAmaSaqotIzXRBRwID1NT.",
        "admin.passwordMtime": "'$(date +%FT%T)'"
    }}'
argocd login localhost:10443 --username admin --password admin --insecure
  • Expose ArgoCD UI:
kubectl port-forward svc/argocd-server -n argocd 10443:443 2>&1 > /dev/null &
Login AgroCD
AgroCD Home Page

3. Create a deployment GIT repository – 2 min

As we have seen before ArgoCD use a GIT repository as the source of truth. Therefore a repository must be created with the Kubernetes application configuration in one of its compatible ways. In our case, we are going to use a simple K8s yaml file.

  • Create a repo in your Github account
gh repo create --public git2k8s10min -y && cd git2k8s10min
echo "From GIT to Kubernetes in 10 minutes with ArgoCD." > Readme.md
git add Readme.md
git commit -m "Added Readme.md" && git push --set-upstream origin master
  • Create live branch and push to Github with no kubernetes resources.
git branch live
git push --set-upstream origin live
  • Upload “Guestbook” deployment file and commit changes to Github:
git checkout master
curl -kSs https://raw.githubusercontent.com/kubernetes/examples/master/guestbook/all-in-one/guestbook-all-in-one.yaml -o guestbook_app.yaml
git add guestbook_app.yaml
git commit -m "Added guestbook_app.yaml"
git push --set-upstream origin master

4. Deploy using ArgoCD – 3 min

After the repo is ready, we must create an ArgoCD app using its own custom kubernetes resource. Furthermore, we are going to create a new namespace to deploy on it.

  • Obtain HTTPS url of the GIT repository:
HTTPS_REPO_URL=$(git remote show origin |  sed -nr 's/.+Fetch URL: git@(.+):(.+).git/https:\/\/\1\/\2.git/p')
  • Create k8s namespace:
kubectl create namespace git2k8s10min
  • Deploy App:
cat <<EOF | kubectl apply -f -
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: git2k8s10min
  namespace: argocd
spec:
  destination:
    namespace: git2k8s10min
    server: https://kubernetes.default.svc
  project: default
  source:
    repoURL: $HTTPS_REPO_URL
    path: .
    targetRevision: master
EOF
Agro CD deployed App
Agro CD deployed App
  • Although the app is created, it has not been “deployed”. So we have to synchronize the app manually:
argocd app sync git2k8s10min
Manual synchronization AgroCD
  • Check app status using Arcgocd CLI:
argocd app get git2k8s10min
  • Check Kubernetes resources using kubectl:
kubectl get -n git2k8s10min svc/frontend pods
  • Run the following command to forward port 18080 on your local machine to port 80 on the service.

kubectl port-forward -n git2k8s10min svc/frontend 18080:80

Finally again, test http://localhost:18080 in your browser to view Guestbook app.

5. Deploy again but in auto mode – 2 min

In the last 2 minutes, we are going to see how ArgosCD automatically syncs apps based in GIT commits as if we were working in multiple environments. The idea is to:

  • Simulate a live environment.
  • Deploy the app with the live branch created using a branch with no resources.
  • Update the branch with the latest commit that add the yaml file.
  • Wait until ArgoCD updates the app automatically.

So, let´s go:

  • Create a new namespace for live environment:
kubectl create namespace git2k8s10min-live
  • Deploy a new app in auto mode and listening to the live branch:
cat <<EOF | kubectl apply -f -
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: git2k8s10min-live
  namespace: argocd
spec:
  destination:
namespace: git2k8s10min-live
server: https://kubernetes.default.svc
project: default
source:
repoURL: $HTTPS_REPO_URL
path: .
targetRevision: live
syncPolicy:
automated: {}
EOF
  • Now, we can see the app but with no resources.
  • Sync live branch:

git checkout live

git merge master

git push –set-upstream origin live

  • Go to ArgoCD UI and wait until changes have been applied:

YES, we did it!

After few simple steps, we have been able to deploy an app in 2 environments in 10 minutes. That was awesome!

Besides we’ve learned a lot of things:

  • How to use GIT always as the source of truth of your deployments.
  • GitOps is easier with ArgoCD.
  • How to deploy automatically to multiple environments using different GIT branches.

Thank you and see you in the next challenge!

Santander Global Tech is the global technology company, part of Santander’s Technology and Operations (T&O) division. With more than 2,000 employees and based in Madrid, we work to make Santander an open platform for financial services.

Do you want to join this great team? Check out the positions we have open here and Be Tech! with Santander ?

Follow us on LinkedIn and Instagram.

Santos Andres Alvaro Cloud Solution Architect

Álvaro Santos Andrés

Santander

Cloud Solution Architect with more than 15 years of experience. In the last years I’ve led multiple projects into the public cloud and designed cloud migration strategies for several companies. I am also a professional AWS and GCP architect.

 

Other posts