本文共 8061 字,大约阅读时间需要 26 分钟。
目录
关于Redis复制和Redis哨兵的主题仍然是——使用Python的库的几个例子。
以前的系列帖子:
可以在此处找到Python的所有Redis客户端——。
首先,我们将使用没有哨兵的,只是为了检查它是如何工作的。
然后——我们将启动哨兵并检查主机和从机的发现。
在上一篇文章中已经配置名为Redis复制和Redis 哨兵的工作环境——这里只是Python示例。
redis-py在Debian上安装:
root@redis-0:/home/admin# apt install python-redis
此外,pip可以使用,但我更喜欢使用普通的存储库。
检查:
root@redis-0:/home/admin# pythonPython 2.7.13 (default, Sep 26 2018, 18:42:22)[GCC 6.3.0 20170516] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import redis>>> r = redis.Redis(host='localhost', port=6379, db=0)>>> r.get('test')Traceback (most recent call last):File "", line 1, in File "/usr/lib/python2.7/dist-packages/redis/client.py", line 880, in getreturn self.execute_command('GET', name)File "/usr/lib/python2.7/dist-packages/redis/client.py", line 573, in execute_commandreturn self.parse_response(connection, command_name, **options)File "/usr/lib/python2.7/dist-packages/redis/client.py", line 585, in parse_responseresponse = connection.read_response()File "/usr/lib/python2.7/dist-packages/redis/connection.py", line 582, in read_responseraise responseredis.exceptions.ResponseError: NOAUTH Authentication required.
好的,有效。
在文档中查找授权:
>>> help(redis.Redis)
添加password:
>>> r = redis.Redis(host='localhost', password='foobared', port=6379, db=0)>>> r.get('test')'test'
好了。
现在运行哨兵实例(如果它们尚未启动),让我们通过Redis 哨兵集群客户端尝试Redis。
sentinel monitor redis-test 52.58.69.184 6379 2sentinel down-after-milliseconds redis-test 6001sentinel failover-timeout redis-test 60000bind 0.0.0.0sentinel auth-pass redis-test foobared
哨兵实例启动后——它将更新配置,所以现在redis-1(这是第一个从机),配置看起来像:
admin@redis-1:~$ cat /etc/redis/sentinel.confsentinel myid e3e62e6577aa975f93346dad3d4f8e25833fd8f1sentinel monitor redis-test 52.29.101.118 6379 2sentinel down-after-milliseconds redis-test 6001bind 0.0.0.0sentinel failover-timeout redis-test 60000Generated by CONFIG REWRITEport 26379dir "/home/admin"sentinel auth-pass redis-test foobaredsentinel config-epoch redis-test 1sentinel leader-epoch redis-test 1sentinel known-slave redis-test 52.58.69.184 6379sentinel known-slave redis-test 35.159.18.26 6379sentinel known-sentinel redis-test 172.31.46.202 26379 e0fe655c59aa3cc32eab1c0858c52418700abe79sentinel known-sentinel redis-test 172.31.41.39 26379 07a450af0d2f178410b78ee0f5ae99ce1cd0ac62sentinel current-epoch 1
检查哨兵群集状态:
root@redis-0:/home/admin# redis-cli -p 26379 info 哨兵
root@redis-0:/home/admin# redis-cli -p 26379 info sentinelSentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=redis-test,status=ok,address=52.58.69.184:6379,slaves=2,sentinels=3
并使用redis-py:
>>> from redis.sentinel import Sentinel>>> sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1)
您可以使用以下discover_master()方法获取主机IP :
>>> sentinel.discover_master('redis-test')('52.58.69.184', 6379)
和从机IP:
>>> sentinel.discover_slaves('redis-test')[('52.29.101.118', 6379), ('35.159.18.26', 6379)]
要使用master,请使用方法:
| master_for(self, service_name, redis_class=, connection_pool_class= , **kwargs)| Returns a redis client instance for the "service_name" master.
>>> master = sentinel.master_for('redis-test', socket_timeout=0.1)
但是如果现在调用master——它会告诉我们我们没有被授权:
>>> master.set('test-key', 'test-value')Traceback (most recent call last):File "", line 1, in File "/usr/lib/python2.7/dist-packages/redis/client.py", line 1072, in setreturn self.execute_command('SET', *pieces)File "/usr/lib/python2.7/dist-packages/redis/client.py", line 573, in execute_commandreturn self.parse_response(connection, command_name, **options)File "/usr/lib/python2.7/dist-packages/redis/client.py", line 585, in parse_responseresponse = connection.read_response()File "/usr/lib/python2.7/dist-packages/redis/sentinel.py", line 55, in read_responsereturn super(SentinelManagedConnection, self).read_response()File "/usr/lib/python2.7/dist-packages/redis/connection.py", line 582, in read_responseraise responseredis.exceptions.ResponseError: NOAUTH Authentication required.
所以在创建master对象时——添加password:
>>> master = sentinel.master_for('redis-test', socket_timeout=0.1, password='foobared')
现在检查:
>>> master.set('test-key', 'test-value')True
此外,您可以获得整个主机的配置:
>>> master.config_get(){'appendonly': 'no', ... 'slave-announce-port': '0'}
或者以更易读的视图:
>>> for i in master.config_get():... print(i)...appendonlyrequirepassdaemonizeprotected-modezset-max-ziplist-entrieszset-max-ziplist-valuedirslave-serve-stale-datacluster-require-full-coverageslowlog-log-slower-thanmasterauthrdbchecksum...
在从机上——检查我们之前添加的密钥:
admin@redis-1:~$ redis-cli -a foobared get test-key"test-value"
使用从机与主机方式类似:
>>> slave = sentinel.slave_for('redis-test', socket_timeout=0.1, password='foobared')>>> slave.get('test-key')'test-value'
我们现在来看看主机:
>>> sentinel.discover_master('redis-test')('52.58.69.184', 6379)
最后,让我们尝试在主机上停止Redis主机redis-0:
root@redis-0:/home/admin# systemctl stop redis-server
或者通过:
root@redis-0:/home/admin# redis-cli -a foobared DEBUG sleep 600
(如果你只是用kill来杀死redis-server进程——哨兵将重新启动它)。
检查哨兵的日志:
10633:X 01 Apr 13:46:10.430 # +sdown master redis-test 52.58.69.184 637910633:X 01 Apr 13:46:10.486 # +odown master redis-test 52.58.69.184 6379 #quorum 2/210633:X 01 Apr 13:46:10.486 # +new-epoch 110633:X 01 Apr 13:46:10.486 # +try-failover master redis-test 52.58.69.184 637910633:X 01 Apr 13:46:10.488 # +vote-for-leader e3e62e6577aa975f93346dad3d4f8e25833fd8f1 110633:X 01 Apr 13:46:10.492 # 07a450af0d2f178410b78ee0f5ae99ce1cd0ac62 voted for e3e62e6577aa975f93346dad3d4f8e25833fd8f1 110633:X 01 Apr 13:46:10.492 # e0fe655c59aa3cc32eab1c0858c52418700abe79 voted for e3e62e6577aa975f93346dad3d4f8e25833fd8f1 110633:X 01 Apr 13:46:10.559 # +elected-leader master redis-test 52.58.69.184 637910633:X 01 Apr 13:46:10.559 # +failover-state-select-slave master redis-test 52.58.69.184 637910633:X 01 Apr 13:46:10.611 # +selected-slave slave 52.29.101.118:6379 52.29.101.118 6379 @ redis-test 52.58.69.184 637910633:X 01 Apr 13:46:10.612 * +failover-state-send-slaveof-noone slave 52.29.101.118:6379 52.29.101.118 6379 @ redis-test 52.58.69.184 637910633:X 01 Apr 13:46:10.664 * +failover-state-wait-promotion slave 52.29.101.118:6379 52.29.101.118 6379 @ redis-test 52.58.69.184 637910633:X 01 Apr 13:46:11.498 # +promoted-slave slave 52.29.101.118:6379 52.29.101.118 6379 @ redis-test 52.58.69.184 637910633:X 01 Apr 13:46:11.498 # +failover-state-reconf-slaves master redis-test 52.58.69.184 637910633:X 01 Apr 13:46:11.557 * +slave-reconf-sent slave 35.159.18.26:6379 35.159.18.26 6379 @ redis-test 52.58.69.184 637910633:X 01 Apr 13:46:12.550 * +slave-reconf-inprog slave 35.159.18.26:6379 35.159.18.26 6379 @ redis-test 52.58.69.184 637910633:X 01 Apr 13:46:12.550 * +slave-reconf-done slave 35.159.18.26:6379 35.159.18.26 6379 @ redis-test 52.58.69.184 637910633:X 01 Apr 13:46:12.632 # -odown master redis-test 52.58.69.184 637910633:X 01 Apr 13:46:12.632 # +failover-end master redis-test 52.58.69.184 637910633:X 01 Apr 13:46:12.632 # +switch-master redis-test 52.58.69.184 6379 52.29.101.118 637910633:X 01 Apr 13:46:12.632 * +slave slave 35.159.18.26:6379 35.159.18.26 6379 @ redis-test 52.29.101.118 637910633:X 01 Apr 13:46:12.632 * +slave slave 52.58.69.184:6379 52.58.69.184 6379 @ redis-test 52.29.101.118 637910633:X 01 Apr 13:46:18.647 # +sdown slave 52.58.69.184:6379 52.58.69.184 6379 @ redis-test 52.29.101.118 6379
从Python检查:
>>> sentinel.discover_master('redis-test')('52.29.101.118', 6379)
主机改变了——52.29.101.118——这是redis-1主几。
检查redis-1状态:
admin@redis-1:~$ redis-cli -a foobared info replication
复制角色:master connected_slaves:1 slave0:ip = 35.159.18.26,port = 6379,state = online,offset = 76847,lag = 0 ...
检查redis-0主机上的Redis节点状态——它现在必须成为从机:
root@redis-0:/home/admin# redis-cli -a foobared info replicationReplicationrole:slave...
并尝试使用我们之前创建的相同master对象在redis-0主机上添加Python的新密钥:
>>> master.set('test-key2', 'test-value2')True
校验:
admin@redis-2:~$ redis-cli -a foobared get test-key2"test-value2"
完成。
原文地址:
转载地址:http://hczhj.baihongyu.com/