Commit 5173d954 authored by Marcel Wunderlich's avatar Marcel Wunderlich

Minimal high available redis

parents
Minimal Redis HA-Setup
======================
This is a collection of ansible scripts to create a minimal high availability
redis setup, consisting of
- 2 Redis servers where one is the slave of the other
- 3 Redis sentinels to manage failover
- n HAProxys that serve as clients and select the master server
Target platform: centos 7
At least 3 computers are required.
Usage
-----
Connect to 127.0.0.1:6379 on each host running HAProxy to access Redis
Installation instructions
-------------------------
1. configure hosts in hosts.ini
2. maybe another step to setup HAProxy and Redis Configs
3. ansible-playbooks -i hosts.ini install.yml
###################################
# !!! no more than one master !!! #
###################################
# script will pic first master anyway
[master]
zivsddc101a
# in theory you should be able to have more than one slave
# in theory
[slaves]
zivsddc102a
######################
# !!! be careful !!! #
######################
# redis uses a parameter to set the number of nodes required for quorum
# this script collection expects you to have 3 sentinesl, so 2 nodes need to
# agree on a master.
# this is of course problematic if you install 4 sentinels
# 3 sentinels should be enough for every case though
[sentinels]
zivsddc101a
zivsddc102a
zivsddc103a
# probably you want haproxy on each machine that needs to connect to redis so
# you minimize network hops
[haproxy]
zivsddc103a
- hosts: master,slaves
roles:
- redis
- hosts: sentinels
roles:
- sentinel
- hosts: haproxy
roles:
- haproxy
- hosts: master
roles:
- master
- hosts: slaves
roles:
- slave
---
language: python
python: "2.7"
# Use the new container infrastructure
sudo: false
# Install ansible
addons:
apt:
packages:
- python-pip
install:
# Install ansible
- pip install ansible
# Check ansible version
- ansible --version
# Create ansible.cfg with correct roles_path
- printf '[defaults]\nroles_path=../' >ansible.cfg
script:
# Basic role syntax check
- ansible-playbook tests/test.yml -i tests/inventory --syntax-check
notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/
\ No newline at end of file
HAProxy
=======
Installs and sets up HAProxy so it connects to the active Redis master
Role Variables
--------------
no variables until now
Dependencies
------------
no deps until now
---
- name: Configure haproxy
template:
src: haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg
owner: root
group: root
mode: '0644'
register: haproxycfg
- name: Restart haproxy if necessary
service:
name: haproxy
state: restarted
when: haproxycfg.changed
---
# tasks file for common
- name: Install haproxy
yum:
name: haproxy
state: present
---
# tasks file for owncloud-ingress
- import_tasks: install.yml
tags:
- haproxy
- haproxy:install
- import_tasks: config.yml
tags:
- haproxy
- haproxy:config
listen redis
bind localhost:6379
mode tcp
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
option tcp-check
# the following line is necessary by HAProxy documentation, but on older
# redhat relaeses, e.g. 8.el7 it was not enforced
tcp-check connect port 6379
tcp-check send PING\r\n
tcp-check expect string +PONG
tcp-check send info\ replication\r\n
tcp-check expect string role:master
tcp-check send QUIT\r\n
tcp-check expect string +OK
# use only first master
server {{ groups['master'][0] }} {{ groups['master'][0] }}:6379 maxconn 1024 check inter 1s
{% for node in groups['slaves'] %}
server {{ node }} {{ node }}:6379 maxconn 1024 check inter 1s
{% endfor %}
---
# vars file for roles/haproxy
\ No newline at end of file
---
language: python
python: "2.7"
# Use the new container infrastructure
sudo: false
# Install ansible
addons:
apt:
packages:
- python-pip
install:
# Install ansible
- pip install ansible
# Check ansible version
- ansible --version
# Create ansible.cfg with correct roles_path
- printf '[defaults]\nroles_path=../' >ansible.cfg
script:
# Basic role syntax check
- ansible-playbook tests/test.yml -i tests/inventory --syntax-check
notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/
\ No newline at end of file
Master
======
sets slaveof no one
- name: Test redis connection
shell: echo info replication | nc localhost 6379
- name: Set slaveof no one
shell: "echo slaveof no one | nc localhost 6379"
register: master_set
- name: Test if it worked
fail:
msg: "setting master failed"
when:
- master_set.stdout != "+OK"
---
language: python
python: "2.7"
# Use the new container infrastructure
sudo: false
# Install ansible
addons:
apt:
packages:
- python-pip
install:
# Install ansible
- pip install ansible
# Check ansible version
- ansible --version
# Create ansible.cfg with correct roles_path
- printf '[defaults]\nroles_path=../' >ansible.cfg
script:
# Basic role syntax check
- ansible-playbook tests/test.yml -i tests/inventory --syntax-check
notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/
\ No newline at end of file
Role Name
=========
Install redis on selected servers.
Role Variables
--------------
redis_timeout:
sets the timeout value in redis.conf
Dependencies
------------
No dependencies so far.
- name: Shut down redis before configuration
service:
name: redis
state: stopped
- name: Initial Redis configuration
template:
src: redis.conf.j2
dest: /etc/redis.conf
owner: redis
group: root
mode: '0644'
- name: Restart redis after configuration
service:
name: redis
state: started
enabled: yes
- name: Install EPEL release
yum:
name: epel-release
state: present
- name: Install REMI
yum:
name: http://rpms.remirepo.net/enterprise/remi-release-7.rpm
state: present
- name: Install redis
yum:
name:
- redis5
state: present
- name: Install netcat as it is useful
yum:
name:
nc
state: present
---
# tasks file for roles/redis
- import_tasks: install.yml
tags:
- redis
- redis:install
- import_tasks: config.yml
tags:
- redis
- redis:config
# Redis always rewrites its config so it is
port 6379
bind 0.0.0.0
timeout {{ redis_timeout }}
logfile "/var/log/redis/redis.log"
repl-diskless-sync yes
appendonly yes
dir "/var/lib/redis"
appendfilename "appendonly.aof"
---
# vars file for roles/redis
redis_timeout: 0
---
language: python
python: "2.7"
# Use the new container infrastructure
sudo: false
# Install ansible
addons:
apt:
packages:
- python-pip
install:
# Install ansible
- pip install ansible
# Check ansible version
- ansible --version
# Create ansible.cfg with correct roles_path
- printf '[defaults]\nroles_path=../' >ansible.cfg
script:
# Basic role syntax check
- ansible-playbook tests/test.yml -i tests/inventory --syntax-check
notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/
\ No newline at end of file
redis sentinel
==============
sentinel servers for redis
Requirements
------------
you want at least 3 servers for the sentinel daemons
Role Variables
--------------
no variables until now
Dependencies
------------
no dependencies until now
- name: Shut down redis before configuration
service:
name: redis-sentinel
state: stopped
- name: Initial Redis configuration
template:
src: redis-sentinel.conf.j2
dest: /etc/redis-sentinel.conf
owner: redis
group: root
mode: '0644'
- name: Restart redis after configuration
service:
name: redis-sentinel
state: started
enabled: yes
- name: Install EPEL release
yum:
name: epel-release
state: present
- name: Install REMI
yum:
name: http://rpms.remirepo.net/enterprise/remi-release-7.rpm
state: present
- name: Install redis (sentinel is provided by the redis package)
yum:
name:
- redis5
state: present
- name: Install netcat as it is useful
yum:
name:
nc
state: present
---
# tasks file for roles/redis
- import_tasks: install.yml
tags:
- sentinel
- sentinel:install
- import_tasks: config.yml
tags:
- sentinel
- sentinel:config
logfile "/var/log/redis/redis-sentinel.log"
protected-mode no
port 26379
sentinel monitor redismaster {{ groups['master'][0] }} 6379 2
daemonize yes
---
# vars file for roles/sentinel
\ No newline at end of file
---
language: python
python: "2.7"
# Use the new container infrastructure
sudo: false
# Install ansible
addons:
apt:
packages:
- python-pip
install:
# Install ansible
- pip install ansible
# Check ansible version
- ansible --version
# Create ansible.cfg with correct roles_path
- printf '[defaults]\nroles_path=../' >ansible.cfg
script:
# Basic role syntax check
- ansible-playbook tests/test.yml -i tests/inventory --syntax-check
notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/
\ No newline at end of file
Master
======
sets slaveof no one
- name: Test connection
shell: echo info replication | nc localhost 6379
- name: Set slave-of {{ groups['master'][0] }}
shell: "echo slaveof {{ groups['master'][0] }} 6379 | nc localhost 6379"
register: slave_set
-
- name: Check if it worked
fail:
msg: "slave setzen fehlgeschlagen"
when:
- not slave_set.stdout is match "\+OK.*"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment