How to set keepalived config

Hi, I'm a freshman, and use Op from 2017.Sorry,my english is poor.
I have a homelab installed Virtual Machine,and deploy Openwrt x86.
Now I try to setup keepalived,but it'not working properly.
The problem is similar to this post([Keepalived setup - where to put config?]).And wulfy23's reply is useless.

/etc/config/keepalived

config global_defs
         option alt_config_file "/etc/keepalived/keepalived.conf"

/tmp/keepalived.conf

! Configuration file for keepalived (autogenerated via init script)
! Written Mon May 24 20:52:17 2021

global_defs {
}

static_ipaddress {
}

static_routes {
}

/etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface br-lan
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       192.168.0.95/24 dev br-lan
    }
}

Thanks in advance!

Especially on x86, there is no reason to use an outdated/ obsolete/ unsupported version of OpenWrt, please give 19.07.7 or 21.02.0-rc1 a test, which fix important security issues and increase the chances that others can help you.

/etc/keepalived/keepalived.conf is where config is read from by default.
AFAIK, you shouldn't have to modify anything in /etc/config after installing the package; except to enable/start the service after you write the config file.

I use OpenWrt R21.4.18 / LuCI Master (git-21.114.56541-974fb04),is my own complied from github.The version of keepalived is V2.0.20.

I read /etc/init.d/keepalived code.

....
KEEPALIVED_CONF=/tmp/keepalived.conf
...
process_config() {
   ...
   [ -f /etc/config/keepalived ] 
   ...

It's not read /etc/keepalived/keepalived.conf at OpenWrt's root.

Thanks, I updated the wiki to mention alt_config_file

That's a pretty gnarly init script.

It's almost as if someone wanted to build a luci ui on top and expose all the keepalived options as UCI options (there's so much), but then they didn't and we ended up with two complicated configs - one in UCI and one that comes with the tool.

I think the alt_config_file setting makes it recreate /tmp/keepalived.conf as a symlink to whatever you specify. Based on looking at start_service, process_config here: https://github.com/openwrt/packages/blob/master/net/keepalived/files/keepalived.init . Can you verify that's how it works now?

Thanks for your reply.
I have downloaded keepalived.init. keepalived.init is the same as /etc/init.d/keepalived in my openwrt.
I try to ln -s /etc/keepalived/keepalived.conf /tmp/keepalived.conf,and it's failed.
I read keepalived.conf

process_config() {
	local alt_config_file linkbeat_use_polling

	rm -f "$KEEPALIVED_CONF"

	# First line
	printf '! Configuration file for keepalived (autogenerated via init script)\n' > "$KEEPALIVED_CONF"
	printf '! Written %s\n\n' "$(date +'%c')" >> "$KEEPALIVED_CONF"

	[ -f /etc/config/keepalived ] || return 0
	config_load 'keepalived'
	config_get alt_config_file globals alt_config_file

	# If "alt_config_file" specified, use that instead
	[ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
		rm -f "$KEEPALIVED_CONF"
		# Symlink "alt_config_file" since it's a bit easier and safer
		ln -s "$alt_config_file" "$KEEPALIVED_CONF"
		return 0
	}

	config_get_bool linkbeat_use_polling globals linkbeat_use_polling 0
	[ "$linkbeat_use_polling" -gt 0 ] && printf 'linkbeat_use_polling\n\n' >> "$KEEPALIVED_CONF"

	config_section_open "global_defs"
	config_foreach_wrapper globals
	config_section_close

	config_section_open "static_ipaddress"
	config_foreach_wrapper static_ipaddress
	config_section_close

	config_section_open "static_routes"
	config_foreach_wrapper static_routes
	config_section_close

	config_foreach_wrapper vrrp_script
	config_foreach_wrapper vrrp_sync_group
	config_foreach_wrapper vrrp_instance
	config_foreach_wrapper virtual_server
	return 0
}

/tmp/keepalived.conf is created by this code,and it's blank as 1/6.Then,the others codes is over my head. :sob:

Now I'm ban init.d/keepalived,and add /usr/sbin/keepalived -f -n /etc/keepalived/keepalived.conf.

config_get alt_config_file ... reads /etc/config/keepalived value

These lines

[ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
		rm -f "$KEEPALIVED_CONF"
		# Symlink "alt_config_file" since it's a bit easier and safer
		ln -s "$alt_config_file" "$KEEPALIVED_CONF"
		return 0
	}

Make it so that if alt_config_file is specified and exists the /tmp/keepalived.conf is deleted, a symlink is created where it was before, and init script exits

You can try adding echo alt config fille is "$alt_config_file" > /dev/stderr && exit 1 statements right before those lines, and then run the script. The script will then fail, but will print some stuff out that tells you two things... a) script made it to your lines, and b) it'll tell you the parsed value.

You can move that to other places and replace variable name with other stuff you want to print out.

It sounds like there's a typo or a weird bug somewhere.

Thank you for your help.
/etc/init.d/keepalived

	# If "alt_config_file" specified, use that instead   
   echo alt config fille is "$alt_config_file" > /dev/stderr && exit 1 
   	[ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
		rm -f "$KEEPALIVED_CONF"
		# Symlink "alt_config_file" since it's a bit easier and safer
		ln -s "$alt_config_file" "$KEEPALIVED_CONF"
		return 0
}

I run it and /dev/stderr

alt config fille is 

I read /etc/config/luci,find file path use single quotation marks.So I replace this file -- /etc/config/keepalived

config global_defs                                                                             
   option alt_config_file '/etc/keepalived/keepalived.conf'

It's failed again.

And if use the method of the seventh post,system can't shutdown until kill keepalived's processes.

Hi, i'm back.
Now, i delete the config of keepalived, and update keepalived from opkg.
So the version of keepalived is 2.2.2-1,and it work well.
Thanks for all of help.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.