-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigmap.yaml
188 lines (166 loc) · 6.72 KB
/
configmap.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
apiVersion: v1
kind: ConfigMap
metadata:
name: gen-conf
data:
genconf-sentinel: |
#!/usr/bin/env python3
import time
import os
import subprocess
import threading
from kubernetes import client, config, watch
from redis.sentinel import Redis, Sentinel
f = open("/etc/redis/sentinel.conf", "w")
f.write("port 26379\n")
f.close()
def monitor_new_master(name, ip):
"waiting for the sentinel to start"
cli = "redis-cli -p 26379 "
cmd_list = ["sentinel monitor {} {} 6379 2".format(name, ip),
"sentinel set {} down-after-milliseconds 1000".format(name),
"sentinel flushconfig"
]
for cmd in cmd_list:
subprocess.run((cli + cmd).split(" "))
config.load_incluster_config()
v1 = client.CoreV1Api()
appsV1 = client.AppsV1Api()
sentinels = []
pods_sentinel = appsV1.read_namespaced_stateful_set(name = "sentinel", namespace = "redis-sentinel")
for i in range(pods_sentinel.spec.replicas):
sentinels.append(("sentinel-{}.sentinel-hs.redis-sentinel.svc.cluster.local".format(i), 26379))
print(sentinels)
masters = {}
for s in sentinels:
try:
sc = Redis(*s, socket_timeout=5)
masters = sc.sentinel_masters()
print("got master:", masters)
except Exception as e:
print("error get masters from {}. ".format(s), e)
continue
break
if masters:
for master in masters.keys():
name = masters[master]["name"]
ip = masters[master]["ip"]
down_after_milliseconds = masters[master]["down-after-milliseconds"]
r = Redis("localhost", 26379)
while True:
try:
r.ping()
except:
continue
break
monitor_new_master(name, ip)
genconf-redis: |
#!/usr/bin/env python3
import time
import os
import subprocess
import threading
from kubernetes import client, config, watch
from redis.sentinel import Redis, Sentinel
pod_is_slave = False
config.load_incluster_config()
v1 = client.CoreV1Api()
appsV1 = client.AppsV1Api()
def monitor_new_master(name, ip):
"waiting for the sentinel to start"
#如果是第一次启动,启动前要确保sentinel都启动完成
while True:
sentinels = appsV1.read_namespaced_stateful_set(name = "sentinel", namespace = "redis-sentinel")
if sentinels.status.ready_replicas == sentinels.spec.replicas:
break
time.sleep(1)
#保证redis也都启动完成,其实没必要?
while True:
redises = appsV1.read_namespaced_stateful_set(name = name, namespace = "redis-sentinel")
if redises.status.ready_replicas == redises.spec.replicas:
break
time.sleep(1)
#需要让每一个sentinel都监控
sentinels = appsV1.read_namespaced_stateful_set(name = "sentinel", namespace = "redis-sentinel")
for i in range(sentinels.spec.replicas):
cli = "redis-cli -h sentinel-{}.sentinel-hs.redis-sentinel.svc.cluster.local -p 26379 ".format(i)
cmd_list = ["sentinel monitor {} {} 6379 2".format(name, ip),
"sentinel set {} down-after-milliseconds 1000".format(name),
"sentinel flushconfig"
]
for cmd in cmd_list:
subprocess.run((cli + cmd).split(" "))
def sentinel_reset_master(master):
sentinels = appsV1.read_namespaced_stateful_set(name = "sentinel", namespace = "redis-sentinel")
for i in range(sentinels.spec.replicas):
cli = "redis-cli -h sentinel-{}.sentinel-hs.redis-sentinel.svc.cluster.local -p 26379 ".format(i)
subprocess.run((cli + "sentinel reset {}".format(master)).split(" "))
def try_ping(c, t):
n = t
while n > 0:
try:
c.ping()
except:
time.sleep(1)
n -= 1
print("retring ping the redis server", n)
continue
break
pod_name = os.getenv("HOSTNAME")
pod_ip = os.getenv("POD_IP")
f = open("/etc/redis/redis.conf", "w")
hostnum = int(pod_name.split("-")[-1])
f.write("port 6379\n")
statefulsetname = "-".join(pod_name.split("-")[:-1])
first_pod = v1.read_namespaced_pod(namespace="redis-sentinel", name=statefulsetname+"-0")
print("first pod ip: ", first_pod.status.pod_ip)
redis_servers = []
#因为第一次启动时以第0个pod作为master,因此后面的启动一定不能进入那里,一定要从这里得到master
#但是如果直接获取,时机不对的话,(比如master刚刚挂掉,或者还没有选出master),则不能得到正确的master
#所以,需要一直等
sentinels = []
pods_sentinel = appsV1.read_namespaced_stateful_set(name = "sentinel", namespace = "redis-sentinel")
for i in range(pods_sentinel.spec.replicas):
sentinels.append(("sentinel-{}.sentinel-hs.redis-sentinel.svc.cluster.local".format(i), 26379))
masters = {}
for s in sentinels:
try:
sc = Redis(*s, socket_timeout=5)
masters = sc.sentinel_masters()
except Exception as e:
print("error get masters from {}. ".format(s), e)
continue
break
sentinel = Sentinel(sentinels, socket_timeout=1)
if masters:
print("masters monitored: ", masters)
n = 0
while True:
try:
current_master = sentinel.discover_master(statefulsetname)
print("current master: ", current_master)
master_ip = current_master[0]
except Exception as e:
print("discover master error: ", e)
time.sleep(1)
n += 1
print("waiting for master, retry {} times".format(n))
continue
break
sentinel_reset_master(statefulsetname)
f.write("slaveof {} 6379\n".format(master_ip))
else:
print("no master monitored now")
#第一次启动时,都作为0号pod的replica
if not pod_name.endswith("-0"):
master_ip = first_pod.status.pod_ip
r = Redis(master_ip, 6379)
try_ping(r, 20)
f.write("slaveof {} 6379\n".format(master_ip))
else:
thread = threading.Thread(target=monitor_new_master, args=(statefulsetname, pod_ip))
thread.start()
f.write("protected-mode no\n")
f.write("appendonly yes\n")
f.write("bind 0.0.0.0\n")
f.close()