Search
Close this search box.

MTVLab: Pioneering DevOps Cloud Training

Maximize Kubernetes Security: Automate TLS Certificate Management with cert-manager on KIND Clusters

By Rajesh Gheware

In the realm of Kubernetes, managing certificates effectively is pivotal for ensuring the security and integrity of the communication between different components within the cluster. With the advent of cloud-native technologies, the complexity and dynamism of service deployments have escalated, making traditional certificate management approaches cumbersome and inefficient. This article introduces cert-manager, a Kubernetes add-on, as a solution to automate the management of TLS certificates, enhancing security while reducing manual intervention and potential human errors. Our focus will be on deploying and managing certificates in a KIND-based Kubernetes cluster.

Introduction to cert-manager

cert-manager is an open-source Kubernetes tool designed to automate the issuance, renewal, and management of TLS certificates from various issuing sources such as Let’s Encrypt, HashiCorp Vault, Venafi, simple signing key pairs, or self-signed. It ensures that certificates are valid and up to date, and attempts to renew certificates at a configured time before expiration.

Setting Up a KIND-based Kubernetes Cluster

KIND (Kubernetes IN Docker) is a tool for running local Kubernetes clusters using Docker container “nodes”. KIND was primarily designed for testing Kubernetes itself, but may be used for local development or CI.

To create a cluster, ensure you have Docker and KIND installed on your system. Then, use the following command:

kind create cluster

This command creates a default cluster named “kind”. You can specify a configuration file to customize the cluster further.

Installing cert-manager

Before installing cert-manager, ensure your Kubernetes cluster is up and running. You can verify this with kubectl get nodes. Once confirmed, proceed with the following steps to install cert-manager.

Step 1: Install the Custom Resource Definitions (CRDs)

kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.6.1/cert-manager.crds.yaml

Step 2: Add the Jetstack Helm repository

helm repo add jetstack https://charts.jetstack.io

Step 3: Update your local Helm chart repository cache

helm repo update

Step 4: Install cert-manager

helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --version v1.6.1

This installs cert-manager in the cert-manager namespace and ensures isolation from other applications.

Configuring cert-manager for Automatic Certificate Management

After installing cert-manager, the next step is to configure it to automate certificate issuance and renewal. This involves creating Issuers or ClusterIssuers resources, which represent entities that issue certificates, and Certificate resources to request certificates from the Issuers.

Example: Configuring a ClusterIssuer for Let’s Encrypt

apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  name: letsencrypt-prod
spec:
  acme:
    email: your-email@example.com
    server: https://acme-v02.api.letsencrypt.org/directory
    privateKeySecretRef:
      name: letsencrypt-prod
    solvers:
    - http01:
        ingress:
          class: nginx

Replace your-email@example.com with your email address. This configuration uses the ACME protocol with HTTP-01 challenge to validate domain ownership.

Requesting a Certificate

After defining an Issuer or ClusterIssuer, you can request a certificate by creating a Certificate resource:

apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: example-com
  namespace: default
spec:
  secretName: example-com-tls
  issuerRef:
    name: letsencrypt-prod
    kind: ClusterIssuer
  dnsNames:
  - example.com
  - www.example.com

This request generates a certificate for example.com and www.example.com, storing it in a Kubernetes secret named example-com-tls.

Automating Certificate Renewal

cert-manager automatically monitors and renews certificates based on their validity period. Typically, certificates are renewed 30 days before expiration. No additional configuration is needed beyond the initial setup.

Automating Ingress Certificate Management with cert-manager

One of the most powerful features of cert-manager is its ability to automatically issue certificates for Kubernetes Ingress resources, simplifying the process of securing your applications with HTTPS. This capability allows developers to automatically secure their applications without the need to manually create and renew certificates for each Ingress. Here, we’ll delve into how to set up an Ingress resource to automatically request and apply certificates using cert-manager.

Step 1: Define an Ingress Resource

First, ensure your Kubernetes cluster has an Ingress controller installed. The Nginx Ingress Controller is a common choice and can be deployed from the Kubernetes official documentation or through Helm charts.

With an Ingress controller in place, define your Ingress resource. Here’s an example that routes traffic to a sample web application:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web-application
  annotations:
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: web-application-service
            port:
              number: 80
  tls:
  - hosts:
    - example.com
    secretName: example-com-tls

In this Ingress definition, note the following key points:

  • The cert-manager.io/cluster-issuer annotation specifies the ClusterIssuer to use for obtaining a certificate. Replace “letsencrypt-prod” with the name of your ClusterIssuer.
  • The kubernetes.io/ingress.class annotation is set to “nginx”, indicating that this Ingress should be handled by the Nginx Ingress Controller.
  • The tls section requests a TLS certificate for the host example.com, which cert-manager will automatically provision and store in the specified secretName. The only condition is that the domain must be accessible over the internet.

Step 2: Deploy the Ingress Resource

Deploy the Ingress resource to your cluster using kubectl apply -f ingress.yaml, where ingress.yaml is the file containing the Ingress resource definition.

Step 3: Automatic Certificate Issuance

Upon deployment, cert-manager detects the new Ingress resource and reads the annotations to understand that a certificate is requested. It then communicates with the specified ClusterIssuer to issue a certificate for the hosts defined under the tls section. The process involves:

  • Performing domain validation as per the ClusterIssuer’s configuration (e.g., HTTP-01 challenge for Let’s Encrypt).
  • Once validated, cert-manager obtains the certificate and stores it in the specified Kubernetes secret (example-com-tls in this case).
  • The Ingress controller then uses the certificate from the secret to secure traffic to the example.com domain.

Step 4: Verifying the Certificate

After a few minutes, you can verify that the certificate has been successfully applied by accessing your application over HTTPS (https://example.com) and checking the certificate details. Additionally, you can inspect the Kubernetes secret (example-com-tls) to see the certificate and private key:

kubectl get secret example-com-tls -o yaml

By automating certificate issuance and renewal for Ingress resources, cert-manager significantly simplifies the process of securing Kubernetes applications. Developers can focus on their application’s functionality, knowing that their Ingress URLs are automatically secured with valid TLS certificates. This approach not only enhances security but also streamlines deployment workflows, making it an essential practice for modern Kubernetes-based applications.

Conclusion

Integrating cert-manager into your KIND-based Kubernetes cluster significantly simplifies TLS certificate management, automating issuance, renewal, and deployment of certificates. This not only enhances security but also reduces the operational overhead associated with manual certificate management. By following the steps outlined in this guide, developers and administrators can ensure that their services are always secured with valid certificates, allowing them to focus on building and deploying applications rather than managing infrastructure details.

Remember, while cert-manager automates many aspects of certificate management, it’s essential to monitor its operations, especially in production environments, to handle any unforeseen issues promptly. Embracing tools like cert-manager reflects a strategic approach towards maintaining robust security practices in cloud-native ecosystems, aligning with the broader goal of leveraging technology for competitive advantage and innovation.

Share:

More Posts

Send Us A Message