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

> This article explores efficient management of Kubernetes manifests across multiple directories using Kustomize for better organization and deployment.

In this article, we explore how to efficiently manage Kubernetes manifests spread across multiple directories using Kustomize. Up to now, you have seen only a basic example with a simple kustomization.yaml file. However, even with limited knowledge, you can leverage powerful features in Kustomize to better organize your configurations.

Consider a scenario where you have a directory named "k8s" containing four YAML files:

* API deployment
* API service
* Database deployment
* Database service

Initially, you might deploy these configurations with the standard Kubernetes command:

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

As your project grows and the number of YAML files increases to 20, 30, or even 50, you might decide to organize them into subdirectories. For example, you could move the API deployment and service YAML files into an "api" subdirectory and the database configurations into a "db" subdirectory. After reorganizing, you would deploy each set of configurations separately:

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

<Callout icon="lightbulb" color="#1CB2FE">
  With multiple subdirectories, managing deployments can become cumbersome. Every change might require separate commands for each subdirectory and even adjustments to your CI/CD pipeline.
</Callout>

This is where Kustomize proves its worth. You can create a root kustomization.yaml file in your "k8s" directory that lists every resource by its relative path:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

# Kubernetes resources to be managed by Kustomize
resources:
  - api/api-depl.yaml
  - api/api-service.yaml
  - db/db-depl.yaml
  - db/db-service.yaml
```

With this configuration, you can deploy all resources with a single command:

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

Alternatively, you can leverage kubectl's native support for Kustomize:

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

The output will indicate that your API and database services and deployments have been successfully created:

```bash theme={null}
$ kustomize build k8s/ | kubectl apply -f -
service/api-service created
service/db-service created
deployment.apps/api-deployment created
deployment.apps/db-deployment created

$ kubectl apply -k k8s/
service/api-service created
service/db-service created
deployment.apps/api-deployment created
deployment.apps/db-deployment created
```

<Callout icon="lightbulb" color="#1CB2FE">
  While the above approach works, it can get unwieldy if the number of directories increases. A modular strategy with separate kustomization.yaml files in each subdirectory helps to keep the configuration clean and maintainable.
</Callout>

Imagine expanding your project to include additional components, such as a cache and Kafka. Your root kustomization.yaml file might then list resources from API, database, cache, and Kafka directories as follows:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

# Kubernetes resources to be managed by Kustomize
resources:
  - api/api-depl.yaml
  - api/api-service.yaml
  - db/db-depl.yaml
  - db/db-service.yaml
  - cache/redis-depl.yaml
  - cache/redis-service.yaml
  - cache/redis-config.yaml
  - kafka/kafka-depl.yaml
  - kafka/kafka-service.yaml
  - kafka/kafka-config.yaml
```

This file can quickly become cluttered and hard to maintain. A better solution is to add a separate kustomization.yaml file within each subdirectory. For example, in the "db" directory, the kustomization.yaml might look like this:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - db-depl.yaml
  - db-service.yaml
```

Similarly, you should create kustomization.yaml files in the "api", "cache", and "kafka" directories. Then, update your root kustomization.yaml file to only reference these subdirectories:

```yaml theme={null}
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - api/
  - db/
  - cache/
  - kafka/
```

When you run either of the following commands, Kustomize will automatically locate each subdirectory's own kustomization.yaml file and build a combined configuration:

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

or

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

This modular approach keeps your root kustomization.yaml file clean and simplifies the deployment process across multiple directories.

## Summary

* Organize your YAML files into directories as your project scales.
* Use a root kustomization.yaml file to combine configurations.
* For enhanced maintainability, create separate kustomization.yaml files in each subdirectory and reference them in the root file.

By following these practices, you optimize the organization of your Kubernetes manifests, making deployments easier to manage while keeping your configuration files neat and modular.

<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/2ce4f9d0-46e1-4fb3-bd7d-1d44669edd64" />
</CardGroup>
