start-two-nodes-in-docker.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. ## EMQX can only start with longname (https://erlang.org/doc/reference_manual/distributed.html)
  4. ## The host name part of EMQX's node name has to be static, this means we should either
  5. ## pre-assign static IP for containers, or ensure containers can communiate with each other by name
  6. ## this is why a docker network is created, and the containers's names have a dot.
  7. # ensure dir
  8. cd -P -- "$(dirname -- "$0")/../../"
  9. HAPROXY_PORTS=(-p 18083:18083 -p 8883:8883 -p 8084:8084)
  10. NET='emqx.io'
  11. NODE1="node1.$NET"
  12. NODE2="node2.$NET"
  13. COOKIE='this-is-a-secret'
  14. IPV6=0
  15. cleanup() {
  16. docker rm -f haproxy >/dev/null 2>&1 || true
  17. docker rm -f "$NODE1" >/dev/null 2>&1 || true
  18. docker rm -f "$NODE2" >/dev/null 2>&1 || true
  19. docker network rm "$NET" >/dev/null 2>&1 || true
  20. }
  21. show_help() {
  22. echo "Usage: $0 [options] EMQX_IMAGE1 [EMQX_IAMGE2]"
  23. echo ""
  24. echo "Specifiy which docker image to run with EMQX_IMAGE1"
  25. echo "EMQX_IMAGE2 is the same as EMQX_IMAGE1 if not set"
  26. echo ""
  27. echo "Options:"
  28. echo " -h, --help Show this help message and exit."
  29. echo " -P Add -p options for docker run to expose more HAProxy container ports."
  30. echo " -6 Test with IPv6"
  31. echo " -c Cleanup: delete docker network, force delete the containers."
  32. }
  33. while getopts "hc6P:" opt
  34. do
  35. case $opt in
  36. # -P option is treated similarly to docker run -P:
  37. # publish ports to random available host ports
  38. P) HAPROXY_PORTS=(-p 18083 -p 8883 -p 8084);;
  39. c) cleanup; exit 0;;
  40. h) show_help; exit 0;;
  41. 6) IPV6=1;;
  42. *) ;;
  43. esac
  44. done
  45. shift $((OPTIND - 1))
  46. IMAGE1="${1:-}"
  47. IMAGE2="${2:-${IMAGE1}}"
  48. if [ -z "${IMAGE1:-}" ] || [ -z "${IMAGE2:-}" ]; then
  49. show_help
  50. exit 1
  51. fi
  52. cleanup
  53. if [ ${IPV6} = 1 ]; then
  54. docker network create --ipv6 --subnet 2001:0DB8::/112 "$NET"
  55. RPC_ADDRESS="::"
  56. PROTO_DIST='inet6_tls'
  57. else
  58. docker network create "$NET"
  59. RPC_ADDRESS="0.0.0.0"
  60. PROTO_DIST='inet_tls'
  61. fi
  62. docker run -d -t --restart=always --name "$NODE1" \
  63. --net "$NET" \
  64. -e EMQX_LOG__CONSOLE_HANDLER__LEVEL=debug \
  65. -e EMQX_NODE_NAME="emqx@$NODE1" \
  66. -e EMQX_NODE_COOKIE="$COOKIE" \
  67. -e EMQX_CLUSTER__PROTO_DIST="${PROTO_DIST}" \
  68. -e EMQX_RPC__LISTEN_ADDRESS="${RPC_ADDRESS}" \
  69. -e EMQX_RPC__IPV6_ONLY="true" \
  70. -e EMQX_listeners__ssl__default__enable=false \
  71. -e EMQX_listeners__wss__default__enable=false \
  72. -e EMQX_listeners__tcp__default__proxy_protocol=true \
  73. -e EMQX_listeners__ws__default__proxy_protocol=true \
  74. "$IMAGE1"
  75. docker run -d -t --restart=always --name "$NODE2" \
  76. --net "$NET" \
  77. -e EMQX_LOG__CONSOLE_HANDLER__LEVEL=debug \
  78. -e EMQX_NODE_NAME="emqx@$NODE2" \
  79. -e EMQX_NODE_COOKIE="$COOKIE" \
  80. -e EMQX_CLUSTER__PROTO_DIST="${PROTO_DIST}" \
  81. -e EMQX_RPC__LISTEN_ADDRESS="${RPC_ADDRESS}" \
  82. -e EMQX_RPC__IPV6_ONLY="true" \
  83. -e EMQX_listeners__ssl__default__enable=false \
  84. -e EMQX_listeners__wss__default__enable=false \
  85. -e EMQX_listeners__tcp__default__proxy_protocol=true \
  86. -e EMQX_listeners__ws__default__proxy_protocol=true \
  87. "$IMAGE2"
  88. mkdir -p tmp
  89. cat <<EOF > tmp/haproxy.cfg
  90. ##----------------------------------------------------------------
  91. ## global 2021/04/05
  92. ##----------------------------------------------------------------
  93. global
  94. log stdout format raw daemon debug
  95. # Replace 1024000 with deployment connections
  96. maxconn 1000
  97. nbproc 1
  98. nbthread 2
  99. cpu-map auto:1/1-2 0-1
  100. tune.ssl.default-dh-param 2048
  101. ssl-default-bind-ciphers ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:DES-CBC3-SHA:HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP
  102. # Enable the HAProxy Runtime API
  103. # e.g. echo "show table emqx_tcp_back" | sudo socat stdio tcp4-connect:172.100.239.4:9999
  104. stats socket :9999 level admin expose-fd listeners
  105. ##----------------------------------------------------------------
  106. ## defaults
  107. ##----------------------------------------------------------------
  108. defaults
  109. log global
  110. mode tcp
  111. option tcplog
  112. # Replace 1024000 with deployment connections
  113. maxconn 1000
  114. timeout connect 30000
  115. timeout client 600s
  116. timeout server 600s
  117. ##----------------------------------------------------------------
  118. ## API
  119. ##----------------------------------------------------------------
  120. frontend emqx_dashboard
  121. mode tcp
  122. option tcplog
  123. bind *:18083
  124. default_backend emqx_dashboard_back
  125. backend emqx_dashboard_back
  126. # Must use a consistent dispatch when EMQX is running on different versions
  127. # because the js files for the dashboard is chunked, having the backends sharing
  128. # load randomly will cause the browser fail to GET some chunks (or get bad chunks if names clash)
  129. balance source
  130. mode http
  131. server emqx-1 $NODE1:18083
  132. server emqx-2 $NODE2:18083
  133. ##----------------------------------------------------------------
  134. ## TLS
  135. ##----------------------------------------------------------------
  136. frontend emqx_ssl
  137. mode tcp
  138. option tcplog
  139. bind *:8883 ssl crt /tmp/emqx.pem ca-file /usr/local/etc/haproxy/certs/cacert.pem verify required no-sslv3
  140. default_backend emqx_ssl_back
  141. frontend emqx_wss
  142. mode tcp
  143. option tcplog
  144. bind *:8084 ssl crt /tmp/emqx.pem ca-file /usr/local/etc/haproxy/certs/cacert.pem verify required no-sslv3
  145. default_backend emqx_wss_back
  146. backend emqx_ssl_back
  147. mode tcp
  148. balance static-rr
  149. server emqx-1 $NODE1:1883 check-send-proxy send-proxy-v2-ssl-cn
  150. server emqx-2 $NODE2:1883 check-send-proxy send-proxy-v2-ssl-cn
  151. backend emqx_wss_back
  152. mode tcp
  153. balance static-rr
  154. server emqx-1 $NODE1:8083 check-send-proxy send-proxy-v2-ssl-cn
  155. server emqx-2 $NODE2:8083 check-send-proxy send-proxy-v2-ssl-cn
  156. EOF
  157. haproxy_cid=$(docker run -d --name haproxy \
  158. --net "$NET" \
  159. -v "$(pwd)/tmp/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg" \
  160. -v "$(pwd)/apps/emqx/etc/certs:/usr/local/etc/haproxy/certs" \
  161. -w /usr/local/etc/haproxy \
  162. "${HAPROXY_PORTS[@]}" \
  163. "public.ecr.aws/docker/library/haproxy:2.4" \
  164. bash -c 'set -euo pipefail;
  165. cat certs/cert.pem certs/key.pem > /tmp/emqx.pem;
  166. haproxy -f haproxy.cfg')
  167. haproxy_ssl_port=$(docker inspect --format='{{(index (index .NetworkSettings.Ports "8084/tcp") 0).HostPort}}' "$haproxy_cid")
  168. wait_limit=60
  169. wait_for_emqx() {
  170. container="$1"
  171. wait_limit="$2"
  172. wait_sec=0
  173. while ! docker exec "$container" emqx ctl status; do
  174. wait_sec=$(( wait_sec + 1 ))
  175. if [ $wait_sec -gt "$wait_limit" ]; then
  176. echo "timeout wait for EMQX"
  177. exit 1
  178. fi
  179. echo -n '.'
  180. sleep 1
  181. done
  182. }
  183. wait_for_haproxy() {
  184. wait_sec=0
  185. wait_limit="$1"
  186. set +x
  187. while ! openssl s_client \
  188. -CAfile apps/emqx/etc/certs/cacert.pem \
  189. -cert apps/emqx/etc/certs/cert.pem \
  190. -key apps/emqx/etc/certs/key.pem \
  191. localhost:"$haproxy_ssl_port" </dev/null; do
  192. wait_sec=$(( wait_sec + 1 ))
  193. if [ $wait_sec -gt "$wait_limit" ]; then
  194. echo "timeout wait for haproxy"
  195. exit 1
  196. fi
  197. echo -n '.'
  198. sleep 1
  199. done
  200. }
  201. wait_for_emqx "$NODE1" 30
  202. wait_for_emqx "$NODE2" 30
  203. wait_for_haproxy 10
  204. echo
  205. docker exec $NODE1 emqx ctl cluster join "emqx@$NODE2"