From 66599dacf378dd2cb9b707fb4333cd5be5684d8e Mon Sep 17 00:00:00 2001 From: Ludovic Poujol Date: Wed, 29 Aug 2018 19:24:21 +0200 Subject: [PATCH] Importing cron, rsyslog & redis-cluster(wip) Dockerfiles --- cron/Dockerfile | 7 ++ redis-cluster/Dockerfile | 19 +++++ redis-cluster/docker-entrypoint.sh | 107 +++++++++++++++++++++++++++++ redis-cluster/sentinel.conf | 8 +++ rsyslog/Dockerfile | 17 +++++ 5 files changed, 158 insertions(+) create mode 100644 cron/Dockerfile create mode 100644 redis-cluster/Dockerfile create mode 100644 redis-cluster/docker-entrypoint.sh create mode 100644 redis-cluster/sentinel.conf create mode 100644 rsyslog/Dockerfile diff --git a/cron/Dockerfile b/cron/Dockerfile new file mode 100644 index 0000000..06058b5 --- /dev/null +++ b/cron/Dockerfile @@ -0,0 +1,7 @@ +FROM debian:stretch + +RUN apt update \ + && apt install -y --no-install-recommends cron procps \ + && rm -rf /var/lib/apt/lists/* + +CMD ["cron", "-f"] diff --git a/redis-cluster/Dockerfile b/redis-cluster/Dockerfile new file mode 100644 index 0000000..2ebefe5 --- /dev/null +++ b/redis-cluster/Dockerfile @@ -0,0 +1,19 @@ +FROM redis:4.0 + +COPY docker-entrypoint.sh /usr/local/bin/ +COPY sentinel.conf /usr/local/etc/redis/sentinel.conf + + +ENV SENTINEL_QUORUM=2 \ + SENTINEL_DOWN_AFTER=1000 \ + SENTINEL_FAILOVER=1000 \ + REDIS_MASTER_NAME=redismaster \ + REDIS_MASTER=redis \ + REDIS_SENTINEL_IP=redissentinel \ + REDIS_SENTINEL_PORT=26379 + + +ENTRYPOINT ["docker-entrypoint.sh"] + +EXPOSE 6379 +CMD ["redis-server"] diff --git a/redis-cluster/docker-entrypoint.sh b/redis-cluster/docker-entrypoint.sh new file mode 100644 index 0000000..3993e4a --- /dev/null +++ b/redis-cluster/docker-entrypoint.sh @@ -0,0 +1,107 @@ +#!/bin/sh +set -e + +count_tasks() { + getent hosts tasks."$1" |wc -l +} + +# Dirty hack to ensure that "getent hosts tasks.XXXX" will list us +sleep 3 + + +## STARTING REDIS ## +if [ "$1" = "redis-server" ]; then + echo "[DEBUG] PREPARING to start redis-server..." + + # Check how many redis replicas are currently running... + if [ "$(count_tasks redis)" -gt 1 ]; then + # Other instances are running, we shall ask to one sentine who is master + echo "[DEBUG] Already one redis (or more) instance running, we'll ask the master IP to a sentinel and set ourself as slave..." + + # TODO: sanity check : Manage the case where there's no sentinel ? + + # Wait for a sentinel to be alive + until [ "$(redis-cli -h "$REDIS_SENTINEL_IP" -p "$REDIS_SENTINEL_PORT" ping)" = "PONG" ]; do + echo "$REDIS_SENTINEL_IP is unavailable - sleeping" + sleep 1 + done + + # Get current master IP + master_info=$(redis-cli -h "$REDIS_SENTINEL_IP" -p "$REDIS_SENTINEL_PORT" sentinel get-master-addr-by-name "$REDIS_MASTER_NAME") + + # Ensure we have something from sentinel + until [ "$master_info" ]; do + echo "$REDIS_MASTER_NAME not found - sleeping" + sleep 1 + master_info=$(redis-cli -h "$REDIS_SENTINEL_IP" -p "$REDIS_SENTINEL_PORT" sentinel get-master-addr-by-name "$REDIS_MASTER_NAME") + done + + master_ip=$(echo $master_info | awk '{print $1}') + master_port=$(echo $master_info | awk '{print $2}') + + echo "[DEBUG] STARTING redis as a SLAVE of $master_ip $master_port" + redis-server --slaveof $master_ip $master_port + + else + # Only one redis - So we may be master? - TODO: Add sanity checks ? + # Like : Is there any sentinels ? What are they doing ?... + echo "[DEBUG] No other redis are running.... Starting redis as MASTER !!" + exec "$@" + + fi + + +## STARTING SENTINEL ## +elif [ "$1" = "redis-sentinel" ]; then + echo "[DEBUG] PREPARING sentinel" + + # Update config with ENV + sed -i "s/{{ SENTINEL_QUORUM }}/$SENTINEL_QUORUM/g" /usr/local/etc/redis/sentinel.conf + sed -i "s/{{ SENTINEL_DOWN_AFTER }}/$SENTINEL_DOWN_AFTER/g" /usr/local/etc/redis/sentinel.conf + sed -i "s/{{ SENTINEL_FAILOVER }}/$SENTINEL_FAILOVER/g" /usr/local/etc/redis/sentinel.conf + sed -i "s/{{ REDIS_MASTER_NAME }}/$REDIS_MASTER_NAME/g" /usr/local/etc/redis/sentinel.conf + + # Check how many sentinels replicas are currently running... + if [ "$(count_tasks redissentinel)" -gt 1 ]; then + echo "[DEBUG] Already one (or more) sentinel instance running, we'll ask the master IP to a sentinel and set it as the master" + + # Fetch sentiten + sentinel_ips=$(getent hosts tasks.redissentinel | awk '{print $1}'); + + for ip in $sentinel_ips; do + REDIS_IP=$(redis-cli -h "$ip" -p 26379 sentinel get-master-addr-by-name "$REDIS_MASTER_NAME" | head -1) + echo "[DEBUG] According to sentinel at $ip - Master_info is : $REDIS_IP" + + if [ "$REDIS_IP" ]; then + break + fi + done + + + sed -i "s/{{ REDIS_IP }}/$REDIS_IP/g" /usr/local/etc/redis/sentinel.conf + + echo "[DEBUG] Starting sentinel with redismaster as $REDIS_IP" + redis-server /usr/local/etc/redis/sentinel.conf --sentinel + + else + # No other sentinels around. + echo "[DEBUG] No other sentinel running...." + + # Is there redis instance running ? + if [ "$(count_tasks redis)" -gt 1 ]; then + # More than 1 redis running... Not so fun case to manage.... + echo "[DEBUG] More than 1 redis running - Giving up...." + + elif [ "$(count_tasks redis)" -eq 1 ]; then + + echo "[DEBUG] Only one redis is runnig. Let's say its a master!" + REDIS_IP=$(getent hosts tasks.redis | awk '{print $1}'); + + sed -i "s/{{ REDIS_IP }}/$REDIS_IP/g" /usr/local/etc/redis/sentinel.conf + redis-server /usr/local/etc/redis/sentinel.conf --sentinel + + else + echo "[DEBUG] No redis running... - Giving up...." + fi; + fi; +fi; diff --git a/redis-cluster/sentinel.conf b/redis-cluster/sentinel.conf new file mode 100644 index 0000000..db4249f --- /dev/null +++ b/redis-cluster/sentinel.conf @@ -0,0 +1,8 @@ +port 26379 + +dir /tmp + +sentinel monitor {{ REDIS_MASTER_NAME }} {{ REDIS_IP }} 6379 {{ SENTINEL_QUORUM }} +sentinel down-after-milliseconds {{ REDIS_MASTER_NAME }} {{ SENTINEL_DOWN_AFTER }} +sentinel parallel-syncs {{ REDIS_MASTER_NAME }} 1 +sentinel failover-timeout {{ REDIS_MASTER_NAME }} {{ SENTINEL_FAILOVER }} diff --git a/rsyslog/Dockerfile b/rsyslog/Dockerfile new file mode 100644 index 0000000..1cf9c21 --- /dev/null +++ b/rsyslog/Dockerfile @@ -0,0 +1,17 @@ +FROM debian:stretch + +# procps is for debugging purpose only +RUN apt-get update \ + && apt-get install -y --no-install-recommends rsyslog procps \ + && rm -rf /var/lib/apt/lists/* + +# Enable UDP syslog reception +RUN sed -i 's/^#\(module(load="imudp")\)/\1/; s/^#\(input(type="imudp" port="514")\)/\1/' /etc/rsyslog.conf + +# Disable kernel logging support +RUN sed -i 's/^module(load="imklog")/#&/' /etc/rsyslog.conf + +VOLUME /var/log/ +EXPOSE 514/udp + +CMD /usr/sbin/rsyslogd -n