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 ,
180182RECOVERY_CONF_FILE  =  "postgresql-auto-failover-standby.conf" 
181183RECOVERY_SET_FILE  =  "postgresql-auto-failover.conf" 
182184STANDBY_SIGNAL  =  "standby.signal" 
185+ GET_INET_CMD  =  "ip addr | grep inet" 
183186
184187
185188def  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 
10301034def  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