Managing Directories
Managing Directories
-
This example uses a
k8sdirectory.-
Inside are the following yaml files:
api-depl.yamlapi-service.yamldb-depl.yamldb-service.yaml
-
-
We can manage Kubernetes manifests spread across multiple directories.
-
Usually we go into the
k8sdirectory and run akubectl applyto the manifests there. -
For example, over time the amount of yaml files grows.
-
Over time however, we move yaml files into sub directories. For example all of the
dbfiles go into thedbdirectory andapifiles go into theapidirectory. - To deploy manifests in particular directories, you would use the below command as an example:
kubectl apply -f k8s/api/ kubectl apply -f k8s/db/ -
Every time we apply or delete configs, we’d have to run the above commands in each directory and the CI/CD pipelines become murky.
-
Therefore a good way is to add a
kustomization.yamlfile just under thek8sdirectory. - The
kustomization.yamlfile would look like the following in this example: ``` 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 ```
- Now
kustomizeis aware and we’d just need to run this command:kustomize build k8s/ | kubectl apply -f -- We run the above in the root of the
k8sdirectory.
- We run the above in the root of the
-
Can apply this natively with Kubernetes using
kubectl apply -k k8s -
What happens if the amount of directories grows - we now have a
cachedirectory as well as akafkadirectory.-
That means all of the
kustomizationresources will be pulled in from the above directories.-
Can also add a
kustomization.yamlfile within each of the sub-directories. The configs will only be imported for that single directory.- Within the sub-directory
kustomization.yamlfiles, we add the following:
- Within the sub-directory
-
-
- Example below for the
dbdirectory:
k8s/db/kustomization.yaml
resources:
- db-depl.yaml
- db-service.yaml
- Then in the
rootk8sdirectory, you would have (each of the above directories listed) ``` resources:- api/
- db/
- cache/
-
kafka/ ```
- It will go into the sub-directory and look for a
kustomization.yamlfile.
-
Keeps everything clean and neat.
- Then as before, to apply the manifests: ``` kustomize build k8s/ | kutcle apply -f -
or
kubectl apply -k k8s/ ```