Skip to content

TLS certificates with cert-manager and Cloudflare DNS-01

Kubernetes certificate automation for private IPs, wildcards, and APISIX

This document covers everything needed to issue and auto-renew Let's Encrypt certificates via Cloudflare's DNS-01 challenge inside Kubernetes — including services on private/internal IPs, wildcard certificates, APISIX TLS integration, and migrating existing HTTP-01 certificates over to DNS-01.

Prerequisite: DNS for your domain (e.g. castis.io) is already hosted on Cloudflare's nameservers.


1. Why DNS-01 (and how it works)

DNS-01 proves domain ownership by creating a temporary TXT record (_acme-challenge.yourdomain.com) via the Cloudflare API — Let's Encrypt checks that record via public DNS, never by contacting your server.

Because of this: - Private IPs are not an issue. The A/AAAA record behind the domain can point anywhere, including 10.x.x.x or 192.168.x.x — DNS-01 never checks reachability of the origin. - Wildcards are supported (*.hotel.castis.io) — HTTP-01 cannot issue wildcards at all, only DNS-01 can. - Cloudflare proxy status (orange/grey cloud) is irrelevant — DNS-01 never touches HTTP traffic or the proxy layer, so turning the proxy on or off has zero effect on issuance or renewal, at any time. - Fully supported on Cloudflare's free plan — no paid tier required.


2. Add a Private DNS Record (for internal-only services)

In Cloudflare dashboard → DNS → Records, add an A record pointing at the private IP:

Type Name Content Proxy status
A nexus DNS only (grey cloud)

This just publishes the record — no traffic is proxied. Certificate issuance works the same regardless of proxy status, but for a private-only record grey cloud is typically correct anyway, since Cloudflare's proxy expects a publicly reachable origin.


3. Install cert-manager

kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.15.3/cert-manager.yaml
kubectl get pods -n cert-manager

Confirm all 3 pods (cert-manager, cert-manager-cainjector, cert-manager-webhook) show Running before continuing.


4. Create a Cloudflare API Token + Kubernetes Secret

  1. Cloudflare dashboard → profile icon (top right) → My Profile
  2. API Tokens tab → Create Token
  3. Use the "Edit zone DNS" template
  4. Zone Resources → Include | Specific zone | castis.io (scope tightly, not account-wide)
  5. Create → copy the token (shown once only)
kubectl create secret generic cloudflare-api-token-secret \
  --namespace cert-manager \
  --from-literal=api-token=YOUR_CLOUDFLARE_API_TOKEN_HERE

Avoid committing the raw token to git. For GitOps workflows, use a secret manager (Sealed Secrets, SOPS, External Secrets Operator) instead of a plain manifest.


5. ClusterIssuer Examples

Use one DNS-01 ClusterIssuer for everything — including hostnames that previously used HTTP-01. A single, consistent method avoids per-hostname edge cases entirely.

5.1 Staging issuer (test first — no rate-limit risk)

apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  name: letsencrypt-staging
spec:
  acme:
    server: https://acme-staging-v02.api.letsencrypt.org/directory
    email: your-email@example.com   # <-- change this
    privateKeySecretRef:
      name: letsencrypt-staging-account-key
    solvers:
      - dns01:
          cloudflare:
            apiTokenSecretRef:
              name: cloudflare-api-token-secret
              key: api-token
        selector:
          dnsZones:
            - "castis.io"   # <-- change to your domain

Certs from staging are not browser-trusted — that's expected. Use this only to confirm the DNS-01 mechanism works end-to-end before switching to production.

5.2 Production issuer (real, trusted certs)

apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  name: letsencrypt-prod
spec:
  acme:
    server: https://acme-v02.api.letsencrypt.org/directory
    email: your-email@example.com   # <-- change this
    privateKeySecretRef:
      name: letsencrypt-prod-account-key
    solvers:
      - dns01:
          cloudflare:
            apiTokenSecretRef:
              name: cloudflare-api-token-secret
              key: api-token
        selector:
          dnsZones:
            - "castis.io"   # <-- change to your domain

5.3 Multiple solvers on one issuer (if some domains still need HTTP-01)

ClusterIssuer supports multiple solvers routed by selector.dnsZones, so different domains can use different challenge types under the same issuer if needed during a transition period:

spec:
  acme:
    solvers:
      - http01:
          ingress:
            class: nginx   # or apisix
        selector:
          dnsZones:
            - "some-other-domain.com"
      - dns01:
          cloudflare:
            apiTokenSecretRef:
              name: cloudflare-api-token-secret
              key: api-token
        selector:
          dnsZones:
            - "castis.io"

6. Certificate Examples

6.1 Standard certificate (single hostname, private IP)

apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: castis-io-tls
  namespace: default   # <-- namespace where the cert is needed
