Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 22, 2025
In Kubernetes, namespaces help isolate resources within a cluster, making it easier to manage workloads efficiently. While we can manually create namespaces with kubectl, leveraging Helm templates offers consistency, repeatability, and better control in infrastructure-as-code environments.
Manual namespace creation might work for small setups, but as clusters scale and multiple teams deploy applications, maintaining consistency becomes challenging. Helm allows us to define configurations as reusable templates, integrate with CI/CD pipelines, and streamline deployments across environments.
In this tutorial, we’ll look into how to create a namespace using Helm templates, ensuring consistency and reusability in our Kubernetes deployments.
Helm is a package manager for Kubernetes that simplifies application deployment using charts. A Helm chart is essentially a collection of templates that define Kubernetes resources in a reusable and customizable way.
By defining a namespace in a Helm template, we automate the deployment process and ensure consistency. Helm templates use Go templating syntax to generate YAML files dynamically, enabling us to set parameters like the namespace name and modify other resource definitions as needed. This makes deployments more flexible and easier to manage.
To create a namespace using Helm, we can define a Helm chart containing the necessary templates. Helm templates enable us to parameterize configurations, making it easy to deploy Kubernetes resources across different environments without modifying core YAML files.
Before defining the namespace, the first step is to create a Helm chart to store the templates. Let’s generate a new Helm chart named baeldung-namespace-chart from the command line:
$ helm create baeldung-namespace-chart
Creating baeldung-namespace-chart
This creates a directory structure with a templates directory where we’ll define the Kubernetes resources. However, by default, the namespace.yaml file will not be present in the templates directory. We need to create it manually to define the desired namespace.
To define the namespace resource, let’s manually create a new Helm template file named namespace.yaml inside the templates directory. The namespace.yaml file path should be baeldung-namespace-chart/templates/namespace.yaml and should contain:
apiVersion: v1
kind: Namespace
metadata:
name: {{ .Values.namespace.name }}
labels:
environment: {{ .Values.namespace.environment }}
app: {{ .Values.namespace.app }}
In this template, the placeholders {{ .Values.namespace.name }}, {{ .Values.namespace.environment }}, and {{ .Values.namespace.app }} are dynamically populated with values from the values.yaml file. This approach allows us to customize the namespace without modifying the template directly.
Adding labels like environment and app is optional but considered a best practice. It helps with better organization and resource filtering within the cluster.
To provide values for the template, we add our configuration to the values.yaml file in the Helm chart directory. The file path is baeldung-namespace-chart/values.yaml:
namespace:
name: baeldung-ops
environment: dev
app: ops-app
This configuration ensures that the Helm chart creates a namespace named baeldung-ops with the specified environment and app labels when deployed. By keeping these values separate from the template, we improve flexibility, enhance reusability, and simplify updates across multiple environments and deployments.
With the Helm chart and values in place, we can now deploy the namespace to the Kubernetes cluster. Helm simplifies this process by efficiently rendering the template and seamlessly applying the generated manifest, ensuring consistency and avoiding manual configuration errors.
To deploy the namespace, we run the helm install command:
$ helm install my-namespace baeldung-namespace-chart
In this command, my-namespace is the release name, and baeldung-namespace-chart is the chart directory. Helm takes the values from values.yaml, processes the namespace.yaml template, and applies the resulting manifest to the cluster.
Now that the chart is installed, let’s verify the namespace creation using the kubectl command:
$ kubectl get namespaces
NAME STATUS AGE
baeldung-ops Active 10s
default Active 50m
kube-system Active 50m
kube-public Active 50m
This output shows the newly created baeldung-ops namespace alongside the existing ones.
When we want to remove the namespace along with all the resources deployed within it, uninstalling the Helm release is the most effective approach. This ensures that Helm properly cleans up everything defined in the chart.
To uninstall the release, let’s run the helm uninstall command:
$ helm uninstall my-namespace
This command triggers Helm to remove all resources associated with the release, including the namespace itself if it was created as part of the chart.
However, Helm only deletes resources managed by the chart. If any external resources were created manually within the namespace such as persistent volumes, they will remain. Therefore, it’s always a good practice to verify that the namespace has been completely removed:
$ kubectl get namespaces
If the namespace baeldung-ops is no longer listed in the output, this confirms that Helm successfully removed it. Otherwise, if the namespace still exists, it could indicate that some unmanaged resources are still present. In such cases, we’ll need to manually delete the namespace:
$ kubectl delete namespace baeldung-ops
By handling it this way, we avoid leaving behind orphaned resources that could cause issues in future deployments.
Effectively managing Kubernetes namespaces with Helm requires following best practices that enhance efficiency and prevent misconfigurations. By focusing on these strategies, we can maintain a more stable and scalable cluster:
| Practice | Key Insights |
|---|---|
| Version Control for Namespace Charts | Storing namespace-specific Helm charts in a Git repository helps track changes, maintain consistency, and roll back configurations when needed. |
| Parameterize Namespace Values | Defining the namespace name and labels in the values.yaml file allows flexibility when deploying to different environments without modifying the chart itself. |
| Validate Namespace Templates Before Applying | Running helm template allows us to preview the rendered manifest and catch potential syntax errors before applying changes to the cluster. |
| Enforce Namespace-Level RBAC | Implementing Role-Based Access Control (RBAC) within the namespace prevents unauthorized access and strengthens security boundaries. |
| Separate Namespace Management from Application Resources | Deploying the namespace as a separate Helm release from deployments and services prevents accidental deletion during chart upgrades. |
| Automate Namespace Cleanup | Using Helm hooks or automated scripts to clean up unused namespaces after Helm release removal prevents resource bloat and improves cluster management. |
By following these best practices, we unlock the true power of Helm in managing Kubernetes namespaces. This approach streamlines namespace creation, minimizes misconfigurations, and strengthens security boundaries. Ultimately, it ensures a more organized and scalable cluster as our environment evolves.
In this article, we explored how Helm simplifies managing Kubernetes resources, particularly when creating namespaces. By defining resources in Helm charts, we streamline deployments and maintain better control over the cluster.
We also covered key best practices, such as managing values in the values.yaml file, validating templates before applying them, and enforcing RBAC for security. These strategies help prevent common mistakes and enhance cluster stability.
Ultimately, Helm empowers us to manage Kubernetes resources efficiently while adapting to different environments. By following the best practices outlined in this article, we can maintain a clean, organized cluster that scales effortlessly as workloads grow, making the Kubernetes experience far more seamless.