105
105
LVS_BODY ,
106
106
LVS_REAL_MAIN_SERVER ,
107
107
LVS_REAL_READ_SERVER ,
108
+ LVS_REAL_EMPTY_SERVER ,
108
109
LVS_SET_NET ,
110
+ LVS_UNSET_NET ,
109
111
CLUSTER_STATUS_CREATE ,
110
112
CLUSTER_STATUS_UPDATE ,
111
113
CLUSTER_STATUS_RUN ,
180
182
RECOVERY_CONF_FILE = "postgresql-auto-failover-standby.conf"
181
183
RECOVERY_SET_FILE = "postgresql-auto-failover.conf"
182
184
STANDBY_SIGNAL = "standby.signal"
185
+ GET_INET_CMD = "ip addr | grep inet"
183
186
184
187
185
188
def set_cluster_status (meta : kopf .Meta , statefield : str , state : str ,
@@ -1027,6 +1030,7 @@ def restore_postgresql(
1027
1030
restore_postgresql_fromssh (meta , spec , patch , status , logger , conn )
1028
1031
1029
1032
1033
+ # LABEL: MULTI_PG_VERSIONS
1030
1034
def create_log_table (logger : logging .Logger , conn : InstanceConnection ,
1031
1035
postgresql_major_version : int ) -> None :
1032
1036
logger .info ("create postgresql log table" )
@@ -1973,16 +1977,23 @@ def create_services(
1973
1977
machines = spec .get (POSTGRESQL ).get (READONLYINSTANCE ).get (
1974
1978
MACHINES )
1975
1979
read_vip = service [VIP ]
1980
+ READ_SERVER = LVS_REAL_READ_SERVER
1976
1981
elif service [SELECTOR ] == SERVICE_STANDBY_READONLY :
1977
1982
machines = spec .get (POSTGRESQL ).get (READWRITEINSTANCE ).get (
1978
1983
MACHINES ).copy ()
1979
1984
machines += spec .get (POSTGRESQL ).get (READONLYINSTANCE ).get (
1980
1985
MACHINES )
1981
1986
read_vip = service [VIP ]
1987
+ READ_SERVER = LVS_REAL_READ_SERVER
1982
1988
else :
1983
1989
logger .error (f"unsupport service { service } " )
1984
1990
continue
1985
1991
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
+
1986
1997
for machine in machines :
1987
1998
if service [SELECTOR ] == SERVICE_PRIMARY :
1988
1999
real_main_servers .append (
@@ -1992,7 +2003,7 @@ def create_services(
1992
2003
meta , spec , patch , status , logger )))
1993
2004
else :
1994
2005
real_read_servers .append (
1995
- LVS_REAL_READ_SERVER .format (
2006
+ READ_SERVER .format (
1996
2007
ip = machine .split (':' )[2 ],
1997
2008
port = get_postgresql_config_port (
1998
2009
meta , spec , patch , status , logger )))
@@ -2001,6 +2012,7 @@ def create_services(
2001
2012
net = "eth0" ,
2002
2013
main_vip = main_vip ,
2003
2014
read_vip = read_vip ,
2015
+ routeid = hash (main_vip )% 255 + 1 ,
2004
2016
port = get_postgresql_config_port (meta , spec , patch , status , logger ),
2005
2017
real_main_servers = "\n " .join (real_main_servers ),
2006
2018
real_read_servers = "\n " .join (real_read_servers ))
@@ -2315,6 +2327,25 @@ async def correct_keepalived(
2315
2327
if conn .get_machine () == None :
2316
2328
break
2317
2329
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
+
2318
2349
output = machine_exec_command (conn .get_machine ().get_ssh (),
2319
2350
STATUS_KEEPALIVED ,
2320
2351
interrupt = False )
@@ -2693,6 +2724,7 @@ def delete_services(
2693
2724
machine_exec_command (conn .get_machine ().get_ssh (), STOP_KEEPALIVED )
2694
2725
machine_exec_command (conn .get_machine ().get_ssh (),
2695
2726
"rm -rf " + KEEPALIVED_CONF )
2727
+ machine_exec_command (conn .get_machine ().get_ssh (), LVS_UNSET_NET )
2696
2728
conns .free_conns ()
2697
2729
readonly_conns .free_conns ()
2698
2730
0 commit comments