redis 負載均衡 集羣配置

redis 官網 http://redis.io/node

中文網站 http://redis.cn/linux

谷歌代碼的redis項目 https://code.google.com/p/redis/redis

 http://www.oschina.net/p/redis/數據庫

在oschina.net的介紹:緩存

Redis 是一個高性能的key-value數據庫。 redis的出現,很大程度補償了memcached這類keyvalue存儲的不足,在部 分場合能夠對關係數據庫起到很好的補充做用。它提供了Python,Ruby,Erlang,PHP客戶端,使用很方便。

性能測試結果:

SET操做每秒鐘 110000 次,GET操做每秒鐘 81000 次,服務器配置以下:

Linux 2.6, Xeon X3320 2.5Ghz.

stackoverflow 網站使用 Redis 作爲緩存服務器。

我要作的事情就是,在多臺linux服務器中,部署redis,因爲redis的集羣功能沒有徹底編寫好,見:http://redis.io/topics/cluster-spec安全

目前 redis的最新版本是:redis-2.6.13.tar.gzbash

可是redis能夠作一個主從複製的設置,見redis.conf的 複製【REPLICATION】 部分註釋和設置服務器

################################# REPLICATION #################################
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. Note that the configuration is local to the slave
# so for example it is possible to configure the slave to save the DB with a
# different interval, or to listen to another port, and so on.
#
# slaveof <masterip> <masterport>

# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth <master-password>

# When a slave loses its connection with the master, or when the replication
# is still in progress, the slave can act in two different ways:
#
# 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will
#    still reply to client requests, possibly with out of date data, or the
#    data set may just be empty if this is the first synchronization.
#
# 2) if slave-serve-stale-data is set to 'no' the slave will reply with
#    an error "SYNC with master in progress" to all the kind of commands
#    but to INFO and SLAVEOF.
#
slave-serve-stale-data yes
# You can configure a slave instance to accept writes or not. Writing against
# a slave instance may be useful to store some ephemeral data (because data
# written on a slave will be easily deleted after resync with the master) but
# may also cause problems if clients are writing to it because of a
# misconfiguration.
#
# Since Redis 2.6 by default slaves are read-only.
#
# Note: read only slaves are not designed to be exposed to untrusted clients
# on the internet. It's just a protection layer against misuse of the instance.
# Still a read only slave exports by default all the administrative commands
# such as CONFIG, DEBUG, and so forth. To a limited extend you can improve
# security of read only slaves using 'rename-command' to shadow all the
# administrative / dangerous commands.
slave-read-only yes

# Slaves send PINGs to server in a predefined interval. It's possible to change
# this interval with the repl_ping_slave_period option. The default value is 10
# seconds.
#
# repl-ping-slave-period 10

# The following option sets a timeout for both Bulk transfer I/O timeout and
# master data or ping response timeout. The default value is 60 seconds.
#
# It is important to make sure that this value is greater than the value
# specified for repl-ping-slave-period otherwise a timeout will be detected
# every time there is low traffic between the master and the slave.
#
# repl-timeout 60

# Disable TCP_NODELAY on the slave socket after SYNC?
#
# If you select "yes" Redis will use a smaller number of TCP packets and
# less bandwidth to send data to slaves. But this can add a delay for
# the data to appear on the slave side, up to 40 milliseconds with
# Linux kernels using a default configuration.
#
# If you select "no" the delay for data to appear on the slave side will
# be reduced but more bandwidth will be used for replication.
#
# By default we optimize for low latency, but in very high traffic conditions
# or when the master and slaves are many hops away, turning this to "yes" may
# be a good idea.
repl-disable-tcp-nodelay no

# The slave priority is an integer number published by Redis in the INFO output.
# It is used by Redis Sentinel in order to select a slave to promote into a
# master if the master is no longer working correctly.
# A slave with a low priority number is considered better for promotion, so
# for instance if there are three slaves with priority 10, 100, 25 Sentinel will
# pick the one wtih priority 10, that is the lowest.
#
# However a special priority of 0 marks the slave as not able to perform the
# role of master, so a slave with priority of 0 will never be selected by
# Redis Sentinel for promotion.
#
# By default the priority is 100.
slave-priority 100
View Code

