-
Notifications
You must be signed in to change notification settings - Fork 96
feat: Add Valkey Sentinel & HAProxy support in High Availability setup. #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
khtee
wants to merge
16
commits into
valkey-io:main
Choose a base branch
from
khtee:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4c7c981
feat: Add Valkey Sentinel support in High Availability setup.
khtee 32de638
feat: Add Valkey Sentinel & HAProxy support in High Availability setup.
khtee 708c707
Add securityContext to HAProxy & Sentinel watcher
khtee adc429d
Use .Chart.AppVersion for sentinel watcher
khtee 7184810
Add init-addr last,libc,none to HAProxy
khtee 91017b0
Enable TLS option in valkey-cli
khtee 5337c57
Add HAProxy to value schema
khtee e7f10b2
Adding extraInitContainer and fix healthcheck command
khtee c3800eb
Add volumeClaimTemplates for sentinel-data
khtee 43f9a4c
Use dynamic accessModes for sentinel persistence
khtee 2421963
feat(sentinel): add native ACL support with secure password handling
fccfbbc
add Inter-Sentinel and Sentinel-Monitor authentication configuration
0e9d6f3
updated
97b2838
feat(sentinel): Updated HAProxy Sentinel Watcher
d6b55d5
Clenning haproxy-service and added support for commonLabels and podA…
jdominguez-ops d8f854d
Merge pull request #1 from jose-10000/fix/sentinel-acl-hardening-and-…
khtee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,219 @@ | ||||||||||||
| {{- if .Values.haproxy.enabled }} | ||||||||||||
| apiVersion: v1 | ||||||||||||
| kind: ConfigMap | ||||||||||||
| metadata: | ||||||||||||
| name: {{ include "valkey.fullname" . }}-haproxy | ||||||||||||
| labels: | ||||||||||||
| {{- include "valkey.labels" . | nindent 4 }} | ||||||||||||
| data: | ||||||||||||
| haproxy.cfg: | | ||||||||||||
| global | ||||||||||||
| log stdout format raw local0 | ||||||||||||
| maxconn 1024 | ||||||||||||
| stats socket /var/run/haproxy/admin.sock mode 660 level admin expose-fd listeners | ||||||||||||
|
|
||||||||||||
| defaults | ||||||||||||
| log global | ||||||||||||
| timeout connect {{ .Values.haproxy.config.timeout.connect }} | ||||||||||||
| timeout client {{ .Values.haproxy.config.timeout.client }} | ||||||||||||
| timeout server {{ .Values.haproxy.config.timeout.server }} | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| retries 3 | ||||||||||||
|
|
||||||||||||
| frontend valkey_frontend_write | ||||||||||||
| bind *:{{ .Values.haproxy.service.port | default 6379 }} | ||||||||||||
| mode tcp | ||||||||||||
| option tcplog | ||||||||||||
| default_backend valkey_backend_master | ||||||||||||
|
|
||||||||||||
| frontend valkey_frontend_read | ||||||||||||
| bind *:{{ .Values.haproxy.service.readPort | default 6380 }} | ||||||||||||
| mode tcp | ||||||||||||
| option tcplog | ||||||||||||
| default_backend valkey_backend_read | ||||||||||||
|
|
||||||||||||
| backend valkey_backend_master | ||||||||||||
| mode tcp | ||||||||||||
| # no-check: sentinel-watcher solely controls which server is active via | ||||||||||||
| # the runtime socket (set server addr + enable/disable server). | ||||||||||||
| # This prevents HAProxy's own DNS resolver from putting servers into | ||||||||||||
| # DNS NX maintenance, which cannot be cleared by enable server. | ||||||||||||
| {{- range $i := until (add (int .Values.replica.replicas) 1 | int) }} | ||||||||||||
| server valkey-{{ $i }} 127.0.0.1:6379 check-send-proxy disabled | ||||||||||||
| {{- end }} | ||||||||||||
|
|
||||||||||||
| backend valkey_backend_read | ||||||||||||
| mode tcp | ||||||||||||
| option tcp-check | ||||||||||||
|
|
||||||||||||
| # Step 1-2: Connect (must use SSL if TLS is enabled) | ||||||||||||
| {{- if .Values.tls.enabled }} | ||||||||||||
| tcp-check connect port {{ .Values.service.port }} ssl | ||||||||||||
| {{- else }} | ||||||||||||
| tcp-check connect port {{ .Values.service.port }} | ||||||||||||
| {{- end }} | ||||||||||||
|
|
||||||||||||
| # Step 3: Send PING | ||||||||||||
| tcp-check send "PING\r\n" | ||||||||||||
|
|
||||||||||||
| # Step 4: Expect PONG or any Auth error (which proves the service is up) | ||||||||||||
| # We use a broader regex to catch "-NOAUTH" and "-ERR AUTH" | ||||||||||||
| tcp-check expect rstring ^(\+PONG|-[Nn][Oo][Aa][Uu][Tt][Hh]|-[Ee][Rr][Rr]) | ||||||||||||
|
|
||||||||||||
| timeout connect 2s | ||||||||||||
| timeout check 5s | ||||||||||||
| # Read backend uses health checks since any replica is acceptable. | ||||||||||||
| # Servers start enabled; all will pass a simple PING check. | ||||||||||||
| {{- range $i := until (add (int .Values.replica.replicas) 1 | int) }} | ||||||||||||
| server valkey-{{ $i }} {{ include "valkey.fullname" $ }}-{{ $i }}.{{ include "valkey.headlessServiceName" $ }}.{{ $.Release.Namespace }}.svc.{{ $.Values.clusterDomain }}:{{ $.Values.service.port }} check inter 5s fall 3 rise 1 init-addr last,libc,none {{ if $.Values.tls.enabled }}ssl verify none{{ end }} | ||||||||||||
| {{- end }} | ||||||||||||
|
|
||||||||||||
| sentinel-watcher.sh: |- | ||||||||||||
| #!/bin/sh | ||||||||||||
| # Sentinel watcher: polls Sentinel for master changes and updates HAProxy | ||||||||||||
| # via the runtime socket. | ||||||||||||
| # | ||||||||||||
| # KEY DESIGN: All valkey_backend_master servers are configured as | ||||||||||||
| # `no-check disabled` in haproxy.cfg. This watcher is the SOLE controller | ||||||||||||
| # of which server is active. It resolves the master hostname to a Pod IP | ||||||||||||
| # and uses `set server addr` so HAProxy connects directly to the IP, | ||||||||||||
| # completely bypassing HAProxy's internal DNS resolver (which causes | ||||||||||||
| # DNS NX maintenance that cannot be overridden by `enable server`). | ||||||||||||
| set -eu | ||||||||||||
|
|
||||||||||||
| SENTINEL_PORT="{{ .Values.replica.sentinel.port }}" | ||||||||||||
| MASTER_SET="{{ .Values.replica.sentinel.masterSet }}" | ||||||||||||
| POLL_INTERVAL="{{ .Values.haproxy.config.checkInterval | default 2 }}" | ||||||||||||
| VALKEY_PORT="{{ .Values.service.port }}" | ||||||||||||
| HAPROXY_SOCKET="/var/run/haproxy/admin.sock" | ||||||||||||
| BACKEND="valkey_backend_master" | ||||||||||||
| TOTAL_SERVERS="{{ add (int .Values.replica.replicas) 1 }}" | ||||||||||||
|
|
||||||||||||
| # Logging to stderr prevents polluting stdout | ||||||||||||
| log() { echo "$(date) $1" >&2; } | ||||||||||||
|
|
||||||||||||
| {{- if .Values.auth.enabled }} | ||||||||||||
| # Authentication helpers | ||||||||||||
| get_user_password() { | ||||||||||||
| username="$1" | ||||||||||||
| password_key="${2:-$username}" | ||||||||||||
| if [ -f "/valkey-users-secret/$password_key" ]; then | ||||||||||||
| cat "/valkey-users-secret/$password_key" | ||||||||||||
| elif [ -f "/valkey-auth-secret/${username}-password" ]; then | ||||||||||||
| cat "/valkey-auth-secret/${username}-password" | ||||||||||||
| fi | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| {{- $watcherUser := .Values.haproxy.sentinelWatcher.user | default "default" }} | ||||||||||||
| {{- $userObj := index .Values.auth.sentinelAclUsers $watcherUser | default (dict "passwordKey" "") }} | ||||||||||||
| {{- $passKey := $userObj.passwordKey | default $watcherUser }} | ||||||||||||
|
|
||||||||||||
| WATCHER_USER="{{ $watcherUser }}" | ||||||||||||
| WATCHER_PASS=$(get_user_password "$WATCHER_USER" "{{ $passKey }}") | ||||||||||||
| {{- end }} | ||||||||||||
|
|
||||||||||||
| # Base command assembly for Auth and TLS support | ||||||||||||
| CLI_BASE="valkey-cli -p ${SENTINEL_PORT}" | ||||||||||||
|
|
||||||||||||
| {{- if .Values.auth.enabled }} | ||||||||||||
| if [ "$WATCHER_USER" != "default" ]; then | ||||||||||||
| CLI_BASE="${CLI_BASE} --user ${WATCHER_USER} -a ${WATCHER_PASS}" | ||||||||||||
| else | ||||||||||||
| CLI_BASE="${CLI_BASE} -a ${WATCHER_PASS}" | ||||||||||||
| fi | ||||||||||||
| {{- end }} | ||||||||||||
|
|
||||||||||||
| {{- if .Values.tls.enabled }} | ||||||||||||
| CLI_BASE="${CLI_BASE} --tls --cacert /tls/{{ .Values.tls.caPublicKey }} --cert /tls/{{ .Values.tls.serverPublicKey }} --key /tls/{{ .Values.tls.serverKey }}" | ||||||||||||
| {{- end }} | ||||||||||||
|
|
||||||||||||
| # Wrapper to query Sentinel | ||||||||||||
| sentinel_cmd() { | ||||||||||||
| local host=$1 | ||||||||||||
| shift | ||||||||||||
| ${CLI_BASE} -h "${host}" "$@" | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| # Wrapper to update HAProxy via socket | ||||||||||||
| haproxy_cmd() { | ||||||||||||
| echo "$1" | socat stdio "${HAPROXY_SOCKET}" | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| resolve_ip() { | ||||||||||||
| getent hosts "$1" | awk '{print $1}' | ||||||||||||
| } | ||||||||||||
| LAST_MASTER_HOST="" | ||||||||||||
|
|
||||||||||||
| while true; do | ||||||||||||
| MASTER_HOST="" | ||||||||||||
|
|
||||||||||||
| # 1. Ask Sentinels who the master is | ||||||||||||
| i=0 | ||||||||||||
| while [ "${i}" -lt "${TOTAL_SERVERS}" ]; do | ||||||||||||
| S_HOST="{{ include "valkey.fullname" . }}-${i}.{{ include "valkey.headlessServiceName" . }}.{{ .Release.Namespace }}.svc.{{ .Values.clusterDomain }}" | ||||||||||||
|
|
||||||||||||
| RESP=$(sentinel_cmd "${S_HOST}" sentinel get-master-addr-by-name "${MASTER_SET}" 2>/dev/null) || { | ||||||||||||
| i=$((i + 1)) | ||||||||||||
| continue | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if [ -n "${RESP}" ]; then | ||||||||||||
| MASTER_HOST=$(echo "${RESP}" | head -n 1) | ||||||||||||
| break | ||||||||||||
| fi | ||||||||||||
| i=$((i + 1)) | ||||||||||||
| done | ||||||||||||
|
|
||||||||||||
| if [ -z "${MASTER_HOST}" ]; then | ||||||||||||
| sleep "${POLL_INTERVAL}" | ||||||||||||
| continue | ||||||||||||
| fi | ||||||||||||
|
|
||||||||||||
| # 2. Check if master actually changed | ||||||||||||
| if [ "${MASTER_HOST}" = "${LAST_MASTER_HOST}" ]; then | ||||||||||||
| sleep "${POLL_INTERVAL}" | ||||||||||||
| continue | ||||||||||||
| fi | ||||||||||||
|
|
||||||||||||
| # 3. Find the actual HAProxy backend index for this master | ||||||||||||
| MASTER_IDX="" | ||||||||||||
| j=0 | ||||||||||||
| while [ "${j}" -lt "${TOTAL_SERVERS}" ]; do | ||||||||||||
| EXPECTED_HOST="{{ include "valkey.fullname" . }}-${j}.{{ include "valkey.headlessServiceName" . }}.{{ .Release.Namespace }}.svc.{{ .Values.clusterDomain }}" | ||||||||||||
| if [ "${MASTER_HOST}" = "${EXPECTED_HOST}" ]; then | ||||||||||||
| MASTER_IDX="${j}" | ||||||||||||
| break | ||||||||||||
| fi | ||||||||||||
| j=$((j + 1)) | ||||||||||||
| done | ||||||||||||
|
|
||||||||||||
| if [ -z "${MASTER_IDX}" ]; then | ||||||||||||
| log "WARN: Unknown master host '${MASTER_HOST}'" | ||||||||||||
| sleep "${POLL_INTERVAL}" | ||||||||||||
| continue | ||||||||||||
| fi | ||||||||||||
|
|
||||||||||||
| log "Master changed: ${LAST_MASTER_HOST:-none} -> ${MASTER_HOST} (HAProxy Backend Index: valkey-${MASTER_IDX})" | ||||||||||||
|
|
||||||||||||
| MASTER_IP=$(resolve_ip "${MASTER_HOST}") | ||||||||||||
| if [ -z "${MASTER_IP}" ]; then | ||||||||||||
| log "WARN: Could not resolve IP for '${MASTER_HOST}', retrying..." | ||||||||||||
| sleep "${POLL_INTERVAL}" | ||||||||||||
| continue | ||||||||||||
| fi | ||||||||||||
|
|
||||||||||||
| # 4. Update HAProxy routing safely | ||||||||||||
| j=0 | ||||||||||||
| while [ "${j}" -lt "${TOTAL_SERVERS}" ]; do | ||||||||||||
| if [ "${j}" != "${MASTER_IDX}" ]; then | ||||||||||||
| haproxy_cmd "disable server ${BACKEND}/valkey-${j}" > /dev/null || true | ||||||||||||
| fi | ||||||||||||
| j=$((j + 1)) | ||||||||||||
| done | ||||||||||||
|
|
||||||||||||
| haproxy_cmd "set server ${BACKEND}/valkey-${MASTER_IDX} addr ${MASTER_IP} port ${VALKEY_PORT}" > /dev/null | ||||||||||||
| haproxy_cmd "enable server ${BACKEND}/valkey-${MASTER_IDX}" > /dev/null | ||||||||||||
|
|
||||||||||||
| LAST_MASTER_HOST="${MASTER_HOST}" | ||||||||||||
| sleep "${POLL_INTERVAL}" | ||||||||||||
| done | ||||||||||||
| {{- end }} | ||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deploy_valkey.yamlto fail if haproxy is enabled in standalone mode.