Ansible 部署 Redis 主从 一、安装ansible服务工具
在安装服务之前先添加epel的yum源,因为ansible的安装依赖epel源
1 2 3 curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum makecache yum -y install ansible
ansible作为一款自动化软件工具,不需要修改配置文件就可以直接启动 ansible的目录结构:
1 2 3 /etc/ansible/ansible.cfg --- ansible服务配置文件 /etc/ansible/hosts --- 主机清单文件 定义可以管理的主机信息 /etc/ansible/roles --- 角色目录
二、创建roles角色目录 官方地址:
1 https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html
官方的目录定义:
1 2 3 4 5 6 7 8 [root@master1 /etc/ansible/roles] . ├── nfs │ ├── files │ ├── handlers │ ├── tasks │ ├── templates │ └── vars
1.创建目录 根据官方的目录规划,依次创建目录:
1 2 3 4 5 6 mkdir redis_cluster cd redis_cluster/mkdir files handlers tasks templates vars cd files/mkdir redis_{63{79,80},cmd} mkdir redis_63{79,80}/{conf,logs,pid}
2.将redis的二进制文件复制到redis_cmd目录下 1 2 3 4 5 6 7 8 9 mkdir /opt/cluster/redis_cluster/src cd /opt/cluster/redis_cluster/srcwget http://download.redis.io/releases/redis-5.0.7.tar.gz tar xf redis-5.0.7.tar.gz -C /opt/cluster/redis_cluster/ cd /opt/cluster/redis_cluster/redis-5.0.7/src/make cp -a redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server /etc/ansible/roles/redis_cluster/files/redis_cmd/
3.生成ssh免交互 1 2 3 4 5 6 7 ssh-keygen 使用 ssh-copy-id username@your-server-ip 命令
4.编写主机清单 1 2 3 4 vim /etc/ansible/hosts ... [redis-cluster] 192.168.2.246
5.配置文件jinja模版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 vim /etc/ansible/roles/redis_cluster/templates/redis_6379.conf.j2 bind {{ansible_facts.eth0.ipv4.address}}port 6379 daemonize yes pidfile "/opt/cluster/redis_cluster/redis_6379/pid/redis_6379.pid" logfile "/opt/cluster/redis_cluster/redis_6379/logs/redis_6379.log" dbfilename "redis_6379.rdb" dir "/data/cluster/redis_cluster/redis_6379/" appendonly yes appendfilename "redis.aof" appendfsync everysec cluster-enabled yes cluster-config-file nodes_6379.conf cluster-node-timeout 15000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 vim /etc/ansible/roles/redis_cluster/templates/redis_6380.conf.j2 bind {{ansible_facts.eth0.ipv4.address}}port 6380 daemonize yes pidfile "/opt/cluster/redis_cluster/redis_6380/pid/redis_6380.pid" logfile "/opt/cluster/redis_cluster/redis_6380/logs/redis_6380.log" dbfilename "redis_6380.rdb" dir "/data/cluster/redis_cluster/redis_6380/" appendonly yes appendfilename "redis.aof" appendfsync everysec cluster-enabled yes cluster-config-file nodes_6380.conf cluster-node-timeout 15000
6.配置启动脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 vim /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/cluster/redis_cluster/redis_6379/conf/redis_6379.conf --supervised systemd ExecStop=/usr/local /bin/redis-cli -h {{ansible_facts.eth0.ipv4.address }} -p 6379 shutdown Type=notify User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 [Install] WantedBy=multi-user.target
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 vim /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/cluster/redis_cluster/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
7.tasks清单 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 vim /etc/ansible/roles/redis_cluster/tasks/main.yaml - name: 01 -create_group group: name: redis gid: 777 - name: 02 -create_user user: name: redis group: redis uid: 777 shell: /sbin/nologin create_home: no - name: 03_copy_cmd copy: src: redis_cmd/ dest: /usr/local/bin/ mode: '0755' - name: 04_copy_conf copy: src: "{{ item }} " dest: /opt/cluster/redis_cluster/ owner: redis group: redis loop: - redis_6380 - redis_6379 - name: 05_create_data file: dest: "/data/cluster/redis_cluster/{{ item }} " state: directory owner: redis group: redis loop: - redis_6379 - redis_6380 - name: 06_copy_conf template: src: "{{ item.src }} " dest: "{{ item.dest }} " backup: yes with_items: - { src: 'redis_6379.conf.j2' , dest: '/opt/cluster/redis_cluster/redis_6379/conf/redis_6379.conf' } - { src: 'redis_6380.conf.j2' , dest: '/opt/cluster/redis_cluster/redis_6380/conf/redis_6380.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' } - name: 07_start_redis_service ignore_errors: yes systemd: name: "{{ item }} " state: started daemon_reload: yes loop: - redis-master - redis-slave notify: - restart redis-master - restart redis-slave
8.handles清单 1 2 3 4 5 6 7 8 9 vim /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
9.执行任务清单 1 2 3 4 vim /etc/ansible/redis_cluster.yaml - hosts: redis-cluster roles: - redis_cluster
10.检查剧本语法 1 ansible-playbook --syntax-check /etc/ansible/redis_cluster.yaml
11.模拟执行剧本 1 ansible-playbook -C /etc/ansible/redis_cluster.yaml
12.执行剧本 1 ansible-playbook /etc/ansible/redis_cluster.yaml