ansible建立redis集群

整个实验持续时间非常长,踩了好多坑,坑踩多了再遇到问题就能更好处理

准备工作

<root@linux0 /etc/ansible>$ ls -ld hosts redis-5.0.3 redis.conf.j2 redis_master.yml 
-rw-r--r-- 1 root root   37 3月  11 23:58 hosts   #hosts定义文件,也可以不使用,在yml文件里直接指定IP;
drwxr-xr-x 6 root root  309 3月  12 01:25 redis-5.0.3  #redis包,编译后(make && make install);
-rw-r--r-- 1 root root  244 3月  12 03:30 redis.conf.j2  #配置文件,使用template功能实现对不同机器的修改;
-rw-r--r-- 1 root root 1547 3月  12 03:59 redis_master.yml #脚本;

文件内容

<root@linux0 /etc/ansible>$ cat hosts
[thosts]  #组
linux0   #组内机器主机名,需配合/etc/hosts文件使用,或用内部DNS;ip是192.168.3.16;
[host2]   #组2
192.168.3.17  #也可直接使用IP来访问机器;
<root@linux0 /etc/ansible>$ cat redis_master.yml   #理论运行一次就会给hosts装上一个redis节点,节点端口在文件里定义,可在hosts上安装多个节点,只需改动一个变量,就是redis_port;
---
- hosts: thosts:192.168.3.17    #主机,组用分号分隔;
  remote_user: root  
  vars:
    redis_port: "7001"    #设置redis节点的端口号;
    IP: "{{ansible_default_ipv4['address']}}"   #从setup模块里取得IP,这个IP是ansible与主机通信的默认IP,使用IP作为参数,用于修改配置文件,生成不同目录,不同文件;
  tasks:
    - name: copy redis packet after make & make install
      copy: src=redis-5.0.3.tar.gz dest=/usr/local owner=root group=root
    - name: uncompress
      unarchive: src=/usr/local/redis-5.0.3.tar.gz dest=/usr/local remote_src=yes
    - name: cofig file
      template: src=redis.conf.j2 dest=/etc/redis_{{ redis_port }}.conf owner=root group=root mode=0644
    - name: make data dir
      file: path=/data/redis_data/{{ redis_port }} state=directory mode=755 
    - name: enter dir
      shell: cd /usr/local/redis-5.0.3/src
    - name: start the redis
      shell: redis-server /etc/redis_{{ redis_port }}.conf
<root@linux0 /etc/ansible>$ cat redis.conf.j2   #配置文件模板;

port {{ redis_port }}   #要让模板根据各自机器的配置而修改配置文件,需要取得机器数据,定义为变量,再在模板里引用变量;
bind {{ IP }}
daemonize yes
pidfile /var/run/redis_{{ redis_port }}.pid
dir /data/redis_data/{{ redis_port }}  #数据路径;
cluster-enabled yes   #集群开启;
cluster-config-file nodes_{{ redis_port }}.conf    #集群的信息会保存在此文件;
#cluster-node-timeout 10100
appendonly yes  #开启aof;

#
#
#
#
#
#
#
#
#

运行

<root@linux0 /etc/ansible>$ ansible-playbook redis_master.yml   #一次运行结果;
 
PLAY [thosts:192.168.3.17] *****************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************
ok: [linux0]
ok: [192.168.3.17]

TASK [copy redis packet after make & make install] *****************************************************************************************************************************
ok: [192.168.3.17]
ok: [linux0]

TASK [uncompress] **************************************************************************************************************************************************************
ok: [192.168.3.17]
ok: [linux0]

TASK [cofig file] **************************************************************************************************************************************************************
ok: [192.168.3.17]
ok: [linux0]

TASK [make data dir] ***********************************************************************************************************************************************************
changed: [linux0]
changed: [192.168.3.17]

TASK [enter dir] ***************************************************************************************************************************************************************
changed: [192.168.3.17]
changed: [linux0]

TASK [start the redis] *********************************************************************************************************************************************************
changed: [linux0]
changed: [192.168.3.17]

PLAY RECAP *********************************************************************************************************************************************************************
192.168.3.17               : ok=7    changed=3    unreachable=0    failed=0   
linux0                     : ok=7    changed=3    unreachable=0    failed=0   

#
#
#
#
#
#
#
#
#
#
  • redis_port: 7000 运行一次;
  • redis_port: 7001 运行一次;
  • redis_port: 7002 运行一次;即在两个电脑上,生成了6个redis副本,之后可以将他们集合成集群;