spec:
  secretName: castis-io-tls-secret
  issuerRef:
    name: letsencrypt-prod   # use letsencrypt-staging first to test
    kind: ClusterIssuer
  commonName: nexus.castis.io
  dnsNames:
    - nexus.castis.io
  duration: 2160h    # 90 days — Let's Encrypt's fixed certificate lifetime
  renewBefore: 720h  # cert-manager auto-renews 30 days before expiry

6.2 Wildcard certificate

Important: a wildcard does not automatically include the bare domain — both must be listed explicitly.

apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: hotel-castis-io-tls
  namespace: default
spec:
  secretName: hotel-castis-io-tls-secret
  issuerRef:
    name: letsencrypt-prod
    kind: ClusterIssuer
  commonName: hotel.castis.io
  dnsNames:
    - hotel.castis.io
    - "*.hotel.castis.io"
  duration: 2160h
  renewBefore: 720h

6.3 Verify issuance

kubectl describe certificate hotel-castis-io-tls -n default
kubectl get challenges -n default   # shows the live DNS-01 TXT record challenge
kubectl get secret hotel-castis-io-tls-secret -n default -o yaml

Behind the scenes, cert-manager will: 1. Create _acme-challenge.hotel.castis.io as a TXT record in Cloudflare 2. Wait for Let's Encrypt to validate it via public DNS 3. Delete the TXT record 4. Store the resulting cert + key in the hotel-castis-io-tls-secret Secret

If issuance stalls, kubectl describe challenge <name> -n default shows the exact error — common causes are an incorrectly scoped API token or outbound HTTPS being blocked from the cluster to api.cloudflare.com.


7. Using the Certificate with APISIX

Option A — ApisixTls CRD

apiVersion: apisix.apache.org/v2
kind: ApisixTls
metadata:
  name: hotel-castis-io-tls
  namespace: default
spec:
  hosts:
    - hotel.castis.io
    - "*.hotel.castis.io"
  secret:
    name: hotel-castis-io-tls-secret
    namespace: default

Option B — Standard Ingress with APISIX Ingress Controller

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: hotel-castis-io
  namespace: default
  annotations:
    kubernetes.io/ingress.class: apisix
spec:
  tls:
    - hosts:
        - hotel.castis.io
        - "*.hotel.castis.io"
      secretName: hotel-castis-io-tls-secret
  rules:
    - host: hotel.castis.io
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: your-service-name
                port:
                  number: 80

Either way, cert-manager doesn't need to know APISIX exists — it writes a standard tls.crt + tls.key Secret that any Ingress controller consumes the same way.


8. Migrating an Existing HTTP-01 Certificate to DNS-01

  1. Point the Certificate's issuerRef at the DNS-01 issuer (e.g. letsencrypt-prod above) instead of the HTTP-01 one.
  2. That edit alone is sufficient for a future switch — cert-manager only re-runs the challenge at issuance or renewal time. It does not retroactively change how the current valid certificate was issued. The existing cert keeps working uninterrupted the whole time.
  3. Waiting for the natural renewal has no downside. The current cert stays valid until its 90-day expiry regardless; once it renews (~30 days before expiry), it will automatically use DNS-01 from then on.
  4. To force an immediate switch instead of waiting (e.g. before turning on the Cloudflare proxy today):
    kubectl delete certificate <cert-name> -n <namespace>
    kubectl delete secret <cert-name>-tls-secret -n <namespace>
    kubectl apply -f updated-certificate.yaml   # now pointing at the DNS-01 issuer
    
  5. Once a certificate has been issued or renewed via DNS-01, Cloudflare's proxy toggle (orange/grey cloud) becomes permanently irrelevant to it — turn it on or off at any time, before or after any renewal, with zero effect on issuance reliability.
  6. Clean up the old HTTP-01 ClusterIssuer once nothing references it, along with any Ingress annotations or firewall rules that existed solely to support the HTTP-01 challenge (keep port 80 open only if still needed for real traffic/redirects).

9. Renewal — Fully Automatic

cert-manager continuously watches each certificate's expiry and automatically re-runs the DNS-01 flow ~30 days before the fixed 90-day Let's Encrypt expiry (per the renewBefore field). No cron jobs, no manual certbot renew, no scheduled task of any kind — this repeats indefinitely as long as: - The Cloudflare API token remains valid and correctly scoped - The cluster retains outbound HTTPS access to api.cloudflare.com - DNS for the zone remains on Cloudflare nameservers


10. File Reference

File Purpose
00-install-cert-manager.sh Installs cert-manager into the cluster
01-cloudflare-secret.yaml Template for the Cloudflare API token Secret
02-clusterissuer-staging.yaml Test issuer (untrusted certs, no rate-limit risk)
03-clusterissuer-prod.yaml Production issuer (real, trusted certs)
04-certificate.yaml Standard certificate request (single hostname)
05-certificate-wildcard-hotel.yaml Wildcard certificate request
06-apisixtls-hotel.yaml APISIX TLS integration (both CRD and Ingress patterns)