Skip to content

Commit e785bf3

Browse files
committed
feat(docker): add config image and fix errors
1 parent f8feb50 commit e785bf3

File tree

6 files changed

+228
-127
lines changed

6 files changed

+228
-127
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ In Rancher's UI, go to **Admin/Settings** and add a new custom catalog:
1313
## Templates
1414

1515
* **redis**: Redis Sentinel Cluster for production environment
16+
17+
## Docker Images
18+
19+
* **redis-config**:
20+
Docker image used as sidekick container of all redis containers to provide scripts and data.

config/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM alpine:3.6@sha256:f006ecbb824d87947d0b51ab8488634bf69fe4094959d935c0c103f4820a417d
2+
3+
LABEL maintainer "Leonardo Gatica <lgatica@protonmail.com>"
4+
5+
ENV GIDDYUP_VERSION=0.19.0 CURL_VERSION=7.56.1
6+
7+
RUN mkdir -p /opt/redis
8+
COPY scripts /opt/redis/scripts
9+
RUN apk add --no-cache curl && \
10+
mkdir -p /usr/local/etc/redis && \
11+
curl -o /usr/local/etc/redis/redis.conf http://download.redis.io/redis-stable/redis.conf && \
12+
curl -o /usr/local/etc/redis/sentinel.conf http://download.redis.io/redis-stable/sentinel.conf && \
13+
curl -sL -o /opt/redis/scripts/giddyup https://github.com/rancher/giddyup/releases/download/v${GIDDYUP_VERSION}/giddyup && \
14+
chmod +x /opt/redis/scripts/*
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env sh
2+
3+
function leader_ip {
4+
echo -n $(wget -q -O - http://rancher-metadata/latest/stacks/$1/services/$2/containers/0/primary_ip)
5+
}
6+
7+
REDIS_USER_ID=${REDIS_USER_ID:-100}
8+
REDIS_GROUP_ID=${REDIS_GROUP_ID:-101}
9+
10+
chown $REDIS_USER_ID:$REDIS_GROUP_ID /usr/local/etc/redis/sentinel.conf
11+
12+
/opt/redis/scripts/giddyup service wait scale --timeout 120
13+
stack_name=`echo -n $(wget -q -O - http://rancher-metadata/latest/self/stack/name)`
14+
my_ip=$(/opt/redis/scripts/giddyup ip myip)
15+
master_ip=$(leader_ip $stack_name redis-server)
16+
17+
echo "my ip is $my_ip"
18+
echo "master ip is $master_ip"
19+
20+
sed -i -E "s/^ *# *bind +.*$/bind 0.0.0.0/g" /usr/local/etc/redis/sentinel.conf
21+
sed -i -E "s/^ *dir +.*$/dir .\//g" /usr/local/etc/redis/sentinel.conf
22+
sed -i -E "s/^ *# *sentinel +announce-ip 1.2.3.4+.*$/sentinel announce-ip ${my_ip}/" /usr/local/etc/redis/sentinel.conf
23+
sed -i -E "s/^ *sentinel +monitor +([A-z0-9._-]+) +([0-9.]+)? +([0-9]+) +([0-9]+).*$/sentinel monitor \1 ${master_ip} \3 $SENTINEL_QUORUM/g" /usr/local/etc/redis/sentinel.conf
24+
sed -i -E "s/^ *sentinel +down-after-milliseconds +([A-z0-9._-]+) +([0-9]+).*$/sentinel down-after-milliseconds \1 $SENTINEL_DOWN_AFTER/g" /usr/local/etc/redis/sentinel.conf
25+
sed -i -E "s/^ *sentinel +failover-timeout +([A-z0-9._-]+) +([0-9]+).*$/sentinel failover-timeout \1 $SENTINEL_FAILOVER/g" /usr/local/etc/redis/sentinel.conf
26+
sed -i -E "s/^ *# *sentinel +auth-pass +([A-z0-9._-]+) +([A-z0-9._-]+).*$/sentinel auth-pass \1 $REDIS_PASSWORD/g" /usr/local/etc/redis/sentinel.conf
27+
28+
exec docker-entrypoint.sh "$@"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env sh
2+
3+
function leader_ip {
4+
echo -n $(wget -q -O - http://rancher-metadata/latest/stacks/$1/services/$2/containers/0/primary_ip)
5+
}
6+
7+
REDIS_USER_ID=${REDIS_USER_ID:-100}
8+
REDIS_GROUP_ID=${REDIS_GROUP_ID:-101}
9+
10+
chown $REDIS_USER_ID:$REDIS_GROUP_ID /usr/local/etc/redis/redis.conf
11+
12+
/opt/redis/scripts/giddyup service wait scale --timeout 120
13+
stack_name=`echo -n $(wget -q -O - http://rancher-metadata/latest/self/stack/name)`
14+
my_ip=$(/opt/redis/scripts/giddyup ip myip)
15+
master_ip=$(leader_ip $stack_name redis-server)
16+
17+
sed -i -E "s/^ *bind +.*$/bind 0.0.0.0/g" /usr/local/etc/redis/redis.conf
18+
sed -i -E "s/^ *appendonly +.*$/appendonly yes/g" /usr/local/etc/redis/redis.conf
19+
sed -i -E "s/^ *# +masterauth +(.*)$/masterauth $REDIS_PASSWORD/g" /usr/local/etc/redis/redis.conf
20+
sed -i -E "s/^ *# +requirepass +(.*)$/requirepass $REDIS_PASSWORD/g" /usr/local/etc/redis/redis.conf
21+
22+
echo "my ip is $my_ip"
23+
echo "master ip is $master_ip"
24+
25+
if [ "$my_ip" == "$master_ip" ]
26+
then
27+
sed -i -E "s/^ *slaveof +([^ ]*) +([^ ]*)$/#slaveof \1 \2/g" /usr/local/etc/redis/redis.conf
28+
echo "i am the leader"
29+
else
30+
port=`echo -n $(grep -E "^ *port +.*$" /usr/local/etc/redis/redis.conf | sed -E "s/^ *port +(.*)$/\1/g")`
31+
sed -i -E "s/^ *(# +)?slaveof +.*/slaveof $master_ip $port/g" /usr/local/etc/redis/redis.conf
32+
fi
33+
34+
exec docker-entrypoint.sh "$@"
Lines changed: 75 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,105 @@
11
version: '2'
22

