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

9 Mẹo lập trình Web “ẩn mình” giúp tiết kiệm hàng giờ đồng hồ

Hầu hết các lập trình viên (kể cả những người giỏi) đều tốn thời gian x

Can GPT-4o Generate Images? All You Need to Know about GPT-4o-image

OpenAI‘s GPT-4o, introduced on March 25, 2025, has revolutionized the way we create visual con

Khi nào nên dùng main, section, article, header, footer, và aside trong HTML5

HTML5 đã giới thiệu các thẻ ngữ nghĩa giúp cấu trúc nội dung web một cách có

So sánh Webhook và API: Khi nào nên sử dụng?

Trong lĩnh vực công nghệ thông tin và phát triển phần mềm, Webhook và API là hai th