sh Unbounce脚本片段,用于在零停机时间内重新启动HAProxy

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh Unbounce脚本片段,用于在零停机时间内重新启动HAProxy相关的知识,希望对你有一定的参考价值。

echo "Flipping tables! (╯°□°)╯︵ ┻━┻"

num_rules=3

real=3    # exposed to the ELB as port 443
test=4    # used to install test certs for domain verification
health=5  # used by the ELB healthcheck

blue_prefix=855
green_prefix=866

function iptables_status {
   blue=$(sudo iptables -t nat -L -n -v | grep REDIRECT | grep ${blue_prefix} | wc -l)
   green=$(sudo iptables -t nat -L -n -v | grep REDIRECT | grep ${green_prefix} | wc -l)

   if [[ ${blue} == 0 && ${green} == 0 ]]; then
      echo "none"
   elif [[ ${blue} == ${num_rules} && ${green} == ${num_rules} ]]; then
      echo "both"
   elif [[ ${blue} == ${num_rules} ]]; then
      echo "blue"
   elif [[ ${green} == ${num_rules} ]]; then
      echo "green"
   else
      echo "unknown"
   fi
}

function add {
  instance=$1
  instance_prefix="${instance}_prefix"

  real_port="${!instance_prefix}${real}"
  test_port="${!instance_prefix}${test}"
  health_port="${!instance_prefix}${health}"

  sudo iptables -t nat -A PREROUTING -m state --state NEW -p tcp --dport 8443 -j REDIRECT --to ${real_port}
  sudo iptables -t nat -A PREROUTING -m state --state NEW -p tcp --dport 8444 -j REDIRECT --to ${test_port}
  sudo iptables -t nat -A PREROUTING -m state --state NEW -p tcp --dport 8445 -j REDIRECT --to ${health_port}
}

function remove {
  instance=$1
  instance_prefix="${instance}_prefix"

  real_port="${!instance_prefix}${real}"
  test_port="${!instance_prefix}${test}"
  health_port="${!instance_prefix}${health}"

  sudo iptables -t nat -D PREROUTING -m state --state NEW -p tcp --dport 8443 -j REDIRECT --to ${real_port}
  sudo iptables -t nat -D PREROUTING -m state --state NEW -p tcp --dport 8444 -j REDIRECT --to ${test_port}
  sudo iptables -t nat -D PREROUTING -m state --state NEW -p tcp --dport 8445 -j REDIRECT --to ${health_port}
}

# check which one was last reloaded -> i.e. via iptables list
status=$(iptables_status)
echo "Currently: "${status}

# if none exists default to blue (e.g. after boot)
# otherwise choose the opposite one reload it and swap the rules
if [[ ${status} == "none" ]]; then
  echo "Initially routing to Blue"
  sudo service haproxy-blue reload
  add blue
elif [[ ${status} == "green" ]]; then
  echo "Switching routing to Blue"
  sudo service haproxy-blue reload
  add blue
  remove green
elif [[ ${status} == "blue" ]]; then
  echo "Switching routing to Green"
  sudo service haproxy-green reload
  add green
  remove blue
else
  echo "[ALERT] unknown ipfilters state!"
  sudo iptables -t nat -L -n -v
fi

以上是关于sh Unbounce脚本片段,用于在零停机时间内重新启动HAProxy的主要内容,如果未能解决你的问题,请参考以下文章

如何在零停机的情况下迁移 Kubernetes 集群

javascript Unbounce事件跟踪器脚本

sh Laravel Forge无需停机即可部署脚本

如何在集群环境中进行零停机时间部署?

sh 一个片段,可以帮助您在rc脚本中找到“source”的时间成本

为啥这个带有 shebang #!/bin/sh 和 exec python 的片段在 4 个单引号内起作用?