看了上面的默認配置,其實也很容易理解,修改下就能夠配置主從複製了,有 slaveof <masterip> <masterport> ,就成了 從服務器 ,沒有就是 主服務器。app

還有 主服務器 安全的設置 less

################################## SECURITY ###################################
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared
# Command renaming.
#
# It is possible to change the name of dangerous commands in a shared
# environment. For instance the CONFIG command may be renamed into something
# hard to guess so that it will still be available for internal-use tools
# but not available for general clients.
#
# Example:
#
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
#
# It is also possible to completely kill a command by renaming it into
# an empty string:
#
# rename-command CONFIG ""
#
# Please note that changing the name of commands that are logged into the
# AOF file or transmitted to slaves may cause problems.
View Code

 

對了,redis的編譯安裝很是簡單,下載redis-2.6.13.tar.gz後,

tar xvf redis-2.6.13.tar.gz

cd redis-2.6.13

make && make install

將會把redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump 五個文件複製到 /usr/local/bin/下

[root@localhost ~]# ll /usr/local/bin
總用量 12464
-rwxr-xr-x. 1 root root 3853805 6月  18 18:54 redis-benchmark
-rwxr-xr-x. 1 root root   16459 6月  18 18:54 redis-check-aof
-rwxr-xr-x. 1 root root   37707 6月  18 18:54 redis-check-dump
-rwxr-xr-x. 1 root root 3909826 6月  18 18:54 redis-cli
-rwxr-xr-x. 1 root root 4931736 6月  18 18:54 redis-server
[root@localhost ~]#

而後將源碼中的 redis.conf 複製到 /etc/redis.conf

再製做一個 init.d 的啓動腳本:

#!/usr/bin/env bash
#
# redis start up the redis server daemon
#
# chkconfig: 345 99 99
# description: redis service in /etc/init.d/redis \
#             chkconfig --add redis or chkconfig --list redis \
#             service redis start  or  service redis stop
# processname: redis-server
# config: /etc/redis.conf

PATH=/usr/local/bin:/sbin:/usr/bin:/bin

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis.pid
CONF="/etc/redis.conf"
#make sure some dir exist
if [ ! -d /var/lib/redis ] ;then
    mkdir -p /var/lib/redis
    mkdir -p /var/log/redis
fi

case "$1" in
    status)
        ps -A|grep redis
        ;;
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        if [ "$?"="0" ]
        then
              echo "Redis is running..."
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $REDIS_CLI -p $REDISPORT SHUTDOWN
                while [ -x ${PIDFILE} ]
               do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
   restart|force-reload)
        ${0} stop
        ${0} start
        ;;
  *)
    echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
        exit 1
esac

將上面內容複製到 /etc/init.d/redis

chkconfig --add redis

chkconfig --list redis

service redis start 

開啓了服務,對了忘記了 /etc/redis.conf裏面能夠把  daemonize no 修改成

 daemonize yes

就能夠默認在後臺執行redis-server了。

這就是主服務器,從服務器,配置同樣,只不過 修改/etc/redis.conf 中 

slaveof <masterip> 6379

而後開啓從服務器的redis服務。

 

測試

#主服務器
redis-cli -p 6379 set hello world

#從服務器

redis-cli -p 6379 get hello
"world"

#主服務器
redis-cli -p 6379 set hello world2

#從服務器

redis-cli -p 6379 get hello
"world2"

redis-cli -p 6379 set hello world
(error) READONLY You can't write against a read only slave.

#成功 配置主從redis 服務器。好簡單啊。

因爲配置中有一條 從服務器 是隻讀的,因此從服務器 無法設置數據,只能夠讀取數據。

相關文章
相關標籤/搜索