Install Redis

Execute the command

docker pull redisCopy the code

Deployment plan

My official group click here.

Start three machines, with 6379 containers as the master node and the rest as slave nodes

Start preparations


The directory structure

├ ─ ─ the conf │ ├ ─ ─ redis_6379. Conf │ ├ ─ ─ redis_6380. Conf │ ├ ─ ─ redis_6381. Conf │ ├ ─ ─ sentinel_26379. Conf │ ├ ─ ─ Sentinel_26380. Conf │ └ ─ ─ sentinel_26381. Conf ├ ─ ─ data_6379 │ ├ ─ ─ appendonly. Aof │ └ ─ ─ dump. The RDB ├ ─ ─ data_6380 │ └ ─ ─ ├── ├─ ├─ runCopy the code

Conf configuration file

Download the configuration file and adjust the configuration separately

https://raw.githubusercontent.com/antirez/redis/5.0/redis.confCopy the code

Sentinel_.conf configuration file

port 26379
dir "/etc/redis"Sentinel monitor myMaster 172.17.0.2 6379 1 Sentinel Down-after-milliseconds myMaster 60000 Sentinel Failover -timeout mymaster 180000Copy the code

Sh Start script file

#! /usr/bin/env bash
set -e

The current directory of the script
cPath=$(cd $(dirname "$0") | |exit; pwd)

# the root directory
dirPath=$(dirname "$cPath")

# fetch port
port="The $1"
if[[!"$port"]].then
  port=6379
fi

Create a data directory
mkdir -p "$dirPath"/data_"$port"

Delete the started service
containerId=$(docker ps -a | grep "redis_$port" | awk -F' ' '{print $1}')
if [[ "$containerId"]].then
    echo "Deleting service:${containerId}"
    docker rm -f ${containerId} > /dev/null
fi

# start service
docker run -itd --privileged=true -p "$port":6379 --name redis_"$port" \
-v="$dirPath"/conf/redis_"$port".conf:/etc/redis/redis.conf \
-v="$dirPath"/data_"$port":/data \
redis \
redis-server /etc/redis/redis.confCopy the code

Sentinel. sh Startup script file

#! /usr/bin/env bash
set -e

The current directory of the script
cPath=$(cd $(dirname "$0") | |exit; pwd)

# the root directory
dirPath=$(dirname "$cPath")

# set port
port=The $1
if[[!"$port"]].then
  port=26379
fi

Delete the started service
containerId=$(docker ps -a | grep "sentinel_$port" | awk -F' ' '{print $1}')
if [[ "$containerId"]].then
    echo "Deleting service:${containerId}"
    docker rm -f ${containerId} > /dev/null
fi

# start service
docker run -itd --privileged=true -p "$port":26379 --name "sentinel_$port" \
-v="${dirPath}/conf/sentinel_${port}.conf":/etc/redis/sentinel.conf \
-w=/etc/redis/ \
redis \
redis-server /etc/redis/sentinel.conf --sentinel
Copy the code

Pay attention to

Start sentinel service if following error

1:X 10 May 2020 08:10:48.234 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:X 10 May 2020 08:10:48.235 # Sentinel config file /etc/redis/sentinel.conf is not writable: Permission denied. Exiting...

Copy the code

The solution

  • Add == to docker run-w=/etc/redis/==, see for detailssentinel.shThe script
  • Modifying Directory Permissions
docker run -it --rm --privileged=true -v=/root/docker-config/redis/conf/:/etc/redis/ redis chown -R redis /etc/redis

Copy the code

Chown -r redis /etc/redis==



View the modified permission

root@DESKTOP-Q13EI52:~/docker-config/redis# docker run -it --rm --privileged=true -v=/root/docker-config/redis/conf/:/etc/redis/ redis ls -l /etc/redis/
total 204
-rw-r--r-- 1 redis root 61793 May 10 07:41 redis_6379.conf
-rw-r--r-- 1 redis root 61864 May 10 12:52 redis_6380.conf
-rw-r--r-- 1 redis root 61890 May 10 12:52 redis_6381.conf
-rw-r--r-- 1 redis root   459 May 10 12:52 sentinel_26379.conf
-rw-r--r-- 1 redis root   161 May 10 07:56 sentinel_26380.conf
-rw-r--r-- 1 redis root   161 May 10 10:16 sentinel_26381.conf

