Cài đặt redis sentinel bằng docker và kết nối tới redis sentinel bằng golang
Bài đăng này đã không được cập nhật trong 3 năm
Hướng dẫn cài đặt redis sentinel với docker theo mô hình + kết nối golang tới redis sentinel
- 1 master
- 2 slave
- 3 redis sentinel

B1 : Clone project : git clone https://github.com/minhvu2510/GowithRedisSentinel.git
B2 : Chạy docker compose :
docker-compose up -d
Kiểm tra các container đã lên :
docker ps

Có 4 container trong đó :
1: redis_master1 : Cài đặt master và sentinel
2: redis_slave và redis_sentinel cài đặt slave và sentinel
3: core_go chạy code golang
B3: Cài đặt redis trên từng server:
-
Cài đặt node master
- Truy cập vào container để cài đặt :
docker exec -it redis_master1 bash - Cấu hình redis master bằng cách sửa thông tin file config như sau : vim /etc/redis/redis.conf
- Các thông tin cần sửa:
# bind 127.0.0.1 ::1 # Để redis public ra ngoài cho slave có thể connect requirepass CkkUdLPMT # Đặt password dùng cli masterauth CkkUdLPMT # Đặt password cho node master- Cấu hình redis sentinel bằng cách sửa file config như sau :
vim /etc/redis/sentinel.conf
sentinel monitor mymaster 172.31.0.2 6379 2 # 172.31.0.2 là ip node master lấy bằng lệnh ifconfig sentinel auth-pass mymaster CkkUdLPMT sentinel parallel-syncs mymaster 1- Start redis-server và redis-sentinel
service redis-server start service redis-sentinel start - Truy cập vào container để cài đặt :
-
Cài đặt 2 node slave vào sentinel:
- Truy cập vào 2 container redis_slave, redis_sentinel để cài đặt :
docker exec -it redis_slave bash - Cấu hình redis master bằng cách sửa thông tin file config như sau : vim /etc/redis/redis.conf
- Các thông tin cần sửa:
# bind 127.0.0.1 ::1 # Để redis public ra ngoài cho slave có thể connect requirepass CkkUdLPMT # Đặt password dùng cli masterauth CkkUdLPMT # Đặt password cho node master replicaof 172.31.0.2 6379 # 172.31.0.2 là ip node master- Cấu hình redis sentinel bằng cách sửa file config như sau :
vim /etc/redis/sentinel.conf
sentinel monitor mymaster 172.31.0.2 6379 2 # 172.31.0.2 là ip node master lấy bằng lệnh ifconfig sentinel auth-pass mymaster CkkUdLPMT sentinel parallel-syncs mymaster 1- Start redis-server và redis-sentinel
service redis-server start service redis-sentinel start - Truy cập vào 2 container redis_slave, redis_sentinel để cài đặt :
-
Kiểm tra cài đặt:
- Truy cập vào container master :
docker exec -it redis_master1 bash - Kiểm tra thông tin:

- Xem phần Replication có role:master, connected_slaves:2 là cài đặt thành công:

- Kiểm tra service đã bật bằng lệnh : netstat -tnlp

- Truy cập vào container master :
-
Kết nối golang với redis sentinel:
- Truy cập từng container lấy từng ip bằng lệnh: ifconfig
- Trong ví dụ :
redis_master1 : 172.31.0.2
redis_slave : 172.31.0.4
redis_sentinel : 172.31.0.5
-
Truy cập vào container core_go:
docker exec -it core_go bash -
Sửa lại connect redis :
cd /home/app/redisSentinel vim connectRedisSentinel.go- Sử lại config như hình :

- Chạy file để kiểm tra :
go run connectRedisSentinel.go - Kết quả :

- Truy cập từng node redis kiểm tra kết quả được kết quả như hình :
Node master :

2 node slave :

All rights reserved