由于一些需要,我在编辑一个redis镜象,出于一些考虑,需要传递一些变量来做简单的修改,以便于使用,只修改部分参数。有些机器并不是单独跑一个业务,这就不能使用固定的配置文件,缺少灵活度。这就又需要改变,我使用一个变量的传递加shell的判断来做,那么我至少要满足以下三点:
- 通过变量修改部分配置文件中的关键参数
- 这些参数可以开关
- 关闭参数可以手动调整全部配置文件
Dockerfile编写思路
我们先编写Dockerfile,如下:
其中ENV RS_VSON_URL有大量的环境变量,在中间将被引用
其中RUN buildADD作为赋值使用,简化操作
在编译完成后仍然保持卸载和删除多余的包和目录,其中需要下载配置文件和启动脚本
其中使用supervisor守护进程,可参考docker的supervisor与inotifywait的使用技巧
gettext工具使用,可参考compose中的变量传递与docker-createrepo构建
其中
xport MAXMEMORY_SIZE=`echo "expr $(($(awk '/MemTotal/{print $2}' /proc/meminfo)*88/102400))"|awk '{print $2}'`
来计算内存的百分之88(见启动脚本部分),最终传递给配置文件
FROM alpine:3.8
MAINTAINER www.linuxea.com mark
ENV RS_VSON="4.0.11"
ENV RS_USER="redis"
ENV RS_VSON_URL="http://download.redis.io/releases/redis-${RS_VSON}.tar.gz"
BATADIR=/usr/local/redis
DATADIR=/data/redis
DATALOG=/data/logs
DATACIG=/etc/redis
DATAOPT=/opt
RUN buildADD='gcc make musl-dev linux-headers tar curl'
&& set -x
&& addgroup -g 401 -S ${RS_USER} && adduser -u 401 -S -H -s /sbin/nologin -g 'redis' -G ${RS_USER} ${RS_USER}
&& mkdir -p ${BATADIR} ${DATADIR} ${DATALOG} ${DATACIG} ${DATAOPT}
&& chown ${RS_USER}.${RS_USER} ${DATADIR}
&& apk add --no-cache --virtual .build-deps ${buildADD}
&& curl -Lks4 ${RS_VSON_URL}|tar xz -C ${BATADIR} --strip-components=1
&& cd ${BATADIR} && make && make install
&& curl -Lk https://raw.githubusercontent.com/LinuxEA-Mark/docker-alpine-Redis/master/4.0.11/redis.env -o /${DATAOPT}/redis.env
&& curl -Lk https://raw.githubusercontent.com/LinuxEA-Mark/docker-alpine-Redis/master/4.0.11/redis.conf -o /${DATALOG}/redis.conf
&& curl -Lk https://raw.githubusercontent.com/LinuxEA-Mark/docker-alpine-Redis/master/4.0.11/supervisord.conf -o /etc/supervisord.conf
&& curl -Lk https://raw.githubusercontent.com/LinuxEA-Mark/docker-alpine-Redis/master/4.0.11/startup.sh -o /startup.sh
&& chmod +x /startup.sh
&& apk del .build-deps
&& apk add supervisor gettext
&& rm -rf /var/cache/apk/* ${BATADIR}
#auto configure maxmemory_size
&& export MAXMEMORY_SIZE=`echo "expr $(($(awk '/MemTotal/{print $2}' /proc/meminfo)*88/102400))"|awk '{print $2}'`
EXPOSE 6379/tcp 26379/tcp
ENTRYPOINT ["/startup.sh"]
启动脚本
为了便于理解,我这里以compose为例
environment:
- REDIS_CONF=on
- REQUIREPASSWD=OTdmOWI4ZTM4NTY1M2M4OTZh
- MASTERAUTHPAD=OTdmOWI4ZTM4NTY1M2M4OTZh
- MAXCLIENTS_NUM=600
- MAXMEMORY_SIZE=4096
上述中有5个参数,使用environment最终会传递到容器内
之后判断REDIS_CONF=on是否等于on,如果是则执行envsubst,执行envsubst我准备了两个文件,分别是redis.env和redis.conf,在文件中最下面
if [ env |grep REDIS_CONF=on|wc -l -ne 0 ];then envsubst < /opt/redis.env > /etc/redis/redis.conf; fi
而后借助supervisord 启动,脚本如下
#!/bin/sh
# File Name: startup.sh
# Author: www.linuxea.com
# Created Time: 2018年08月11日 星期六 21时46分20秒
########################################################################################
# If REDIS_CONF=on replaces the passed variable: #
# - REQUIREPASSWD #
# - MASTERAUTHPAD #
# - MAXCLIENTS_NUM #
# - MAXMEMORY_SIZE #
# If equal to REDIS_CONF=off, you can use the /etc/redis/redis.conf configuration file.#
########################################################################################
export MAXMEMORY_SIZE=`echo "expr $(($(awk '/MemTotal/{print $2}' /proc/meminfo)*88/102400))"|awk '{print $2}'`
if [ `env |grep REDIS_CONF=on|wc -l` -ne 0 ];then envsubst < /opt/redis.env > /etc/redis/redis.conf; fi
supervisord -n -c /etc/supervisord.conf
supervisord配置文件
在dockerfile中已经将supervisord安装,在之前的docker的supervisor与inotifywait的使用技巧对此说明过,不理解可移步
[unix_http_server]
file=/tmp/supervisor.sock ; the path to the socket file
[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
[program:redis.4.0.11]
command=/bin/sh -c "exec /usr/local/bin/redis-server /etc/redis/redis.conf"
autostart=true
autorestart=false
startretries=0
stdout_events_enabled=true
stderr_events_enabled=true
compose文件
这里提供了compose文件,在compose文件中我已经将image上传至dockerhub,可以直接下载使用,或者使用github上的Dockerfile
其中挂载/data和/logs到本地,以及配置文件,我在github上做了叙述
version: '2'
services:
redis:
image: marksugar/redis:4.0.11
# build:
# context: https://raw.githubusercontent.com/LinuxEA-Mark/docker-alpine-Redis/master/4.0.11/Dockerfile
container_name: redis
restart: always
network_mode: "host"
privileged: true
environment:
- REDIS_CONF=on
- REQUIREPASSWD=OTdmOWI4ZTM4NTY1M2M4OTZh
- MASTERAUTHPAD=OTdmOWI4ZTM4NTY1M2M4OTZh
- MAXCLIENTS_NUM=600
- MAXMEMORY_SIZE=4096
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/redis:/etc/redis
- /data/redis-data:/data/redis:Z
- /data/logs:/data/logs
redis配置文件
我准备了两个配置文件用做变量的替换,redis.env
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/data/logs/redis_6379.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir "/data/redis"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
maxmemory-policy allkeys-lfu
lfu-log-factor 10
lfu-decay-time 1
maxclients ${MAXCLIENTS_NUM}
maxmemory ${MAXMEMORY_SIZE}M
masterauth "${MASTERAUTHPAD}"
requirepass "${REQUIREPASSWD}"
redis.conf
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/data/logs/redis_6379.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir "/data/redis"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
maxmemory-policy allkeys-lfu
lfu-log-factor 10
lfu-decay-time 1
maxclients 800
maxmemory 4096M
masterauth "OTdmOWI4ZTM4NTY1M2M4OTZh"
requirepass "OTdmOWI4ZTM4NTY1M2M4OTZh"
快速安装
[root@linuxea-VM-Node_10_10_240_145 /data/redis]$ curl -Lk https://raw.githubusercontent.com/LinuxEA-Mark/docker-alpine-Redis/master/4.0.11/install_redis_4.0.11.sh|bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 506 100 506 0 0 651 0 --:--:-- --:--:-- --:--:-- 653
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 602 100 602 0 0 1368 0 --:--:-- --:--:-- --:--:-- 1371
Creating redis ... done
查看日志