Cài đặt redis sentinel bằng docker và kết nối tới redis sentinel bằng golang

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

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

image

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

image

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:

  1. 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
    
  2. 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
    
  3. 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:

    image

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

    image

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

    image

  4. 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 :

    image

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

    image

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

    Node master :

    image

    2 node slave :

    image

Nguồn: viblo.asia

Bài viết liên quan

Thay đổi Package Name của Android Studio dể dàng với plugin APR

Nếu bạn đang gặp khó khăn hoặc bế tắc trong việc thay đổi package name trong And

Lỗi không Update Meta_Value Khi thay thế hình ảnh cũ bằng hình ảnh mới trong WordPress

Mã dưới đây hoạt động tốt có 1 lỗi không update được postmeta ” meta_key=

Bài 1 – React Native DevOps các khái niệm và các cài đặt căn bản

Hướng dẫn setup jenkins agent để bắt đầu build mobile bằng jenkins cho devloper an t

Chuyển đổi từ monolith sang microservices qua ví dụ

1. Why microservices? Microservices là kiến trúc hệ thống phần mềm hướng dịch vụ,