33
services:
4-
master:
5-
image: redis:${REDIS_VERSION}-alpine
4+
redis-server:
5+
image: ${REDIS_VERSION}
66
environment:
77
REDIS_PASSWORD: '${REDIS_PASSWORD}'
88
stdin_open: true
9-
volumes:
10-
- redis-master:/data
119
tty: true
12-
command:
13-
- redis-server
14-
- --appendonly
15-
- 'yes'
16-
- --masterauth
17-
- '${REDIS_PASSWORD}'
18-
- --requirepass
19-
- '${REDIS_PASSWORD}'
2010
labels:
11+
{{- if ne .Values.REDIS_SERVER_HOST_LABEL ""}}
12+
io.rancher.scheduler.affinity:host_label: ${REDIS_SERVER_HOST_LABEL}
13+
{{- end}}
14+
io.rancher.container.start_once: 'true'
2115
io.rancher.container.pull_image: always
22-
io.rancher.scheduler.affinity:host_label: '${REDIS_MASTER_HOST_LABEL}'
16+
io.rancher.sidekicks: redis-server-config
2317
io.rancher.scheduler.affinity:container_label_ne: io.rancher.stack_service.name=$${stack_name}/$${service_name}
24-
25-
slave:
26-
image: redis:${REDIS_VERSION}-alpine
27-
environment:
28-
REDIS_PASSWORD: '${REDIS_PASSWORD}'
29-
stdin_open: true
30-
volumes:
31-
- redis-slave:/data
32-
tty: true
18+
io.rancher.container.hostname_override: container_name
19+
volumes_from:
20+
- redis-server-config
21+
entrypoint: /opt/redis/scripts/server-entrypoint.sh
3322
command:
34-
- redis-server
35-
- --appendonly
36-
- 'yes'
37-
- --slaveof
38-
- master
39-
- '6379'
40-
- --masterauth
41-
- '${REDIS_PASSWORD}'
42-
- --requirepass
43-
- '${REDIS_PASSWORD}'
44-
labels:
45-
io.rancher.container.pull_image: always
46-
io.rancher.scheduler.affinity:host_label: '${REDIS_SLAVE_HOST_LABEL}'
47-
io.rancher.scheduler.affinity:container_label_ne: io.rancher.stack_service.name=$${stack_name}/$${service_name}
23+
- "redis-server"
24+
- "/usr/local/etc/redis/redis.conf"
4825

