update
This commit is contained in:
147
redis/ADD/ccmd
Executable file
147
redis/ADD/ccmd
Executable file
@@ -0,0 +1,147 @@
|
||||
#!/bin/bash
|
||||
|
||||
##################################################
|
||||
# Docker #
|
||||
# - --privileged #
|
||||
# Mount dir #
|
||||
# - LOG_DIR #
|
||||
# - DATA_DIR #
|
||||
# ENV #
|
||||
# - _CONF_* #
|
||||
# - MASTER_NODES #
|
||||
# - SLAVE_NODES #
|
||||
##################################################
|
||||
|
||||
set -euo pipefail
|
||||
export LANG=en_US.UTF-8
|
||||
trap Quit EXIT
|
||||
|
||||
PIDS=
|
||||
GOT_SIGTERM=
|
||||
LOG_DIR='/var/log/redis'
|
||||
DATA_DIR='/var/lib/redis'
|
||||
SOCK_FILE='/run/redis/redis.sock'
|
||||
MASTER_NODES=(${MASTER_NODES:-})
|
||||
SLAVE_NODES=(${SLAVE_NODES:-})
|
||||
DEPLOY_CLUSTER=
|
||||
CLUSTER_CONFIG_FILE="$DATA_DIR/nodes.conf"
|
||||
REQUIREPASS=
|
||||
|
||||
function Print {
|
||||
local file=/dev/null
|
||||
[ '-f' = "$1" ] && file=$2 && shift && shift
|
||||
date +"[%F %T] $*" | tee -a $file
|
||||
}
|
||||
|
||||
function Quit {
|
||||
local errCode=$?
|
||||
Print Killing redis-server ...
|
||||
[ -f $SOCK_FILE ] \
|
||||
&& redis-cli ${REQUIREPASS:+-a $REQUIREPASS --no-auth-warning} \
|
||||
-s $SOCK_FILE shutdown \
|
||||
|| true
|
||||
while :; do
|
||||
pkill -f redis-server && Print Killing redis-server ... || break
|
||||
sleep 1
|
||||
done
|
||||
[ 100 -eq $errCode ] && rm -f $CLUSTER_CONFIG_FILE
|
||||
Print Container stopped.
|
||||
test -n "$GOT_SIGTERM"
|
||||
}
|
||||
|
||||
function ModifyConf {
|
||||
local kv=
|
||||
local clusterEnabled=
|
||||
[ -e /etc/redis.conf ] && Print /etc/redis.conf already exists && return 0
|
||||
Print Modify conf ...
|
||||
echo "bind 0.0.0.0
|
||||
protected-mode no
|
||||
maxmemory 6442450944
|
||||
maxmemory-policy volatile-random
|
||||
" > /etc/redis.conf
|
||||
while read kv; do
|
||||
[ 'cluster-enabled=yes' == "$kv" ] && clusterEnabled=1
|
||||
[[ "$kv" =~ ^requirepass= ]] && REQUIREPASS=${kv#*=}
|
||||
echo "${kv/=/ }" >> /etc/redis.conf
|
||||
done <<< "$(env | grep '^_CONF_' | sed 's/_CONF_//')"
|
||||
[ -e "$CLUSTER_CONFIG_FILE" -o -z "$clusterEnabled" -o -z "${MASTER_NODES[*]}" ] \
|
||||
|| DEPLOY_CLUSTER=1
|
||||
}
|
||||
|
||||
function DeployCluster {
|
||||
Print Deploy redis cluster ...
|
||||
local i=
|
||||
local node=
|
||||
local nodesId=
|
||||
local clusterEnabled=
|
||||
local redisCliCmd="redis-cli ${REQUIREPASS:+-a $REQUIREPASS --no-auth-warning}"
|
||||
[ 0 -ne ${#SLAVE_NODES[@]} -a ${#SLAVE_NODES[@]} -ne ${#MASTER_NODES[@]} ] \
|
||||
&& Print The num of slave nodes not equal to the num of master nodes! \
|
||||
&& exit 100
|
||||
for node in ${MASTER_NODES[@]} ${SLAVE_NODES[@]}; do
|
||||
for i in {0..5}; do
|
||||
[ 5 -eq $i ] \
|
||||
&& Print Failed to connect to $node! \
|
||||
&& exit 100
|
||||
sleep 6
|
||||
clusterEnabled=$($redisCliCmd --raw -d , -h ${node/:/ -p } \
|
||||
CONFIG GET cluster-enabled) \
|
||||
|| continue
|
||||
[[ "$clusterEnabled" =~ ,yes$ ]] && break
|
||||
Print $node not enable cluster mode! && exit 100
|
||||
done
|
||||
done
|
||||
Print Create redis cluster with master nodes: ${MASTER_NODES[@]}
|
||||
$redisCliCmd --cluster-yes --cluster create ${MASTER_NODES[@]}
|
||||
nodesId="$($redisCliCmd -s $SOCK_FILE cluster nodes | cut -d@ -f1)"
|
||||
for i in ${!SLAVE_NODES[@]}; do
|
||||
Print Add slave node ${SLAVE_NODES[$i]} with master: ${MASTER_NODES[$i]}
|
||||
$redisCliCmd --cluster-yes --cluster add-node ${SLAVE_NODES[$i]} \
|
||||
${MASTER_NODES[0]} --cluster-slave \
|
||||
--cluster-master-id $(echo "$nodesId" \
|
||||
| grep "${MASTER_NODES[$i]}$" \
|
||||
| awk '{print $1}')
|
||||
done
|
||||
}
|
||||
|
||||
function Init {
|
||||
echo 1024 > /proc/sys/net/core/somaxconn \
|
||||
&& echo 1 > /proc/sys/vm/overcommit_memory \
|
||||
|| Print Not specified "--privileged".
|
||||
mkdir -p $LOG_DIR $DATA_DIR
|
||||
}
|
||||
|
||||
function StartProc {
|
||||
Print Start Redis ...
|
||||
redis-server /etc/redis.conf \
|
||||
--daemonize no \
|
||||
--dir $DATA_DIR \
|
||||
--cluster-config-file $CLUSTER_CONFIG_FILE \
|
||||
--logfile $LOG_DIR/redis.log \
|
||||
--unixsocket $SOCK_FILE &
|
||||
PIDS="$PIDS $!"
|
||||
while sleep 1; do
|
||||
[ -e $SOCK_FILE ] && break || echo -n .
|
||||
[ ! -e /proc/$! ] && echo && Print Unexpected error! && exit
|
||||
done
|
||||
echo
|
||||
[ -n "$DEPLOY_CLUSTER" ] && DeployCluster
|
||||
Print Redis is ready for connections.
|
||||
}
|
||||
|
||||
function Main {
|
||||
local pid=
|
||||
ModifyConf
|
||||
Init
|
||||
StartProc
|
||||
trap "GOT_SIGTERM=1; Print Got SIGTERM ..." SIGTERM
|
||||
while [ -z "$GOT_SIGTERM" ] && sleep 1; do
|
||||
for pid in $PIDS; do
|
||||
[ ! -e /proc/$pid ] && Print Unexpected error! && exit
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
# Start here
|
||||
Main
|
||||
|
15
redis/Demo/SingleNode/README.md
Normal file
15
redis/Demo/SingleNode/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# 部署 redis 单点
|
||||
|
||||
- 按实际环境修改
|
||||
- docker-compose.yml
|
||||
|
||||
- 创建目录
|
||||
```
|
||||
grep '\<source:' docker-compose.yml cut -d: -f2 | xargs mkdir -p
|
||||
```
|
||||
|
||||
- 启动
|
||||
```
|
||||
docker-compose up -d
|
||||
```
|
||||
|
22
redis/Demo/SingleNode/docker-compose.yml
Normal file
22
redis/Demo/SingleNode/docker-compose.yml
Normal file
@@ -0,0 +1,22 @@
|
||||
version: "3.7"
|
||||
|
||||
services:
|
||||
redis:
|
||||
image: harbor.colben.cn/general/redis
|
||||
container_name: redis
|
||||
restart: "on-failure"
|
||||
stop_grace_period: 1m
|
||||
privileged: true
|
||||
environment:
|
||||
_CONF_maxmemory: 1GB
|
||||
_CONF_port: 6379
|
||||
ports:
|
||||
- 6379:6379
|
||||
volumes:
|
||||
- type: bind
|
||||
source: ./redis/data
|
||||
target: /var/lib/redis
|
||||
- type: bind
|
||||
source: ./redis/log
|
||||
target: /var/log/redis
|
||||
|
15
redis/Demo/SixNodes/README.md
Normal file
15
redis/Demo/SixNodes/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# 部署 redis 集群三主三从
|
||||
|
||||
- 按实际环境修改
|
||||
- docker-compose.yml
|
||||
|
||||
- 创建目录
|
||||
```
|
||||
grep '\<source:' docker-compose.yml cut -d: -f2 | xargs mkdir -p
|
||||
```
|
||||
|
||||
- 启动
|
||||
```
|
||||
docker-compose up -d
|
||||
```
|
||||
|
137
redis/Demo/SixNodes/docker-compose.yml
Normal file
137
redis/Demo/SixNodes/docker-compose.yml
Normal file
@@ -0,0 +1,137 @@
|
||||
version: "3.7"
|
||||
|
||||
services:
|
||||
redis-6371:
|
||||
image: harbor.colben.cn/general/redis
|
||||
container_name: redis-6371
|
||||
restart: on-failure
|
||||
stop_grace_period: 1m
|
||||
privileged: true
|
||||
network_mode: host
|
||||
environment:
|
||||
_CONF_cluster-enabled: "yes"
|
||||
_CONF_port: 6371
|
||||
_CONF_bind: 127.0.0.1
|
||||
_CONF_maxmemory: 256MB
|
||||
_CONF_masterauth: Pass_1234
|
||||
_CONF_requirepass: Pass_1234
|
||||
MASTER_NODES: "127.0.0.1:6371 127.0.0.1:6372 127.0.0.1:6373"
|
||||
SLAVE_NODES: "127.0.0.1:6374 127.0.0.1:6375 127.0.0.1:6376"
|
||||
volumes:
|
||||
- type: bind
|
||||
source: ./redis-6371/db
|
||||
target: /var/lib/redis
|
||||
- type: bind
|
||||
source: ./redis-6371/log
|
||||
target: /var/log/redis
|
||||
|
||||
redis-6372:
|
||||
image: harbor.colben.cn/general/redis
|
||||
container_name: redis-6372
|
||||
restart: on-failure
|
||||
stop_grace_period: 1m
|
||||
privileged: true
|
||||
network_mode: host
|
||||
environment:
|
||||
_CONF_cluster-enabled: "yes"
|
||||
_CONF_port: 6372
|
||||
_CONF_bind: 127.0.0.1
|
||||
_CONF_maxmemory: 256MB
|
||||
_CONF_masterauth: Pass_1234
|
||||
_CONF_requirepass: Pass_1234
|
||||
volumes:
|
||||
- type: bind
|
||||
source: ./redis-6372/db
|
||||
target: /var/lib/redis
|
||||
- type: bind
|
||||
source: ./redis-6372/log
|
||||
target: /var/log/redis
|
||||
|
||||
redis-6373:
|
||||
image: harbor.colben.cn/general/redis
|
||||
container_name: redis-6373
|
||||
restart: on-failure
|
||||
stop_grace_period: 1m
|
||||
privileged: true
|
||||
network_mode: host
|
||||
environment:
|
||||
_CONF_cluster-enabled: "yes"
|
||||
_CONF_port: 6373
|
||||
_CONF_bind: 127.0.0.1
|
||||
_CONF_maxmemory: 256MB
|
||||
_CONF_masterauth: Pass_1234
|
||||
_CONF_requirepass: Pass_1234
|
||||
volumes:
|
||||
- type: bind
|
||||
source: ./redis-6373/db
|
||||
target: /var/lib/redis
|
||||
- type: bind
|
||||
source: ./redis-6373/log
|
||||
target: /var/log/redis
|
||||
|
||||
redis-6374:
|
||||
image: harbor.colben.cn/general/redis
|
||||
container_name: redis-6374
|
||||
restart: on-failure
|
||||
stop_grace_period: 1m
|
||||
privileged: true
|
||||
network_mode: host
|
||||
environment:
|
||||
_CONF_cluster-enabled: "yes"
|
||||
_CONF_port: 6374
|
||||
_CONF_bind: 127.0.0.1
|
||||
_CONF_maxmemory: 256MB
|
||||
_CONF_masterauth: Pass_1234
|
||||
_CONF_requirepass: Pass_1234
|
||||
volumes:
|
||||
- type: bind
|
||||
source: ./redis-6374/db
|
||||
target: /var/lib/redis
|
||||
- type: bind
|
||||
source: ./redis-6374/log
|
||||
target: /var/log/redis
|
||||
|
||||
redis-6375:
|
||||
image: harbor.colben.cn/general/redis
|
||||
container_name: redis-6375
|
||||
restart: on-failure
|
||||
stop_grace_period: 1m
|
||||
privileged: true
|
||||
network_mode: host
|
||||
environment:
|
||||
_CONF_cluster-enabled: "yes"
|
||||
_CONF_port: 6375
|
||||
_CONF_bind: 127.0.0.1
|
||||
_CONF_maxmemory: 256MB
|
||||
_CONF_masterauth: Pass_1234
|
||||
_CONF_requirepass: Pass_1234
|
||||
volumes:
|
||||
- type: bind
|
||||
source: ./redis-6375/db
|
||||
target: /var/lib/redis
|
||||
- type: bind
|
||||
source: ./redis-6375/log
|
||||
target: /var/log/redis
|
||||
|
||||
redis-6376:
|
||||
image: harbor.colben.cn/general/redis
|
||||
container_name: redis-6376
|
||||
restart: on-failure
|
||||
stop_grace_period: 1m
|
||||
privileged: true
|
||||
network_mode: host
|
||||
environment:
|
||||
_CONF_cluster-enabled: "yes"
|
||||
_CONF_port: 6376
|
||||
_CONF_bind: 127.0.0.1
|
||||
_CONF_maxmemory: 256MB
|
||||
_CONF_masterauth: Pass_1234
|
||||
_CONF_requirepass: Pass_1234
|
||||
volumes:
|
||||
- type: bind
|
||||
source: ./redis-6376/db
|
||||
target: /var/lib/redis
|
||||
- type: bind
|
||||
source: ./redis-6376/log
|
||||
target: /var/log/redis
|
||||
|
15
redis/Demo/ThreeNodes/README.md
Normal file
15
redis/Demo/ThreeNodes/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# 部署 redis 三主集群
|
||||
|
||||
- 按实际环境修改
|
||||
- docker-compose.yml
|
||||
|
||||
- 创建目录
|
||||
```
|
||||
grep '\<source:' docker-compose.yml cut -d: -f2 | xargs mkdir -p
|
||||
```
|
||||
|
||||
- 启动
|
||||
```
|
||||
docker-compose up -d
|
||||
```
|
||||
|
70
redis/Demo/ThreeNodes/docker-compose.yml
Normal file
70
redis/Demo/ThreeNodes/docker-compose.yml
Normal file
@@ -0,0 +1,70 @@
|
||||
version: "3.7"
|
||||
|
||||
services:
|
||||
redis-6371:
|
||||
image: harbor.colben.cn/general/redis
|
||||
container_name: redis-6371
|
||||
restart: on-failure
|
||||
stop_grace_period: 1m
|
||||
privileged: true
|
||||
network_mode: host
|
||||
environment:
|
||||
_CONF_cluster-enabled: "yes"
|
||||
_CONF_port: 6371
|
||||
_CONF_bind: 127.0.0.1
|
||||
_CONF_maxmemory: 256MB
|
||||
_CONF_masterauth: Pass_1234
|
||||
_CONF_requirepass: Pass_1234
|
||||
MASTER_NODES: "127.0.0.1:6371 127.0.0.1:6372 127.0.0.1:6373"
|
||||
volumes:
|
||||
- type: bind
|
||||
source: ./redis-6371/db
|
||||
target: /var/lib/redis
|
||||
- type: bind
|
||||
source: ./redis-6371/log
|
||||
target: /var/log/redis
|
||||
|
||||
redis-6372:
|
||||
image: harbor.colben.cn/general/redis
|
||||
container_name: redis-6372
|
||||
restart: on-failure
|
||||
stop_grace_period: 1m
|
||||
privileged: true
|
||||
network_mode: host
|
||||
environment:
|
||||
_CONF_cluster-enabled: "yes"
|
||||
_CONF_port: 6372
|
||||
_CONF_bind: 127.0.0.1
|
||||
_CONF_maxmemory: 256MB
|
||||
_CONF_masterauth: Pass_1234
|
||||
_CONF_requirepass: Pass_1234
|
||||
volumes:
|
||||
- type: bind
|
||||
source: ./redis-6372/db
|
||||
target: /var/lib/redis
|
||||
- type: bind
|
||||
source: ./redis-6372/log
|
||||
target: /var/log/redis
|
||||
|
||||
redis-6373:
|
||||
image: harbor.colben.cn/general/redis
|
||||
container_name: redis-6373
|
||||
restart: on-failure
|
||||
stop_grace_period: 1m
|
||||
privileged: true
|
||||
network_mode: host
|
||||
environment:
|
||||
_CONF_cluster-enabled: "yes"
|
||||
_CONF_port: 6373
|
||||
_CONF_bind: 127.0.0.1
|
||||
_CONF_maxmemory: 256MB
|
||||
_CONF_masterauth: Pass_1234
|
||||
_CONF_requirepass: Pass_1234
|
||||
volumes:
|
||||
- type: bind
|
||||
source: ./redis-6373/db
|
||||
target: /var/lib/redis
|
||||
- type: bind
|
||||
source: ./redis-6373/log
|
||||
target: /var/log/redis
|
||||
|
10
redis/Dockerfile
Normal file
10
redis/Dockerfile
Normal file
@@ -0,0 +1,10 @@
|
||||
ARG ARCH
|
||||
FROM harbor.colben.cn/general/alpine$ARCH
|
||||
MAINTAINER Colben colbenlee@gmail.com
|
||||
ADD --chown=root:root /ADD/ /opt/
|
||||
RUN apk update \
|
||||
&& apk add --no-cache redis \
|
||||
&& mv /etc/redis.conf /etc/redis.conf.origin \
|
||||
&& rm -rf /var/cache/apk/*
|
||||
CMD ["/opt/ccmd"]
|
||||
|
22
redis/README.md
Normal file
22
redis/README.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# 构建 redis 镜像
|
||||
|
||||
## 定制
|
||||
- 安装 redis
|
||||
- 固定一些常用配置
|
||||
- __主节点数量最少三个,从节点要么没有,要么与主节点数量一致,否则出错__
|
||||
- docker 参数: --privileged
|
||||
|
||||
## 外挂目录和文件
|
||||
- /var/lib/redis: redis 数据目录
|
||||
- /var/log/redis: redis 日志目录
|
||||
|
||||
## 引入环境变量
|
||||
- MASTER_NODES: 主节点列表,__用空格间隔__
|
||||
- SLAVE_NODES: 从节点列表,__用空格间隔__
|
||||
- \_CONF\_\*: redis 配置
|
||||
|
||||
## 案例
|
||||
- [Demo/SingleNode/](Demo/SingleNode/): 部署单点
|
||||
- [Demo/ThreeNodes/](Demo/ThreeNodes/): 部署集群,只有三个主节点
|
||||
- [Demo/SixNodes/](Demo/SixNodes/): 部署集群,有三个主节点和三个从节点
|
||||
|
67
redis/redis.sh
Executable file
67
redis/redis.sh
Executable file
@@ -0,0 +1,67 @@
|
||||
#!/bin/bash
|
||||
|
||||
#=========================================
|
||||
# Author : colben
|
||||
#=========================================
|
||||
|
||||
set -euo pipefail
|
||||
export LANG=en_US.UTF-8
|
||||
trap Quit EXIT
|
||||
|
||||
[ 'x86_64' == "$(uname -m)" ] && ARCH='' || ARCH="-$(uname -m)"
|
||||
ROOT_DIR="$(cd $(dirname $0) && pwd)"
|
||||
IMAGE="harbor.colben.cn/general/$(basename ${0%.sh})$ARCH:latest"
|
||||
|
||||
if [ -t 0 ]; then
|
||||
function Print { echo -e "\033[36;1m$(date +'[%F %T]')\033[32;1m $*\033[0m"; }
|
||||
function Warn { echo -e "\033[36;1m$(date +'[%F %T]')\033[33;1m $*\033[0m"; }
|
||||
function Error { echo -e "\033[36;1m$(date +'[%F %T]')\033[31;1m $*\033[0m"; exit 1; }
|
||||
else
|
||||
function Print { echo -e "$(date +'[%F %T INFO]') $*"; }
|
||||
function Warn { echo -e "$(date +'[%F %T WARN]') $*"; }
|
||||
function Error { echo -e "$(date +'[%F %T ERROR]') $*"; exit 1; }
|
||||
fi
|
||||
|
||||
function Quit {
|
||||
local exitCode=$?
|
||||
[ 0 -ne $exitCode ] && Error Failed to build or push image!
|
||||
[ -z "${END:-}" ] && echo && Error Interrupted manually!
|
||||
Print Succeeded to build and push image.
|
||||
}
|
||||
|
||||
function YesOrNo {
|
||||
Warn $*
|
||||
local sw=
|
||||
while :; do
|
||||
read -p '(Yes/No/Quit) ' -n1 sw
|
||||
[[ "$sw" =~ ^Y|y$ ]] && echo && return 0
|
||||
[[ "$sw" =~ ^N|n$ ]] && echo && return 1
|
||||
[[ "$sw" =~ ^Q|q$ ]] && echo && exit 0
|
||||
[ -n "$sw" ] && echo
|
||||
done
|
||||
}
|
||||
|
||||
function Update {
|
||||
:
|
||||
}
|
||||
|
||||
function Build {
|
||||
local yn
|
||||
cd $ROOT_DIR
|
||||
docker images --format='{{.Repository}}:{{.Tag}}' | grep "^$IMAGE$" \
|
||||
&& Warn Removing image $IMAGE ... \
|
||||
&& docker rmi $IMAGE
|
||||
Warn Building image: $IMAGE ...
|
||||
docker build --force-rm --build-arg ARCH="$ARCH" -t $IMAGE .
|
||||
YesOrNo Push image: $IMAGE? && docker push $IMAGE
|
||||
}
|
||||
|
||||
function Main {
|
||||
Update
|
||||
Build
|
||||
END=1
|
||||
}
|
||||
|
||||
# Start here
|
||||
Main
|
||||
|
Reference in New Issue
Block a user