问题原文
原帖链接 · TroyWorks · CC BY-SA 3.0
Trying to figure out why environmental variables in haproxy.cfg aren't working in HA-Proxy version 1.5.2
on the command line, using Printenv I get a list of environmental variables like FE_PORT_8000_TCP_ADDR=172.17.0.4
Which I need to use in the haproxy.cfg. According to this and the docs How can I use environment variables in haproxy.conf using $FE_PORT_8000_TCP_ADDR or ${FE_PORT_8000_TCP_ADDR} should work. However that is not working.
In Haporxy.cfg hardcoding DOES work, and accessed in a browser it shows as expected:
backend FE
# balance roundrobin
server FE1 172.17.0.4:8000 maxconn 256
But environmental variable with same supposed value doesn't, in the browser it gives 503 Service Unavailable.
backend FE
# balance roundrobin
server FE1 $FE_PORT_8000_TCP_ADDR:8000 maxconn 256
Any ideas on what is being done wrong?
UPDATE: This person has what looks like the same issue How can I use environment variables in haproxy.conf
已采纳解答
原帖链接 · notpeter · CC BY-SA 3.0
Since you're stopping/starting with the service command, you need to specify the environment variables in your init script (e.g. /etc/init.d/haproxy on ubuntu) not in your interactive terminal session where you're controlling the service (service haproxy start). You can verify the environment variables available to a specific pid in the proc filesytem. If you check yours for haproxy, it'll likely be only TERM and LANG, because that's the only environment which gets passed via service to the init script (manpage for service).
# cat /proc/$(pgrep haproxy)/environ
If instead of starting daemonized haproxy from the init script you directly run haproxy you'll likely see the behavior you're looking for:
# haproxy -f /etc/haproxy/haproxy.cfg
To solve this, edit the init script /etc/init.d/haproxy and set your variables there:
export FE_PORT_8000_TCP_ADDR=172.17.0.4
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
