Deploying Microservices with Helm: Leveraging Custom Resource Definitions (CRDs)

Deploying Microservices with Helm: Leveraging Custom Resource Definitions (CRDs)

Microservices architecture has become a popular approach for building scalable and maintainable applications. When deploying microservices on Kubernetes, Helm, a package manager for Kubernetes, offers a streamlined solution. In this article, we'll explore how Helm, along with Custom Resource Definitions (CRDs), can simplify the deployment of microservices.

Introduction to Helm and CRDs

Helm is a powerful tool for managing Kubernetes applications through the use of charts, which contain pre-configured Kubernetes resources. On the other hand, Custom Resource Definitions (CRDs) extend the Kubernetes API to enable the creation of custom resources tailored to specific application needs.

Structuring Helm Charts for Microservices

To deploy microservices using Helm and CRDs, we'll start by organizing our Helm chart:

my-microservices/
├── Chart.yaml
├── templates/
│   ├── user-service-crd.yaml
│   ├── product-service-crd.yaml
│   ├── user-service-deployment.yaml
│   ├── product-service-deployment.yaml
│   └── ...
├── values.yaml
└── ...

Custom Resource Definitions (CRDs)

CRDs define custom resources specific to our microservices. For example:

# user-service-crd.yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: users.example.com
spec:
  group: example.com
  names:
    kind: User
    plural: users
    singular: user
  scope: Namespaced
  versions:
    - name: v1
      served: true
      storage: true

# product-service-crd.yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: products.example.com
spec:
  group: example.com
  names:
    kind: Product
    plural: products
    singular: product
  scope: Namespaced
  versions:
    - name: v1
      served: true
      storage: true

Helm Templates for Microservices

We define Helm templates to deploy our microservices, referencing the CRDs:

# user-service-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: user-service-deployment
spec:
  replicas: {{ .Values.user.replicaCount }}
  selector:
    matchLabels:
      app: user-service
  template:
    metadata:
      labels:
        app: user-service
    spec:
      containers:
        - name: user-service
          image: {{ .Values.user.image }}
          ports:
            - containerPort: 8080
          # Add other configuration as needed

# product-service-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: product-service-deployment
spec:
  replicas: {{ .Values.product.replicaCount }}
  selector:
    matchLabels:
      app: product-service
  template:
    metadata:
      labels:
        app: product-service
    spec:
      containers:
        - name: product-service
          image: {{ .Values.product.image }}
          ports:
            - containerPort: 8080
          # Add other configuration as needed

Values Configuration

Configure values for the microservices in the values.yaml file:

user:
  replicaCount: 3
  image: "user-service:v1.0.0"

product:
  replicaCount: 2
  image: "product-service:v1.0.0"

Deployment

Deploy the Helm chart to Kubernetes:

helm install my-microservices ./my-microservices

Conclusion

In this article, we've demonstrated how Helm, combined with Custom Resource Definitions (CRDs), simplifies the deployment of microservices on Kubernetes. By organizing Helm charts to include CRDs and leveraging Helm's templating capabilities, users can effectively manage and deploy microservices architectures, paving the way for scalable and maintainable applications in Kubernetes environments.