I'm diving into Kubernetes for my homelab projects and I'm facing a challenge. I've organized my services into different directories, each containing Helm charts, CRDs, and configurations. For instance, I have directories for CertManager, OpenEBS, Traefik, CloudNativePG, and my first app, Authentik. My goal is to apply everything in one go using `kubectl apply -f deploy/`, but I'm running into issues where the order of deployment leads to errors. For example, if a namespace is needed before the cluster is created, I get namespace-related errors. How can I ensure that my YAML files deploy in the correct order, so I don't run into these issues? I'm open to using tools if `kubectl apply` alone can't manage these dependencies.
5 Answers
If you're not using ArgoCD yet, that's another option you should consider. It has a feature called sync waves that can help manage the deployment order. However, since this is a homelab for learning, Terraform might work well for you too. You can structure each deployment as a module and make use of the 'depends_on' directive to declare dependencies explicitly, which is super handy for managing resources in a smaller environment.
Have you looked into using Pulumi? It simplifies defining dependencies by letting you specify that one resource depends on another directly. I've been using it for deploying Helm charts and managing CRDs, and it handles those relationships really well!
I suggest starting with Helm charts where possible. This approach keeps things loosely coupled, allowing Kubernetes to handle dependencies naturally in many cases. For namespaces, Helm makes it easier to manage different releases by creating them as needed. For example:
helm install
cert-manager oci://quay.io/jetstack/charts/cert-manager
--namespace cert-manager
--create-namespace
--set crds.enabled=true
Though not all Helm charts install CRDs before the main app, this method is manageable!
Another efficient way to manage dependencies is by using Terraform along with its Helm and Kubernetes providers. You can group your resources into modules, which makes it great for managing dependencies, especially if you're working with external services like AWS IAM roles or S3 buckets. It gives you a clear structure and real visibility into what's done and what's pending.
You should check out Kustomize! It's integrated into kubectl, so you would use `-k` instead of `-f` when you apply it. Kustomize helps you manage your YAML files and handles dependency ordering automatically, plus it doesn't require any extra tools. If you're looking for more automation down the line, tools like Argo or Pulumi can work with Kustomize too! Here's the official guide for reference: https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/

Totally agree with this! Kustomize is perfect for organizing your manifests, but don't forget to look into Flux as it can help you with the deployment process too!