Copy the code

Start the Redis service


Starting the Primary Service

sh scripts/run.sh 6379Copy the code

Start slave service

sh scripts/run.sh 6380
sh scripts/run.sh 6381Copy the code

Enter slave service

docker exec -it redis_6380 bash
docker exec -it redis_6381 bash
Run the command after entering the container
redis-cli

Set to slaveSlaveof 172.17.0.2 6379Copy the code

rendering




Starting Sentinel service

sh scripts/sentinel.shCopy the code

rendering




Manually stop the active service

  • The current machine service structure is as follows
root@DESKTOP-Q13EI52:~/docker-config/redis# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                NAMES
e518580ce8eb        redis               "Docker - entrypoint. S..."About a minute ago Up About a minute 6379/ TCP, 0.0.0.00:26379 ->26379/ TCP sentinel_26379 e2EF253D3513 redis"Docker - entrypoint. S..."2 hours ago Up 2 hours 0.0.0.0:6381->6379/ TCP redis_6381 15fdc81eb530 redis"Docker - entrypoint. S..."2 hours ago Up 2 hours 0.0.0.0:6380->6379/ TCP redis_6380 77f4fdc84cb1 redis"Docker - entrypoint. S..."2 hours ago Up 2 hours 0.0.0.0:6379->6379/ TCP redis_6379Copy the code

Run docker stop redis_6379

1:X 10 May 2020 12:52:15.634 # +sdown master myMaster 172.17.0.26379
1:X 10 May 2020 12:52:15.634 # +odown master mymaster 172.17.0.26379 #quorum 1/1
1:X 10 May 2020 12:52:15.634 # +new-epoch 1
1:X 10 May 2020 12:52:15.634 # +try-failover master mymaster 172.17.0.26379
1:X 10 May 2020 12:52:15.649 # +vote-for-leader 6905a137af7baedbdfce4978d1d9126fdaad4faf 1
1:X 10 May 2020 12:52:15.649 # + election-leader master myMaster 172.17.0.26379
1:X 10 May 2020 12:52:15.649 # + fail-state-select-slave master mymaster 172.17.0.26379
1:X 10 May 2020 12:52:15.725 # +selected-slave slave 172.17.0.3:6379 172.17.0.36379 @mymaster 172.17.0.263791:X 10 May 2020 12:52:15.725 * + Fail-state-send-slaveof -noone slave 172.17.0.3:6379 172.17.0.36379@mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:15.781 * + Fail-state-wait-promotion slave 172.17.0.3:6379 172.17.0.36379@ Mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:16.798# +promoted-slave slave 172.17.0.3:6379 172.17.0.3 6379 @ mymaster 172.17.0.2 6379
1:X 10 May 2020 12:52:16.798 # + Fail-state-reconf-Slaves Master MyMaster 172.17.0.263791:X 10 May 2020 12:52:16.845 * +slave-reconf-sent slave 172.17.0.4:6379 172.17.0.4 6379 @mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:17.837 * +slave-reconf-inprog slave 172.17.0.4:6379 172.17.0.4 6379 @mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:17.837 * +slave-reconf-done slave 172.17.0.4:6379 172.17.0.4 6379 @mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:17. 937# + fail-end master mymaster 172.17.0.26379

Build to perform failover
1:X 10 May 2020 12:52:17.937 # +switch-master mymaster 172.17.0.2 6379 172.17.0.3 63791:X 10 May 2020 12:52:17.938 * +slave slave 172.17.0.4:6379 172.17.0.4 6379 @mymaster 172.17.0.3 6379 1:X 10 May 2020 12:52:17.938 * +slave slave 172.17.0.2:6379 172.17.0.2 6379 @ mymaster 172.17.0.3 6379 1:X 10 May 2020 12:53:18.010# +sdown slave 172.17.0.2:6379 172.17.0.2 6379 @mymaster 172.17.0.36379

Copy the code

Pay attention to

The primary 172.17.0.2:6379 node that has been offline is automatically switched to the secondary service after it comes online again

Docker quick build master/Slave + Sentinel monitoring

To learn more, please visit:

Tencent T3-T4 standard boutique PHP architect tutorial directory directory, as long as you finish the guarantee salary rise a step (continue to update)

zhuanlan.zhihu.com

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc.