Managing GKE (k8s) clusters and applications inside them has become a never ending battle for many of us. Managing various attributes like node-pools / add-ons / ingress controllers / SSL certificate manager / rollout of applications and its associated configuration accurately has become cumbersome for many. This has become more common given the rise of micro-services and event-driven architecture having many components.
GKE / K8S cluster setup is a complicated process. A good approach is to automate the creation part as a IAC artefact. We will be using terraform as the IAC tool in this article.
Creating modules with parameters / attributes for every resources is the key to maintain environment consistency (parity). We will have 3 modules in our repo. 1. GKE Cluster module having two node pools. 2. nginx ingress controller module using helm chart provisioner. 3. kcert letsencrypt SSL certificate provider module for your public endpoints.
The Code for all the below modules are provided in our repository at : https://github.com/agileguru/gke_nginx_kcert_quick_start
terraform init
terraform plan -var-file=sample.tfvars ( change sample.tfvars if needed)
terraform apply -var-file=sample.tfvars ( change sample.tfvars if needed)
gcloud container clusters get-credentials <cluster name> — zone <cluster zone> — project <project id having the cluster>
Cluster is now ready for workload deployment. We will be using Kustomize plugin to manage it in a easier way. We will use a simple use case for this article.
Step 1 : Creating the folder structure
Step 2 : Customise each environment using overlays
Step 3 : Change Ingress Host Name Mapping
Change hostname in dev-ingress-patch.json & sit-ingress-patch.json to valid host / domain. It looks similar to code given below…
[
{
"op": "replace",
"path": "/spec/rules/0/host",
"value": "dev.agileguru.org"
},
{
"op": "replace",
"path": "/spec/tls/0/hosts/0",
"value": "dev.agileguru.org"
}
][
{
"op": "replace",
"path": "/spec/rules/0/host",
"value": "sit.agileguru.org"
},
{
"op": "replace",
"path": "/spec/tls/0/hosts/0",
"value": "sit.agileguru.org"
}
]
Step 4 : Deploying your Applications
$ kubectl apply -k overlays/dev
namespace/dev created
configmap/config-map-api-1 created
configmap/config-map-api-2 created
service/api-1-service created
service/api-2-service created
deployment.apps/api-1-deployment created
deployment.apps/api-2-deployment created
Step 5 : Un-deploying your Applications
$ kubectl delete -k overlays/dev
namespace "dev" deleted
configmap "config-map-api-1" deleted
configmap "config-map-api-2" deleted
service "api-1-service" deleted
service "api-2-service" deleted
deployment.apps "api-1-deployment" deleted
deployment.apps "api-2-deployment" deleted
ingress.networking.k8s.io "app-ingress" deleted
After completing the above steps we have 1. k8s which is easy to manage / upgrade with nginx and kcert SSL certificate manager without having to manage you SSL certificates ever for your public endpoints. 2. Mechanism / Framework to manage your secure web based endpoints following IAC / Devops / DRY principles.