To set up Harbor with Let's Encrypt certificates using the DNS-01 challenge via Cloudflare, follow these steps. This approach ensures that your Harbor service can securely obtain SSL certificates without exposing it directly to the internet.
Prerequisites
- Kubernetes Cluster: Ensure you have a running Kubernetes cluster.
- Helm Installed: Make sure Helm is installed on your local machine.
- MetalLB Installed: You should have MetalLB configured as the LoadBalancer for your services.
- Cloudflare Account: You'll need a Cloudflare account with access to manage DNS records for your domain.
- Domain Name: A domain managed by Cloudflare.
Step 1: Set Up Cloudflare API Token
To allow Cert-Manager to perform DNS-01 challenges, you need an API token with permissions to manage DNS records.
- Log in to Cloudflare and navigate to your account.
- Go to My Profile > API Tokens.
- Click on Create Token.
- Select Edit zone DNS template or create a custom token with the following permissions:
- Permissions:
- Zone Resources:
- Include All zones or specify particular zones as needed.
- Save the token securely; you'll need it later.
Step 2: Install Cert-Manager
Cert-Manager automates the management and issuance of TLS certificates.
-
Add the Jetstack Helm repository:
helm repo add jetstack https://charts.jetstack.io
helm repo update
-
Install Cert-Manager:
kubectl apply --validate=false -f https://github.com/jetstack/cert-manager/releases/download/v1.12.0/cert-manager.crds.yaml
helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --version v1.12.0
Note: Replace v1.12.0 with the latest Cert-Manager version if necessary.
Step 3: Configure Cloudflare Credentials in Kubernetes
-
Create a Kubernetes Secret with your Cloudflare API token:
kubectl create secret generic cloudflare-api-token-secret \
--from-literal=api-token=YOUR_CLOUDFLARE_API_TOKEN \
--namespace cert-manager
Replace YOUR_CLOUDFLARE_API_TOKEN with the token you created earlier.
Step 4: Configure ClusterIssuer for Let's Encrypt
Create a ClusterIssuer resource that uses the DNS-01 challenge with Cloudflare.
# cluster-issuer.yaml
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: letsencrypt-dns
spec:
acme:
# You must replace this email address with your own.
email: your-email@example.com
server: https://acme-v02.api.letsencrypt.org/directory
privateKeySecretRef:
name: letsencrypt-dns
solvers:
- dns01:
cloudflare:
email: your-email@example.com
apiTokenSecretRef:
name: cloudflare-api-token-secret
key: api-token
Apply the ClusterIssuer:
kubectl apply -f cluster-issuer.yaml
Ensure you replace your-email@example.com with your actual email address.
Step 5: Prepare Harbor values.yaml for Helm Installation
Below is a sample values.yaml configuration for installing Harbor with Helm, integrating Cert-Manager for TLS certificates using Let's Encrypt via Cloudflare.
# values.yaml
expose:
type: loadBalancer
tls:
enabled: true
certificate:
# Leave these empty since Cert-Manager will handle them
secretName: harbor-tls
certSource: auto
autoGeneration:
enabled: true
# Configures Cert-Manager as the certificate manager
certManager:
enabled: true
# Reference the ClusterIssuer created earlier
clusterIssuer: letsencrypt-dns
externalURL: https://your.domain.com
harborAdminPassword: Harbor12345
# Optionally, configure Ingress settings if needed
# ingress:
# enabled: false
# Configure persistence as needed
persistence:
enabled: true
# Define storage classes and sizes as required
# Optionally, specify additional configurations
# For example, database, cache settings, etc.
Key Points in values.yaml:
- expose.type: Set to
loadBalancer to use MetalLB.
- tls.enabled: Enables TLS for Harbor.
- tls.certificate.secretName: The name of the Kubernetes Secret where the certificate will be stored (
harbor-tls in this case).
- tls.certificate.certSource: Set to
auto to let Cert-Manager handle certificate provisioning.
- autoGeneration: Enables auto-generation of certificates via Cert-Manager.
- autoGeneration.certManager.clusterIssuer: Specifies the
ClusterIssuer to use (letsencrypt-dns).
- externalURL: The URL through which Harbor is accessed (
https://your.domain.com). Replace with your actual domain.
Step 6: Deploy Harbor Using Helm
-
Add the Harbor Helm repository:
helm repo add harbor https://helm.goharbor.io
helm repo update
-
Create a namespace for Harbor (if not already existing):
kubectl create namespace harbor
-
Install Harbor with your values.yaml:
helm install harbor harbor/harbor --namespace harbor -f values.yaml
If Harbor is already installed and you want to upgrade:
helm upgrade harbor harbor/harbor --namespace harbor -f values.yaml
Step 7: Verify Certificate Issuance
-
Monitor the Certificate Request:
kubectl get certificates -A
kubectl describe certificate harbor-tls -n harbor
-
Check Cert-Manager Pods:
Ensure that Cert-Manager is running and not encountering errors.
kubectl get pods -n cert-manager
-
Verify TLS Certificate:
Once the certificate is issued, verify that the harbor-tls Secret contains the TLS certificate and key.
kubectl get secret harbor-tls -n harbor -o yaml
Step 8: Accessing Harbor
After successful deployment, access Harbor via the externalURL you specified (e.g., https://your.domain.com). The connection should now be secured with a valid Let's Encrypt certificate managed by Cert-Manager, eliminating the x509: certificate signed by unknown authority error.
Additional Notes
- Cloudflare API Token Security: Ensure that the API token has only the necessary permissions and is stored securely within Kubernetes Secrets.
- ACME Rate Limits: Be aware of Let's Encrypt's rate limits, especially during testing. Use the staging server for initial tests by changing the
server field in the ClusterIssuer to https://acme-staging-v02.api.letsencrypt.org/directory.
- Renewal: Cert-Manager automatically handles certificate renewals before expiration.
- Firewall and Networking: Ensure that your Kubernetes cluster can communicate with Let's Encrypt's ACME servers and that DNS-01 challenges can be performed via Cloudflare DNS API.
Summary
By following the above steps, you've configured Harbor to use Let's Encrypt certificates via Cert-Manager with Cloudflare DNS-01 challenges. This setup ensures your Harbor instance is secured without exposing it unnecessarily, leveraging the power of Kubernetes, Helm, and Cert-Manager for automated TLS management.