· OptiPod Team · Security · 4 min read
Securing OptiPod: Prometheus Authentication Best Practices
Learn how to securely connect OptiPod to your Prometheus instance using basic auth, bearer tokens, or mTLS.
Security is paramount when deploying Kubernetes operators. OptiPod needs to query Prometheus for metrics, and doing so securely is essential. In this guide, we’ll cover three authentication methods and when to use each.
Authentication Methods
OptiPod supports three authentication methods for Prometheus:
- Basic Authentication - Username and password
- Bearer Token - Token-based authentication
- mTLS - Mutual TLS with client certificates
Method 1: Basic Authentication
Basic auth is the simplest method and works well for most deployments.
Setup
First, create a Kubernetes secret with your credentials:
kubectl create secret generic prometheus-auth \ --from-literal=username=optipod \ --from-literal=password=your-secure-password \ -n optipod-systemThen configure OptiPod to use it:
prometheus: url: https://prometheus.example.com auth: type: basic secretName: prometheus-auth usernameKey: username passwordKey: passwordInstall with Helm:
helm install optipod optipod/optipod \ -f values.yaml \ -n optipod-systemWhen to Use
- Internal Prometheus instances
- Development and testing environments
- When simplicity is preferred
Method 2: Bearer Token
Bearer tokens provide better security than basic auth and integrate well with Kubernetes RBAC.
Setup
Create a ServiceAccount and token for OptiPod:
apiVersion: v1kind: ServiceAccountmetadata: name: optipod-prometheus namespace: monitoring---apiVersion: v1kind: Secretmetadata: name: optipod-prometheus-token namespace: monitoring annotations: kubernetes.io/service-account.name: optipod-prometheustype: kubernetes.io/service-account-tokenGrant permissions to query Prometheus:
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: name: prometheus-readerrules:- apiGroups: [""] resources: ["services/proxy"] resourceNames: ["prometheus"] verbs: ["get"]---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: name: optipod-prometheus-readerroleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: prometheus-readersubjects:- kind: ServiceAccount name: optipod-prometheus namespace: monitoringConfigure OptiPod:
prometheus: url: https://prometheus.example.com auth: type: bearer secretName: optipod-prometheus-token tokenKey: tokenWhen to Use
- Kubernetes-native Prometheus deployments
- When using Prometheus Operator
- Production environments with RBAC
Method 3: Mutual TLS (mTLS)
mTLS provides the highest level of security with certificate-based authentication.
Setup
Generate client certificates (or use your PKI):
# Generate client keyopenssl genrsa -out client.key 2048
# Generate CSRopenssl req -new -key client.key -out client.csr \ -subj "/CN=optipod/O=optipod-system"
# Sign with your CA (example with self-signed CA)openssl x509 -req -in client.csr \ -CA ca.crt -CAkey ca.key \ -CAcreateserial -out client.crt \ -days 365Create a secret with the certificates:
kubectl create secret generic prometheus-mtls \ --from-file=ca.crt=ca.crt \ --from-file=client.crt=client.crt \ --from-file=client.key=client.key \ -n optipod-systemConfigure OptiPod:
prometheus: url: https://prometheus.example.com auth: type: mtls secretName: prometheus-mtls caCertKey: ca.crt clientCertKey: client.crt clientKeyKey: client.keyWhen to Use
- Highly regulated environments
- Multi-tenant clusters
- When certificate-based auth is required by policy
Security Best Practices
1. Use Kubernetes Secrets
Never hardcode credentials in values files or ConfigMaps:
# ❌ Badprometheus: url: https://prometheus.example.com username: admin password: password123
# ✅ Goodprometheus: url: https://prometheus.example.com auth: type: basic secretName: prometheus-auth2. Rotate Credentials Regularly
Set up a rotation schedule:
# Update secretkubectl create secret generic prometheus-auth \ --from-literal=username=optipod \ --from-literal=password=new-secure-password \ --dry-run=client -o yaml | \ kubectl apply -f -
# Restart OptiPod to pick up new credentialskubectl rollout restart deployment optipod-controller \ -n optipod-system3. Use Network Policies
Restrict network access to Prometheus:
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: optipod-to-prometheus namespace: optipod-systemspec: podSelector: matchLabels: app: optipod policyTypes: - Egress egress: - to: - namespaceSelector: matchLabels: name: monitoring - podSelector: matchLabels: app: prometheus ports: - protocol: TCP port: 90904. Enable TLS
Always use HTTPS for Prometheus connections:
prometheus: url: https://prometheus.example.com # Not http:// auth: type: bearer secretName: prometheus-token tls: insecureSkipVerify: false # Verify certificates5. Limit Permissions
Grant OptiPod only the permissions it needs:
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: name: optipod-prometheus-readerrules:- nonResourceURLs: ["/api/v1/query", "/api/v1/query_range"] verbs: ["get"]Troubleshooting
Authentication Failures
Check OptiPod logs:
kubectl logs -n optipod-system \ deployment/optipod-controller \ | grep prometheusCommon issues:
- 401 Unauthorized: Wrong credentials or expired token
- 403 Forbidden: Insufficient permissions
- Certificate errors: CA cert mismatch or expired certificates
Testing Connectivity
Test Prometheus access from within the cluster:
# For basic authkubectl run -it --rm debug \ --image=curlimages/curl \ --restart=Never -- \ curl -u username:password \ https://prometheus.example.com/api/v1/query?query=up
# For bearer tokenkubectl run -it --rm debug \ --image=curlimages/curl \ --restart=Never -- \ curl -H "Authorization: Bearer $TOKEN" \ https://prometheus.example.com/api/v1/query?query=upConclusion
Securing your Prometheus connection is crucial for production deployments. Choose the authentication method that best fits your security requirements:
- Basic Auth: Simple and effective for most use cases
- Bearer Token: Kubernetes-native and integrates with RBAC
- mTLS: Maximum security for regulated environments
For detailed configuration examples, check our Prometheus authentication documentation.