Skip to content

Commit

Permalink
New reload strategy - reusesocket
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmoraisjr committed Dec 17, 2017
1 parent ed2e7f0 commit 72fea25
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ The `--reload-strategy` command-line argument is used to select which reload str
HAProxy should use. The following options are available:

* `native`: Uses native HAProxy reload option `-sf`. This is the default option.
* `reusesocket`: (starting on v0.6) Uses HAProxy `-x` command-line option to pass the listening sockets between old and new HAProxy process, allowing hitless reloads.
* `multibinder`: Uses GitHub's [multibinder](https://github.com/github/multibinder). This [link](https://githubengineering.com/glb-part-2-haproxy-zero-downtime-zero-delay-reloads-with-multibinder/)
describes how it works.

Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (haproxy *HAProxyController) UpdateIngressStatus(*extensions.Ingress) []api
// command line arguments
func (haproxy *HAProxyController) ConfigureFlags(flags *pflag.FlagSet) {
haproxy.reloadStrategy = flags.String("reload-strategy", "native",
`Name of the reload strategy. Options are: native (default) or multibinder`)
`Name of the reload strategy. Options are: native (default), reusesocket or multibinder`)
ingressClass := flags.Lookup("ingress-class")
if ingressClass != nil {
ingressClass.Value.Set("haproxy")
Expand All @@ -112,7 +112,7 @@ func (haproxy *HAProxyController) ConfigureFlags(flags *pflag.FlagSet) {

// OverrideFlags allows controller to override command line parameter flags
func (haproxy *HAProxyController) OverrideFlags(flags *pflag.FlagSet) {
if *haproxy.reloadStrategy == "native" {
if *haproxy.reloadStrategy == "native" || *haproxy.reloadStrategy == "reusesocket" {
haproxy.configFile = "/etc/haproxy/haproxy.cfg"
haproxy.template = newTemplate("haproxy.tmpl", "/etc/haproxy/template/haproxy.tmpl")
} else if *haproxy.reloadStrategy == "multibinder" {
Expand Down
2 changes: 1 addition & 1 deletion rootfs/etc/haproxy/template/haproxy.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{{- $cfg := .Cfg -}}
global
daemon
stats socket {{ $cfg.StatsSocket }} level admin
stats socket {{ $cfg.StatsSocket }} level admin expose-fd listeners
{{- if $cfg.LoadServerState }}
server-state-file state-global
server-state-base /var/lib/haproxy/
Expand Down
14 changes: 14 additions & 0 deletions rootfs/haproxy-reload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
# native <.cfg>
# Uses native HAProxy soft restart. Running it for the first time starts
# HAProxy, each subsequent invocation will perform a soft-reload.
# reusesocket <.cfg>
# Pass the listening sockets to the new HAProxy process instead of
# rebinding them, allowing hitless reloads.
# multibinder <.cfg.erb>
# Used on multibinder deployment. Send USR2 to the
# multibinder-haproxy-wrapper process.
Expand All @@ -30,6 +33,7 @@
# -D run as daemon
# -sf soft reload, wait for pids to finish handling requests
# send pids a resume signal if reload of new config fails
# -x get the listening sockets from the old HAProxy process

set -e

Expand All @@ -47,6 +51,16 @@ case "$1" in
HAPROXY_PID=/var/run/haproxy.pid
haproxy -f "$CONFIG" -p "$HAPROXY_PID" -D -sf $(cat "$HAPROXY_PID" 2>/dev/null || :)
;;
reusesocket)
CONFIG="$2"
HAPROXY_PID=/var/run/haproxy.pid
OLD_PID=$(cat "$HAPROXY_PID" 2>/dev/null || :)
if [ -S "$HAPROXY_SOCKET" ]; then
haproxy -f "$CONFIG" -p "$HAPROXY_PID" -sf $OLD_PID -x "$HAPROXY_SOCKET"
else
haproxy -f "$CONFIG" -p "$HAPROXY_PID" -sf $OLD_PID
fi
;;
multibinder)
HAPROXY=/usr/local/sbin/haproxy
CONFIG="$2"
Expand Down
2 changes: 1 addition & 1 deletion rootfs/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ init() {
)
reloadStrategy="${reloadStrategy:-native}"
case "$reloadStrategy" in
native)
native|reusesocket)
;;
multibinder)
HAPROXY=/usr/local/sbin/haproxy
Expand Down

0 comments on commit 72fea25

Please sign in to comment.