commit 58a30358b6cce8a5394bb6a244a14d7d4528578a Author: lza_menace Date: Mon Sep 21 22:02:03 2020 -0700 init diff --git a/Brewfile b/Brewfile new file mode 100644 index 0000000..b4b71b1 --- /dev/null +++ b/Brewfile @@ -0,0 +1 @@ +kustomize diff --git a/README.md b/README.md new file mode 100644 index 0000000..744ea7e --- /dev/null +++ b/README.md @@ -0,0 +1,72 @@ +# Kustomize Example + +The intention of this repo is to showcase some of the capabilities of Kustomize. I have adapted one of the examples from the Kustomize Github repo; the rest can be found here: https://github.com/kubernetes-sigs/kustomize/tree/master/examples + +## Bases vs Overlays + +### Bases + +When you run `kustomize` the "base" is loaded into memory and if any "overlays" exist that match they are merged over top of your "base" configuration. The "base" is the core module which defines the container infrastructure and their properties. + +In this example, my base consists solely of the "hello" Golang application and Kubernetes objects for deployment, service, and configmap. + +``` +base +├── hello +│   ├── configMap.yaml +│   ├── deployment.yaml +│   ├── kustomization.yaml +│   └── service.yaml +└── kustomization.yaml +``` + +I could deploy this as-is to a cluster by running `kustomize build base | kubectl apply -f -`, but I would miss out on being able to make minor adjustments to account for variances between environments. Maybe you want labels to reflect the environment it's running in; maybe things should be in a different namespace; maybe you want different environment variables or secrets. + +Enter overlays. + +### Overlays + +Overlays are where you can make those minor adjustments that build upon the base. In this example I've setup overlays for each environment, though I've seen them used for particular clusters or namespaces; use it however makes the most sense. + +``` +overlays +├── production +│   ├── deployment.yaml +│   └── kustomization.yaml +└── staging + ├── kustomization.yaml + └── map.yaml +``` + +#### Staging + +For the [staging overlay](overlays/staging/kustomization.yaml) I'm adjusting the labels and annotations to reflect the staging environment. Also, I'm using a different configmap for the "hello" application whch is referenced in the base deployment; this will reflect in the output when reaching the service at the load balancer via HTTP. + +Now I can apply staging specific resources to my cluster by running `kustomize build overlays/staging | kubectl apply -f -`. Compare the diffs between the rendered base and rendered staging overlay: + +![](images/diff_staging_base.png) + +#### Production + +For the [production overlay](overlays/production/kustomization.yaml) I'm adjusting the labels and annotations as well as bumping deployment replicas and injecting a new environment variable. + +Apply it with `kustomize build overlays/production | kubectl apply -f -`. Now compare the diffs between rendered base and rendered production overlay: + +![](images/diff_production_base.png) + +#### Staging vs Production + +Finally, comparing diffs between staging and production helps to see the differences between them. + +![](images/diff_production_staging.png) + +# Further Reading + +I found these links helpful in my evaluation of tooling which ultimately lead to my recommendations: + +* :fire: https://stackoverflow.com/questions/60519939/what-is-the-difference-between-helm-and-kustomize +* :fire: https://argoproj.github.io/argo-cd/user-guide/best_practices/ +* https://foghornconsulting.com/2020/06/04/helm-versus-kustomize/#:~:text=To%20boil%20it%20all%20down,anything%20in%20a%20Kubernetes%20manifest +* https://medium.com/@alexander.hungenberg/helm-vs-kustomize-how-to-deploy-your-applications-in-2020-67f4d104da69 +* https://luktom.net/en/e1683-argocd-vs-flux#:~:text=The%20basic%20features%20are%20the,git%20repositories%20to%20one%20cluster +* https://blog.container-solutions.com/fluxcd-argocd-or-jenkins-x-which-is-the-right-gitops-tool-for-you diff --git a/base/hello/configMap.yaml b/base/hello/configMap.yaml new file mode 100644 index 0000000..e335ab8 --- /dev/null +++ b/base/hello/configMap.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: the-map +data: + altGreeting: "Good Morning!" + enableRisky: "false" diff --git a/base/hello/deployment.yaml b/base/hello/deployment.yaml new file mode 100644 index 0000000..da5c261 --- /dev/null +++ b/base/hello/deployment.yaml @@ -0,0 +1,33 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: the-deployment +spec: + replicas: 1 + selector: + matchLabels: + deployment: hello + template: + metadata: + labels: + deployment: hello + spec: + containers: + - name: the-container + image: monopole/hello:1 + command: ["/hello", + "--port=8080", + "--enableRiskyFeature=$(ENABLE_RISKY)"] + ports: + - containerPort: 8080 + env: + - name: ALT_GREETING + valueFrom: + configMapKeyRef: + name: the-map + key: altGreeting + - name: ENABLE_RISKY + valueFrom: + configMapKeyRef: + name: the-map + key: enableRisky diff --git a/base/hello/kustomization.yaml b/base/hello/kustomization.yaml new file mode 100644 index 0000000..4196595 --- /dev/null +++ b/base/hello/kustomization.yaml @@ -0,0 +1,9 @@ +# Example configuration for the webserver +# at https://github.com/monopole/hello +commonLabels: + app: hello + +resources: +- deployment.yaml +- service.yaml +- configMap.yaml diff --git a/base/hello/service.yaml b/base/hello/service.yaml new file mode 100644 index 0000000..baf6c16 --- /dev/null +++ b/base/hello/service.yaml @@ -0,0 +1,12 @@ +kind: Service +apiVersion: v1 +metadata: + name: the-service +spec: + selector: + deployment: hello + type: LoadBalancer + ports: + - protocol: TCP + port: 80 + targetPort: 8080 diff --git a/base/kustomization.yaml b/base/kustomization.yaml new file mode 100644 index 0000000..156d398 --- /dev/null +++ b/base/kustomization.yaml @@ -0,0 +1,2 @@ +bases: +- ./hello diff --git a/images/diff_production_base.png b/images/diff_production_base.png new file mode 100644 index 0000000..5b131ec Binary files /dev/null and b/images/diff_production_base.png differ diff --git a/images/diff_production_staging.png b/images/diff_production_staging.png new file mode 100644 index 0000000..cd081f7 Binary files /dev/null and b/images/diff_production_staging.png differ diff --git a/images/diff_staging_base.png b/images/diff_staging_base.png new file mode 100644 index 0000000..aefa7bf Binary files /dev/null and b/images/diff_staging_base.png differ diff --git a/overlays/production/deployment.yaml b/overlays/production/deployment.yaml new file mode 100644 index 0000000..af3a980 --- /dev/null +++ b/overlays/production/deployment.yaml @@ -0,0 +1,13 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: the-deployment +spec: + replicas: 5 + template: + spec: + containers: + - name: the-container + env: + - name: MY_NEW_VAR + value: Value defined by Kustomize for production overlay diff --git a/overlays/production/kustomization.yaml b/overlays/production/kustomization.yaml new file mode 100644 index 0000000..2a92448 --- /dev/null +++ b/overlays/production/kustomization.yaml @@ -0,0 +1,10 @@ +namePrefix: production- +commonLabels: + variant: production + org: acmeCorporation +commonAnnotations: + note: Hello, I am production! +bases: +- ../../base +patchesStrategicMerge: +- deployment.yaml diff --git a/overlays/staging/configmap.yaml b/overlays/staging/configmap.yaml new file mode 100644 index 0000000..d89e760 --- /dev/null +++ b/overlays/staging/configmap.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: the-map +data: + altGreeting: "Have a pineapple!" + enableRisky: "true" diff --git a/overlays/staging/kustomization.yaml b/overlays/staging/kustomization.yaml new file mode 100644 index 0000000..955f2dd --- /dev/null +++ b/overlays/staging/kustomization.yaml @@ -0,0 +1,10 @@ +namePrefix: staging- +commonLabels: + variant: staging + org: acmeCorporation +commonAnnotations: + note: Hello, I am staging! +bases: +- ../../base +patchesStrategicMerge: +- configmap.yaml