使用 Ansible Playbook 部署 Redis 一主两从三哨兵架构
我们将使用 Ansible Playbook 自动化部署 Redis 集群,包括一个主节点(Master)和两个从节点(Slave),以及三个哨兵节点(Sentinel)。
三台服务器:Node-1、Node-2、Node-3,其中 Node-1 将作为 Redis 主节点,Node-2 和 Node-3 将作为 Redis 从节点,三台都作为哨兵节点。
目录结构
├── templates
│ ├── redis.conf.j2
│ ├── redis-slave.conf.j2
│ ├── sentinel.conf.j2
├── roles
│ ├── common
│ │ ├── tasks
│ │ │ └── main.yml
│ ├── redis-master
│ │ ├── tasks
│ │ │ └── main.yml
│ ├── redis-slave
│ │ ├── tasks
│ │ │ └── main.yml
│ ├── redis-sentinel
│ │ ├── tasks
│ │ │ └── main.yml
├── deploy-redis.yml
手动创建目录和文件
1.创建主目录结构
cd /etc/ansible
touch ansible.cfg
touch inventory
cd /root/
mkdir -p ansible-redis-playbook
cd ansible-redis-playbook
mkdir templates
mkdir -p roles/{common,redis-master,redis-slave,redis-sentinel}/tasks
touch deploy-redis.yml
2.编辑 ansible.cfg
确保 ansible.cfg
文件内容正确,用于全局设置
cat <<EOF > /etc/ansible/ansible.cfg
[defaults]
inventory = ./inventory
remote_user = root
host_key_checking = False
EOF
3.编辑 inventory
确保 inventory
文件内容正确,用于hosts配置
vim /etc/ansible/inventory
[redis-master]
Node-1
[redis-slave]
Node-2
Node-3
[redis-sentinel]
Node-1
Node-2
Node-3
4.编辑 roles/common/tasks/main.yml
所有角色都要执行安装Redis。
cd /root/ansible-redis-playbook
vim roles/common/tasks/main.yml
- name: Install Redis dependencies
apt:
name: "{{ item }}"
state: present
loop:
- redis-server
- redis-tools
- redis-sentinel
5.编辑 roles/redis-master/tasks/main.yml
确保 redis-master
角色的任务文件内容正确:
vim roles/redis-master/tasks/main.yml
- name: Configure Redis master
template:
src: templates/redis.conf.j2
dest: /etc/redis/redis.conf
owner: redis
group: redis
mode: '0644'
- name: Enable and start Redis service
systemd:
name: redis-server
enabled: yes
state: started
6.编辑 roles/redis-slave/tasks/main.yml
确保 redis-slave
角色的任务文件内容正确:
vim roles/redis-slave/tasks/main.yml
- name: Configure Redis slave
template:
src: templates/redis-slave.conf.j2
dest: /etc/redis/redis.conf
owner: redis
group: redis
mode: '0644'
- name: Enable and start Redis service
systemd:
name: redis-server
enabled: yes
state: started
7.编辑 roles/redis-sentinel/tasks/main.yml
确保 redis-sentinel
角色的任务文件内容正确:
vim roles/redis-sentinel/tasks/main.yml
- name: Configure Redis sentinel
template:
src: templates/sentinel.conf.j2
dest: /etc/redis/sentinel.conf
owner: redis
group: redis
mode: '0644'
- name: Enable and start Redis sentinel service
systemd:
name: redis-sentinel
enabled: yes
state: started
8.编辑配置模板
确保配置模板文件内容正确:
redis.conf.j2(主节点配置)
vim templates/redis.conf.j2
bind {{ ansible_default_ipv4.address }}
protected-mode yes
port 6379
daemonize no
pidfile /var/run/redis/redis-server.pid
logfile /var/log/redis/redis-server.log
dir /var/lib/redis
requirepass {{ redis_password }}
redis-slave.conf.j2(从节点配置)
vim templates/redis-slave.conf.j2
bind {{ ansible_default_ipv4.address }}
protected-mode yes
port 6379
daemonize no
pidfile /var/run/redis/redis-server.pid
logfile /var/log/redis/redis-server.log
dir /var/lib/redis
replicaof {{ master_ip }} 6379
masterauth {{ redis_password }}
requirepass {{ redis_password }}
sentinel.conf.j2(哨兵配置)
vim templates/sentinel.conf.j2
bind {{ ansible_default_ipv4.address }}
port 26379
daemonize no
logfile /var/log/redis/redis-sentinel.log
dir /var/lib/redis
sentinel monitor mymaster {{ master_ip }} 6379 2
sentinel auth-pass mymaster {{ redis_password }}
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
9.验证主 Playbook (deploy-redis.yml)
确保主 Playbook 文件内容正确:
vim deploy-redis.yml
- name: Deploy Redis master-slave-sentinel cluster
hosts: all
become: yes
roles:
- common
- name: Deploy Redis master
hosts: redis-master
become: yes
roles:
- redis-master
vars:
redis_password: "root123"
- name: Deploy Redis slaves
hosts: redis-slave
become: yes
roles:
- redis-slave
vars:
master_ip: "{{ hostvars['Node-1']['ansible_default_ipv4']['address'] }}"
redis_password: "root123"
- name: Deploy Redis sentinels
hosts: redis-sentinel
become: yes
roles:
- redis-sentinel
vars:
master_ip: "{{ hostvars['Node-1']['ansible_default_ipv4']['address'] }}"
redis_password: "root123"
10.运行 Playbook 并验证结果
最后,运行 Playbook 并验证结果:
ansible-playbook deploy-redis.yml
确保 Playbook 成功执行,且 Redis 和 Sentinel 服务正确启动。你可以通过以下命令验证 Redis 集群的状态:
检查 Redis 主节点
redis-cli -h Node-1 -p 6379 -a root123 info replication
检查 Redis 从节点
redis-cli -h Node-2 -p 6379 -a root123 info replication
redis-cli -h Node-3 -p 6379 -a root123 info replication
检查哨兵节点
redis-cli -h Node-1 -p 26379 -a root123 info sentinel
redis-cli -h Node-2 -p 26379 -a root123 info sentinel
redis-cli -h Node-3 -p 26379 -a root123 info sentinel
总结
确保每个部分的配置文件和任务文件都没有错误,并且 Playbook 能够成功执行。如果有任何问题,可以根据错误信息进行调试和修复。