0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ホスト(macos)からdocker上のredis sentinelに接続

Posted at

Overview

docker上にテスト用のredis sentinelを動かして、ホスト上で動いているプログラムから接続する

Problem

sentinel get-master-addr-by-name で返却されるマスタのIPはdocker環境上のIPなので、ホストからは繋げない。

解決

  • hostモードで動かす => macosは対応していない?後日ドキュメント確認
  • lo0にループバックアドレスにエイリアスを追加 => 暫定対応

エイリアス追加

sudo ifconfig lo0 alias ${DOCKER_SUBNET}

テスト

package main

import (
	"context"
	"fmt"

	"github.com/go-redis/redis/v8"
)

func main() {
	opt := redis.FailoverOptions{}
	opt.MasterName = "master"
	opt.SentinelAddrs = []string{"127.0.0.1:26379"}
	opt.Password = "xxxx"

	r := redis.NewFailoverClient(&opt)

        ctx := context.TODO()
	r.Ping(ctx)
	
	err := r.Set(ctx, "test", "yeah!", 0).Err()
	if err != nil {
		panic(err)
	}
	val, err := r.Get(ctx, "test").Result()
	if err != nil {
		panic(err)
	}
	fmt.Println("key", val)
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?