Commit 34c943d0 by anun Committed by anun

script autocluster vernemq

parent 8fbdb9dc
......@@ -10,7 +10,7 @@ RUN apt-get update && apt-get install -y \
jq \
&& rm -rf /var/lib/apt/lists/*
ENV VERNEMQ_VERSION 1.3.1
ENV VERNEMQ_VERSION 1.5.0
# Defaults
ENV DOCKER_VERNEMQ_KUBERNETES_NAMESPACE default
......
......@@ -17,35 +17,36 @@ if env | grep -q "DOCKER_VERNEMQ_DISCOVERY_NODE"; then
echo "-eval \"vmq_server_cmd:node_join('VerneMQ@${DOCKER_VERNEMQ_DISCOVERY_NODE}')\"" >> /etc/vernemq/vm.args
fi
# If you encounter "SSL certification error (subject name does not match the host name)", you may try to set DOCKER_VERNEMQ_KUBERNETES_INSECURE to "1".
insecure=""
if env | grep -q "DOCKER_VERNEMQ_KUBERNETES_INSECURE"; then
insecure="--insecure"
fi
if env | grep -q "DOCKER_VERNEMQ_DISCOVERY_KUBERNETES"; then
# Let's set our nodename correctly
VERNEMQ_KUBERNETES_SUBDOMAIN=$(curl -X GET $insecure --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://kubernetes.default.svc.cluster.local/api/v1/namespaces/$DOCKER_VERNEMQ_KUBERNETES_NAMESPACE/pods?labelSelector=app=$DOCKER_VERNEMQ_KUBERNETES_APP_LABEL -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" | jq '.items[0].spec.subdomain' | sed 's/"//g' | tr '\n' '\0')
VERNEMQ_KUBERNETES_HOSTNAME=${MY_POD_NAME}.${VERNEMQ_KUBERNETES_SUBDOMAIN}.${DOCKER_VERNEMQ_KUBERNETES_NAMESPACE}.svc.cluster.local
sed -i.bak -r "s/VerneMQ@.+/VerneMQ@${VERNEMQ_KUBERNETES_HOSTNAME}/" /etc/vernemq/vm.args
# Hack into K8S DNS resolution (temporarily)
kube_pod_names=$(curl -X GET $insecure --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://kubernetes.default.svc.cluster.local/api/v1/namespaces/$DOCKER_VERNEMQ_KUBERNETES_NAMESPACE/pods?labelSelector=app=$DOCKER_VERNEMQ_KUBERNETES_APP_LABEL -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" | jq '.items[].spec.hostname' | sed 's/"//g' | tr '\n' ' ')
for kube_pod_name in $kube_pod_names;
# Cluster discovery implementation based on https://github.com/thesandlord/kubernetes-pod-ip-finder
if env | grep -q "KUBE_VERNEMQ_DISCOVERY_URL"; then
response=$(curl ${KUBE_VERNEMQ_DISCOVERY_URL})
IFS=','
nodes=($(echo "$response" | tr -d '[]"'))
length=$(echo ${#nodes[@]})
for i in "${nodes[@]}"
do
if [ $kube_pod_name == "null" ]
then
echo "Kubernetes discovery selected, but no pods found. Maybe we're the first?"
echo "Anyway, we won't attempt to join any cluster."
break
fi
if [ $kube_pod_name != $MY_POD_NAME ]
then
echo "Will join an existing Kubernetes cluster with discovery node at ${kube_pod_name}.${VERNEMQ_KUBERNETES_SUBDOMAIN}.${DOCKER_VERNEMQ_KUBERNETES_NAMESPACE}.svc.cluster.local"
echo "-eval \"vmq_server_cmd:node_join('VerneMQ@${kube_pod_name}.${VERNEMQ_KUBERNETES_SUBDOMAIN}.${DOCKER_VERNEMQ_KUBERNETES_NAMESPACE}.svc.cluster.local')\"" >> /etc/vernemq/vm.args
break
if [ "$i" != "null" ] && [ "$i" != "$IP_ADDRESS" ] && (($length > 1)); then
echo "Start Joining to VerneMQ@${i}."
echo "-eval \"vmq_server_cmd:node_join('VerneMQ@${i}')\"" >> /etc/vernemq/vm.args
fi
done
IFS=''
fi
if env | grep -q "DOCKER_VERNEMQ_ALLOW_ANONYMOUS=on"; then
echo "allow_anonymous = on" >> /etc/vernemq/vernemq.conf
fi
if env | grep -q "DOCKER_VERNEMQ_TRADE_CONSISTENCY=on"; then
echo "trade_consistency = on" >> /etc/vernemq/vernemq.conf
fi
if env | grep -q "DOCKER_VERNEMQ_ALLOW_MULTIPLE_SESSIONS=on"; then
echo "allow_multiple_sessions = on" >> /etc/vernemq/vernemq.conf
fi
if env | grep -q "DOCKER_VERNEMQ_MAX_CLIENT_ID_SIZE"; then
echo "max_client_id_size = ${DOCKER_VERNEMQ_MAX_CLIENT_ID_SIZE}" >> /etc/vernemq/vernemq.conf
fi
if [ -f /etc/vernemq/vernemq.conf.local ]; then
......@@ -121,3 +122,4 @@ while true
do
tail -f /var/log/vernemq/console.log & wait ${!}
done
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment