Skip to content

Commit d42f491

Browse files
committed
fix lvs bugs
1 parent a8c47a4 commit d42f491

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

platforms/kubernetes/postgres-operator/postgres/constants.py

+21-8
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,13 @@
153153
# real_main_servers
154154
# real_read_servers
155155
LVS_BODY = '''
156-
global_defs {{
157-
router_id postgres
158-
}}
159-
160156
vrrp_instance VI_1 {{
161157
state BACKUP
162158
nopreempt
163159
interface {net}
164-
virtual_router_id 100
160+
virtual_router_id {routeid}
165161
priority 100
166162
167-
advert_int 2
168163
authentication {{
169164
auth_type PASS
170165
auth_pass pass
@@ -179,7 +174,6 @@
179174
delay_loop 10
180175
lb_algo lc
181176
lb_kind DR
182-
nat_mask 255.255.255.0
183177
protocol TCP
184178
185179
{real_main_servers}
@@ -188,7 +182,6 @@
188182
delay_loop 10
189183
lb_algo lc
190184
lb_kind DR
191-
nat_mask 255.255.255.0
192185
protocol TCP
193186
194187
{real_read_servers}
@@ -219,6 +212,16 @@
219212
}}
220213
'''
221214

215+
LVS_REAL_EMPTY_SERVER = '''
216+
real_server {ip} {port} {{
217+
weight 1
218+
MISC_CHECK {{
219+
misc_path "ls /tmp/file_not_exists_pg"
220+
misc_timeout 60
221+
}}
222+
}}
223+
'''
224+
222225
# main_vip
223226
# read_vip
224227
LVS_SET_NET = '''
@@ -233,3 +236,13 @@
233236
/sbin/ifconfig lo:1 {read_vip} broadcast {read_vip} netmask 255.255.255.255 up;
234237
/sbin/route add -host {read_vip} dev lo:1;
235238
'''
239+
240+
# LO net
241+
LVS_UNSET_NET = '''
242+
/sbin/ifconfig lo:0 down;
243+
/sbin/ifconfig lo:1 down;
244+
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore;
245+
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce;
246+
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore;
247+
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce;
248+
'''

platforms/kubernetes/postgres-operator/postgres/handle.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@
105105
LVS_BODY,
106106
LVS_REAL_MAIN_SERVER,
107107
LVS_REAL_READ_SERVER,
108+
LVS_REAL_EMPTY_SERVER,
108109
LVS_SET_NET,
110+
LVS_UNSET_NET,
109111
CLUSTER_STATUS_CREATE,
110112
CLUSTER_STATUS_UPDATE,
111113
CLUSTER_STATUS_RUN,
@@ -180,6 +182,7 @@
180182
RECOVERY_CONF_FILE = "postgresql-auto-failover-standby.conf"
181183
RECOVERY_SET_FILE = "postgresql-auto-failover.conf"
182184
STANDBY_SIGNAL = "standby.signal"
185+
GET_INET_CMD = "ip addr | grep inet"
183186

184187

185188
def set_cluster_status(meta: kopf.Meta, statefield: str, state: str,
@@ -1027,6 +1030,7 @@ def restore_postgresql(
10271030
restore_postgresql_fromssh(meta, spec, patch, status, logger, conn)
10281031

10291032

1033+
# LABEL: MULTI_PG_VERSIONS
10301034
def create_log_table(logger: logging.Logger, conn: InstanceConnection,
10311035
postgresql_major_version: int) -> None:
10321036
logger.info("create postgresql log table")
@@ -1973,16 +1977,23 @@ def create_services(
19731977
machines = spec.get(POSTGRESQL).get(READONLYINSTANCE).get(
19741978
MACHINES)
19751979
read_vip = service[VIP]
1980+
READ_SERVER = LVS_REAL_READ_SERVER
19761981
elif service[SELECTOR] == SERVICE_STANDBY_READONLY:
19771982
machines = spec.get(POSTGRESQL).get(READWRITEINSTANCE).get(
19781983
MACHINES).copy()
19791984
machines += spec.get(POSTGRESQL).get(READONLYINSTANCE).get(
19801985
MACHINES)
19811986
read_vip = service[VIP]
1987+
READ_SERVER = LVS_REAL_READ_SERVER
19821988
else:
19831989
logger.error(f"unsupport service {service}")
19841990
continue
19851991

1992+
if machines == None or len(machines) == 0:
1993+
machines = [spec.get(POSTGRESQL).get(READWRITEINSTANCE).get(
1994+
MACHINES)[0]]
1995+
READ_SERVER = LVS_REAL_EMPTY_SERVER
1996+
19861997
for machine in machines:
19871998
if service[SELECTOR] == SERVICE_PRIMARY:
19881999
real_main_servers.append(
@@ -1992,7 +2003,7 @@ def create_services(
19922003
meta, spec, patch, status, logger)))
19932004
else:
19942005
real_read_servers.append(
1995-
LVS_REAL_READ_SERVER.format(
2006+
READ_SERVER.format(
19962007
ip=machine.split(':')[2],
19972008
port=get_postgresql_config_port(
19982009
meta, spec, patch, status, logger)))
@@ -2001,6 +2012,7 @@ def create_services(
20012012
net="eth0",
20022013
main_vip=main_vip,
20032014
read_vip=read_vip,
2015+
routeid=hash(main_vip)%255 + 1,
20042016
port=get_postgresql_config_port(meta, spec, patch, status, logger),
20052017
real_main_servers="\n".join(real_main_servers),
20062018
real_read_servers="\n".join(real_read_servers))
@@ -2315,6 +2327,25 @@ async def correct_keepalived(
23152327
if conn.get_machine() == None:
23162328
break
23172329

2330+
main_vip = ""
2331+
read_vip = ""
2332+
for service in spec[SERVICES]:
2333+
if service[SELECTOR] == SERVICE_PRIMARY:
2334+
main_vip = service[VIP]
2335+
elif service[SELECTOR] == SERVICE_READONLY:
2336+
read_vip = service[VIP]
2337+
elif service[SELECTOR] == SERVICE_STANDBY_READONLY:
2338+
read_vip = service[VIP]
2339+
else:
2340+
logger.error(f"unsupport service {service}")
2341+
2342+
output = machine_exec_command(conn.get_machine().get_ssh(),
2343+
GET_INET_CMD,
2344+
interrupt=False)
2345+
if len(main_vip) > 0 and len(read_vip) > 0 and (output.find(main_vip) == -1 or output.find(read_vip) == -1):
2346+
machine_exec_command(conn.get_machine().get_ssh(),
2347+
LVS_SET_NET.format(main_vip=main_vip, read_vip=read_vip))
2348+
23182349
output = machine_exec_command(conn.get_machine().get_ssh(),
23192350
STATUS_KEEPALIVED,
23202351
interrupt=False)
@@ -2693,6 +2724,7 @@ def delete_services(
26932724
machine_exec_command(conn.get_machine().get_ssh(), STOP_KEEPALIVED)
26942725
machine_exec_command(conn.get_machine().get_ssh(),
26952726
"rm -rf " + KEEPALIVED_CONF)
2727+
machine_exec_command(conn.get_machine().get_ssh(), LVS_UNSET_NET)
26962728
conns.free_conns()
26972729
readonly_conns.free_conns()
26982730

0 commit comments

Comments
 (0)