Components
Components
-
This is a feature of
kustomize.-
Provides the ability to define reusable pieces of configuration logic (resources + patches) that can be included in multiple overlays.
-
Useful in situations where applications support multiple optional features that need to be enabled only in a subset of overlays.
-
Components are essentially reusable blocks of Kubernetes configurations.
- All of the config-maps etc go into a Component.
-
-
For example, we have an application that is deployed on different configurations:
-
base -
dev -
Premium- Premium Customers -
Self-hosted
-
-
Our application has an optional feature called
Caching, this is available for Premium and Self-hosted customers only.- Need to enable a Redis database and all of the configs with Redis to enable caching.
-
An external Postgres database is also needed in the
dev/Premiumspaces. -
Where do we put the caching config in the directory structure?
-
Only self-hosted and Premium should have this.
-
Can copy the config into Premium and Self-hosted, however that causes double-work.
* Components are reusable blocks of code that we can use in all of the overlays.
-
-
At the root directory, we add another directory called
Componentsand this includescachinganddb.- Contains all of the configs that we need for a specific feature.
-
For all of the overlays that import the database, they can import the database component.
-
Under
components/dbyou would have the following:-
kustomization.yaml -
deployment-patch.yaml -
postgres-depl.yaml
-
-
The
postgres-depl.yamlfile is a deployment for apostgresdatabase. -
The
postgres-depl.yamllooks like this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres-deployment
spec:
replicas: 1
selector:
matchLabels:
component: postgres
template:
metadata:
labels:
component: postgres
spec:
containers:
- name: postgres
image: postgres
- The
kustomization.yamlfile looks like this:
apiVersion:
kustomize.config.k8s.io/v1alpha1
kind: Component
resources:
- postgres-depl.yaml
secretGenerator:
- name: postgres-cred
literals:
- password=postgres123
patches:
- deployment-patch.yaml
-
resourcesis where we input the yaml file we need. -
secretGeneratoris where we need to add a password. -
patchesis an environment variable, so it provides the password to the database. -
deployment-patchlooks like this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
template:
spec:
containers:
- name: api
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-cred
key: password
-
There is also the
kustomization.yamlfile in the Overlays, we need to import theComponentsfrom above. -
Example of
Overlay/premium/kustomization:
bases:
- ../../base
components:
- ../../components/db