From 9d3252cd55b5e33eabcd8a61116ef3a4f6c5045e Mon Sep 17 00:00:00 2001 From: ofreax Date: Tue, 9 Jun 2015 20:41:43 +0200 Subject: [PATCH 2/5] init --- CHANGELOG.md | 4 + Makefile | 20 ++++ image/Dockerfile | 27 +++++ image/env.yml | 14 +++ image/service/keepalived/assets/README.md | 1 + .../service/keepalived/assets/keepalived.conf | 19 ++++ image/service/keepalived/container-start.sh | 22 ++++ image/service/keepalived/daemon.sh | 2 + test/test.bats | 9 ++ test/test_helper.bash | 101 ++++++++++++++++++ 10 files changed, 219 insertions(+) create mode 100644 CHANGELOG.md create mode 100644 Makefile create mode 100644 image/Dockerfile create mode 100644 image/env.yml create mode 100644 image/service/keepalived/assets/README.md create mode 100644 image/service/keepalived/assets/keepalived.conf create mode 100755 image/service/keepalived/container-start.sh create mode 100755 image/service/keepalived/daemon.sh create mode 100644 test/test.bats create mode 100644 test/test_helper.bash diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..ba10274 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,4 @@ +# Changelog + +## 0.1.0 + - Initial release diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e6b8112 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +NAME = osixia/keepalived +VERSION = 0.1.0 + +.PHONY: all build test tag_latest release + +all: build + +build: + docker build -t $(NAME):$(VERSION) --rm image + +test: + env NAME=$(NAME) VERSION=$(VERSION) bats test/test.bats + +tag_latest: + docker tag -f $(NAME):$(VERSION) $(NAME):latest + +release: build test tag_latest + @if ! docker images $(NAME) | awk '{ print $$2 }' | grep -q -F $(VERSION); then echo "$(NAME) version $(VERSION) is not yet built. Please run 'make build'"; false; fi + docker push $(NAME) + @echo "*** Don't forget to run 'twgit release/hotfix finish' :)" diff --git a/image/Dockerfile b/image/Dockerfile new file mode 100644 index 0000000..07f9a1e --- /dev/null +++ b/image/Dockerfile @@ -0,0 +1,27 @@ +FROM osixia/baseimage:0.10.4 +MAINTAINER Bertrand Gouny + +# This container need run options : +# --privileged=true +# --net=host + +# Use baseimage-docker's init system. +CMD ["/sbin/my_init"] + +# Install Keepalived +RUN apt-get -y update \ + && LC_ALL=C DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + keepalived + +# Add Keepalived assets +ADD service/keepalived/assets /osixia/keepalived + +# Run clean all +RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* + +# Add default env variables +ADD env.yml /etc/env.yml + +# Add Keepalived container start config & daemon +ADD service/keepalived/container-start.sh /etc/my_init.d/keepalived +ADD service/keepalived/daemon.sh /etc/service/keepalived/run diff --git a/image/env.yml b/image/env.yml new file mode 100644 index 0000000..c0cafad --- /dev/null +++ b/image/env.yml @@ -0,0 +1,14 @@ +KEEPALIVED_PASSWORD: d0cker + +# for electing MASTER, highest priority wins. +# to be MASTER, make 50 more than other machines +KEEPALIVED_PRIORITY: 150 + +KEEPALIVED_UNICAST_PEER: + - 172.17.8.101 + - 172.17.8.102 + - 172.17.8.103 + +KEEPALIVED_VIRTUAL_IPS: + - 192.168.200.17/24 dev eth1 + - 192.168.200.18/24 dev eth2 label eth2:1 diff --git a/image/service/keepalived/assets/README.md b/image/service/keepalived/assets/README.md new file mode 100644 index 0000000..a4695d4 --- /dev/null +++ b/image/service/keepalived/assets/README.md @@ -0,0 +1 @@ +Add your custom keepalived.conf file here or mount one at docker run to /etc/keepalived/keepalived.conf diff --git a/image/service/keepalived/assets/keepalived.conf b/image/service/keepalived/assets/keepalived.conf new file mode 100644 index 0000000..65d53dd --- /dev/null +++ b/image/service/keepalived/assets/keepalived.conf @@ -0,0 +1,19 @@ +vrrp_instance VI_1 { + interface eno1 + state MASTER + virtual_router_id 51 + priority {{ keepalived_priority }} + + unicast_peer { + {{ peer_ip }} + } + + virtual_ipaddress { + {{ floating_ip }} + } + + authentication { + auth_type PASS + auth_pass {{ keepalived_password }} + } +} diff --git a/image/service/keepalived/container-start.sh b/image/service/keepalived/container-start.sh new file mode 100755 index 0000000..8a1c1c5 --- /dev/null +++ b/image/service/keepalived/container-start.sh @@ -0,0 +1,22 @@ +#!/bin/bash -e + +FIRST_START_DONE="/etc/docker-keepalived-first-start-done" + +# container first start +if [ ! -e "$FIRST_START_DONE" ]; then + + # config folder is empty use bootstrap config if available + if [ ! -e /etc/keepalived/keepalived.conf ]; then + echo "No keepalived.conf provided using image default one" + if [ ! -e /osixia/keepalived/keepalived.conf ]; then + echo "Error: No default keepalived.conf found in /osixia/keepalived/keepalived.conf" + exit 1 + else + ln -s /osixia/keepalived/keepalived.conf /etc/keepalived/keepalived.conf + fi + fi + + touch $FIRST_START_DONE +fi + +exit 0 diff --git a/image/service/keepalived/daemon.sh b/image/service/keepalived/daemon.sh new file mode 100755 index 0000000..f4166ff --- /dev/null +++ b/image/service/keepalived/daemon.sh @@ -0,0 +1,2 @@ +#!/bin/bash -e +exec /usr/sbin/keepalived -f /etc/keepalived/keepalived.conf --dont-fork --log-console diff --git a/test/test.bats b/test/test.bats new file mode 100644 index 0000000..fb7fc13 --- /dev/null +++ b/test/test.bats @@ -0,0 +1,9 @@ +#!/usr/bin/env bats +load test_helper + +@test "image build" { + + run build_image + [ "$status" -eq 0 ] + +} diff --git a/test/test_helper.bash b/test/test_helper.bash new file mode 100644 index 0000000..31bdd43 --- /dev/null +++ b/test/test_helper.bash @@ -0,0 +1,101 @@ +setup() { + IMAGE_NAME="$NAME:$VERSION" +} + +# function relative to the current container / image +build_image() { + #disable outputs + docker build -t $IMAGE_NAME $BATS_TEST_DIRNAME/../image &> /dev/null +} + +run_image() { + CONTAINER_ID=$(docker run $@ -d $IMAGE_NAME) + CONTAINER_IP=$(get_container_ip_by_cid $CONTAINER_ID) +} + +start_container() { + start_containers_by_cid $CONTAINER_ID +} + +stop_container() { + stop_containers_by_cid $CONTAINER_ID +} + +remove_container() { + remove_containers_by_cid $CONTAINER_ID +} + +clear_container() { + stop_containers_by_cid $CONTAINER_ID + remove_containers_by_cid $CONTAINER_ID +} + +is_service_running() { + is_service_running_by_cid $CONTAINER_ID $1 +} + +wait_service() { + wait_service_by_cid $CONTAINER_ID $@ +} + + +# generic functions +get_container_ip_by_cid() { + local IP=$(docker inspect -f "{{ .NetworkSettings.IPAddress }}" $1) + echo "$IP" +} + +start_containers_by_cid() { + for cid in "$@" + do + #disable outputs + docker start $cid &> /dev/null + done +} + +stop_containers_by_cid() { + for cid in "$@" + do + #disable outputs + docker stop $cid &> /dev/null + done +} + +remove_containers_by_cid() { + for cid in "$@" + do + #disable outputs + docker rm $cid &> /dev/null + done +} + +clear_containers_by_cid() { + stop_containers_by_cid $@ + remove_containers_by_cid $@ +} + +is_service_running_by_cid() { + docker exec $1 ps cax | grep $2 > /dev/null +} + +wait_service_by_cid() { + + cid=$1 + + # first wait image init end + while ! is_service_running_by_cid $cid syslog-ng + do + sleep 1 + done + + for service in "${@:2}" + do + # wait service + while ! is_service_running_by_cid $cid $service + do + sleep 1 + done + done + + sleep 5 +} \ No newline at end of file From ac8b9469677cb092c18fba4b1a301ad2f4b9f918 Mon Sep 17 00:00:00 2001 From: Bertrand Gouny Date: Mon, 15 Jun 2015 16:04:40 +0200 Subject: [PATCH 3/5] init --- image/Dockerfile | 1 + image/env.yml | 3 +- .../service/keepalived/assets/keepalived.conf | 2 +- image/service/keepalived/container-start.sh | 39 ++++++++++++++++++- pop | 0 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 pop diff --git a/image/Dockerfile b/image/Dockerfile index 07f9a1e..eddab4d 100644 --- a/image/Dockerfile +++ b/image/Dockerfile @@ -4,6 +4,7 @@ MAINTAINER Bertrand Gouny # This container need run options : # --privileged=true # --net=host +# -v /lib/modules:/lib/modules # Use baseimage-docker's init system. CMD ["/sbin/my_init"] diff --git a/image/env.yml b/image/env.yml index c0cafad..f0a51cd 100644 --- a/image/env.yml +++ b/image/env.yml @@ -1,10 +1,11 @@ +KEEPALIVED_INTERFACE: eno1 KEEPALIVED_PASSWORD: d0cker # for electing MASTER, highest priority wins. # to be MASTER, make 50 more than other machines KEEPALIVED_PRIORITY: 150 -KEEPALIVED_UNICAST_PEER: +KEEPALIVED_UNICAST_PEERS: - 172.17.8.101 - 172.17.8.102 - 172.17.8.103 diff --git a/image/service/keepalived/assets/keepalived.conf b/image/service/keepalived/assets/keepalived.conf index 65d53dd..3f76140 100644 --- a/image/service/keepalived/assets/keepalived.conf +++ b/image/service/keepalived/assets/keepalived.conf @@ -1,5 +1,5 @@ vrrp_instance VI_1 { - interface eno1 + interface {{ keepalived_interface }} state MASTER virtual_router_id 51 priority {{ keepalived_priority }} diff --git a/image/service/keepalived/container-start.sh b/image/service/keepalived/container-start.sh index 8a1c1c5..ccc6ec5 100755 --- a/image/service/keepalived/container-start.sh +++ b/image/service/keepalived/container-start.sh @@ -1,4 +1,4 @@ -#!/bin/bash -e +#!/bin/bash -ex FIRST_START_DONE="/etc/docker-keepalived-first-start-done" @@ -13,6 +13,43 @@ if [ ! -e "$FIRST_START_DONE" ]; then exit 1 else ln -s /osixia/keepalived/keepalived.conf /etc/keepalived/keepalived.conf + + # + # bootstrap config + # + sed -i "s|{{ keepalived_interface }}|$KEEPALIVED_INTERFACE|g" /etc/keepalived/keepalived.conf + sed -i "s|{{ keepalived_priority }}|$KEEPALIVED_PRIORITY|g" /etc/keepalived/keepalived.conf + sed -i "s|{{ keepalived_password }}|$KEEPALIVED_PASSWORD|g" /etc/keepalived/keepalived.conf + + # unicast peers + KEEPALIVED_UNICAST_PEERS=($KEEPALIVED_UNICAST_PEERS) + for peer in "${KEEPALIVED_UNICAST_PEERS[@]}" + do + # it's just a peer + # stored in a variable + if [ -n "${!peer}" ]; then + sed -i "s|{{ peer_ip }}|${!peer}\n {{ peer_ip }}|g" /etc/keepalived/keepalived.conf + # directly + else + sed -i "s|{{ peer_ip }}|${peer}\n {{ peer_ip }}|g" /etc/keepalived/keepalived.conf + fi + done + sed -i "/{{ peer_ip }}/d" /etc/keepalived/keepalived.conf + + # virtual ips + KEEPALIVED_VIRTUAL_IPS=($KEEPALIVED_VIRTUAL_IPS) + for vip in "${KEEPALIVED_VIRTUAL_IPS[@]}" + do + # it's just a peer + # stored in a variable + if [ -n "${!vip}" ]; then + sed -i "s|{{ floating_ip }}|${!vip}\n {{ floating_ip }}|g" /etc/keepalived/keepalived.conf + # directly + else + sed -i "s|{{ floating_ip }}|${vip}\n {{ floating_ip }}|g" /etc/keepalived/keepalived.conf + fi + done + sed -i "/{{ floating_ip }}/d" /etc/keepalived/keepalived.conf fi fi diff --git a/pop b/pop new file mode 100644 index 0000000..e69de29 From 2df1fb5c42994bf326354a77d85a952a8d463af9 Mon Sep 17 00:00:00 2001 From: ofreax Date: Mon, 15 Jun 2015 21:33:06 +0200 Subject: [PATCH 4/5] init --- image/Dockerfile | 2 ++ image/env.yml | 9 +++------ image/service/keepalived/assets/keepalived.conf | 17 ++++++++++++----- image/service/keepalived/container-start.sh | 12 ++++++------ image/service/keepalived/daemon.sh | 2 +- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/image/Dockerfile b/image/Dockerfile index eddab4d..ce13e35 100644 --- a/image/Dockerfile +++ b/image/Dockerfile @@ -6,6 +6,8 @@ MAINTAINER Bertrand Gouny # --net=host # -v /lib/modules:/lib/modules +# sudo docker run --privileged=true --net=host -v /lib/modules:/lib/modules -d osixia/keepalived:0.1.0 + # Use baseimage-docker's init system. CMD ["/sbin/my_init"] diff --git a/image/env.yml b/image/env.yml index f0a51cd..18420fd 100644 --- a/image/env.yml +++ b/image/env.yml @@ -1,4 +1,4 @@ -KEEPALIVED_INTERFACE: eno1 +KEEPALIVED_INTERFACE: wlan0 KEEPALIVED_PASSWORD: d0cker # for electing MASTER, highest priority wins. @@ -6,10 +6,7 @@ KEEPALIVED_PASSWORD: d0cker KEEPALIVED_PRIORITY: 150 KEEPALIVED_UNICAST_PEERS: - - 172.17.8.101 - - 172.17.8.102 - - 172.17.8.103 + - 192.168.1.9 KEEPALIVED_VIRTUAL_IPS: - - 192.168.200.17/24 dev eth1 - - 192.168.200.18/24 dev eth2 label eth2:1 + - 192.168.1.230 diff --git a/image/service/keepalived/assets/keepalived.conf b/image/service/keepalived/assets/keepalived.conf index 3f76140..552493d 100644 --- a/image/service/keepalived/assets/keepalived.conf +++ b/image/service/keepalived/assets/keepalived.conf @@ -1,19 +1,26 @@ -vrrp_instance VI_1 { +vrrp_instance vip-1 { interface {{ keepalived_interface }} + + track_interface { + {{ keepalived_interface }} + } + state MASTER virtual_router_id 51 priority {{ keepalived_priority }} + nopreempt - unicast_peer { - {{ peer_ip }} - } + unicast_peer {{ keepalived_unicast_peers }} virtual_ipaddress { - {{ floating_ip }} + {{ keepalived_virtual_ips }} } authentication { auth_type PASS auth_pass {{ keepalived_password }} } + + debug + } diff --git a/image/service/keepalived/container-start.sh b/image/service/keepalived/container-start.sh index ccc6ec5..9424cce 100755 --- a/image/service/keepalived/container-start.sh +++ b/image/service/keepalived/container-start.sh @@ -28,13 +28,13 @@ if [ ! -e "$FIRST_START_DONE" ]; then # it's just a peer # stored in a variable if [ -n "${!peer}" ]; then - sed -i "s|{{ peer_ip }}|${!peer}\n {{ peer_ip }}|g" /etc/keepalived/keepalived.conf + sed -i "s|{{ keepalived_unicast_peers }}|${!peer}\n {{ keepalived_unicast_peers }}|g" /etc/keepalived/keepalived.conf # directly else - sed -i "s|{{ peer_ip }}|${peer}\n {{ peer_ip }}|g" /etc/keepalived/keepalived.conf + sed -i "s|{{ keepalived_unicast_peers }}|${peer}\n {{ keepalived_unicast_peers }}|g" /etc/keepalived/keepalived.conf fi done - sed -i "/{{ peer_ip }}/d" /etc/keepalived/keepalived.conf + sed -i "/{{ keepalived_unicast_peers }}/d" /etc/keepalived/keepalived.conf # virtual ips KEEPALIVED_VIRTUAL_IPS=($KEEPALIVED_VIRTUAL_IPS) @@ -43,13 +43,13 @@ if [ ! -e "$FIRST_START_DONE" ]; then # it's just a peer # stored in a variable if [ -n "${!vip}" ]; then - sed -i "s|{{ floating_ip }}|${!vip}\n {{ floating_ip }}|g" /etc/keepalived/keepalived.conf + sed -i "s|{{ keepalived_virtual_ips }}|${!vip}\n {{ keepalived_virtual_ips }}|g" /etc/keepalived/keepalived.conf # directly else - sed -i "s|{{ floating_ip }}|${vip}\n {{ floating_ip }}|g" /etc/keepalived/keepalived.conf + sed -i "s|{{ keepalived_virtual_ips }}|${vip}\n {{ keepalived_virtual_ips }}|g" /etc/keepalived/keepalived.conf fi done - sed -i "/{{ floating_ip }}/d" /etc/keepalived/keepalived.conf + sed -i "/{{ keepalived_virtual_ips }}/d" /etc/keepalived/keepalived.conf fi fi diff --git a/image/service/keepalived/daemon.sh b/image/service/keepalived/daemon.sh index f4166ff..99921d8 100755 --- a/image/service/keepalived/daemon.sh +++ b/image/service/keepalived/daemon.sh @@ -1,2 +1,2 @@ #!/bin/bash -e -exec /usr/sbin/keepalived -f /etc/keepalived/keepalived.conf --dont-fork --log-console +exec /usr/sbin/keepalived -f /etc/keepalived/keepalived.conf --dont-fork --log-console -D -d From 0d5963eb5ec98031eaecb1161ec18238447c9b81 Mon Sep 17 00:00:00 2001 From: Bertrand Gouny Date: Tue, 16 Jun 2015 11:13:22 +0200 Subject: [PATCH 5/5] init --- README.md | 12 +++++++-- image/Dockerfile | 25 +++++++++++-------- image/env.yml | 6 ++--- .../service/keepalived/assets/keepalived.conf | 7 +++--- image/service/keepalived/container-start.sh | 4 ++- image/service/keepalived/daemon.sh | 2 +- pop | 0 7 files changed, 35 insertions(+), 21 deletions(-) delete mode 100644 pop diff --git a/README.md b/README.md index 5f1c132..08206ae 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ -# docker-keepalived -A docker image to run Keepalived +# osixia/keepalived + +A docker image to run Keepalived. +> [keepalived.org](http://keepalived.org/) + +## Quick start + +This image need to be run with : --privileged --net=host + + docker run --privileged --net=host -d osixia/keepalived diff --git a/image/Dockerfile b/image/Dockerfile index ce13e35..8d6015f 100644 --- a/image/Dockerfile +++ b/image/Dockerfile @@ -1,26 +1,31 @@ FROM osixia/baseimage:0.10.4 MAINTAINER Bertrand Gouny -# This container need run options : -# --privileged=true -# --net=host -# -v /lib/modules:/lib/modules - -# sudo docker run --privileged=true --net=host -v /lib/modules:/lib/modules -d osixia/keepalived:0.1.0 +# Keepalived version +ENV KEEPALIVED_VERSION 1.2.17 # Use baseimage-docker's init system. CMD ["/sbin/my_init"] # Install Keepalived RUN apt-get -y update \ - && LC_ALL=C DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ - keepalived + && LC_ALL=C DEBIAN_FRONTEND=noninteractive apt-get install -y \ + make gcc libssl-dev \ + && curl -o keepalived.tar.gz -SL http://keepalived.org/software/keepalived-${KEEPALIVED_VERSION}.tar.gz \ + && mkdir -p /osixia/keepalived-sources \ + && tar -xzf keepalived.tar.gz --strip 1 -C /osixia/keepalived-sources \ + && cd osixia/keepalived-sources \ + && ./configure --with-kernel-dir=/lib/modules/$(uname -r)/build \ + && make && make install \ + && cd - && mkdir -p /etc/keepalived \ + && apt-get remove -y --purge --auto-remove make gcc libssl-dev # Add Keepalived assets ADD service/keepalived/assets /osixia/keepalived -# Run clean all -RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* +# Clean all +RUN rm keepalived.tar.gz \ + && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* # Add default env variables ADD env.yml /etc/env.yml diff --git a/image/env.yml b/image/env.yml index 18420fd..3121883 100644 --- a/image/env.yml +++ b/image/env.yml @@ -1,4 +1,4 @@ -KEEPALIVED_INTERFACE: wlan0 +KEEPALIVED_INTERFACE: p4p1 KEEPALIVED_PASSWORD: d0cker # for electing MASTER, highest priority wins. @@ -6,7 +6,7 @@ KEEPALIVED_PASSWORD: d0cker KEEPALIVED_PRIORITY: 150 KEEPALIVED_UNICAST_PEERS: - - 192.168.1.9 + - 192.168.1.10 KEEPALIVED_VIRTUAL_IPS: - - 192.168.1.230 + - 192.168.1.231 diff --git a/image/service/keepalived/assets/keepalived.conf b/image/service/keepalived/assets/keepalived.conf index 552493d..e92f9d7 100644 --- a/image/service/keepalived/assets/keepalived.conf +++ b/image/service/keepalived/assets/keepalived.conf @@ -10,7 +10,9 @@ vrrp_instance vip-1 { priority {{ keepalived_priority }} nopreempt - unicast_peer {{ keepalived_unicast_peers }} + unicast_peer { + {{ keepalived_unicast_peers }} + } virtual_ipaddress { {{ keepalived_virtual_ips }} @@ -20,7 +22,4 @@ vrrp_instance vip-1 { auth_type PASS auth_pass {{ keepalived_password }} } - - debug - } diff --git a/image/service/keepalived/container-start.sh b/image/service/keepalived/container-start.sh index 9424cce..9b58ce9 100755 --- a/image/service/keepalived/container-start.sh +++ b/image/service/keepalived/container-start.sh @@ -1,4 +1,4 @@ -#!/bin/bash -ex +#!/bin/bash -e FIRST_START_DONE="/etc/docker-keepalived-first-start-done" @@ -12,6 +12,7 @@ if [ ! -e "$FIRST_START_DONE" ]; then echo "Error: No default keepalived.conf found in /osixia/keepalived/keepalived.conf" exit 1 else + ln -s /osixia/keepalived/keepalived.conf /etc/keepalived/keepalived.conf # @@ -51,6 +52,7 @@ if [ ! -e "$FIRST_START_DONE" ]; then done sed -i "/{{ keepalived_virtual_ips }}/d" /etc/keepalived/keepalived.conf fi + fi touch $FIRST_START_DONE diff --git a/image/service/keepalived/daemon.sh b/image/service/keepalived/daemon.sh index 99921d8..00750a1 100755 --- a/image/service/keepalived/daemon.sh +++ b/image/service/keepalived/daemon.sh @@ -1,2 +1,2 @@ #!/bin/bash -e -exec /usr/sbin/keepalived -f /etc/keepalived/keepalived.conf --dont-fork --log-console -D -d +exec /usr/local/sbin/keepalived -f /etc/keepalived/keepalived.conf --dont-fork --log-console -D -d diff --git a/pop b/pop deleted file mode 100644 index e69de29..0000000