This is a proposed solution to the KubeOps challenge.
In order to run this solution you'll need:
- A Github account
- A Docker Hub account
- Minikube with the ingress controller. You can install it with
minikube addons enable ingress
. - ArgoCD
You can install and run argocd on minikube, and forward the argocd port to a localhost port:
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
kubectl -n argocd port-forward svc/argocd-server 8080:443
Follow these instructions to configure your environment.
Generate an Access Token in Account Settings -> Security
- Fork the repository https://github.com/dvdjaco/ktx-kubeops
- In the new repo, go to Settings -> Secrets and variables -> Actions
- Create two new "Repository secrets", DOCKER_USERNAME and DOCKER_KEY, with the information from your Docker Hub account.
- Go to Actions -> General
- In the "Workflow permissions" section, select "Read and write permissions"
You can retrieve the admin password with:
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
- Open https://localhost:8080 in your browser.
- Log in and click on "+ NEW APP"
- Fill in these fields:
- Application name: KubeOps
- Project Name: default
- SYNC POLICY: AUTOMATIC , and check on "SELF HEAL"
- Repository URL: The URL of your fork
- Path: helm
- Cluster URL: https://kubernetes.default.svc
- Namespace: default
- VALUES FILES: values.yaml
Click on "CREATE" and the Application will be created. In a few seconds you should see the Status as "Healthy" and "Synced".
Check the IP Address of minikube with minikube ip
. Open the URL http://minikube_ip:30005
and you will see the message:
"Kubeops :: Hello, world! :: VERSION"
, showing the app version.
If you want to trigger the deployment of a new version of the app:
- Make your changes to the repository. The code of the app is in the
hello_world.py
file. - Change the version in the
version
file. - Commit the changes and
git push
them. - The change in the
version
file will trigger the Github Action defined in.github/workflows/cd.yaml
- The Action will build the Docker image using the version as the tag and push it to Docker Hub.
- The Action will change the value of APP_VERSION in
helm/values.yaml
and commit the change. - ArgoCD will detect the change in the helm directory.
- The change in the APP_VERSION affects the deployment defined in
helm/templates/deployment.yaml
- The update in the deployment triggers a rollout using a rolling update strategy. Since the deployment has 3 replicas, there will be no downtime.
This solution could be improved a lot!
To deploy to different environments (e.g. PRE and PRO) we could create the PRE and PRO namespaces in kubernetes. We would have two different helm directories in our repo, one for each environment (e.g. helm_pre and helm_pro). We would change the APP_VERSION value in helm_pre for every version change, but only for specific tags in helm_pro. Then we would define two Applications in ArgoCD, each one of them configured like the one in this solution with a different source path and a different namespace.
We should keep application definitions in source control instead of creating them manually.