Sentinelとは
SentinelとはRedisクラスターを簡単に構築することができるソフトウェアです。マスター・スレーブ構成になっており、マスターの自動昇格も行ってくれる優れものです。
Kubernetesでの構築
この手順は 「入門Kubernetes」に記載されているものを実践しています。
ConfigMapの作成
まずはRedisのマスター用の設定ファイルmaster.confを記述します。
1 2 3 4 |
bind 0.0.0.0 port 6379 dir /redis-data |
次にスレーブ用の設定ファイルslave.confを記述します。
1 2 3 4 5 6 |
bind 0.0.0.0 port 6379 dir . slaveof redis-0.redis 6379 |
最後にsentinel用の設定ファイルsentinel.confを記述します。
1 2 3 4 5 6 7 |
bind 0.0.0.0 port 26379 sentinel monitor redis redis-0.redis 6379 2 sentinel parallel-syncs redis 1 sentinel down-after-milliseconds redis 10000 sentinel failover-timeout redis 20000 |
コンテナ内でRedisを起動するときのスクリプトinit.shです。
1 2 3 4 5 6 |
#!/bin/bash if [[ ${HOSTNAME} == 'redis-0' ]]; then redis-server /redis-config/master.conf else redis-server /redis-config/slave.conf fi |
Redisスレーブがマスターに接続できるまで監視して、sentinelを起動するスクリプトsentinel.shです。
1 2 3 4 5 6 7 |
#!/bin/bash while ! ping -c 1 redis-0.redis; do echo 'Waiting for server' sleep 1 done redis-sentinel /redis-config/sentinel.conf |
上記の設定ファイルとスクリプトをまとめたConfigMapを作成します。redis-configという名前をつけています。
1 2 3 4 5 6 7 |
$ kubectl create configmap \ > --from-file=slave.conf=./slave.conf \ > --from-file=master.conf=./master.conf \ > --from-file=sentinel.conf=./sentinel.conf \ > --from-file=init.sh=./init.sh \ > --from-file=sentinel.sh=./sentinel.sh \ > redis-config |
ConfigMapの確認を行います。
1 |
$ kubectl get configmap |
Serviceの作成
PodへアクセスするためのServiceの設定redis-service.yamlです。
1 2 3 4 5 6 7 8 9 10 11 |
apiVersion: v1 kind: Service metadata: name: redis spec: ports: - port: 6379 name: peer clusterIP: None selector: app: redis |
上記設定からServiceを作成します。
1 |
$ kubectl apply -f redis-service.yaml |
Serviceの確認を行います。
1 |
$ kubectl get service |
StatefulSetの作成
StatefulSetの設定redis.yamlです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
apiVersion: apps/v1 kind: StatefulSet metadata: name: redis spec: selector: matchLabels: app: redis replicas: 3 serviceName: redis template: metadata: labels: app: redis spec: containers: - command: [sh, -c, source /redis-config/init.sh ] image: redis:3.2.7-alpine name: redis ports: - containerPort: 6379 name: redis volumeMounts: - mountPath: /redis-config name: config - mountPath: /redis-data name: data - command: [sh, -c, source /redis-config/sentinel.sh] image: redis:3.2.7-alpine name: sentinel volumeMounts: - mountPath: /redis-config name: config volumes: - configMap: defaultMode: 420 name: redis-config name: config - emptyDir: name: data |
StatefulSetの作成を行います。
1 |
$ kubectl apply -f redis.yaml |
StatefulSetの確認を行います。
1 |
$ kubectl get statefulset |
Sentinelの動作確認
Redisスレーブに対してコマンドを実行し、seintinelのRedisマスタのIPを確認します。
1 2 |
$ kubectl exec redis-2 -c redis \ > -- redus-cli -p 26379 sentinel get-master-addr-by-name redis |
ここでRedisスレーブに対して接続ができなかったのでredis-0コンテナのログを確認します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
$ kubectl -c sentinel logs redis-0 ping: bad address 'redis-0.redis' Waiting for server PING redis-0.redis (172.17.0.4): 56 data bytes 64 bytes from 172.17.0.4: seq=0 ttl=64 time=0.164 ms --- redis-0.redis ping statistics --- 1 packets transmitted, 1 packets received, 0% packet loss round-trip min/avg/max = 0.164/0.164/0.164 ms _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.7 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379 | `-._ `._ / _.-' | PID: 9 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 9:X 18 May 07:36:03.983 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 9:X 18 May 07:36:03.983 # Sentinel config file /redis-config/sentinel.conf is not writable: Read-only file system. Exiting... |
sentinelの設定がよくなさそう。。。ここで詰んでしまいRedisクラスタの動作確認ができてません。
だれか同じような人いたら解決策をおしえてほしいです!
おすすめ書籍
Dockerの使い方から説明されているので初心者の方も安心して学べる書籍です。
Kubernetesに関してはPod, Service, Deploymentなどを順を追って説明し、実際に動かすところまでを解説しているので非常に実践的な内容となっています。
これからKuebernetesを使ってサービスを開発してみたい方にとって最初のよい手助けとなると思います。是非読んでみてください。
髙妻智一
最新記事 by 髙妻智一 (全て見る)
- 【無料公開】「Goで始めるBitcoin」3章 Bitcoinノードとの通信 技術書典8 - 2020-03-08
- エンジニアがゼロから技術ブログを書くための方法をまとめました - 2019-05-25
- FlutterからCloud Firestoreのデータを追加、更新、取得、削除する方法 - 2019-05-23
コンテナ側から見ると error メッセージのようにread onlyでmountされています。
/dev/sda1 on /redis-config type xfs (ro,relatime,attr2,inode64,noquota)
解決方法としては、configmapをwriteできるように登録するか(できるかは調べていないです)
上記の内容で指定されたsentinel.confの内容を持つコンテナのイメージをbuildしてあげるのが良いかと思います。
とりあえず以下は私がbuildしたものになります。
https://hub.docker.com/r/salvare000/redis-cluster/
Dockerfileの内容は以下の感じ
# cat Dockerfile
FROM redis:3.2.7-alpine
ADD sentinel.conf /etc/redis-sentinel.conf
あとは、sentinel.sh等の/redis-config/sentinel.conf を参照しているpathを配置したpathに変えると
とりあえず起動できました。
2回作成したところ、3回程度restartするnodeがあったので、apply後少し時間がかかるようです。
# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-pod 1/1 Running 0 19d
rabbitmq-79d69c478f-8kkrr 1/1 Running 0 12d
rabbitmq-79d69c478f-v8bjd 1/1 Running 0 12d
rabbitmq-79d69c478f-w5fv5 1/1 Running 0 12d
redis-0 2/2 Running 3 7m
redis-1 2/2 Running 0 6m
redis-2 2/2 Running 0 6m
# kubectl -c sentinel logs redis-0
ping: bad address 'redis-0.redis'
Waiting for server
Waiting for server
ping: bad address 'redis-0.redis'
ping: bad address 'redis-0.redis'
Waiting for server
PING redis-0.redis (10.244.1.42): 56 data bytes
64 bytes from 10.244.1.42: seq=0 ttl=64 time=0.027 ms
--- redis-0.redis ping statistics ---
.
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.027/0.027/0.027 ms
_._
_.-
__ ''-._
_.-
_. ''-._ Redis 3.2.7 (00000000/0) 64 bit
.-
.-
.
\/ _.,_ ''-._
|( ' , .-
, ) Running in sentinel mode
-._|
-...-
__…-.-._|’
_.-'| Port: 26379
-._|
._ / _.-' | PID: 12
-._-._
-./ _.-‘ _.-‘|
-._
-._-.__.-' _.-'_.-'|
-._|
-._ _.-'_.-' | http://redis.io
-._-._
-.__.-‘_.-‘ _.-‘|
-._
-._-.__.-' _.-'_.-'|
-._|
-._ _.-'_.-' |
-._-._
-.__.-‘_.-‘ _.-‘-._
-.__.-‘ _.-‘-._ _.-'
-.__.-‘12:X 09 Aug 00:18:27.515 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
12:X 09 Aug 00:18:27.522 # Sentinel ID is d4af3163134a6626a9cdf799edd30576e9ed02c2
12:X 09 Aug 00:18:27.522 # +monitor master redis 10.244.1.42 6379 quorum 2
12:X 09 Aug 00:18:27.522 * +slave slave 10.244.3.44:6379 10.244.3.44 6379 @ redis 10.244.1.42 6379
12:X 09 Aug 00:18:27.524 * +slave slave 10.244.2.41:6379 10.244.2.41 6379 @ redis 10.244.1.42 6379
12:X 09 Aug 00:18:28.112 * +sentinel sentinel ed7174fead22ecef3624a6fb5109f51aa0a1bb90 10.244.3.44 26379 @ redis 10.244.1.42 6379
12:X 09 Aug 00:18:28.333 * +sentinel sentinel 92b9eba18e8337a04b48089fbe258058e337f36d 10.244.2.41 26379 @ redis 10.244.1.42 6379
`