Security and monitoring
ACLs and TLS for Redis on OpenShift, and the metrics that tell you it is full, slow, or failing over, in Prometheus and Grafana.
The earlier labs left Redis with no password, on a ClusterIP that any pod in the cluster can dial, and with no graph. That is a reasonable way to learn SET. It is not a reasonable way to keep sessions. This lab adds an ACL, TLS, a NetworkPolicy, and a ServiceMonitor the platform's Prometheus can scrape.
The instance is still cache in the cache namespace, operator API redis.redis.opstreelabs.in/v1beta2.
A user that is not the admin
Redis 6 and later has ACLs. The operator's redisSecret sets the password of the default user, which is an administrator. Applications should not use it. Give the application a user that can touch its own key prefix and nothing else.
oc create secret generic cache-auth -n cache --from-literal=password="$(openssl rand -base64 24)"
Add the application user in the same ConfigMap key the persistence lab uses (redis-additional.conf). Pick a password and store it in a Secret the workload mounts. Do not commit it.
user app on >REPLACE_WITH_SECRET ~cache:* ~session:* ~q:* ~doc:* +@read +@write +@connection -@dangerous -@admin
user default on sanitize-payload >REPLACE_WITH_ADMIN_SECRET ~* &* +@all
~cache:* limits keys. -@dangerous removes FLUSHALL, DEBUG and friends. -@admin removes ACL, CONFIG and SHUTDOWN. A compromised application can still delete cache:*. It cannot reconfigure Redis or read session:* unless you added that pattern. Keep sessions and vectors on separate users if they are separate trust domains.
apiVersion: redis.redis.opstreelabs.in/v1beta2
kind: Redis
metadata:
name: cache
namespace: cache
spec:
kubernetesConfig:
image: quay.io/opstree/redis:v8.2.1
redisSecret:
name: cache-auth
key: password
redisConfig:
additionalRedisConfig: cache-redis-config
redisExporter:
enabled: true
image: quay.io/opstree/redis-exporter:v1.58.0
storage:
volumeClaimTemplate:
spec:
accessModes: ["ReadWriteOnce"]
resources: {requests: {storage: 2Gi}}
If redisSecret and an explicit user default line disagree, the pod will refuse one of them. Check the log. The operator documents, for the version you installed, whether the secret is rendered as requirepass or as an ACL line. requirepass is the older switch and it sets the default user's password. Prefer one mechanism.
oc exec -n cache cache-0 -- redis-cli -a "$ADMIN" ACL LIST
oc exec -n cache cache-0 -- redis-cli --user app --pass "$APP" PING
oc exec -n cache cache-0 -- redis-cli --user app --pass "$APP" CONFIG GET save
PING returns PONG. CONFIG GET returns NOPERM. That is the test. Run it. A user that can still run CONFIG SET is the admin.
The exporter needs INFO, PING, CLIENT LIST and SLOWLOG. If you locked it out with the default user, give it its own ACL user and set the exporter's password field the operator exposes (oc explain redis.spec.redisExporter). A scrape that is down looks like Redis is down. It is not.
TLS and the network
Inside the cluster, TLS stops any pod that can sniff or spoof a neighbour from reading sessions. The operator terminates TLS when you point it at a Secret of tls.crt, tls.key and ca.crt:
spec:
TLS:
secret:
secretName: cache-tls
certKey: tls.crt
keyKey: tls.key
caKey: ca.crt
Issue the certificate for the Service DNS name cache.cache.svc. Clients then use rediss:// (two s) or redis-cli --tls --cacert ca.crt. A client that still speaks plain TCP will hang or reset. Roll the client configuration in the same change as the server.
Confirm the field names against oc explain redis.spec.TLS if the operator version differs. Then confirm the listener:
oc exec -n cache cache-0 -- redis-cli --tls --cacert /path/ca.crt PING
A NetworkPolicy is the other half. Only the application namespace and the exporter's scraper should reach 6379. Prometheus on OpenShift scrapes from the openshift-monitoring namespace when you enable user-workload monitoring, so the policy has to admit that namespace or the scrape fails closed. Allow 9121 for the exporter, not for the whole cluster.
Do not put a public Route in front of Redis. Applications inside the cluster use the Service. An administrator who needs a shell uses oc exec.
Metrics
Enable user-workload monitoring once per cluster (uwm in openshift-user-workload-monitoring). Then a ServiceMonitor in the cache namespace:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: cache
namespace: cache
spec:
selector:
matchLabels:
app: cache
endpoints:
- port: redis-exporter
interval: 30s
The port name has to match the Service the operator created. oc get svc -n cache -o yaml shows it. If the target is down in the Prometheus UI, the label or the port name is wrong, or the NetworkPolicy is. The metric names that pay the rent:
| Metric | Why you look |
|---|---|
redis_up |
The exporter reached Redis. Zero is an outage or an ACL mistake. |
redis_memory_used_bytes and redis_memory_max_bytes |
The dataset approaching maxmemory. Vector indexes live here. |
redis_connected_clients |
A leak, or a restart storm. |
redis_commands_processed_total |
Rate, by command if the exporter adds the label. A sudden KEYS is someone about to block the shard. |
redis_keyspace_hits_total and redis_keyspace_misses_total |
Whether the cache is earning its memory. |
redis_rdb_last_bgsave_status |
The snapshot from the persistence lab. Non-ok means you cannot restore. |
Alert on redis_up == 0 for a few minutes, on a failed RDB save, and on memory above the level where an eviction would hurt. Do not alert on every miss. A cache is allowed to miss.
Grafana ships with OpenShift. Import a Redis dashboard and point it at these metrics, or build four panels: up, memory, hit ratio, connected clients. A failover from the HA lab should show a dip in redis_up or a move in the primary's identity, then a return. If the graph does not move when you delete the primary, you are looking at the wrong instance.
What's next
This is the last Redis lab. The series started at Redis. The same questions for an Infinispan cluster are the Data Grid labs. Platform-wide traces, which a Redis metric will not give you, are Tracing.