1. 程式人生 > >redis安裝及cluster叢集環境搭建

redis安裝及cluster叢集環境搭建

===========================redis安裝=========================== 1.下載redis包redis-3.2.1.tar.gz 2.將redis-3.2.1.tar.gz包拷貝到/usr/local下,並解壓 3.修改資料夾名為redis [[email protected] local]# mv redis-3.2.1 redis 4.切換到redis目錄下 [[email protected] local]# cd redis 5.編譯安裝(指定安裝路徑) [[email protected] redis]#make PREFIX=/usr/local/redis install 6.等待安裝完畢之後,執行bin資料夾下的redis-server啟動檔案即可啟動redis
[[email protected] redis]# bin/redis-server 61294:C 25 Jul 14:12:14.583 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 61294:C 25 Jul 14:12:14.583 # Redis version=4.0.0, bits=64, commit=00000000, modified=0, pid=61294, just started 61294:C 25 Jul 14:12:14.583 # Warning: no config file specified, using the default config. In order to specify a config file use bin/redis-server /path/to/redis.conf _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.0 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 61294 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 61294:M 25 Jul 14:12:14.585 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 61294:M 25 Jul 14:12:14.585 # Server initialized 61294:M 25 Jul 14:12:14.585 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 61294:M 25 Jul 14:12:14.585 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 61294:M 25 Jul 14:12:14.585 * Ready to accept connections 7.
修改redis.conf配置檔案, daemonize yes 以後端模式啟動。 [[email protected] redis]# bin/redis-server redis.conf & ---------------------------------------------分割線----------------------------------------------------- ==========================redis叢集============================ 叢集採用的2臺伺服器,每臺伺服器上開啟3個redis節點叢集。 1.ruby環境
redis叢集管理工具redis-trib.rb依賴ruby環境,首先需要安裝ruby環境:     安裝ruby       yum install ruby       yum install rubygems 安裝ruby和redis的介面程式 gem install redis(內網環境安裝不了) 到網上下載redis-3.2.1.gem包,使用gem命令安裝 [[email protected] ~]# gem install /home/app/redis-3.2.1.gem 2.叢集環境建立 172.19.2.159搭建: 1)在/usr/local下輸入mkdir redis_cluster //建立叢集目錄 2)在 redis_cluster資料夾下建立 6379,6380,6381 三個資料夾 3)將/usr/local/reids下面的redis.conf配置檔案分別給6379,6380,6381 三個資料夾拷貝一份 [[email protected] redis_cluster]# cp /usr/local/redis/redis.conf ./6379 4)修改6379,6380,6381 三個資料夾下的配置檔案: daemonize yes //設定redis後臺執行 pidfile /var/run/redis_6379.pid //pidfile檔案對應修改為6379,6380,6381 port 6379 //使用埠6379,6380,6381 cluster-enabled yes //開啟叢集 cluster-config-file nodes_6379.conf //叢集的配置 配置檔案首次啟動自動生成 6379,6380,6381 cluster-node-timeout 5000 //請求超時 設定5秒夠了 bind 172.19.2.159 //修改繫結地址,設定為本機的虛擬IP,叢集的時候需要用到 masterauth sound_box123 //設定master密碼 requirepass sound_box123 //設定登陸驗證密碼 5)修改gems客戶端密碼 vim /usr/lib/ruby/gems/1.8/gems/redis-3.2.1/lib/redis/client.rb 修改 password=“sound_box123” 6)切換到redis安裝目錄下的bin資料夾,啟動所有節點 [[email protected] bin]# ./redis-server /usr/local/redis_cluster/6379/redis.conf [[email protected] bin]# ./redis-server /usr/local/redis_cluster/6380/redis.conf [[email protected] bin]# ./redis-server /usr/local/redis_cluster/6381/redis.conf 檢視所有節點狀態: [[email protected] src]# ps -ef|grep redis root 78784 1 0 11:19 ? 00:00:00 ./redis-server 172.19.2.159:6379 [cluster] root 78788 1 0 11:19 ? 00:00:00 ./redis-server 172.19.2.159:6380 [cluster] root 78792 1 0 11:19 ? 00:00:00 ./redis-server 172.19.2.159:6381 [cluster] 172.19.2.158搭建: 搭建方法與172.19.2.159一樣,只是分配埠的時候使用6382、6383、6384三個埠。 3.redis叢集啟動: 登陸到172.19.2.159伺服器,進入到/usr/local/redis/src目錄下,執行命令: ./redis-trib.rb create --replicas 1 172.19.2.159:6379 172.19.2.159:6380 172.19.2.159:6381 172.19.2.158:6382 172.19.2.158:6383 172.19.2.158:6384 解釋下, --replicas  1  表示 自動為每一個master節點分配一個slave節點    上面有6個節點,程式會按照一定規則生成 3個master(主)3個slave(從)     前面已經提醒過的 防火牆一定要開放監聽的埠,否則會建立失敗。 [[email protected] src]# ./redis-trib.rb create --replicas 1 172.19.2.159:6379 172.19.2.159:6380 172.19.2.159:6381 172.19.2.158:6382 172.19.2.158:6383 172.19.2.158:6384 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 172.19.2.158:6382 172.19.2.159:6379 172.19.2.158:6383 Adding replica 172.19.2.159:6380 to 172.19.2.158:6382 Adding replica 172.19.2.158:6384 to 172.19.2.159:6379 Adding replica 172.19.2.159:6381 to 172.19.2.158:6383 M: 76f3069d3efc460f2fb0b84e3ac06e9ccb79f265 172.19.2.159:6379 slots:5461-10922 (5462 slots) master S: 1b593d385f04afd0503c43b88740dbfc3669cd03 172.19.2.159:6380 replicates 41f093d1363a9d9b6ffc7ee68abf5a243713d668 S: 73c6fa6ddd9ed9040a9594b58c8e0a9183b12ba3 172.19.2.159:6381 replicates ea4a71b51f893699f92d94a7b438708042070dee M: 41f093d1363a9d9b6ffc7ee68abf5a243713d668 172.19.2.158:6382 slots:0-5460 (5461 slots) master M: ea4a71b51f893699f92d94a7b438708042070dee 172.19.2.158:6383 slots:10923-16383 (5461 slots) master S: 5fafc112f5ef474707151651ec337639cd5c64c9 172.19.2.158:6384 replicates 76f3069d3efc460f2fb0b84e3ac06e9ccb79f265 Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join... >>> Performing Cluster Check (using node 172.19.2.159:6379) M: 76f3069d3efc460f2fb0b84e3ac06e9ccb79f265 172.19.2.159:6379 slots:5461-10922 (5462 slots) master M: 1b593d385f04afd0503c43b88740dbfc3669cd03 172.19.2.159:6380 slots: (0 slots) master replicates 41f093d1363a9d9b6ffc7ee68abf5a243713d668 M: 73c6fa6ddd9ed9040a9594b58c8e0a9183b12ba3 172.19.2.159:6381 slots: (0 slots) master replicates ea4a71b51f893699f92d94a7b438708042070dee M: 41f093d1363a9d9b6ffc7ee68abf5a243713d668 172.19.2.158:6382 slots:0-5460 (5461 slots) master M: ea4a71b51f893699f92d94a7b438708042070dee 172.19.2.158:6383 slots:10923-16383 (5461 slots) master M: 5fafc112f5ef474707151651ec337639cd5c64c9 172.19.2.158:6384 slots: (0 slots) master replicates 76f3069d3efc460f2fb0b84e3ac06e9ccb79f265 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. 叢集測試: [[email protected] bin]# ./redis-cli -c -h 172.19.2.158 -p 6382 -a sound_box123 172.19.2.158:6382> set test1 "this is test" OK 172.19.2.158:6382> exit [[email protected] bin]# ./redis-cli -c -h 172.19.2.159 -p 6379 -a sound_box123 172.19.2.159:6379> get test1 -> Redirected to slot [4768] located at 172.19.2.158:6382 "this is test" 172.19.2.158:6382>