A wildcard certificate for *.example.com lets one Secret cover every service in a namespace without a new certificate for each hostname. Let's Encrypt will issue one, but only through the DNS-01 challenge. HTTP-01 can't prove control of a wildcard, because there is no single host to serve a token from.
DNS-01 asks you to publish a TXT record at _acme-challenge.example.com. cert-manager can do that for you if it has API access to your DNS provider. This guide uses Cloudflare, which cert-manager supports natively, and walks from the API token to a certificate that is ready. It also covers the self-check flags you need when the cluster sees a different version of your zone than the internet does.
Before you start
You need a cluster with cert-manager v1.21.3 installed, a domain whose DNS is hosted on Cloudflare, and kubectl and dig on your machine. The examples use example.com and put the Certificate in the default namespace. Replace both with your own.
If cert-manager isn't installed yet, the Helm chart is the quickest route.
helm install cert-manager oci://quay.io/jetstack/charts/cert-manager \
--namespace cert-manager \
--create-namespace \
--version v1.21.3 \
--set crds.enabled=true
Step 1: create a Cloudflare API token
Cloudflare offers two kinds of credential. A Global API Key carries every permission your account has. An API Token is scoped to chosen permissions and zones and can be revoked on its own. The cert-manager docs recommend tokens, and there is no reason to hand a cluster your whole account.
Create the token under User Profile, API Tokens. The docs list two permissions. Zone - DNS - Edit lets cert-manager create and delete the TXT record. Zone - Zone - Read lets it find the zone in the first place, and it is the one people forget. For zone resources, the docs suggest Include - All Zones.
The read permission matters because of how the Cloudflare solver finds your zone. It asks the Cloudflare API for a zone named _acme-challenge.example.com, then example.com, walking up one label at a time until a zone comes back. Without Zone - Zone - Read, that lookup fails before any record is written, with an error like requires permission 'com.cloudflare.api.account.zone.list' to list zones on the Challenge.
Copy the token when Cloudflare shows it. You can't view it again later.
Step 2: store the token in a Secret
A ClusterIssuer is cluster-scoped, but Secrets live in namespaces. cert-manager resolves this by reading every Secret a ClusterIssuer references from the cluster resource namespace. That is cert-manager by default, and you can change it with the controller's --cluster-resource-namespace flag.
Put the token there. If you put it next to your Certificate instead, the Challenge will fail because cert-manager can't find the Secret.
kubectl create secret generic cloudflare-api-token \
--namespace cert-manager \
--from-literal=api-token='<your Cloudflare API token>'
The key name, api-token here, is up to you. You reference it by name in the issuer.
Step 3: create a staging ClusterIssuer
Start against the Let's Encrypt staging server. It runs the same protocol with much higher rate limits, so a mistake in your token or selectors costs you nothing. Its certificates chain to roots such as "(STAGING) Pretend Pear X1", which no browser trusts, so you can't confuse them with real ones. ACME accounts are scoped to each environment, which is why the account key Secret gets a name that says staging.
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: letsencrypt-staging
spec:
acme:
email: admin@example.com
server: https://acme-staging-v02.api.letsencrypt.org/directory
privateKeySecretRef:
name: letsencrypt-staging-account-key
solvers:
- selector:
dnsZones:
- example.com
dns01:
cloudflare:
apiTokenSecretRef:
name: cloudflare-api-token
key: api-token
Apply it and check that the account registered.
kubectl apply -f clusterissuer-staging.yaml
kubectl get clusterissuer letsencrypt-staging -o wide
The Ready column should read True, and with -o wide the status message reads The ACME account was registered with the ACME server. The email field belongs to your ACME account. It has nothing to do with Cloudflare, and the Cloudflare solver only needs an email when you use a Global API Key.
Choosing a selector
The selector decides which solver handles which name. That matters as soon as an issuer has more than one solver, for example HTTP-01 for most names and DNS-01 for wildcards. A solver with no selector matches everything.
Use dnsZones for wildcard work. It matches the zone itself and every name under it, so example.com covers both example.com and *.example.com. The dnsNames selector is an exact match, and cert-manager compares the wildcard identifier as the literal string *.example.com. A solver with dnsNames: ["example.com"] will not solve the wildcard. If you use dnsNames, list both entries.
- selector:
dnsNames:
- example.com
- "*.example.com"
When several solvers match, a dnsNames match beats a dnsZones match, and a longer zone beats a shorter one. Quote anything that starts with * in YAML, since a bare asterisk starts an alias.
Step 4: request the certificate
A wildcard matches exactly one label in place of the asterisk. It covers api.example.com but not a.b.example.com, and it does not cover the apex, so *.example.com alone won't work for https://example.com. Put both names on the Certificate.
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: wildcard-example-com
namespace: default
spec:
secretName: wildcard-example-com-tls
issuerRef:
name: letsencrypt-staging
kind: ClusterIssuer
dnsNames:
- "*.example.com"
- example.com
The kind: ClusterIssuer line is required. Without it, cert-manager looks for a namespaced Issuer called letsencrypt-staging in default and finds nothing.
kubectl apply -f certificate.yaml
Step 5: watch the challenges
cert-manager now creates a CertificateRequest, an Order and one Challenge per name. List the Challenges with the extra columns, since the REASON column only appears with -o wide.
kubectl get challenges -n default -o wide
Both Challenges show example.com in the DOMAIN column. That is by design. ACME validates *.example.com by checking a TXT record at _acme-challenge.example.com, the same name the apex uses, so the Challenge stores the base name and sets spec.wildcard: true on the one for the wildcard. You can see the difference with a custom column.
kubectl get challenges -n default \
-o custom-columns=NAME:.metadata.name,DOMAIN:.spec.dnsName,WILDCARD:.spec.wildcard,STATE:.status.state
The two Challenges don't run at the same time. cert-manager's scheduler refuses to process two DNS-01 Challenges that validate the same _acme-challenge name at once, so one waits until the other finishes. A certificate with both names therefore takes longer than one with a single name.
While a Challenge waits, kubectl describe shows its progress. A reason like the one below means the record was created in Cloudflare and cert-manager's self-check hasn't seen it yet.
Waiting for DNS-01 challenge propagation: DNS record for "example.com" not yet propagated
You can run the same check yourself against a public resolver. The value should match the spec.key of the Challenge being processed.
dig +short TXT _acme-challenge.example.com @1.1.1.1
kubectl get challenge <challenge-name> -n default -o jsonpath='{.spec.key}'
The self-check retries every 10 seconds by default, set by --dns01-check-retry-period. Once it sees the record, cert-manager sleeps a fixed 60 seconds before telling Let's Encrypt to validate, which gives the record time to propagate. After that, the Challenge turns valid, cert-manager deletes the TXT record, and the next Challenge starts.
When both are done, the Certificate becomes ready.
kubectl get certificate -n default
NAME READY SECRET AGE
wildcard-example-com True wildcard-example-com-tls 4m
Check that the Secret holds both names.
kubectl get secret wildcard-example-com-tls -n default -o jsonpath='{.data.tls\.crt}' \
| base64 -d | openssl x509 -noout -ext subjectAltName
Step 6: fix the self-check for split-horizon DNS
Some Challenges sit in Waiting for DNS-01 challenge propagation forever, even though dig @1.1.1.1 shows the record. The usual cause is split-horizon DNS, where your company's internal resolvers serve their own copy of example.com.
By default the self-check uses the resolvers from the controller pod's /etc/resolv.conf. It asks them for the zone's SOA and NS records, then queries those authoritative servers directly for the TXT record. With split-horizon DNS, the cluster's resolvers point at your internal servers. Those never receive the record cert-manager wrote to Cloudflare, so the check never passes, while Let's Encrypt, looking from outside, would see it at once.
The controller has two flags for this. --dns01-recursive-nameservers replaces the resolvers from /etc/resolv.conf with a list you choose. Each entry is host:port or a DNS over HTTPS URL such as https://1.1.1.1/dns-query. With public resolvers there, the SOA and NS lookups return Cloudflare's nameservers, and cert-manager queries those.
--dns01-recursive-nameservers-only goes further. It skips the authoritative lookup and asks only your listed resolvers for the TXT record. Use it when the cluster can't reach arbitrary nameservers on port 53, or when you want every self-check query to go over DoH. Two side effects come with it. Every listed resolver has to return the record before the check passes, and resolver caching can make the check take longer.
The Helm chart has a value for each flag. The comma between resolvers must be escaped in --set.
helm upgrade cert-manager oci://quay.io/jetstack/charts/cert-manager \
--namespace cert-manager \
--version v1.21.3 \
--reuse-values \
--set 'dns01RecursiveNameservers=1.1.1.1:53\,8.8.8.8:53' \
--set dns01RecursiveNameserversOnly=true
These flags only change what cert-manager checks before it asks Let's Encrypt to validate. Let's Encrypt always does its own lookup from the internet. They also don't change how the Cloudflare solver finds the zone, since that goes through the Cloudflare API.
Step 7: switch to production
Once staging issues cleanly, create a second ClusterIssuer for production. It needs its own account key Secret, since the staging account doesn't exist on the production server.
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: letsencrypt-prod
spec:
acme:
email: admin@example.com
server: https://acme-v02.api.letsencrypt.org/directory
privateKeySecretRef:
name: letsencrypt-prod-account-key
solvers:
- selector:
dnsZones:
- example.com
dns01:
cloudflare:
apiTokenSecretRef:
name: cloudflare-api-token
key: api-token
Then point the Certificate at it. Changing issuerRef makes cert-manager issue a new certificate, so the staging one in the Secret is replaced without deleting anything.
kubectl apply -f clusterissuer-prod.yaml
kubectl patch certificate wildcard-example-com -n default --type merge \
-p '{"spec":{"issuerRef":{"name":"letsencrypt-prod","kind":"ClusterIssuer"}}}'
Watch the Challenges again. They follow the same path as on staging, and the Secret ends up with a certificate browsers trust.
When Cloudflare rejects the request
Two errors from the Cloudflare docs page are worth knowing. The zone list permission error described in step 1 usually means the token lacks Zone - Zone - Read. A generic error on POST /zones/<id>/dns_records can mean your domain uses .cf, .ga, .gq, .ml or .tk. Cloudflare blocks API updates to DNS records for those TLDs, and the cert-manager docs suggest a different DNS provider for them.