Kustomize — rise and shine

I try to help you here with kustomize issues I had and how got along with them. You can follow for more updates in near future

Image for post
Image for post
by https://ordina-jworks.github.io/

Basic Preparations

$ brew install kustomize
[...]
$ mkdir -p myrepo/base myrepo/sonarqube/
$ cd myrepo
$ kustomize init

Case 1: Preparation

Replace your ingress hostname with kustomize configMapGenerator + vars

  • Define your variables like $(SONARQUBE_EXTERNAL_URL) which should be replaced later on by kustomize
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: sonarqube-ingress
labels:
app.kubernetes.io/name: sonarqube
app.kubernetes.io/instance: default
app.kubernetes.io/version: default
annotations: {}
namespace: default
spec:
# TLS -> can be ignored for now
# tls:
# - hosts:
# - $(SONARQUBE_EXTERNAL_URL)
# secretName: testsecret-tls

rules:
- host: $(SONARQUBE_EXTERNAL_URL)
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: $(SONARQUBE_SERVICE)
port:
number: 9000
apiVersion: v1
kind: Service
metadata:
name: sonarqube-svc
namespace: default
labels:
app.kubernetes.io/name: sonarqube
app.kubernetes.io/component: java
annotations: {}
spec:
selector:
app.kubernetes.io/name: sonarqube
ports:
- port: 9000
protocol: TCP
targetPort: 9000
  • define the resources we’d like to import from base
  • describe vars you’d like to make usable in your kustomize yamls
    NOTE: kustomize searches for $(<MyVarName e.g.: SONARQUBE_SERVICE>)
  • The configMapGenerator will provide us with our literals as parameters which we then can reference from vars or other places
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- base/sonarqube/service.yaml
- base/sonarqube/ingress.yaml
#
# VARIABLES
#
vars:
- name: SONARQUBE_SERVICE
objref:
apiVersion: v1
kind: Service
name: sonarqube-svc
fieldref:
fieldpath: metadata.name
- name: SONARQUBE_EXTERNAL_URL
objref:
apiVersion: v1
kind: ConfigMap
name: test
fieldref:
fieldpath: data.SONARQUBE_EXTERNAL_URL
#
# CONFIGMAP-GENERATOR
#
generatorOptions:
disableNameSuffixHash: true
configMapGenerator:
- name: test
literals:
- SONARQUBE_EXTERNAL_URL=sonarqube.apps.example.com
- POSTGRES_PORT=5432
- SONARQUBE_PORT=9000
#
# ADDITIONAL-CONFIG
#
configurations:
- sonarqube/lookup.yaml
  • Here we store the “paths” where kustomize should look after variabels which should be replaced
varReference:
- kind: Ingress
path: spec/rules[]/http/paths[]/backend/service/name
- kind: Ingress
path: spec/rules[]/hosts
- kind: Ingress
path: spec/tls[]/hosts[]

Case 1: Rendering/Building

  • If you followed carefully then your result should equal the following
$ kustomize build --load_restrictor none
apiVersion: v1
data:
POSTGRES_PORT: "5432"
SONARQUBE_EXTERNAL_URL: sonarqube.apps.example.com
SONARQUBE_PORT: "9000"
kind: ConfigMap
metadata:
labels:
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/part-of: sonarqube
name: test
namespace: default
---
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/component: java
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/name: sonarqube
app.kubernetes.io/part-of: sonarqube
name: sonarqube-svc
namespace: default
spec:
ports:
- port: 9000
protocol: TCP
targetPort: 9000
selector:
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/name: sonarqube
app.kubernetes.io/part-of: sonarqube
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
labels:
app.kubernetes.io/instance: default
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/name: sonarqube
app.kubernetes.io/part-of: sonarqube
app.kubernetes.io/version: default
name: sonarqube-ingress
namespace: default
spec:
rules:
- host: sonarqube.apps.example.com
http:
paths:
- backend:
service:
name: sonarqube-svc
port:
number: 9000
path: /
pathType: Prefix

Stay tuned for more!

Cheers!

Sources

Working as a IT-Operations engineer at NeXenio, a spin-off by Hasso-Plattner-Institute for products around a digitial workspace.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store