0.剧本说明
自动安装部署redis-5.x集群
只是创建和启动,集群初始化命令需要手动执行
1.目录结构
[root@m01 ~]# tree /etc/ansible/roles/redis_cluster/
/etc/ansible/roles/redis_cluster/
├── files
│ ├── redis_6380
│ │ ├── conf
│ │ ├── logs
│ │ └── pid
│ ├── redis_6381
│ │ ├── conf
│ │ ├── logs
│ │ └── pid
│ └── redis_cmd
│ ├── redis-benchmark
│ ├── redis-check-aof
│ ├── redis-check-rdb
│ ├── redis-cli
│ └── redis-server
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
│ ├── redis_6380.conf.j2
│ ├── redis_6381.conf.j2
│ ├── redis-master.service.j2
│ └── redis-slave.service.j2
└── vars
14 directories, 11 files
2.配置文件jinja模版
主节点配置模版
[root@m01 ~]# cat /etc/ansible/roles/redis_cluster/templates/redis_6380.conf.j2
bind {{ ansible_facts.eth0.ipv4.address }}
port 6380
daemonize yes
pidfile "/opt/redis_6380/pid/redis_6380.pid"
logfile "/opt/redis_6380/logs/redis_6380.log"
dbfilename "redis_6380.rdb"
dir "/data/redis_6380/"
appendonly yes
appendfilename "redis.aof"
appendfsync everysec
cluster-enabled yes
cluster-config-file nodes_6380.conf
cluster-node-timeout 15000
从节点配置模版
[root@m01 ~]# cat /etc/ansible/roles/redis_cluster/templates/redis_6381.conf.j2
bind {{ ansible_facts.eth0.ipv4.address }}
port 6381
daemonize yes
pidfile "/opt/redis_6381/pid/redis_6381.pid"
logfile "/opt/redis_6381/logs/redis_6381.log"
dbfilename "redis_6381.rdb"
dir "/data/redis_6381/"
appendonly yes
appendfilename "redis.aof"
appendfsync everysec
cluster-enabled yes
cluster-config-file nodes_6381.conf
cluster-node-timeout 15000
3.启动脚本
主节点启动脚本
[root@m01 ~]# cat /etc/ansible/roles/redis_cluster/templates/redis-master.service.j2
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/redis-server /opt/redis_6380/conf/redis_6380.conf --supervised systemd
ExecStop=/usr/local/bin/redis-cli -h {{ ansible_facts.eth0.ipv4.address }} -p 6380 shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
从节点启动脚本
[root@m01 ~]# cat /etc/ansible/roles/redis_cluster/templates/redis-slave.service.j2
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/redis-server /opt/redis_6381/conf/redis_6381.conf --supervised systemd
ExecStop=/usr/local/bin/redis-cli -h {{ ansible_facts.eth0.ipv4.address }} -p 6381 shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
4.tasks清单
[root@m01 ~]# cat /etc/ansible/roles/redis_cluster/tasks/main.yaml
#01.创建用户组
- name: 01_create_group
group:
name: redis
gid: 777
#02.创建用户
- name: 02_create_user
user:
name: redis
uid: 777
group: redis
shell: /sbin/nologin
create_home: no
#03.拷贝执行文件
- name: 03_copy_cmd
copy:
src: redis_cmd/
dest: /usr/local/bin/
mode: '0755'
#04.拷贝运行目录
- name: 04_mkdir_conf
copy:
src: "{{ item }}"
dest: /opt/
owner: redis
group: redis
loop:
- redis_6380
- redis_6381
#05.创建数据目录
- name: 05_mkdir_data
file:
dest: "/data/{{ item }}"
state: directory
owner: redis
group: redis
loop:
- redis_6380
- redis_6381
#06.拷贝配置文件模版
- name: 06_copy_conf
template:
src: "{{ item.src}}"
dest: "{{ item.dest }}"
backup: yes
loop:
- { src: 'redis_6380.conf.j2', dest: '/opt/redis_6380/conf/redis_6380.conf' }
- { src: 'redis_6381.conf.j2', dest: '/opt/redis_6381/conf/redis_6381.conf' }
- { src: 'redis-master.service.j2', dest: '/usr/lib/systemd/system/redis-master.service' }
- { src: 'redis-slave.service.j2', dest: '/usr/lib/systemd/system/redis-slave.service' }
notify:
- restart redis-master
- restart redis-slave
#07.启动服务
- name: 07_start_redis
systemd:
name: "{{ item }}"
state: started
daemon_reload: yes
loop:
- redis-master
- redis-slave
5.handles清单
[root@m01 ~]# cat /etc/ansible/roles/redis_cluster/handlers/main.yaml
- name: restart redis-master
service:
name: redis-master
state: restarted
- name: restart redis-slave
service:
name: redis-slave
state: restarted
6.执行任务清单
[root@m01 ~]# cat /etc/ansible/redis_cluster.yaml
- hosts: redis_cluster
roles:
- redis_cluster