2018年10月31日水曜日

HAProxy 備忘録

Check version and used config file

[root@node1 ~]# ps -elf | grep haproxy
4 S root     25713     1  0  80   0 - 11182 do_wai Oct30 ?        00:00:00 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
0 S root     25714 25713  0  80   0 - 12730 do_wai Oct30 ?        00:00:00 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
1 S root     25715 25714  0  80   0 - 12730 ep_pol Oct30 ?        00:01:14 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds

[root@node1 ~]# /usr/sbin/haproxy -v
HA-Proxy version 1.5.18 2016/05/10

Copyright 2000-2016 Willy Tarreau <willy@haproxy.org>



Enabling logging:
[root@node1 log]# vim /etc/rsyslog.conf
    ...
    $ModLoad imudp
    $UDPServerRun 514

[root@node1 log]# service rsyslog restart
Redirecting to /bin/systemctl restart rsyslog.service
[root@node1 log]# service haproxy restart

Redirecting to /bin/systemctl restart haproxy.service

How to interpret the health check log format, and log related config options

Refs:
https://cbonte.github.io/haproxy-dconv/1.5/configuration.html#8.2
https://cbonte.github.io/haproxy-dconv/1.7/configuration.html#8.2
https://www.haproxy.com/documentation/aloha/8-0/traffic-management/lb-layer7/health-checks/


Example config (part):
frontend postgres_tcp_front
  mode tcp
  bind *:<some port>
  option tcplog
  log global

  default_backend postgres_tcp_backend

backend postgres_tcp_backend
  option log-health-checks
  mode tcp
  option httpchk GET / HTTP/1.1
  http-check expect status 200

    server <server 1> <server 2>:5432 resolvers some_dns check inter 2000 rise 2 fall 3 port XXXX

tcplog = "TCP format, which is more advanced. This format is enabled when "option
    tcplog" is set on the frontend"
inter = interval
rize = "Number of consecutive valid health checks before considering the server as UP"
port = "TCP port where the TCP connection is established. If not set, uses the server's line port; if set, uses the port on the configured server"


Example log line:
[WARNING] 302/165834 (9050) : Health check for server postgres_tcp_backend/<server name> failed, reason: Layer7 wrong status, code: 503, info: "HTTP status check returned code <3C>503<3E>", check duration: 12ms, status: 0/2 DOWN.

302 = days since 1st Jan
165834 = hhmmss
(9050) = PID
0/2 DOWN = hasn't been down for past two times (coming from 'rise' and 'fail' keywords)










2018年10月10日水曜日

Scalaのコードまたはクラスをちょっとだけ変えてみる

上記と同様の手段で、Scalaも変えてみる。

Scalaをインストールする

function f_setup_scala() {
    local _ver="${1:-2.12.3}"
    local _extract_dir="${2:-/opt}}"
    local _inst_dir="${3:-/usr/local/scala}"

    if [ ! -d "${_extract_dir%/}/scala-${_ver}" ]; then
        if [ ! -s "${_extract_dir%/}/scala-${_ver}.tgz" ]; then
            curl --retry 3 -C - -o "${_extract_dir%/}/scala-${_ver}.tgz" "https://downloads.lightbend.com/scala/${_ver}/scala-${_ver}.tgz" || return $?
        fi
        tar -xf "${_extract_dir%/}/scala-${_ver}.tgz" -C "${_extract_dir%/}/" || return $?
        chmod a+x ${_extract_dir%/}/bin/*
    fi
    [ -d "${_inst_dir%/}" ] || ln -s "${_extract_dir%/}/scala-${_ver}" "${_inst_dir%/}"
    export SCALA_HOME=${_inst_dir%/}
    export PATH=$PATH:$SCALA_HOME/bin
}

書き換えたいクラスファイルを作成したあと、コンパイル

vim TestUtils.scala

export CLASSPATH=...(snip)...

scalac TestUtils.scala

CLASSPATHは走っているプロセスと同じのであれば、下記のファンクションが利用可

function f_javaenvs() {
    local _port="${1}"
    local _p=`lsof -ti:${_port}`
    if [ -z "${_p}" ]; then
        echo "Nothing running on port ${_port}"
        return 11
    fi
    local _user="`stat -c '%U' /proc/${_p}`"
    local _dir="$(dirname `readlink /proc/${_p}/exe` 2>/dev/null)"
    export JAVA_HOME="$(dirname $_dir)"
    export CLASSPATH=".:`sudo -u ${_user} $JAVA_HOME/bin/jcmd ${_p} VM.system_properties | sed -nr 's/^java.class.path=(.+$)/\1/p' | sed 's/[\]:/:/g'`"
}

差し替えたいJarファイルを見つける

function f_jargrep() {
    local _cmd="jar -tf"
    which jar &>/dev/null || _cmd="less"
    find -L ${2:-./} -type f -name '*.jar' -print0 | xargs -0 -n1 -I {} bash -c ''${_cmd}' {} | grep -w '$1' && echo {}'
}

バックアップを作成後(重要!)差し替える

$JAVA_HOME/bin/jar -uf /usr/local/test/lib/test.jar com/test/utils/TestUtils*.class

確認する

$JAVA_HOME/bin/jar -tvf /usr/local/test/lib/test.jar | grep TestUtils
$JAVA_HOME/bin/javap -cp /usr/local/test/lib/test.jar -private com.test.utils.TestUtils