Skip to content

Commit

Permalink
K8SPXC-1276 add logformat for haproxy container (#822)
Browse files Browse the repository at this point in the history
* K8SPXC-1276 add 'option tcplog' option and add logformat for check_pxc.sh

* K8SPXC-1276 add 'option tcplog' option and add logformat for check_pxc.sh

* change format and add additional options

* fix comments
  • Loading branch information
hors authored Jul 25, 2023
1 parent 1f2422f commit 64e3ae4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
14 changes: 12 additions & 2 deletions haproxy/dockerdir/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
#!/bin/sh
set -e
set -o xtrace

log() {
local message=$1
local date=$(/usr/bin/date +"%d/%b/%Y:%H:%M:%S.%3N")

echo "{\"time\":\"${date}\", \"message\": \"${message}\"}"
}

if [ "$1" = 'haproxy' ]; then
if [ ! -f '/etc/haproxy/pxc/haproxy.cfg' ]; then
log '/etc/haproxy/haproxy.cfg /etc/haproxy/pxc'
cp /etc/haproxy/haproxy.cfg /etc/haproxy/pxc
fi

custom_conf='/etc/haproxy-custom/haproxy-global.cfg'
if [ -f "$custom_conf" ]; then
log "haproxy -c -f $custom_conf -f /etc/haproxy/pxc/haproxy.cfg"
haproxy -c -f $custom_conf -f /etc/haproxy/pxc/haproxy.cfg || EC=$?
if [ -n "$EC" ]; then
echo "The custom config $custom_conf is not valid and will be ignored."
log "The custom config $custom_conf is not valid and will be ignored."
fi
fi

Expand All @@ -24,6 +32,8 @@ if [ "$1" = 'haproxy' ]; then
haproxy_opt+='-f /etc/haproxy/pxc/haproxy.cfg -p /etc/haproxy/pxc/haproxy.pid -S /etc/haproxy/pxc/haproxy-main.sock '
fi

log 'test -e /opt/percona/hookscript/hook.sh && source /opt/percona/hookscript/hook.sh'
test -e /opt/percona/hookscript/hook.sh && source /opt/percona/hookscript/hook.sh

log "$@ $haproxy_opt"
exec "$@" $haproxy_opt
3 changes: 3 additions & 0 deletions haproxy/dockerdir/etc/haproxy/haproxy-global.cfg
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
global
log stdout format raw local0
maxconn 2048
external-check
insecure-fork-wanted
stats socket /etc/haproxy/pxc/haproxy.sock mode 600 expose-fd listeners level admin

defaults
no option dontlognull
log-format '{"time":"%t", "client_ip": "%ci", "client_port":"%cp", "backend_source_ip": "%bi", "backend_source_port": "%bp", "frontend_name": "%ft", "backend_name": "%b", "server_name":"%s", "tw": "%Tw", "tc": "%Tc", "Tt": "%Tt", "bytes_read": "%B", "termination_state": "%ts", "actconn": "%ac", "feconn" :"%fc", "beconn": "%bc", "srv_conn": "%sc", "retries": "%rc", "srv_queue": "%sq", "backend_queue": "%bq" }'
default-server init-addr last,libc,none
log global
mode tcp
Expand Down
15 changes: 12 additions & 3 deletions haproxy/dockerdir/usr/bin/add_pxc_nodes.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#!/bin/bash

set -o errexit
set -o xtrace

log() {
local message=$1
local date=$(/usr/bin/date +"%d/%b/%Y:%H:%M:%S.%3N")

echo "{\"time\":\"${date}\", \"message\": \"${message}\"}"
}

function main() {
echo "Running $0"
log "Running $0"

NODE_LIST=()
NODE_LIST_REPL=()
Expand All @@ -24,7 +30,7 @@ function main() {

while read pxc_host; do
if [ -z "$pxc_host" ]; then
echo "Could not find PEERS ..."
log 'Could not find PEERS ...'
exit 0
fi

Expand Down Expand Up @@ -71,6 +77,7 @@ cat <<-EOF > "$path_to_haproxy_cfg/haproxy.cfg"
external-check command /usr/local/bin/check_pxc.sh
EOF

log "number of available nodes are ${#NODE_LIST_REPL[@]}"
echo "${#NODE_LIST_REPL[@]}" > $path_to_haproxy_cfg/AVAILABLE_NODES
( IFS=$'\n'; echo "${NODE_LIST[*]}" ) >> "$path_to_haproxy_cfg/haproxy.cfg"

Expand Down Expand Up @@ -123,12 +130,14 @@ EOF
if [ -n "$main_node" ]; then
if /usr/local/bin/check_pxc.sh '' '' "$main_node"; then
for backup_server in ${NODE_LIST_BACKUP[@]}; do
log "shutdown sessions server $backup_server | socat stdio ${SOCKET}"
echo "shutdown sessions server $backup_server" | socat stdio "${SOCKET}"
done
fi
fi

if [ -S "$path_to_haproxy_cfg/haproxy-main.sock" ]; then
log "reload | socat stdio $path_to_haproxy_cfg/haproxy-main.sock"
echo 'reload' | socat stdio "$path_to_haproxy_cfg/haproxy-main.sock"
fi
}
Expand Down
17 changes: 13 additions & 4 deletions haproxy/dockerdir/usr/local/bin/check_pxc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,31 @@ if [ -f "$path_to_haproxy_cfg/AVAILABLE_NODES" ]; then
AVAILABLE_NODES=$(/bin/cat $path_to_haproxy_cfg/AVAILABLE_NODES)
fi

log() {
local address=$1
local port=$2
local message=$3
local date=$(/usr/bin/date +"%d/%b/%Y:%H:%M:%S.%3N")

echo "{\"time\":\"${date}\", \"backend_source_ip\": \"${address}\", \"backend_source_port\": \"${port}\", \"message\": \"${message}\"}"
}

PXC_NODE_STATUS=($(MYSQL_PWD="${MONITOR_PASSWORD}" $MYSQL_CMDLINE -h $PXC_SERVER_IP -P $PXC_SERVER_PORT \
-e "SHOW STATUS LIKE 'wsrep_local_state';SHOW VARIABLES LIKE 'pxc_maint_mode';SHOW GLOBAL STATUS LIKE 'wsrep_cluster_status';" \
| /usr/bin/grep -A 1 -E 'wsrep_local_state$|pxc_maint_mode$|wsrep_cluster_status$' | /usr/bin/sed -n -e '2p' -e '5p' -e '8p' | /usr/bin/tr '\n' ' '))

# ${PXC_NODE_STATUS[0]} - wsrep_local_state
# ${PXC_NODE_STATUS[1]} - pxc_maint_mod
# ${PXC_NODE_STATUS[2]} - wsrep_cluster_status
echo "The following values are used for PXC node $PXC_SERVER_IP in backend $HAPROXY_PROXY_NAME:"
echo "wsrep_local_state is ${PXC_NODE_STATUS[0]}; pxc_maint_mod is ${PXC_NODE_STATUS[1]}; wsrep_cluster_status is ${PXC_NODE_STATUS[2]}; $AVAILABLE_NODES nodes are available"
log "$PXC_SERVER_IP" "$PXC_SERVER_PORT" "The following values are used for PXC node $PXC_SERVER_IP in backend $HAPROXY_PROXY_NAME:"
log "$PXC_SERVER_IP" "$PXC_SERVER_PORT" "wsrep_local_state is ${PXC_NODE_STATUS[0]}; pxc_maint_mod is ${PXC_NODE_STATUS[1]}; wsrep_cluster_status is ${PXC_NODE_STATUS[2]}; $AVAILABLE_NODES nodes are available"
if [[ ${PXC_NODE_STATUS[2]} == 'Primary' && ( ${PXC_NODE_STATUS[0]} -eq 4 || \
${PXC_NODE_STATUS[0]} -eq 2 && ( "${AVAILABLE_NODES}" -le 1 || "${DONOR_IS_OK}" -eq 1 ) ) \
&& ${PXC_NODE_STATUS[1]} == 'DISABLED' ]];
then
echo "PXC node $PXC_SERVER_IP for backend $HAPROXY_PROXY_NAME is ok"
log "$PXC_SERVER_IP" "$PXC_SERVER_PORT" "PXC node $PXC_SERVER_IP for backend $HAPROXY_PROXY_NAME is ok"
exit 0
else
echo "PXC node $PXC_SERVER_IP for backend $HAPROXY_PROXY_NAME is not ok"
log "$PXC_SERVER_IP" "$PXC_SERVER_PORT" "PXC node $PXC_SERVER_IP for backend $HAPROXY_PROXY_NAME is not ok"
exit 1
fi

0 comments on commit 64e3ae4

Please sign in to comment.