<root@linux0 /etc/ansible>$ /usr/local/redis-5.0.3/src/redis-cli --cluster create 192.168.3.16:7000 192.168.3.17:7000 192.168.3.16:7001 192.168.3.17:7001 192.168.3.16:7002 192.168.3.17:7002 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.3.17:7001 to 192.168.3.16:7000
Adding replica 192.168.3.16:7002 to 192.168.3.17:7000
Adding replica 192.168.3.17:7002 to 192.168.3.16:7001
M: c300df2dd7ffb6054adbbbe89b1e32757eb0b437 192.168.3.16:7000
   slots:[0-5460] (5461 slots) master
M: bdf372777b2227133638a263a2c812e4d8227b62 192.168.3.17:7000
   slots:[5461-10922] (5462 slots) master
M: 47f0043709c0b0b0903b030bb31a6552bb887326 192.168.3.16:7001
   slots:[10923-16383] (5461 slots) master
S: 895491945c0777d5d5782c73f4cfb4d3f4d99862 192.168.3.17:7001
   replicates c300df2dd7ffb6054adbbbe89b1e32757eb0b437
S: 2f4d8f944a17c772a21d9fecd52ee35893544fa7 192.168.3.16:7002
   replicates bdf372777b2227133638a263a2c812e4d8227b62
S: 3d38a38c49fcc5d12f8ccf0d3ccfee8f5a08d50b 192.168.3.17:7002
   replicates 47f0043709c0b0b0903b030bb31a6552bb887326
Can I set the above configuration? (type 'yes' to accept): yes   #同意slot分配;
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
..
>>> Performing Cluster Check (using node 192.168.3.16:7000)
M: c300df2dd7ffb6054adbbbe89b1e32757eb0b437 192.168.3.16:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 3d38a38c49fcc5d12f8ccf0d3ccfee8f5a08d50b 192.168.3.17:7002
   slots: (0 slots) slave
   replicates 47f0043709c0b0b0903b030bb31a6552bb887326
M: bdf372777b2227133638a263a2c812e4d8227b62 192.168.3.17:7000
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 895491945c0777d5d5782c73f4cfb4d3f4d99862 192.168.3.17:7001
   slots: (0 slots) slave
   replicates c300df2dd7ffb6054adbbbe89b1e32757eb0b437
M: 47f0043709c0b0b0903b030bb31a6552bb887326 192.168.3.16:7001
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 2f4d8f944a17c772a21d9fecd52ee35893544fa7 192.168.3.16:7002
   slots: (0 slots) slave
   replicates bdf372777b2227133638a263a2c812e4d8227b62
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#
#
#
#
#
#
#
#
#
#
#
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Ansible Playbook,用来部署Redis集群: ```yaml --- - name: Deploy Redis Cluster hosts: redis become: yes vars: redis_version: "6.2.5" redis_cluster_port: "6379" redis_cluster_config_file: "/etc/redis/redis.conf" redis_cluster_data_dir: "/var/lib/redis" redis_cluster_node_count: 6 tasks: - name: Install Redis dependencies apt: name: "{{ item }}" state: present loop: - build-essential - tcl - libjemalloc-dev - name: Download and extract Redis get_url: url: "http://download.redis.io/releases/redis-{{ redis_version }}.tar.gz" dest: "/tmp/redis-{{ redis_version }}.tar.gz" - name: Extract Redis archive unarchive: src: "/tmp/redis-{{ redis_version }}.tar.gz" dest: "/tmp" remote_src: yes - name: Compile and install Redis shell: | cd /tmp/redis-{{ redis_version }} make make install - name: Create Redis configuration file template: src: "redis.conf.j2" dest: "{{ redis_cluster_config_file }}" vars: redis_cluster_port: "{{ redis_cluster_port }}" redis_cluster_data_dir: "{{ redis_cluster_data_dir }}" redis_cluster_node_count: "{{ redis_cluster_node_count }}" - name: Create Redis data directory file: path: "{{ redis_cluster_data_dir }}" state: directory - name: Start Redis instances shell: | cd /usr/local/bin redis-server {{ redis_cluster_config_file }} ``` 此Playbook假设我们有一个名为"redis"的Ansible组,该组中的主机将运行Redis实例。它还假设我们将使用Redis 6.2.5版本,并创建一个6节点的Redis集群。 在此之前,我们需要编写一个名为"redis.conf.j2"的Jinja2模板文件,用于生成Redis配置文件。以下是示例模板文件: ```ini port {{ redis_cluster_port }} cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes dir {{ redis_cluster_data_dir }} cluster-announce-ip {{ ansible_default_ipv4.address }} cluster-announce-port {{ redis_cluster_port }} {% for i in range(redis_cluster_node_count) %} cluster-announce-bus-port {{ redis_cluster_port+10000+i }} {% endfor %} ``` 在运行此Playbook之前,请确保在Ansible的控制节点上安装了Jinja2和Redis依赖项。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值