浏览代码

Install and use toxiproxy in vagrant

This sends the public zk ports (2181-2185) and kafka ports (6667-6671) through
toxiproxy.
Evan Huus 10 年之前
父节点
当前提交
5dd3218ce9

+ 3 - 1
vagrant/boot_cluster.sh

@@ -1,7 +1,9 @@
-#/bin/sh
+#!/bin/sh
 
 set -ex
 
+vagrant/run_toxiproxy.sh &
+
 for i in 1 2 3 4 5; do
     KAFKA_PORT=`expr $i + 6666`
     cd ${KAFKA_INSTALL_ROOT}/kafka-${KAFKA_PORT} && bin/zookeeper-server-start.sh -daemon config/zookeeper.properties

+ 11 - 2
vagrant/install_cluster.sh

@@ -1,15 +1,23 @@
-#/bin/sh
+#!/bin/sh
 
 set -ex
 
+TOXIPROXY_VERSION=1.0.0
+
 mkdir -p ${KAFKA_INSTALL_ROOT}
 if [ ! -f ${KAFKA_INSTALL_ROOT}/kafka-${KAFKA_VERSION}.tgz ]; then
     wget --quiet http://apache.mirror.gtcomm.net/kafka/${KAFKA_VERSION}/kafka_2.10-${KAFKA_VERSION}.tgz -O ${KAFKA_INSTALL_ROOT}/kafka-${KAFKA_VERSION}.tgz
 fi
+if [ ! -f ${KAFKA_INSTALL_ROOT}/toxiproxy ]; then
+    wget --quiet https://github.com/Shopify/toxiproxy/releases/download/v${TOXIPROXY_VERSION}/toxiproxy-linux-amd64 -O ${KAFKA_INSTALL_ROOT}/toxiproxy
+    chmod +x ${KAFKA_INSTALL_ROOT}/toxiproxy
+fi
 
 for i in 1 2 3 4 5; do
     ZK_PORT=`expr $i + 2180`
+    ZK_PORT_REAL=`expr $i + 21800`
     KAFKA_PORT=`expr $i + 6666`
+    KAFKA_PORT_REAL=`expr $i + 26666`
 
     # unpack kafka
     mkdir -p ${KAFKA_INSTALL_ROOT}/kafka-${KAFKA_PORT}
@@ -18,6 +26,7 @@ for i in 1 2 3 4 5; do
     # broker configuration
     cp ${REPOSITORY_ROOT}/vagrant/server.properties ${KAFKA_INSTALL_ROOT}/kafka-${KAFKA_PORT}/config/
     sed -i s/KAFKAID/${KAFKA_PORT}/g ${KAFKA_INSTALL_ROOT}/kafka-${KAFKA_PORT}/config/server.properties
+    sed -i s/KAFKAPORT/${KAFKA_PORT_REAL}/g ${KAFKA_INSTALL_ROOT}/kafka-${KAFKA_PORT}/config/server.properties
     sed -i s/KAFKA_HOSTNAME/${KAFKA_HOSTNAME}/g ${KAFKA_INSTALL_ROOT}/kafka-${KAFKA_PORT}/config/server.properties
 
     KAFKA_DATADIR="${KAFKA_INSTALL_ROOT}/kafka-${KAFKA_PORT}/data"
@@ -27,7 +36,7 @@ for i in 1 2 3 4 5; do
     # zookeeper configuration
     cp ${REPOSITORY_ROOT}/vagrant/zookeeper.properties ${KAFKA_INSTALL_ROOT}/kafka-${KAFKA_PORT}/config/
     sed -i s/KAFKAID/${KAFKA_PORT}/g ${KAFKA_INSTALL_ROOT}/kafka-${KAFKA_PORT}/config/zookeeper.properties
-    sed -i s/ZK_PORT/${ZK_PORT}/g ${KAFKA_INSTALL_ROOT}/kafka-${KAFKA_PORT}/config/zookeeper.properties
+    sed -i s/ZK_PORT/${ZK_PORT_REAL}/g ${KAFKA_INSTALL_ROOT}/kafka-${KAFKA_PORT}/config/zookeeper.properties
 
     ZK_DATADIR="${KAFKA_INSTALL_ROOT}/zookeeper-${ZK_PORT}"
     mkdir -p ${ZK_DATADIR}

