通过ansible实现自动化搭建 Redis Cluster 集群环境 Docker 搭建Redis 集群
github地址 https://github.com/lilihongjava/ansible-redis-cluster
docker:Docker version 18.03.0-ce linux:redhat8 ansible:ansible 2.9.23 redis:6.0.7
代码位于:ansible-redis-cluster\roles\redis\tasks\main.yml
inventory文件 设置多台主机,ansible_user为执行的用户名,HN为主机的hostname,ports为redis用的端口
[redis]
10.1.12.214 ansible_user=paasopt HN=x86-nlpass-kafka ports=[8091,8092]
10.1.12.215 ansible_user=paasopt HN=x86-nlpass-kafka ports=[8093,8094]
10.1.12.216 ansible_user=paasopt HN=x86-nlpass-redis ports=[8095,8096]
如果没有那么多台主机,也可以部署在同一台
[redis]
10.1.12.223 ansible_user=paasopt HN=x86-nlpass-kafka ports=[8091,8092,8093,8094,8095,8096]
变量文件,redis_password为redis集群password, redis_dir为执行安装redis的目录
{
"workerspace": "/home/paasopt",
"redis_dir": "{{ workerspace }}/redis-cluster",
"redis_password": "PaasDev2.3"
}
{{redis_password}}为vars_install.json文件里的变量,{{ inventory_hostname }}是Ansible所识别的当前正在运行task的主机的主机名。如果在inventory里定义过别名,那么这里就是那个别名。
port ${PORT}
requirepass {{redis_password}}
masterauth {{redis_password}}
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip {{ inventory_hostname }}
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
ports 来源hosts_install里面的ports 此sh用于创建redis配置文件和数据目录
#!/bin/bash
for port in {{ ports | join(' ')}}; do \
mkdir -p ${port}/conf \
&& PORT=${port} envsubst < redis-cluster.tmpl > ${port}/conf/redis.conf \
&& mkdir -p ${port}/data;\
done
创建 Redis 容器,使用 host 网络模式。
#!/bin/bash
for port in {{ ports | join(' ')}}; do \
docker run -di -p ${port}:${port} -p 1${port}:1${port} --restart always --name redis-${port} --sysctl net.core.somaxconn=1024 \
-v {{ redis_dir }}/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v {{ redis_dir }}/${port}/data:/data \
redis:6.0.7 redis-server /usr/local/etc/redis/redis.conf; \
done
创建 Redis Cluster 集群
#!/bin/bash
sudo docker exec -it redis-{{ ports[0]}} bash -c "/usr/local/bin/redis-cli --cluster create {% for host in groups['redis'] -%} {% for port in hostvars[host].ports %}{{host}}:{{port}} {{" "}}{%- endfor %}{%- endfor %} --cluster-replicas 1 --cluster-yes -a {{redis_password}} "
执行ansible-playbook -i hosts_install -e @vars_install.json setup.yml ,引用主机文件hosts_install ,变量文件vars_install.json
[paasopt@x86-nlpass-elk ansible-redis-cluster]$ ansible-playbook -i hosts_install -e @vars_install.json setup.yml
PLAY [redis] ***********************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
[DEPRECATION WARNING]: Distribution redhat 8.4 on host 10.1.12.223 should use /usr/libexec/platform-python, but is using /usr/bin/python for backward compatibility
with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ok: [10.1.12.223]
TASK [redis : 创建目录] ****************************************************************************************************************************************************
changed: [10.1.12.223]
TASK [复制redis-cluster.tmpl文件] ******************************************************************************************************************************************
changed: [10.1.12.223]
TASK [复制redis-conf-create.sh文件] ****************************************************************************************************************************************
changed: [10.1.12.223]
TASK [复制redis-docker-run.sh文件] *****************************************************************************************************************************************
changed: [10.1.12.223]
TASK [复制redis-cluster-cli.sh文件] ****************************************************************************************************************************************
changed: [10.1.12.223]
TASK [复制redis_6.0.7.tar文件] *********************************************************************************************************************************************
changed: [10.1.12.223]
TASK [安装redis] *********************************************************************************************************************************************************
changed: [10.1.12.223]
TASK [执行redis-conf-create.sh脚本] ****************************************************************************************************************************************
changed: [10.1.12.223]
TASK [执行redis-docker-run.sh脚本] *****************************************************************************************************************************************
changed: [10.1.12.223]
TASK [执行redis-cluster-cli.sh脚本] ****************************************************************************************************************************************
changed: [10.1.12.223]
PLAY RECAP *************************************************************************************************************************************************************
10.1.12.223 : ok=11 changed=10 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
参考: https://xie.infoq.cn/article/a5536b928edd12beb32fcabf9