redis-docker-single-machine master-slave sentinel deployment
1. redis stand-alone
Adjust system parameters
sudo vi /etc/rc.local
加入
echo never > /sys/kernel/mm/transparent_hugepage/enabled
sudo vi /etc/sysctl.conf
加入
vm.overcommit_memory = 1
Create redis-related folders in the user directory
mkdir -p /home/jun/docker/redis
cd /home/jun/docker/redis
创建三个目录
mkdir shell #构建、启动、停止等脚本
mkdir dockerfile #dockerfile目录
mkdir volumes #挂载配置、日志、数据文件
Prepare dockerfile
cd dockerfile
vim Dockerfile
Dockerfile contents
FROM centos:7.7.1908
# 创建者
MAINTAINER hbj
run yum install -y wget install make gcc libgcc gcc-c++ glibc-devel make && mkdir -p /docker/redis/{install,conf,data,log} && cd /docker/redis/install && wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" && tar xvzf redis.tar.gz && cd redis-5.0.3 && make && make install PREFIX=/docker/redis && rm -rf /docker/redis/install/*.gz
USER hd
EXPOSE 6379
CMD ["/docker/redis/bin/redis-server","/docker/redis/conf/6379.conf"]
Build image through dockerfile
cd /home/hd/docker/redis/dockerfile
vim Dockerfile
Dockerfile contents
FROM centos:7.7.1908
# 创建者
MAINTAINER hbj
run yum install -y wget install make gcc libgcc gcc-c++ glibc-devel make && mkdir -p /docker/redis/{install,conf,data,log} && cd /docker/redis/install && wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" && tar xvzf redis.tar.gz && cd redis-5.0.3 && make && make install PREFIX=/docker/redis && useradd hd && chown -R hd:hd /docker/redis && rm -rf /docker/redis/install/*.gz
USER hd
EXPOSE 6379
CMD ["/docker/redis/bin/redis-server","/docker/redis/conf/6379.conf"]
Build image through dockerfile
Create a shell script file
build script: build docker image
vi build
#!/bin/bash
DOCKER_PATH=$PWD/../
sudo docker build -f $DOCKER_PATH/dockerfile/Dockerfile -t redis:v1 $DOCKER_PATH/dockerfile/
run script
vi run: Build and start the docker container
#!/bin/sh
DOCKER_PATH=$PWD/../
port=6379
version=v1
imageName=redis
sudo docker run -d -p ${port}:6379 --name redis -v /etc/localtime:/etc/localtime -v $DOCKER_PATH/volumes/conf:/docker/redis/conf -v $DOCKER_PATH/volumes/data:/docker/redis/data -v $DOCKER_PATH/volumes/log:/docker/redis/log --restart=unless-stopped ${imageName}:${version} /docker/redis/bin/redis-server /docker/redis/conf/${port}.conf
exec script
vi exec: Enter the docker container that has been successfully started
#!/bin/sh
sudo docker exec -it redis /bin/bash
logs script
vi logs: Docker container log When starting the container fails, you can execute the query log
#!/bin/bash
sudo docker logs redis
restart
vi restart: docker container restart
#!/bin/sh
sudo docker restart redis
rm
vi rm: delete docker container
#!/bin/sh
sudo docker rm redis
rmi
vi rmi: Delete the docker image and it needs to be executed only when the dockerfile file is modified.
#!/bin/sh
sudo docker rmi redis:v1
start
vi start: start docker container
#!/bin/sh
sudo docker start redis
stop
vi stop: stop docker container
#!/bin/sh
sudo docker stop redis
The docker script execution sequence is as follows:
- If there is no built image when it is new, you need to build -> run
- If the run fails, you can use logs to query the logs
- If it fails, restart the container (without changing the dockerfile) and then stop-》rm->run
- If the dockerfile is modified and it is started, stop->rm->rmi->build->run
- If you just restart without modifying the file, restart or stop-》start
3.4 redis configuration file
logfile /docker/redis/log/6379.log
dir /docker/redis/data/
# requirepass 123456
daemonize no
#bind 127.0.0.1
Place the configuration file in volumes/conf/6379.conf in the same directory as the run script mounting directory.
Start and view
First execute ./build -> run
View docker container status
sudo docker ps -a
View log
./logs
或者
sudo docker logs redis
Permission settings:
文件权限
chown -R hd:hd /home/hd/docker-files/redis
可执行文件
chmod +x /home/hd/docker-files/redis/shell/*
2. redis master-slave mode
i.安装主从节点redis服务
分别安装和配置主节点redis服务和从节点redis服务,方法参见单机模式的安装和配置
1、从节点的redis服务需要安装在6380端口,安装redis在不同端口 (如果在不同的服务器,大可不必)
2、主从节点必须设置访问密码 ,不要设置也可以。。。但基本都会设置
requirepass 123456
-
Adjust slave node redis configuration
Open the configuration file 6379.conf
sudo vi /home/hd/redis/volumes/conf/6379.conf
Find the following line
# replicaof <masterip> <masterport>
Modify to (where 127.0.0.1 and 6379 should be set to the IP and actual port of the redis master node according to the actual situation, here are the local and default ports)
replicaof 127.0.0.1 6379
The master node and slave node have set access passwords, and the slave nodes need to be configured as follows respectively.
Find the following line
# masterauth <master-password>
Modify it to (123456 is assumed to be the access password and needs to be modified according to the actual situation)
masterauth 123456
Restart the redis service of the master-slave node
verify
进入master数据库,写入一个数据,再进入一个slave数据库,立即便可访问刚才写入master数据库的数据。如下所示
主:redis-cli
> auth 123456
> set site blog.jboost
> get site
> info replication
从:redis-cli
>auth 123456
> get site
Five redis sentinel configuration
Others are the same as redis. You can refer to the stand-alone deployment of Redis.
Only the script port and configuration file are different, but the script build is the same because the image is the same, but the container is different.
The port numbers are 26379 26380 26381
vi run changes the port number and docker name startup method
#!/bin/sh
DOCKER_PATH=$PWD/../
port=26379
version=v1
imageName=redis
sudo docker run -d -p ${port}:26379 --name redis-sentine-1 -v /etc/localtime:/etc/localtime -v $DOCKER_PATH/volumes/conf:/docker/redis/conf -v $DOCKER_PATH/volumes/data:/docker/redis/data -v $DOCKER_PATH/volumes/log:/docker/redis/log --restart=unless-stopped ${imageName}:${version} /docker/redis/bin/redis-server /docker/redis/conf/${port}.conf --sentinel
exec
vi exec
#!/bin/sh
sudo docker exec -it redis-sentine-1 /bin/bash
logs script
vi window
#!/bin/bash
sudo docker logs redis-sentine-1
restart
we restart
#!/bin/sh
sudo docker restart redis-sentine-1
rm
vi rm
#!/bin/sh
sudo docker rm redis-sentine-1
rmi
I'm sorry
#!/bin/sh
sudo docker rmi redis:v1
start
we start
#!/bin/sh
sudo docker start redis-sentine-1
stop
we stop
#!/bin/sh
sudo docker stop redis-sentine-1
Sentinel configuration file
Modify the sentinel monitor to (127.0.0.1 and 6379 should be set to the IP and actual port of the redis master node according to the actual situation, here are the local machine and the default port) without a password
1. Key point: If the sentinel wants to switch, the master-slave configuration needs to configure a password, not just the slave machine
2. Key point: There will be a problem in docker deployment. Sentinel communication problem, you need to configure sentinel announce-ip 192.212.8.117 This is Communication address, otherwise the default is docker internal network card
sentinel announce-ip 10.212.8.117
port 26381
daemonize no
pidfile /var/run/redis-sentinel.pid
logfile /docker/redis/log/sentinel.log
dir /docker/redis/data/
sentinel monitor appmaster 192.212.8.114 6379 2
sentinel down-after-milliseconds appmaster 10000
sentinel failover-timeout appmaster 180000
sentinel parallel-syncs appmaster 1
sentinel auth-pass appmaster 123456
bind 0.0.0.0
Check
Execute the exec script in the shell folder to enter the container
./exec
cd docker/redis/bin
./redis-cli -p 26379
127.0.0.1:26379> info
127.0.0.1:26379>sentinel master appmaster
127.0.0.1:26379>sentinel slaves appmaster