使用ansible搭建redis集群

  1. 安装ansible

主控端
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install ansible -y
被控端
yum install libselinux-python -y
  1. 编辑hosts文件

[root@kube-master1 ansible]# cat hosts

[node1]

192.168.1.4

[node2]

192.168.1.5

[node0]

192.168.1.3

  1. 工程的目录结构

  1. 启动文件deploy.yml

[root@kube-master1 ansible]# cat deploy.yml 
---
- hosts: all
  remote_user: root
  roles:
    - redis
  1. 全局变量

[root@kube-master1 ansible]# cd group_vars/
[root@kube-master1 group_vars]# ll
total 4
-rw-r--r--. 1 root root 93 Mar  2 15:23 all.yml
[root@kube-master1 group_vars]# cat all.yml 
master_port: 7000
slave_port: 7001
file_name: 'redis-5.0.14'
install_path: '/u01/app/redis/'
[root@kube-master1 group_vars]# 
  1. 源文件

[root@kube-master1 ansible]# ll
total 28
-rw-r--r--. 1 root root 19985 Mar  2 12:40 ansible.cfg
-rw-r--r--. 1 root root    58 Mar  2 15:23 deploy.yml
drwxr-xr-x. 2 root root    20 Mar  2 15:47 group_vars
-rw-r--r--. 1 root root    60 Mar  2 14:57 hosts
drwxr-xr-x. 3 root root    18 Mar  2 15:22 roles
drwxr-xr-x. 2 root root    69 Mar  2 12:40 scripts
[root@kube-master1 ansible]# cd roles
[root@kube-master1 roles]# ll
total 0
drwxr-xr-x. 4 root root 30 Mar  2 15:44 redis
[root@kube-master1 roles]# cd redis
[root@kube-master1 redis]# ll
total 0
drwxr-xr-x. 2 root root 32 Mar  2 15:28 files
drwxr-xr-x. 2 root root 21 Mar  2 15:31 tasks
[root@kube-master1 redis]# cd file
-bash: cd: file: No such file or directory
[root@kube-master1 redis]# cd files
[root@kube-master1 files]# ll
total 1956
-rw-r--r--. 1 root root 2000179 Oct  4  2021 redis-5.0.14.tar.gz
[root@kube-master1 files]# 
  1. 主playbook

[root@kube-master1 redis]# cd tasks
[root@kube-master1 tasks]# ll
total 4
-rw-r--r--. 1 root root 3488 Mar  2 15:31 main.yml
[root@kube-master1 tasks]# cat main.yml 
---
- name: createdir
  file: dest={{ install_path }}/redis/  state=directory
      

- name: copy tar
  copy: src={{ file_name }}.tar.gz  dest=/tmp mode=u+x
  

- name: tarpackage
  unarchive: src=/tmp/{{ file_name }}.tar.gz dest={{ install_path }}/redis/ mode=0755 copy=no 

- name: install redis
  shell: chdir={{ install_path }}/redis/{{ file_name }}/ make && make install

- name: create master conf dir
  file: dest={{ install_path }}/redis/{{ file_name }}/conf/{{ master_port }}/{{ item }} state=directory
  with_items: 
    - log
    - data
    - pid
  
- name: create slave conf dir
  file: dest={{ install_path }}/redis/{{ file_name }}/conf/{{ slave_port }}/{{ item }} state=directory
  with_items: 
    - log
    - data
    - pid

- name: scp redis.conf
  copy: src={{ install_path }}/redis/{{ file_name }}/redis.conf dest={{ install_path }}/redis/{{ file_name }}/conf/{{ item }}/
  with_items:
    - 7000
    - 7001 

- name: modify master config
  replace: path={{ install_path }}/redis/{{ file_name }}/conf/{{ master_port }}/redis.conf regexp={{ item.regexp }} replace={{ item.line }}
  with_items:
    - { regexp: 'bind 127.0.0.1', line: 'bind 0.0.0.0' }
    - { regexp: 'daemonize no', line: 'daemonize yes' }
    - { regexp: 'pidfile /var/run/redis_6379.pid', line: 'pidfile "{{ install_path }}/redis/{{ file_name }}/conf/{{ master_port }}/pid/redis_{{master_port}}.pid"' }
    - { regexp: 'logfile ""', line: 'logfile "{{ install_path }}/redis/{{ file_name }}/conf/{{ master_port }}/log/{{ master_port }}.log"' }
    - { regexp: 'dir ./', line: 'dir "{{ install_path }}/redis/{{ file_name }}/conf/{{ master_port }}/data"' }
    - { regexp: '# cluster-enabled yes', line: 'cluster-enabled yes' } 
    - { regexp: '# cluster-config-file nodes-6379.conf', line: 'cluster-config-file "nodes{{ master_port }}.conf"' }
    - { regexp: '# cluster-node-timeout 15000', line: 'cluster-node-timeout 15000' }
    - { regexp: 'appendonly no', line: 'appendonly yes' }
    - { regexp: '# requirepass foobared', line: 'requirepass redis' }
    - { regexp: 'port 6379', line: 'port {{ master_port }}' }

- name: modify slave config
  replace: path={{ install_path }}/redis/{{ file_name }}/conf/{{ slave_port }}/redis.conf regexp={{ item.regexp }} replace={{ item.line }}
  with_items:
    - { regexp: 'bind 127.0.0.1', line: 'bind 0.0.0.0' }
    - { regexp: 'daemonize no', line: 'daemonize yes' }
    - { regexp: 'pidfile /var/run/redis_6379.pid', line: 'pidfile "{{ install_path }}/redis/{{ file_name }}/conf/{{ slave_port }}/pid/redis_{{slave_port}}.pid"' }
    - { regexp: 'logfile ""', line: 'logfile "{{ install_path }}/redis/{{ file_name }}/conf/{{ slave_port }}/log/{{ slave_port }}.log"' }
    - { regexp: 'dir ./', line: 'dir "{{ install_path }}/redis/{{ file_name }}/conf/{{ slave_port }}/data"' }
    - { regexp: '# cluster-enabled yes', line: 'cluster-enabled yes' } 
    - { regexp: '# cluster-config-file nodes-6379.conf', line: 'cluster-config-file "nodes{{ slave_port }}.conf"' }
    - { regexp: '# cluster-node-timeout 15000', line: 'cluster-node-timeout 15000' }
    - { regexp: 'appendonly no', line: 'appendonly yes' }
    - { regexp: '# requirepass foobared', line: 'requirepass redis' }
    - { regexp: 'port 6379', line: 'port {{ slave_port }}' }

- name: startredis    
  shell: chdir={{ install_path }}/redis/{{ file_name }}/src/ redis-server {{ install_path }}/redis/{{ file_name }}/conf/{{ item }}/redis.conf
  with_items:     
    - 7000     
    - 7001  
[root@kube-master1 tasks]# 
  1. 运行

ansible-playbook /etc/ansible/deploy.yml

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值