Kubernetes offers powerful tools for managing applications in its ecosystem. One such tool is kubectl
, a command-line interface for running commands against Kubernetes clusters. Among its capabilities is kustomize
, which allows for customizing Kubernetes resources declaratively. This capability is particularly useful when managing configurations across different environments or deploying applications with slight variations.
Here are several examples demonstrating the usage of kubectl kustomize
:
Example 1: Applying a base customization:
Suppose we have a base customization defined in a directory named base
. To apply this customization, use:
kubectl kustomize base/ | kubectl apply -f -
This command reads the base
directory and applies the generated resources to the Kubernetes cluster.
Output: If successful, Kubernetes will apply the resources defined in the base
directory to the cluster.
Example 2: Overriding configurations:
Imagine needing to override a configuration parameter for a specific environment. Create an overlay directory, such as overlay-dev
, containing the modified configuration, then apply it with:
kubectl kustomize base/ overlay-dev/ | kubectl apply -f -
This command merges the base configuration with the overlay-dev
modifications and applies the result to the cluster.
Output: Kubernetes will deploy the customized resources reflecting the changes made in overlay-dev
.
Example 3: Generating YAML output:
To preview the YAML output of a customization without applying it, use:
kubectl kustomize base/ overlay-dev/
This will display the merged Kubernetes resource definitions that would be applied if the | kubectl apply -f -
part were omitted.
Output: The command outputs the merged YAML configurations to the terminal.
Example 4: Verifying applied resources:
After applying a customization, verify the deployed resources using:
kubectl get pods
This command lists all pods currently running in the cluster.
Output: It displays a list of pods along with their statuses, indicating successful deployment if they are in a running state.
Example 5: Rolling back changes:
If an update needs to be rolled back, retrieve the previous configuration using:
kubectl rollout undo deployment/my-app
This command reverts the deployment my-app
to the previous stable state.
Output: Kubernetes initiates a rollback of the specified deployment, ensuring the cluster returns to the previous configuration.
Example 6: Applying a secret:
To apply a secret defined in a customization, use:
kubectl apply -k overlays/secret-dev
Here, overlays/secret-dev
is a directory containing the secret definition.
Output: If successful, Kubernetes will apply the secret to the cluster.
Example 7: Patching resources:
Apply patches to existing resources using:
kubectl kustomize base/ | kubectl patch -f - -p '{"spec":{"replicas":3}}'
This command modifies the replicas count for resources defined in the base
directory.
Output: Kubernetes applies the patch, adjusting the replicas as specified.
Example 8: Labeling resources:
Add labels to resources using:
kubectl kustomize base/ | kubectl label -f - environment=dev
This labels resources from the base
directory with the environment=dev
label.
Output: Kubernetes adds the label to the specified resources.
Example 9: Deleting resources:
Remove resources defined in a customization with:
kubectl kustomize base/ | kubectl delete -f -
This deletes resources specified in the base
directory from the cluster.
Output: Kubernetes removes the specified resources from the cluster.
Example 10: Applying a config map:
Apply a config map using:
kubectl apply -k overlays/configmap-dev
Here, overlays/configmap-dev
contains the definition of the config map.
Output: Kubernetes applies the config map to the cluster.
To verify whether a command executed successfully, check the Kubernetes cluster state using relevant kubectl
commands such as kubectl get
or kubectl describe
depending on the resource type modified. For example, after applying a deployment configuration, use kubectl get pods
to see if the pods are running as expected.
Also check similar articles.
Waiting for Kubernetes Resources with kubectl wait
Replace Kubernetes Resources with kubectl replace
Patching Kubernetes Resources with kubectl patch
Applying Kubernetes Configurations with kubectl apply
Diffing Kubernetes Configurations with kubectl diff
Discussion about this post