通过ansible+docker-compose快速安装一主两从redis+三sentinel
目录
github项目地址
示例主机列表
架构参考
安装前
安装脚本
修改ansible变量文件
修改ansible配置文件和主机清单
修改setup.sh文件
运行方式
验证故障转移master
github项目地址
https://github.com/sulibao/ansible_redis_sentinel.git
示例主机列表
架构参考
安装前
安装脚本
#!/bin/bashset -e
export path=`pwd`
export capath="/opt/.certs"
export docker_data=$(awk -F': ' '/docker_data_dir:/ {print $2}' group_vars/all.yml)
export ansible_log_dir="$path/log"
export ansible_image_url_x86="registry.cn-chengdu.aliyuncs.com/su03/ansible:latest"
export ansible_image_url_arm="registry.cn-chengdu.aliyuncs.com/su03/ansible-arm:latest"
export docker_package_url_x86="https://su-package.oss-cn-chengdu.aliyuncs.com/docker/amd/docker-27.2.0.tgz"
export docker_package_url_arm="https://su-package.oss-cn-chengdu.aliyuncs.com/docker/arm64/docker-27.2.0.tgz"
export target_file_x86="$path/packages/docker/x86/docker-27.2.0.tgz"
export target_file_arm="$path/packages/docker/arm64/docker-27.2.0.tgz"
export target_docker_filedir_x86="$path/roles/docker/files/x86/docker-27.2.0.tgz"
export target_docker_filedir_arm="$path/roles/docker/files/arm64/docker-27.2.0.tgz"
export ssh_pass="sulibao"
export os_arch=$(uname -m)function get_arch_package() {if [ -f /etc/redhat-release ]; thenOS="RedHat"elif [ -f /etc/kylin-release ]; thenOS="kylin"elseecho "Unknow linux distribution."fiif [[ "$os_arch" == "x86_64" ]]; thenARCH="x86"echo -e "Detected Operating System: $OS, Architecture:X86"mkdir -p $ansible_log_dirif [ -f "$target_file_x86" ]; thenecho "The file $target_file_x86 already exists, skip download."elsemkdir -p "$(dirname "$target_file_x86")" && \mkdir -p "$(dirname "$target_docker_filedir_x86")"curl -C - -o "$target_file_x86" "$docker_package_url_x86"if [ $? -eq 0 ]; thenecho "The file downloaded successfully."elseecho "Failed to download the file."fifielif [[ "$os_arch" == "aarch64" ]]; thenARCH="arm64"echo -e "Detected Operating System: $OS, Architecture: ARM64"mkdir -p $ansible_log_dirif [ -f "$target_file_arm" ]; thenecho "The file $target_file_arm already exists, skip download."elsemkdir -p "$(dirname "$target_file_arm")" && \mkdir -p "$(dirname "$target_docker_filedir_arm")"curl -C - -o "$target_file_arm" "$docker_package_url_arm"if [ $? -eq 0 ]; thenecho "The file downloaded successfully."elseecho "Failed to download the file."fifielseecho -e "Unsupported architecture detected: $os_arch"exit 1fi
}function check_docker() {echo "Make sure docker is installed and running."if ! [ -x "$(command -v docker)" ]; thenecho "docker not find."create_docker_group_and_userinstall_dockerelseecho "docker exists."fiif ! systemctl is-active --quiet docker; thenecho "docker is not running."create_docker_group_and_userinstall_dockerelseecho "docker is running."fi
}function check_docker_compose() {if ! [ -x "$(command -v docker-compose)" ]; thenecho "docker-compose not find."install_docker_compose elseecho "docker-compose exist."fi
}function create_docker_group_and_user() {if ! getent group docker >/dev/null 2>&1; thengroupadd dockerecho "docker group created successfully."elseecho "docker group already exists."fiif ! id -u docker >/dev/null 2>&1; thenuseradd -m -s /bin/bash -g docker dockerecho "docker user has been created and added to docker group."elseecho "docker user already exists."fi
}function install_docker() {echo "Installing docker."if [[ "$ARCH" == "x86" ]]thenexport DOCKER_OFFLINE_PACKAGE=$target_file_x86 && \cp -v -f $target_file_x86 $target_docker_filedir_x86elseexport DOCKER_OFFLINE_PACKAGE=$target_file_arm && \cp -v -f $target_file_arm $target_docker_filedir_armfitar axvf $DOCKER_OFFLINE_PACKAGE -C /usr/bin/ --strip-components=1cp -v -f $path/packages/docker/docker.service /usr/lib/systemd/system/test -d /etc/docker || mkdir -p /etc/dockerenvsubst '$docker_data' < $path/packages/docker/daemon.json > /etc/docker/daemon.jsonsystemctl stop firewalldsystemctl disable firewalldsystemctl daemon-reloadsystemctl enable docker.service --nowsystemctl restart docker || :maxSecond=60for i in $(seq 1 $maxSecond); doif systemctl is-active --quiet docker; thenbreakfisleep 1doneif ((i == maxSecond)); thenecho "Failed to start the docker server, please check the docker start log."exit 1fiecho "Docker has started successfully and the installation is complete."
}function install_docker_compose {echo "Installing docker-compose."if [[ "$ARCH" == "x86" ]]thenexport DOCKER_COMPOSE_OFFLINE_PACKAGE=$path/packages/docker-compose/x86/docker-compose-linux-x86_64cp -v -f $DOCKER_COMPOSE_OFFLINE_PACKAGE /usr/local/bin/docker-compose && \chmod 0755 /usr/local/bin/docker-composeelseexport DOCKER_COMPOSE_OFFLINE_PACKAGE=$path/packages/docker-compose/arm64/docker-compose-linux-aarch64cp -v -f $DOCKER_COMPOSE_OFFLINE_PACKAGE /usr/local/bin/docker-compose && \chmod 0755 /usr/local/bin/docker-composefi
}function pull_ansible_image() {if [[ "$ARCH" == "x86" ]]thendocker pull "$ansible_image_url_x86"elsedocker pull "$ansible_image_url_arm"fiecho -e "Pulled ansible image."
}function ensure_ansible() {echo -e "Checking the status of the ansible."if test -z "$(docker ps -a | grep ansible_sulibao)"; thenecho -e "Ansible is not running, will run."run_ansibleelseecho -e "Ansible is running, will restart."docker restart ansible_sulibaofi
}function run_ansible() {echo -e "Installing Ansible container."if [[ "$ARCH" == "x86" ]]thendocker run --name ansible_sulibao --network="host" --workdir=$path -d -e LANG=C.UTF-8 -e ssh_password=$ssh_pass --restart=always -v /etc/localtime:/etc/localtime:ro -v ~/.ssh:/root/.ssh -v $path:$path -v "$capath":"$capath" "$ansible_image_url_x86" sleep 31536000elsedocker run --name ansible_sulibao --network="host" --workdir=$path -d -e LANG=C.UTF-8 -e ssh_password=$ssh_pass --restart=always -v /etc/localtime:/etc/localtime:ro -v ~/.ssh:/root/.ssh -v $path:$path -v "$capath":"$capath" "$ansible_image_url_arm" sleep 31536000fiecho -e "Installed Ansible container."
}function create_ssh_key(){echo -e "Creating sshkey."docker exec -i ansible_sulibao /bin/sh -c 'echo -e "y\n"|ssh-keygen -t rsa -N "" -C "deploy@ansible_redis_sentinel" -f ~/.ssh/id_rsa_ansible_redis -q'echo -e "\nCreated sshkey."}function copy_ssh_key() {echo -e "Copying sshkey."docker exec -i ansible_sulibao /bin/sh -c "cd $path && ansible-playbook ssh-access.yml -e ansible_ssh_pass=$ssh_pass" echo -e "\nCopied sshkey."
}function install_docker_slave() {echo -e "Installing docker for other nodes."docker exec -i ansible_sulibao /bin/sh -c "cd $path && ansible-playbook ./docker.yml"echo -e "\nInstalled docker for other nodes."
}function install_redis() {echo -e "Install redis."docker exec -i ansible_sulibao /bin/sh -c "cd $path && ansible-playbook ./redis.yml"echo -e "\nInstalled redis."
}get_arch_package
check_docker
check_docker_compose
pull_ansible_image
ensure_ansible
create_ssh_key
copy_ssh_key
install_docker_slave
install_redis
修改ansible变量文件
vim group_vars/all.ymldocker_data_dir: /app/docker_data #安装的docker数据目录
data_dir: /app #存放redis文件的数据目录
redis_sentinel_port: 26379 #sentinel端口
redis_pass: "sulibao" #redis认证密码
image_redis: "registry.cn-chengdu.aliyuncs.com/su03/redis:7.2.7" #redis和sentinel使用的镜像
修改ansible配置文件和主机清单
[root@test1 redis_data]# cat ansible.cfg
[defaults]
inventory=./hosts
remote_user = root
transport= ssh
remote_port = 22
private_key_file= /root/.ssh/id_rsa_ansible_redis
log_path = ./log/ansible.log
stdout_callback=debug
host_key_checking=false
command_warnings=False
fact_caching_connections=/tmp/ansible_facts
fact_caching_timeout=86400
gathering=smart
pipelining=True
deprecation_warnings = False[ssh_connection]
ssh_args=-o ControlMaster=auto -o ControlPersist=60s
[root@test1 redis_data]# cat hosts
[redis_master] #初始master的地址
192.168.2.190
[redis_slave1] #初始slave1的地址
192.168.2.191
[redis_slave2] #初始slave2的地址
192.168.2.192[redis_slave:children]
redis_slave1
redis_slave2[redis:children]
redis_master
redis_slave1
redis_slave2
修改setup.sh文件
vim setup.sh
export ssh_pass="sulibao" #此项应为服务器root用户密码
运行方式
bash setup.sh
验证故障转移master
#初始集群信息,test1为master,test2、test3为slave
docker exec -it redis-master bash
root@test1:/data# redis-cli -a sulibao role
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1) "master"
2) (integer) 35726
3) 1) 1) "192.168.2.191"2) "6379"3) "35726"2) 1) "192.168.2.192"2) "6379"3) "35585"#模拟master(test1)挂机,出现新master(test2),test3仍为slave
[root@test1 redis_data]# docker stop redis-master
redis-master
[root@test2 ~]# docker exec -it redis-slave1 bash
root@test2:/data# redis-cli -a sulibao role
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1) "master"
2) (integer) 68953
3) 1) 1) "192.168.2.192"2) "6379"3) "68953"#旧master(test1)恢复,成为slave角色。此时master为test2,test1、test3为slave
[root@test1 redis_data]# docker start redis-master
redis-master
root@test2:/data# redis-cli -a sulibao role
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1) "master"
2) (integer) 87291
3) 1) 1) "192.168.2.192"2) "6379"3) "87291"2) 1) "192.168.2.190"2) "6379"3) "87291"