Transitioning your Kubernetes resources to Helm can streamline management, enhance scalability, and improve your CI/CD pipelines. But what happens when you already have resources deployed in your cluster using kubectl or other tools? Instead of recreating everything from scratch, you can seamlessly migrate these resources to Helm and take full advantage of its power.

In this article, I’ll share a practical guide on how I migrated an existing Kubernetes Deployment and Service to Helm ownership using kubectl patch. Follow along to learn how you can do the same!


Why Migrate to Helm?

Helm simplifies Kubernetes application deployment by packaging resources into charts. It allows for version control, templating, and rollback capabilities that are often challenging with manually managed deployments. Migrating resources to Helm ensures a consistent, maintainable, and scalable workflow.


Problem: Existing Resources Without Helm Ownership

I had a Kubernetes Deployment and Service already running, managed manually by kubectl. My goal was to bring these resources under Helm management without downtime or disruption.


Solution: Using kubectl patch

The trick lies in adding Helm-specific annotations to your existing Kubernetes resources. Helm uses these annotations to identify which resources it should manage. Here’s how you can do it:

Step-by-Step Migration

  1. Identify the Target Resources
    Locate the resources you want Helm to manage. Use kubectl get to fetch their details:
kubectl get all -n <namespace>

2. Patch the Resources with Helm Annotations
Add annotations to the resources using the kubectl patch command. Replace <resource-type>, <resource-name>, and <namespace> with your specifics.

kubectl patch <resource-type> <resource-name> -n <namespace> \
-p '{"metadata": {"annotations": {"meta.helm.sh/release-name": "<release-name>", "meta.helm.sh/release-namespace": "<namespace>"}}}'
kubectl patch <resource-type> <resource-name> -n <namespace> \
-p '{"metadata": {"labels": {"app.kubernetes.io/managed-by": "Helm"}}}'

3. Install Helm Chart with --reuse-values
When installing the Helm chart, use the --reuse-values flag to ensure the existing resource configuration is preserved:

helm install <release-name> <chart-name> -n <namespace> --reuse-values

4. Verify the Migration
Confirm that the resources are now managed by Helm:

helm list -n <namespace>  
kubectl describe <resource-type> <resource-name> -n <namespace>

Example: Migrating a Deployment and Service

Assume we have a Deployment named my-app and a Service named my-app-service in the default namespace. Here’s how I migrated them:

  1. Patch the Deployment
kubectl patch deployment my-app -n default \
-p '{"metadata": {"annotations": {"meta.helm.sh/release-name": "my-release", "meta.helm.sh/release-namespace": "default"}, "labels": {"app.kubernetes.io/managed-by": "Helm"}}}'

2. Patch the Service

kubectl patch service my-app-service -n default \
-p '{"metadata": {"annotations": {"meta.helm.sh/release-name": "my-release", "meta.helm.sh/release-namespace": "default"}, "labels": {"app.kubernetes.io/managed-by": "Helm"}}}'

3. Install the Helm Chart

helm install my-release my-chart -n default --reuse-values

4. Verify

helm list -n default  
kubectl get all -n default

FAQs

1. Why should I migrate resources to Helm?

Migrating to Helm ensures easier management, repeatable deployments, and access to features like version control and rollbacks.

2. Will there be downtime during the migration?

No. By patching existing resources and using --reuse-values, the migration is seamless and does not disrupt running workloads.

3. What happens if I skip adding the annotations?

Without Helm annotations, Helm cannot recognize or manage the resources, leading to potential conflicts.

4. Can I migrate resources across namespaces?

Yes, but ensure the meta.helm.sh/release-namespace annotation matches the namespace where Helm manages the release.


Conclusion

Migrating existing Kubernetes resources to Helm doesn’t have to be a daunting task. By leveraging the kubectl patch command, you can seamlessly bring your resources under Helm’s management without downtime or re-creation. This approach saves time, reduces risks, and helps maintain consistency in your Kubernetes environment.

Ready to simplify your Kubernetes deployments? Give this method a try and unlock the full potential of Helm!

Leave a Reply

Your email address will not be published. Required fields are marked *