> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Managing Directories Demo

> This guide covers managing Kubernetes configuration directories and using Kustomize for efficient deployments.

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."

```yaml theme={null}
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:

```yaml theme={null}
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:

```yaml theme={null}
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-credentials
data:
  username: "redis"
  password: "password123"
```

<Callout icon="lightbulb" color="#1CB2FE">
  For additional information on configuring Kubernetes services and ConfigMaps, refer to the official [Kubernetes Documentation](https://kubernetes.io/docs/).
</Callout>

***

## 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:

```bash theme={null}
kubectl apply -f k8s/
```

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

```bash theme={null}
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:

```bash theme={null}
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:

```yaml theme={null}
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:

```bash theme={null}
kustomize build k8s/
```

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

```bash theme={null}
kustomize build k8s/ | kubectl apply -f -
```

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

```bash theme={null}
kubectl apply -k k8s/
```

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

```bash theme={null}
kubectl get pods
```

Example output:

```bash theme={null}
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
```

<Callout icon="lightbulb" color="#1CB2FE">
  Explore more Kubernetes best practices in our [Kubernetes Basics](https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/) guide.
</Callout>

***

## 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:

```yaml theme={null}
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:

```yaml theme={null}
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:

```yaml theme={null}
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:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - api/
  - cache/
  - db/
```

Now, when you run:

```bash theme={null}
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:

```bash theme={null}
kubectl apply -k k8s/
```

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

```bash theme={null}
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:

```bash theme={null}
kubectl get pods
```

Example output:

```bash theme={null}
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
```

<Callout icon="triangle-alert" color="#FF6B6B">
  Remember to clean up any stale resources in your cluster before applying new configurations to avoid conflicts.
</Callout>

***

## Kubernetes Resource Overview

| Resource Type | Purpose                                     | Example Command                                 |
| ------------- | ------------------------------------------- | ----------------------------------------------- |
| Pod           | Basic execution unit for containerized apps | `kubectl run nginx --image=nginx`               |
| Deployment    | Manages pods with scaling capabilities      | `kubectl create deployment nginx --image=nginx` |
| Service       | Provides stable network access to pods      | `kubectl 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](https://kubernetes.io/docs/).

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/cka-certification-course-certified-kubernetes-administrator/module/031e84b8-bcbc-4f39-94d6-66d93b05bddc/lesson/a5f8f67c-8464-47f3-bce5-af33781f3964" />

  <Card title="Practice Lab" icon="installation" cta="Learn more" href="https://learn.kodekloud.com/user/courses/cka-certification-course-certified-kubernetes-administrator/module/031e84b8-bcbc-4f39-94d6-66d93b05bddc/lesson/cc61109a-4e64-441d-9896-b25712f0d63c" />
</CardGroup>