49-
sentinel:
50-
image: lgatica/redis-sentinel:${REDIS_VERSION}
26+
redis-sentinel:
27+
image: ${REDIS_VERSION}
5128
environment:
5229
REDIS_PASSWORD: '${REDIS_PASSWORD}'
30+
SENTINEL_QUORUM: '${SENTINEL_QUORUM}'
31+
SENTINEL_DOWN_AFTER: '${SENTINEL_DOWN_AFTER}'
32+
SENTINEL_FAILOVER: '${SENTINEL_FAILOVER}'
5333
stdin_open: true
5434
tty: true
55-
links:
56-
- master:master
57-
ports:
58-
- '${REDIS_SENTINEL_PORT}':26379/tcp
5935
labels:
6036
io.rancher.container.pull_image: always
61-
io.rancher.scheduler.affinity:host_label: '${REDIS_SENTINEL_HOST_LABEL}'
62-
io.rancher.scheduler.affinity:container_label_ne: io.rancher.stack_service.name=$${stack_name}/$${service_name}
37+
{{- if ne .Values.REDIS_SENTINEL_HOST_LABEL ""}}
38+
io.rancher.scheduler.affinity:host_label: ${REDIS_SENTINEL_HOST_LABEL}
39+
{{- end}}
40+
io.rancher.sidekicks: redis-sentinel-config
41+
io.rancher.container.hostname_override: container_name
42+
volumes_from:
43+
- redis-sentinel-config
44+
entrypoint: /opt/redis/scripts/sentinel-entrypoint.sh
45+
command:
46+
- "redis-server"
47+
- "/usr/local/etc/redis/sentinel.conf"
48+
- "--sentinel"
6349

6450
haproxy:
6551
image: rancher/lb-service-haproxy:v0.7.9
6652
ports:
67-
- '${REDIS_HAPROXY_PORT}':6379/tcp
53+
- ${REDIS_HAPROXY_PORT}:6379/tcp
6854
labels:
69-
io.rancher.scheduler.affinity:host_label: '${REDIS_SENTINEL_HOST_LABEL}'
55+
{{- if ne .Values.REDIS_SENTINEL_HOST_LABEL ""}}
56+
io.rancher.scheduler.affinity:host_label: ${REDIS_SENTINEL_HOST_LABEL}
57+
{{- end}}
7058
io.rancher.container.agent.role: environmentAdmin
7159
io.rancher.container.create_agent: 'true'
7260

61+
redis-server-config:
62+
image: lgatica/redis-config
63+
environment:
64+
REDIS_PASSWORD: '${REDIS_PASSWORD}'
65+
stdin_open: true
66+
tty: true
67+
volumes:
68+
- /usr/local/etc/redis
69+
- /opt/redis/scripts
70+
- redis-server:/data
71+
labels:
72+
io.rancher.container.pull_image: always
73+
io.rancher.container.hostname_override: container_name
74+
redis-sentinel-config:
75+
image: lgatica/redis-config
76+
environment:
77+
REDIS_PASSWORD: '${REDIS_PASSWORD}'
78+
stdin_open: true
79+
tty: true
80+
volumes:
81+
- /usr/local/etc/redis
82+
- /opt/redis/scripts
83+
- redis-sentinel:/data
84+
labels:
85+
io.rancher.container.pull_image: always
86+
io.rancher.container.hostname_override: container_name
87+
88+
{{- if or (.Values.REDIS_VOLUME_NAME) (.Values.SENTINEL_VOLUME_NAME)}}
7389
volumes:
74-
redis-master:
75-
external: true
90+
{{- if .Values.REDIS_VOLUME_NAME}}
91+
{{.Values.REDIS_VOLUME_NAME}}:
7692
per_container: true
77-
driver: '${VOLUME_DRIVER}'
78-
driver_opts:
79-
size: '${VOLUME_DRIVER_SIZE}'
80-
volumeType: '${VOLUME_DRIVER_TYPE}'
81-
ec2_az: '${VOLUME_DRIVER_AZ}'
82-
iops: '${VOLUME_DRIVER_IOPS}'
93+
{{- if .Values.STORAGE_DRIVER}}
94+
driver: {{.Values.STORAGE_DRIVER}}
95+
{{- end}}
96+
{{- end}}
8397

84-
redis-slave:
85-
external: true
98+
{{- if .Values.SENTINEL_VOLUME_NAME}}
99+
{{.Values.SENTINEL_VOLUME_NAME}}:
86100
per_container: true
87-
driver: '${VOLUME_DRIVER}'
88-
driver_opts:
89-
size: '${VOLUME_DRIVER_SIZE}'
90-
volumeType: '${VOLUME_DRIVER_TYPE}'
91-
ec2_az: '${VOLUME_DRIVER_AZ}'
92-
iops: '${VOLUME_DRIVER_IOPS}'
101+
{{- if .Values.STORAGE_DRIVER}}
102+
driver: {{.Values.STORAGE_DRIVER}}
103+
{{- end}}
104+
{{- end}}
105+
{{- end }}

0 commit comments

Comments
 (0)