Photo by Mark Tryapichnikov on Unsplash
K8s Kustomization Template
Using Kustomize tool to manage Kubernetes configurations.
Apply multi resources with one command is possible thanks to the Kustomize tool of K8s. In this post, we see how to use Kustomize tool to generate, modify and delete multi resources by applying only one command, useful when we have many configuration files to manage.
Resources
Kubernetes cluster
kubectl
tool installed version 1.14 or higher configured to communicate with the cluster.Some Configuration files resources.
You can use these resources as examples:
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world-deployment
labels:
app: hello-world
spec:
replicas: 1
selector:
matchLabels:
app: hello-world
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: hello-world
imagePullPolicy: IfNotPresent
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "200m"
ports:
- containerPort: 80
env:
- name: "ENV_SCOPE"
value: "lab"
servicie.yaml
apiVersion: v1
kind: Service
metadata:
name: hello-world-service
spec:
type: ClusterIP
selector:
app.kubernetes.io/name: hello-world-deployment
ports:
- protocol: TCP
port: 80
targetPort: 80
Also, you can download these example files here.
Building Kustomize File
To use Kustomize we need to create a file called kustomize.yaml
with the next content:
resources:
- deployment.yaml
- service.yaml
Generating Resources
To generate resources with Kustomize, we need to execute the command apply
or create
of kubectl
tool with the argument -k
or --kustomize
to process a kustomization directory. This flag can't be used together with -f or -R, for example:
kubectl apply -k ./
This command process the resources in the current directory.
Using the example files with this structure:
We should do these steps
cd ./kustomize
kubectl create -k ./
You see something like that
service/hello-world-service created
deployment.apps/hello-world-deployment created
Deleting Resources
To delete resources you need to execute the command kubectl delete -k ./
, this command will delete the resources previously created.
service "hello-world-service" deleted
deployment.apps "hello-world-deployment" deleted
Updating Resources
To update the resources is like creating resources with the difference that we used de word apply
instead create
, this will create or modify the resources defined, the modification has been applied only if exists any change between the current resources and the new ones.