+ 22 - 0
vagrant/run_toxiproxy.sh

@@ -0,0 +1,22 @@
+#!/bin/sh
+
+set -ex
+
+${KAFKA_INSTALL_ROOT}/toxiproxy -port 8474 -host 0.0.0.0 &
+PID=$!
+
+sleep 2
+
+wget -S --post-data='{"name":"zk1", "upstream":"localhost:21801", "listen":"0.0.0.0:2181"}' localhost:8474/proxies
+wget -S --post-data='{"name":"zk2", "upstream":"localhost:21802", "listen":"0.0.0.0:2182"}' localhost:8474/proxies
+wget -S --post-data='{"name":"zk3", "upstream":"localhost:21803", "listen":"0.0.0.0:2183"}' localhost:8474/proxies
+wget -S --post-data='{"name":"zk4", "upstream":"localhost:21804", "listen":"0.0.0.0:2184"}' localhost:8474/proxies
+wget -S --post-data='{"name":"zk5", "upstream":"localhost:21805", "listen":"0.0.0.0:2185"}' localhost:8474/proxies
+
+wget -S --post-data='{"name":"kafka1", "upstream":"localhost:26667", "listen":"0.0.0.0:6667"}' localhost:8474/proxies
+wget -S --post-data='{"name":"kafka2", "upstream":"localhost:26668", "listen":"0.0.0.0:6668"}' localhost:8474/proxies
+wget -S --post-data='{"name":"kafka3", "upstream":"localhost:26669", "listen":"0.0.0.0:6669"}' localhost:8474/proxies
+wget -S --post-data='{"name":"kafka4", "upstream":"localhost:26670", "listen":"0.0.0.0:6670"}' localhost:8474/proxies
+wget -S --post-data='{"name":"kafka5", "upstream":"localhost:26671", "listen":"0.0.0.0:6671"}' localhost:8474/proxies
+
+wait $PID

+ 3 - 2
vagrant/server.properties

@@ -22,15 +22,16 @@ broker.id=KAFKAID
 ############################# Socket Server Settings #############################
 
 # The port the socket server listens on
-port=KAFKAID
+port=KAFKAPORT
 
 # Hostname the broker will bind to. If not set, the server will bind to all interfaces
-#host.name=localhost
+host.name=localhost
 
 # Hostname the broker will advertise to producers and consumers. If not set, it uses the
 # value for "host.name" if configured.  Otherwise, it will use the value returned from
 # java.net.InetAddress.getCanonicalHostName().
 advertised.host.name=KAFKA_HOSTNAME
+advertised.port=KAFKAID
 
 # The port to publish to ZooKeeper for clients to use. If this is not set,
 # it will publish the same port that the broker binds to.

+ 6 - 1
vagrant/setup_services.sh

@@ -1,7 +1,12 @@
-#/bin/sh
+#!/bin/sh
 
 set -ex
 
+stop toxiproxy || true
+cp ${REPOSITORY_ROOT}/vagrant/toxiproxy.conf /etc/init/toxiproxy.conf
+cp ${REPOSITORY_ROOT}/vagrant/run_toxiproxy.sh ${KAFKA_INSTALL_ROOT}/
+start toxiproxy
+
 for i in 1 2 3 4 5; do
     ZK_PORT=`expr $i + 2180`
     KAFKA_PORT=`expr $i + 6666`

+ 6 - 0
vagrant/toxiproxy.conf

@@ -0,0 +1,6 @@
+start on started networking
+stop on shutdown
+
+env KAFKA_INSTALL_ROOT=/opt
+
+exec /opt/run_toxiproxy.sh

+ 2 - 2
vagrant/zookeeper.conf

@@ -1,4 +1,4 @@
-start on filesystem or runlevel [2345]
-stop on runlevel [!2345]
+start on started toxiproxy
+stop on stopping toxiproxy
 
 exec /opt/kafka-KAFKAID/bin/zookeeper-server-start.sh /opt/kafka-KAFKAID/config/zookeeper.properties