CKA Certification Course - Certified Kubernetes Administrator

2025 Updates Kustomize Basics

Managing Directories Demo

In this demo, we review how to efficiently manage your Kubernetes configuration directories. The example uses a structured "k8s" directory containing three subdirectories, each dedicated to a distinct application component:

  • api/ – Contains Kubernetes configurations for your API.
  • cache/ – Hosts configurations for caching mechanisms, such as Redis.
  • db/ – Stores configurations for your MongoDB database.

Within each subdirectory, YAML files define deployments, services, config maps, and other Kubernetes resources. This guide describes the setup, key commands, and benefits of using Kustomize to streamline deployments.


MongoDB Deployment Example

Inside the db/ folder, one YAML file defines a standard Kubernetes Deployment for a MongoDB container. The configuration sets one replica and sources environment variables (username and password) from a config map named "db-credentials."

replicas: 1
selector:
  matchLabels:
    component: db
template:
  metadata:
    labels:
      component: db
  spec:
    containers:
      - name: mongo
        image: mongo
        env:
          - name: MONGO_INITDB_ROOT_USERNAME
            valueFrom:
              configMapKeyRef:
                name: db-credentials
                key: username
          - name: MONGO_INITDB_ROOT_PASSWORD
            valueFrom:
              configMapKeyRef:
                name: db-credentials
                key: password

Service and ConfigMap Example

In the cache/ folder, you will find YAML files for Redis. Below is an example of a ClusterIP service definition for a Redis deployment:

apiVersion: v1
kind: Service
metadata:
  name: redis-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: redis
  ports:
    - port: 6379
      targetPort: 6379

Additionally, before introducing Kustomize, the demo deploys resources using the standard method. Here is a sample ConfigMap for Redis credentials:

apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-credentials
data:
  username: "redis"
  password: "password123"

Tip

For additional information on configuring Kubernetes services and ConfigMaps, refer to the official Kubernetes Documentation.


Deploying the Configurations Without Kustomize

To deploy these configurations without Kustomize, open your terminal and use the kubectl apply command to apply manifests from the "k8s" directory. For example:

kubectl apply -f k8s/

This command deploys all configurations within the "k8s" folder. Alternatively, you can apply each subdirectory individually:

kubectl apply -f k8s/api
# Output:
# deployment.apps/api-deployment created
kubectl apply -f k8s/cache
# Output:
# configmap/redis-credentials created
# deployment.apps/redis-deployment created
kubectl apply -f k8s/db
# Output:
# configmap/db-credentials created
# deployment.apps/db-deployment created
# service/db-service created

To remove these resources from your cluster, run:

kubectl delete -f k8s/db -f k8s/cache -f k8s/api

Introducing Kustomize

Kustomize simplifies resource management by consolidating multiple YAML files into a single manifest. Begin by creating a kustomization.yaml file at the root of your k8s directory. This file should list all the resource files to be customized.

Start with these basic declarations:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - api/api-depl.yaml
  - api/api-service.yaml
  - cache/redis-config.yaml
  - cache/redis-depl.yaml
  - cache/redis-service.yaml
  - db/db-config.yaml
  - db/db-depl.yaml
  - db/db-service.yaml

Build the complete manifest with:

kustomize build k8s/

This command outputs the consolidated Kubernetes manifest. To apply the configuration, pipe the output to kubectl apply:

kustomize build k8s/ | kubectl apply -f -

Alternatively, use the built-in Kustomize feature in kubectl:

kubectl apply -k k8s/

After applying, verify that all resources have been deployed by listing the pods:

kubectl get pods

Example output:

NAME                                     READY   STATUS    RESTARTS   AGE
api-deployment-64dd567b46-1mw4c           1/1     Running   0          27s
db-deployment-657c8ffbd8-vnjs7             1/1     Running   0          26s
redis-deployment-587fd758cf-7pt57          1/1     Running   0          26s

Learn More

Explore more Kubernetes best practices in our Kubernetes Basics guide.


Organizing Kustomize Configurations per Directory

For a scalable approach, create individual kustomization.yaml files within each subdirectory (api, cache, db). Then, update the root kustomization.yaml file to reference these folders.

API Directory

Inside the api/ folder, create a kustomization.yaml to include the API deployment and service definitions:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - api-depl.yaml
  - api-service.yaml

Cache Directory

Within the cache/ folder, create a kustomization.yaml for Redis configurations:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - redis-config.yaml
  - redis-depl.yaml
  - redis-service.yaml

Database Directory

In the db/ folder, create a kustomization.yaml for MongoDB resources:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - db-config.yaml
  - db-depl.yaml
  - db-service.yaml

Finally, update the root k8s/ directory’s kustomization.yaml to reference each subdirectory:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - api/
  - cache/
  - db/

Now, when you run:

kubectl apply -k k8s/

Kustomize will traverse each subdirectory, read the respective kustomization.yaml files, and deploy all the resources accordingly.


Final Deployment and Verification

After removing any previously deployed resources, apply the new configuration structure:

kubectl apply -k k8s/

The output should confirm the creation of each resource, as shown below:

configmap/db-credentials created
service/api-service created
service/db-service created
service/redis-cluster-ip-service created
deployment.apps/api-deployment created
deployment.apps/db-deployment created
deployment.apps/redis-deployment created

Verify that all pods are running with:

kubectl get pods

Example output:

NAME                                     READY   STATUS    RESTARTS   AGE
api-deployment-64dd567b46-1mw4c           1/1     Running   0          27s
db-deployment-657c8ffbd8-vnjs7             1/1     Running   0          26s
redis-deployment-587fd758cf-7pt57          1/1     Running   0          26s

Important

Remember to clean up any stale resources in your cluster before applying new configurations to avoid conflicts.


Kubernetes Resource Overview

Resource TypePurposeExample Command
PodBasic execution unit for containerized appskubectl run nginx --image=nginx
DeploymentManages pods with scaling capabilitieskubectl create deployment nginx --image=nginx
ServiceProvides stable network access to podskubectl expose deployment nginx --port=80

This guide has walked you through managing multiple Kubernetes configuration directories and transitioning from standard kubectl apply methods to using Kustomize for a more scalable solution. For further details, check out the Kubernetes Documentation.

Watch Video

Watch video content

Practice Lab

Practice lab

Previous
Managing Directories