Nutcracker (twemproxy)

Homepage: https://github.com/twitter/twemproxy

Install from source

(https://drive.google.com/drive/folders/0B6pVMMV5F5dfMUdJV25abllhUWM):

# cd
# wget http://codingtools.net/uploads/nutcracker-0.4.1.tar.gz
# tar xvzf ./nutcracker-0.4.1.tar.gz
# cd ./nutcracker-0.4.1
# ./configure
# make
# make install

Install on CentOS 7.x from RPM

RHEL 7.x rpms: http://dl.marmotte.net/rpms/redhat/el7/x86_64/twemproxy-0.4.1-0.el7/

# cd
# wget http://dl.marmotte.net/rpms/redhat/el7/x86_64/twemproxy-0.4.1-0.el7/twemproxy-0.4.1-0.el7.x86_64.rpm
# yum install ./twemproxy-0.4.1-0.el7.x86_64.rpm

Configure

# vim /etc/nutcracker.yml
redis:
    listen: /tmp/nutcracker_redis.sock 0666
    hash: fnv1a_64
    distribution: ketama
    redis: true
    timeout: 500
    servers:
        - 127.0.0.1:6379:1
memcached:
    listen: /tmp/nutcracker_memcached.sock 0666
    hash: fnv1a_64
    distribution: ketama
    timeout: 500
    servers:
        - 127.0.0.1:11211:1

 

Systemd (under root):

# vim /etc/systemd/system/nutcracker.service
[Unit]
Description=Nutcracker (twemproxy) redis connection proxy
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/sbin/nutcracker -d -c /etc/nutcracker.yml -s 26379 -a 127.0.0.1 -p /var/run/nutcracker.pid -o /var/log/nutcracker.log

[Install]
WantedBy=multi-user.target

# systemctl start nutcracker
# systemctl status nutcracker
# systemctl enable nutcracker

SysVInit (under nobody user):

# mkdir /var/log/nutcracker
# chown nobody.nobody /var/log/nutcracker
# mkdir /var/run/nutcracker
# chown nobody.nobody /var/run/nutcracker
# vim /etc/sysconfig/nutcracker
OPTIONS="-d -c /etc/nutcracker.yml -s 26379 -a 127.0.0.1 -p /var/run/nutcracker/nutcracker.pid -o /var/log/nutcracker/nutcracker.log"

# vim /etc/init.d/nutcracker

#! /bin/sh
#
# chkconfig: - 55 45
# description:  Twitter's twemproxy nutcracker
# processname: nutcracker
# config: /etc/sysconfig/nutcracker

# Source function library.
. /etc/rc.d/init.d/functions

USER="nobody"
OPTIONS="-d -c /etc/nutcracker.yml"

if [ -f /etc/sysconfig/nutcracker ];then
    . /etc/sysconfig/nutcracker
fi

# Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
    exit 0
fi

RETVAL=0
prog="nutcracker"
exec="/usr/local/sbin/${prog}"

start () {
    echo -n $"Starting $prog: "
    #Test the config before start.
    daemon ${exec} $OPTIONS -t >/dev/null  2>&1
    RETVAL=$?
    if [ $RETVAL -ne 0 ] ; then
        echo  "Config check fail! Please  use 'nutcracker -c /etc/nutcracker.yml' for detail."
        echo_failure;
        echo; 
        exit 1 
    fi

    daemon --user ${USER} ${exec} $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/${prog}
}
stop () {
    echo -n $"Stopping $prog: "
    killproc ${prog}
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ] ; then
        rm -f /var/lock/subsys/${prog}
    fi
}

restart () {
    stop
    start
}


# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status ${prog}
    ;;
  restart|reload)
    restart
    ;;
  condrestart)
    [ -f /var/lock/subsys/nutcracker ] && restart || :
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
    exit 1
esac

exit $?

# service nutcracker start
# service nutcracker status
# telnet 127.0.0.1 26379
# chkconfig --add nutcracker
# chkconfig nutcracker on