Skip to content

Added mechanism to specify ip_hash in the upstream directive #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tpcwang
Copy link

@tpcwang tpcwang commented Nov 21, 2015

This should address #221

@Feijk
Copy link

Feijk commented Jan 4, 2016

Any plans on merging this one into the master branch?

@tpcwang tpcwang force-pushed the iphash branch 2 times, most recently from 1fd3ef0 to 388c18a Compare January 11, 2016 17:54
@tpcwang
Copy link
Author

tpcwang commented Jan 11, 2016

To make this more flexible, I changed the environment variable to be set at the individual container level.

@stanislavb
Copy link

This is awesome. I have been globally inserting ip_hash, but this change makes it possible per service. Please merge. :)

@Skysplit
Copy link

+1

@adrianfalleiro
Copy link

Any update on whether this might be merged?

@joshmanders
Copy link

@fabiomontefuscolo
Copy link

+1 to merge tpcwang commit

This is very useful. Sometimes I scale containers with docker-compose scale web=5 and I need to have ip_hash; to let users upload their files correctly. Actually, I override nginx.tmpl, but I have to update this file whenever jwilder releases a new version of his image.

Please, merge tpcwang commit.

@nwinkler
Copy link

For Docker-Compose, I have worked around this by changing the command that is run at container startup:

version: "2"

services:
  nginx:
    image: jwilder/nginx-proxy
    container_name: nginx
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
    ports:
      - "8082:80"
    command: >
      bash -c "

      sed -i -e 's/^\(upstream .*\)/\1 \n     ip_hash;/g' nginx.tmpl ;
      forego start -r
      "

The sed command adds the ip_hash directive into the template's upstream section. This allows to just add the one change, without having to copy/map the whole template.

@dotw
Copy link

dotw commented Oct 19, 2017

I need this feature too. Why this task still pending? we like tpcwang commit. please merge

@dotw
Copy link

dotw commented Oct 19, 2017

@nwinkler the solution is good but sometime I only want to stick one service not all. Your option is hash all services.

Copy link
Collaborator

@jwilder jwilder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of issues:

  1. Needs a rebase
  2. Needs documentation added to the README about how to enable this, what it actually does and why someone might need it.
  3. bats tests were removed in favor of a python testing framework. We'll need to switch to those test since the bats ones do not run anymore.

@ripper2hl
Copy link

I need this feature please resolve the conflicts :(

@ricktap
Copy link

ricktap commented Nov 7, 2018

Hi,

as I needed this feature right here and now, but didn't have the time to make it a propert PR, I took @nwinkler's composer file and combined it with the informations provided by @tpcwang's pullrequest.
You only have to change @nwinkler's command part to:

command: >
      bash -c "
      sed -i -e 's/^\(upstream .*\)/{{ $$ip_hash := or (first (groupByKeys $$containers \"Env.USE_IP_HASH\")) \"0\" }}\n\n\1 \n{{ if ne $$ip_hash \"0\" }} \n     ip_hash;\n{{ end }}/g' nginx.tmpl ;
      forego start -r
      "

With this change in place, you can now select the ip affinity per service/container by setting the USE_IP_ENV variable to 1.

It's not the most elegant solution, but it should survive future updates to the nginx.tmpl. If I find the time, I'll probably try to takle the PR in the coming days/weeks, but I can't make any promises on that.

@MohammedEssehemy
Copy link

@ricktap

With this change in place, you can now select the ip affinity per service/container by setting the USE_IP_ENV variable to 1.

I think you meant USE_IP_HASH

@kholisrag
Copy link

+1 for this feature...

@rickwierenga
Copy link

What is needed to merge this?

@JuanGamez2W
Copy link

I managed to get this configured with a modifications on the script provided by @ricktap

Here is the script I used as entrypoint in the docker-compose file

#!/bin/bash
set -e
main() {
    template_file="/app/nginx.tmpl"
    if ! grep -q "ip_hash;" "$template_file"; then
        echo "Changing config..."
        sed -i -e 's/^\(upstream .*\)/{{ $ip_hash := or (first (groupByKeys .Containers \"Env.USE_IP_HASH\")) \"0\" }}\n\n\1 \n{{ if ne $ip_hash \"0\" }} \n     ip_hash;\n{{ end }}/g' $template_file
        echo "Change done. Starting..."
    else
        echo "File is already adapted. Starting..."
    fi
    forego start -r
}

main "$@"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/feat PR for a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.