Compare commits

...

36 Commits

Author SHA1 Message Date
ofreax 8eb1d69938 Merge branch 'hotfix-0.1.6' into stable 2015-08-21 11:24:39 +02:00
ofreax ed7277f100 improve doc and better custom config 2015-08-21 11:22:46 +02:00
ofreax 9b82c576bd improve doc and better custom config 2015-08-21 11:21:46 +02:00
ofreax e8b5a271c3 [twgit] Init hotfix 'hotfix-0.1.6'. 2015-08-21 10:56:52 +02:00
ofreax fe6ead3889 Merge branch 'hotfix-0.1.5' into stable 2015-08-19 19:39:37 +02:00
ofreax d67d8be580 version 2015-08-19 19:18:23 +02:00
ofreax 39da36944c - 2015-08-03 19:54:27 +02:00
Bertrand Gouny 6b09984838 upgrade to light-baseimage:0.1.1 2015-07-29 10:45:41 +02:00
Bertrand Gouny 1417cf5326 [twgit] Init hotfix 'hotfix-0.1.5'. 2015-07-29 10:27:08 +02:00
Bertrand Gouny 4cbbbc1778 Merge branch 'hotfix-0.1.4' into stable 2015-07-23 14:40:06 +02:00
Bertrand Gouny 689f1e5c81 imagelayers.io 2015-07-23 14:39:49 +02:00
Bertrand Gouny befaefc9e4 new baseimage 2015-07-23 12:11:36 +02:00
ofreax e107ae776f yml -> yaml 2015-07-17 11:46:49 +02:00
ofreax 5084fe2256 new test helper 2015-07-17 09:23:24 +02:00
ofreax 345188086e new baseimage 2015-07-16 21:43:36 +02:00
ofreax 9d69f3eb7a [twgit] Init hotfix 'hotfix-0.1.4'. 2015-07-16 21:08:45 +02:00
ofreax 43fec88bbb Merge branch 'hotfix-0.1.3' into stable 2015-07-09 23:41:47 +02:00
ofreax b4ad65b1f1 fix libnl 2015-07-09 23:15:19 +02:00
ofreax 0e00000114 [twgit] Init hotfix 'hotfix-0.1.3'. 2015-07-09 23:01:18 +02:00
ofreax f72f907397 Merge branch 'hotfix-0.1.2' into stable 2015-07-09 21:50:51 +02:00
ofreax 6a4a623d54 keepalived 1.2.19 2015-07-09 21:41:14 +02:00
ofreax 712f5daff0 [twgit] Init hotfix 'hotfix-0.1.2'. 2015-07-09 21:05:04 +02:00
ofreax cb4a343ab2 Merge branch 'hotfix-0.1.1' into stable 2015-06-21 22:07:16 +02:00
ofreax d3c3df6c66 add notify script 2015-06-21 21:32:28 +02:00
Bertrand Gouny 00a8279068 add notify script 2015-06-16 14:09:20 +02:00
Bertrand Gouny 84c261f102 add notify script 2015-06-16 14:06:49 +02:00
Bertrand Gouny a7a68de0df add notify script 2015-06-16 13:07:17 +02:00
Bertrand Gouny 64183ccd88 [twgit] Init hotfix 'hotfix-0.1.1'. 2015-06-16 12:46:24 +02:00
Bertrand Gouny 8a92be4d27 Merge branch 'release-0.1.0' into stable 2015-06-16 11:16:17 +02:00
Bertrand Gouny 42835eaaeb Merge branch 'feature-init' into release-0.1.0 2015-06-16 11:13:54 +02:00
Bertrand Gouny 3683d2ad85 [twgit] Init release 'release-0.1.0'. 2015-06-16 11:13:33 +02:00
Bertrand Gouny 0d5963eb5e init 2015-06-16 11:13:22 +02:00
ofreax 2df1fb5c42 init 2015-06-15 21:33:06 +02:00
Bertrand Gouny ac8b946967 init 2015-06-15 16:04:40 +02:00
ofreax 9d3252cd55 init 2015-06-09 20:41:43 +02:00
ofreax 44921f0e18 [twgit] Init feature 'feature-init'. 2015-06-09 12:01:30 +02:00
12 changed files with 432 additions and 2 deletions
+25
View File
@@ -0,0 +1,25 @@
# Changelog
## 0.1.6
- Better way to add custom config
- Improve documentation
## 0.1.5
- Upgrade baseimage: osixia/light-baseimage:0.1.1
## 0.1.4
- Use light-baseimage
## 0.1.3
- Fix libnl dependency
## 0.1.2
- Keepalived version 1.2.19
- Update default config :
- Set start state to BACKUP
## 0.1.1
- Add notify script
## 0.1.0
- Initial release
+20
View File
@@ -0,0 +1,20 @@
NAME = osixia/keepalived
VERSION = 0.1.6
.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' :)"
+86 -2
View File
@@ -1,2 +1,86 @@
# docker-keepalived
A docker image to run Keepalived
# osixia/keepalived
[![](https://badge.imagelayers.io/osixia/keepalived:latest.svg)](https://imagelayers.io/?images=osixia/keepalived:latest 'Get your own badge on imagelayers.io')
A docker image to run Keepalived.
> [keepalived.org](http://keepalived.org/)
## Quick start
This image require the kernel module ip_vs loaded on the host and need to be run with : --privileged --net=host
docker run --privileged --net=host -d osixia/keepalived
## Environment Variables
Environement variables defaults are set in **image/env.yaml**. You can modify environment variable values directly in this file and rebuild the image ([see manual build](#manual-build)). You can also override those values at run time with -e argument or by setting your own env.yaml file as a docker volume to `/etc/env.yaml`. See examples below.
- **KEEPALIVED_INTERFACE**: Keepalived network interface. Defaults to `eth0`
- **KEEPALIVED_PASSWORD**: Keepalived password. Defaults to `d0cker`
- **KEEPALIVED_PRIORITY** Keepalived node priority. Defaults to `150`
- **KEEPALIVED_UNICAST_PEERS** Keepalived unicast peers. Defaults to :
- 192.168.1.10
- 192.168.1.11
If you want to set this variable at docker run command convert the yaml in python :
docker run -e KEEPALIVED_UNICAST_PEERS="[192.168.1.10', '192.168.1.11']" -d osixia/phpldapadmin
To convert yaml to python online : http://yaml-online-parser.appspot.com/
- **KEEPALIVED_VIRTUAL_IPS** Add a read only user. Defaults to :
- 192.168.1.231
- 192.168.1.232
If you want to set this variable at docker run command convert the yaml in python, see above.
- **KEEPALIVED_NOTIFY** Script to execute when node state change. Defaults to `/container/service/keepalived/assets/notify.sh`
### Set environment variables at run time :
Environment variable can be set directly by adding the -e argument in the command line, for example :
docker run -e KEEPALIVED_INTERFACE="eno1" -e KEEPALIVED_PASSWORD="password!" \
-e KEEPALIVED_PRIORITY="100" -d osixia/keepalived
Or by setting your own `env.yaml` file as a docker volume to `/etc/env.yaml`
docker run -v /data/my-env.yaml:/etc/env.yaml \
-d osixia/keepalived
## Manual build
Clone this project :
git clone https://github.com/osixia/docker-keepalived
cd docker-keepalived
Adapt Makefile, set your image NAME and VERSION, for example :
NAME = osixia/keepalived
VERSION = 0.1.6
becomes :
NAME = billy-the-king/keepalived
VERSION = 0.1.0
Build your image :
make build
Run your image :
docker run -d billy-the-king/keepalived:0.1.0
## Tests
We use **Bats** (Bash Automated Testing System) to test this image:
> [https://github.com/sstephenson/bats](https://github.com/sstephenson/bats)
Install Bats, and in this project directory run :
make test
+42
View File
@@ -0,0 +1,42 @@
FROM osixia/light-baseimage:0.1.1
MAINTAINER Bertrand Gouny <bertrand.gouny@osixia.net>
# Keepalived version
ENV KEEPALIVED_VERSION 1.2.19
# Use baseimage's init system.
# https://github.com/osixia/docker-light-baseimage/blob/stable/image/tool/run
CMD ["/container/tool/run"]
# Install Keepalived
RUN apt-get -y update \
&& LC_ALL=C DEBIAN_FRONTEND=noninteractive apt-get install -y \
curl \
make \
gcc \
pkg-config \
libssl-dev \
libnl-3-dev \
libnl-genl-3-dev \
&& curl -o keepalived.tar.gz -SL http://keepalived.org/software/keepalived-${KEEPALIVED_VERSION}.tar.gz \
&& mkdir -p /container/keepalived-sources \
&& tar -xzf keepalived.tar.gz --strip 1 -C /container/keepalived-sources \
&& cd container/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 curl make gcc pkg-config libssl-dev
# Add service directory to /container/service
ADD service /container/service
# Use baseimage install-service script and clean all
# https://github.com/osixia/docker-light-baseimage/blob/stable/image/tool/install-service
RUN /container/tool/install-service \
&& rm -f keepalived.tar.gz \
&& rm -rf /container/keepalived-sources \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Add default env variables
ADD env.yaml /etc/env.yaml
+16
View File
@@ -0,0 +1,16 @@
KEEPALIVED_INTERFACE: eth0
KEEPALIVED_PASSWORD: d0cker
# For electing MASTER, highest priority wins.
# to be MASTER, make 50 more than other machines
KEEPALIVED_PRIORITY: 150
KEEPALIVED_UNICAST_PEERS:
- 192.168.1.10
- 192.168.1.11
KEEPALIVED_VIRTUAL_IPS:
- 192.168.1.231
- 192.168.1.232
KEEPALIVED_NOTIFY: /container/service/keepalived/assets/notify.sh
@@ -0,0 +1 @@
Add your custom keepalived.conf file here or mount one at docker run to /container/service/keepalived/assets/keepalived.conf
@@ -0,0 +1,33 @@
vrrp_sync_group VG_1 {
group {
VI_1
}
{{ keepalived_notify }}
}
vrrp_instance VI_1 {
interface {{ keepalived_interface }}
track_interface {
{{ keepalived_interface }}
}
state BACKUP
virtual_router_id 51
priority {{ keepalived_priority }}
nopreempt
unicast_peer {
{{ keepalived_unicast_peers }}
}
virtual_ipaddress {
{{ keepalived_virtual_ips }}
}
authentication {
auth_type PASS
auth_pass {{ keepalived_password }}
}
}
+31
View File
@@ -0,0 +1,31 @@
#!/bin/bash
# for ANY state transition.
# "notify" script is called AFTER the
# notify_* script(s) and is executed
# with 3 arguments provided by keepalived
# (ie don't include parameters in the notify line).
# arguments
# $1 = "GROUP"|"INSTANCE"
# $2 = name of group or instance
# $3 = target state of transition
# ("MASTER"|"BACKUP"|"FAULT")
TYPE=$1
NAME=$2
STATE=$3
case $STATE in
"MASTER") logger -s -t keepalived-notify "I'm the MASTER! Whup whup."
exit 0
;;
"BACKUP") logger -s -t keepalived-notify "Ok, i'm just a backup, great."
exit 0
;;
"FAULT") logger -s -t keepalived-notify "Fault, what ?"
exit 0
;;
*) logger -s -t keepalived-notify "Unknown state"
exit 1
;;
esac
+56
View File
@@ -0,0 +1,56 @@
#!/bin/bash -e
FIRST_START_DONE="/etc/docker-keepalived-first-start-done"
# container first start
if [ ! -e "$FIRST_START_DONE" ]; then
ln -s /container/service/keepalived/assets/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
if [ -n "$KEEPALIVED_NOTIFY" ]; then
sed -i "s|{{ keepalived_notify }}|notify \"$KEEPALIVED_NOTIFY\"|g" /etc/keepalived/keepalived.conf
else
sed -i "/{{ keepalived_notify }}/d" /etc/keepalived/keepalived.conf
fi
# 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|{{ keepalived_unicast_peers }}|${!peer}\n {{ keepalived_unicast_peers }}|g" /etc/keepalived/keepalived.conf
# directly
else
sed -i "s|{{ keepalived_unicast_peers }}|${peer}\n {{ keepalived_unicast_peers }}|g" /etc/keepalived/keepalived.conf
fi
done
sed -i "/{{ keepalived_unicast_peers }}/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|{{ keepalived_virtual_ips }}|${!vip}\n {{ keepalived_virtual_ips }}|g" /etc/keepalived/keepalived.conf
# directly
else
sed -i "s|{{ keepalived_virtual_ips }}|${vip}\n {{ keepalived_virtual_ips }}|g" /etc/keepalived/keepalived.conf
fi
done
sed -i "/{{ keepalived_virtual_ips }}/d" /etc/keepalived/keepalived.conf
touch $FIRST_START_DONE
fi
exit 0
+2
View File
@@ -0,0 +1,2 @@
#!/bin/bash -e
exec /usr/local/sbin/keepalived -f /etc/keepalived/keepalived.conf --dont-fork --log-console -D -d
+9
View File
@@ -0,0 +1,9 @@
#!/usr/bin/env bats
load test_helper
@test "image build" {
run build_image
[ "$status" -eq 0 ]
}
+111
View File
@@ -0,0 +1,111 @@
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
}
is_file_exists() {
is_file_exists_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
}
is_file_exists_by_cid() {
docker exec $1 cat "/etc/my_init_startup_files_completed" > /dev/null 2>&1
}
wait_service_by_cid() {
cid=$1
sleep 1
# first wait image init end
while ! is_file_exists_by_cid $cid /etc/my_init_startup_files_completed
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
}