Certainly! Below is a comprehensive step-by-step guide to setting up Harbor with Let's Encrypt using the DNS-01 challenge via Cloudflare and Cert-Manager. This approach ensures your Harbor service remains private without exposing it publicly, thereby avoiding the x509: certificate signed by unknown authority error.
Overview
- Prerequisites
- Install Cert-Manager
- Configure Cloudflare API Credentials
- Create ClusterIssuer for Let's Encrypt DNS-01 Challenge
- Prepare Helm
values.yaml for Harbor Installation
- Install Harbor Using Helm
- Verify the Setup
1. Prerequisites
Before proceeding, ensure you have the following:
- Kubernetes Cluster: A running Kubernetes cluster where you can deploy Harbor.
- Helm Installed: Helm should be installed and configured to interact with your Kubernetes cluster.
- Cloudflare Account: Sign up for a Cloudflare account if you don't have one. You will need API credentials to manage DNS records.
- Domain Name Managed by Cloudflare: Ensure your domain is added to Cloudflare and managed there.
2. Install Cert-Manager
Cert-Manager is a Kubernetes add-on to automate the management and issuance of TLS certificates from various issuing sources, including Let's Encrypt.
a. Add the Jetstack Helm repository:
helm repo add jetstack https://charts.jetstack.io
helm repo update
b. Install Cert-Manager:
kubectl create namespace cert-manager
helm install cert-manager jetstack/cert-manager \
--namespace cert-manager \
--version v1.12.0 \
--set installCRDs=true
Note: Replace v1.12.0 with the latest version available if necessary.
c. Verify the Installation:
kubectl get pods --namespace cert-manager
All pods should be in the Running state.
3. Configure Cloudflare API Credentials
Cert-Manager needs access to your Cloudflare DNS to complete the DNS-01 challenge. For this, you'll create a Kubernetes Secret containing your Cloudflare API token.
a. Generate Cloudflare API Token:
- Log in to your Cloudflare account.
- Navigate to My Profile > API Tokens.
- Create Token with the following permissions:
- Zone: Zone DNS – Edit
- Zone: Zone Settings – Read
- Assign it to the specific domain(s) you want to manage.
b. Create Kubernetes Secret:
Replace <CLOUDFLARE_API_TOKEN> with your actual API token.
kubectl create secret generic cloudflare-api-token-secret \
--from-literal=api-token=<CLOUDFLARE_API_TOKEN> \
--namespace cert-manager
Security Note: It's recommended to use Kubernetes External Secrets or sealed-secrets for managing secrets in production environments.
4. Create ClusterIssuer for Let's Encrypt DNS-01 Challenge
This ClusterIssuer tells Cert-Manager to use Let's Encrypt's ACME server with the DNS-01 challenge via Cloudflare.
Create a YAML file named clusterissuer.yaml:
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: letsencrypt-dns-cloudflare
spec:
acme:
# Let's Encrypt Endpoint
server: https://acme-v02.api.letsencrypt.org/directory
# Your email address
email: your-email@example.com
# Enable the HTTP-01 challenge provider
privateKeySecretRef:
name: letsencrypt-dns-cloudflare
solvers:
- dns01:
cloudflare:
email: your-email@example.com
apiTokenSecretRef:
name: cloudflare-api-token-secret
key: api-token
Customize:
- Replace
your-email@example.com with your actual email.
- Ensure the
apiTokenSecretRef matches the name and key of the secret created in step 3.
Apply the ClusterIssuer:
kubectl apply -f clusterissuer.yaml
Verify the ClusterIssuer:
kubectl get clusterissuer
You should see letsencrypt-dns-cloudflare listed.
5. Prepare Helm values.yaml for Harbor Installation
Configure Harbor to use Cert-Manager for TLS certificates without exposing it publicly.
Create a values.yaml file for Harbor:
expose:
tls:
enabled: true
# Use cert-manager to provision certificates
certManager:
# Reference to the ClusterIssuer created earlier
clusterIssuer: letsencrypt-dns-cloudflare
# Common Name and Issuer
commonName: harbor.yourdomain.com
# The secret where the certificate will be stored
certificate:
secretName: harbor-tls
# Optional: Set to false if you don't want to use HTTPS
# Otherwise, ensure the certificates are properly set
# set to false if you want to handle TLS termination separately
ingress:
# Configurations if using Kubernetes Ingress
enabled: true
hosts:
core: harbor.yourdomain.com
notary: notary.harbor.yourdomain.com
# Annotations for Ingress, include cert-manager related annotations
annotations:
kubernetes.io/ingress.class: nginx
cert-manager.io/cluster-issuer: letsencrypt-dns-cloudflare
tls:
- hosts:
- harbor.yourdomain.com
secretName: harbor-tls
- hosts:
- notary.harbor.yourdomain.com
secretName: harbor-tls
# Optional: Path and other settings
externalURL: https://harbor.yourdomain.com
# Enable TLS for Harbor components if needed
tls:
enabled: true
# Additional Harbor configurations can be added below
Customize:
- Replace
harbor.yourdomain.com with your actual domain.
- Adjust the Ingress annotations based on your Ingress controller (the example uses
nginx).
- Ensure
certificate.secretName matches the secretName in the Ingress TLS section.
6. Install Harbor Using Helm
a. Add Harbor Helm Repository:
helm repo add harbor https://helm.goharbor.io
helm repo update
b. Install Harbor with the Customized values.yaml:
helm install harbor harbor/harbor -f values.yaml --namespace harbor --create-namespace
Notes:
- Make sure the
harbor namespace is created or use --create-namespace to create it during installation.
- You can also customize other Harbor settings in the
values.yaml as needed.
c. Verify the Installation:
kubectl get pods --namespace harbor
All Harbor pods should be in the Running state.
7. Verify the Setup
After installation, Cert-Manager will handle the certificate issuance for Harbor using the DNS-01 challenge via Cloudflare.
a. Check Certificate Request:
kubectl describe certificaterequest -n harbor
You should see a CertificateRequest associated with Harbor's TLS secret.
b. Verify the Certificate:
kubectl get secret harbor-tls -n harbor -o yaml
Ensure the secret contains tls.crt and tls.key.
c. Access Harbor:
Navigate to https://harbor.yourdomain.com in your browser. You should see the Harbor UI with a valid TLS certificate issued by Let's Encrypt.
Additional Tips
- Renewal: Cert-Manager automatically handles the renewal of certificates before they expire.
- Monitoring: Monitor Cert-Manager and Harbor logs to troubleshoot any issues with certificate issuance or Harbor operations.
- Security: Regularly rotate your Cloudflare API tokens and follow best practices for Kubernetes secret management.
Summary
By following the steps above, you have:
- Installed Cert-Manager in your Kubernetes cluster.
- Configured Cloudflare as the DNS provider for the DNS-01 challenge.
- Created a ClusterIssuer using Let's Encrypt via Cert-Manager.
- Configured Harbor's Helm chart to use the ClusterIssuer for TLS certificates.
- Installed Harbor with secure, automatically managed TLS certificates without exposing it publicly.
This setup ensures that Harbor is secured with trusted TLS certificates while remaining private within your infrastructure.
If you encounter any issues or have further questions, feel free to ask!