第十一周作业-N67044-张铭扬
1. redis主从复制原理?
1 从节点1、2 2 127.0.0.1:6379> replicaof 10.0.0.157 6379 3 OK 4 127.0.0.1:6379> config set masterauth 123456 5 OK 6 127.0.0.1:6379> info 7 # Replication 8 role:slave 9 master_host:10.0.0.157 10 master_port:6379 11 master_link_status:up 12 13 127.0.0.1:6379> keys * 14 ... 15 99995) "k77506" 16 99996) "k43577" 17 99997) "k64419" 18 99998) "k96650" 19 99999) "k96858" 20 100000) "k13323"
1 主节点 2 127.0.0.1:6379> info replication 3 # Replication 4 role:master 5 connected_slaves:2 6 slave0:ip=10.0.0.156,port=6379,state=online,offset=630,lag=1 7 slave1:ip=10.0.0.158,port=6379,state=online,offset=630,lag=1 8 master_failover_state:no-failover 9 master_replid:e4dc78895bbf1d8119bf49a8ed5161febe53338a 10 master_replid2:0000000000000000000000000000000000000000 11 master_repl_offset:644 12 second_repl_offset:-1 13 repl_backlog_active:1 14 repl_backlog_size:1048576 15 repl_backlog_first_byte_offset:1 16 repl_backlog_histlen:644
2. redis哨兵实现
[root@centos7 ~]# cd /usr/local/src/ [root@centos7 src]# ls redis-6.2.2 redis-6.2.2.tar.gz [root@centos7 src]# cd redis-6.2.2 [root@centos7 redis-6.2.2]# ls 00-RELEASENOTES COPYING MANIFESTO runtest-cluster src BUGS deps README.md runtest-moduleapi tests CONDUCT INSTALL redis.conf runtest-sentinel TLS.md CONTRIBUTING Makefile runtest sentinel.conf utils [root@centos7 redis-6.2.2]# cp sentinel.conf /apps/redis/etc/ [root@centos7 redis-6.2.2]# vim /apps/redis/etc/sentinel.conf bind 0.0.0.0 port 26379 daemonize yes pidfile /apps/redis/run/redis-sentinel.pid logfile "/apps/redis/log/sentinel_26379.log" sentinel monitor mymaster 10.0.0.157 6379 2 sentinel auth-psaa mymaster 123456 sentinel down-after-milliseconds mymaster 3000 #修改完后将配置文件传到其余的节点 修改sentinel.service文件 [root@centos7 ~]# cat /lib/systemd/system/redis-sentinel.service [Unit] Description=Redis Sentinel After=network.target [Service] ExecStart=/apps/redis/bin/redis-sentinel /apps/redis/etc/redis-sentinel.conf --supervised systemd ExecStop=/bin/kill -s QUIT $MAINPID User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 [Install] WantedBy=multi-user.target 三个节点启动redis-sentinel.service # systemctl start redis-sentinel.service 关闭主节点 [root@centos7 etc]# redis-cli -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> shutdown not connected> 新主节点 127.0.0.1:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=10.0.0.158,port=6379,state=online,offset=748637,lag=1 master_failover_state:no-failover master_replid:fbfa0d5aa02f2e97867345d0d4dd3ec13a263780 master_replid2:65b064fc605e9a1c9747603853a3cc509e57d51f master_repl_offset:748637 second_repl_offset:737461 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:159 repl_backlog_histlen:748479
127.0.0.1:6379> set age 18
OK
从节点
127.0.0.1:6379> get age
"18"
重启关闭的节点 [root@centos7 etc]# redis-cli -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> info replication # Replication role:slave master_host:10.0.0.156 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:1101451 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:fbfa0d5aa02f2e97867345d0d4dd3ec13a263780 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:1101451 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1100214 repl_backlog_histlen:1238 127.0.0.1:6379> get age "18"
3. redis集群搭建,迁移,扩容
redis集群搭建(原生命令)
每个节点安装redis 修改以下配置 masterauth 123456 cluster-enabled yes cluster-config-file nodes-6379.conf cluster-require-full-coverage no 重启服务 systemctl restart redis [root@centos7 ~]# ps aux | grep redis redis 5473 0.0 0.1 195548 3948 ? Ssl 22:45 0:00 /apps/redis/bin/redis-server 0.0.0.0:6379 [cluster] root 5480 0.0 0.0 112812 980 pts/0 S+ 22:45 0:00 grep --color=auto redis 在其中一个节点与其他节点进行通信 [root@node1 ~]# redis-cli -a 123456 --no-auth-warning cluster meet 10.0.0.157 6379 OK [root@node1 ~]# redis-cli -a 123456 --no-auth-warning cluster meet 10.0.0.158 6379 OK [root@node1 ~]# redis-cli -a 123456 --no-auth-warning cluster meet 10.0.0.159 6379 OK [root@node1 ~]# redis-cli -a 123456 --no-auth-warning cluster meet 10.0.0.171 6379 OK [root@node1 ~]# redis-cli -a 123456 --no-auth-warning cluster meet 10.0.0.172 6379 OK [root@node1 ~]# redis-cli -a 123456 cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 540516862020d0488f3a7b1ed5b188b624527f4a 10.0.0.157:6379@16379 master - 0 1675868398000 1 connected 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 10.0.0.158:6379@16379 master - 0 1675868398000 2 connected e37097952378288720c436aebf4f01e4096005c0 10.0.0.156:6379@16379 myself,master - 0 1675868399000 0 connected cc22270e6c3ca563c669f3d8f8ba3bcc06f13e28 10.0.0.172:6379@16379 master - 0 1675868399888 5 connected 83bf279100fd098932f737f40f1b6f1a3e69474d 10.0.0.171:6379@16379 master - 0 1675868398879 4 connected b328f1aef7d514f8760c10d2e13c5466ed9f4dfc 10.0.0.159:6379@16379 master - 0 1675868400897 3 connected
分配槽点 root@node1 ~]# cat addslot.sh #!/bin/bash host=$1 port=$2 start=$3 end=$4 pass=123456 for slot in `seq ${start} ${end}`;do echo slot:$slot redis-cli -h ${host} -p $port -a ${pass} --no-auth-warning cluster addslots ${slot} done [root@node1 ~]# ./addslot.sh 10.0.0.156 6379 0 5461 [root@node1 ~]# ./addslot.sh 10.0.0.157 6379 5462 10922 [root@node1 ~]# ./addslot.sh 10.0.0.158 6379 10923 16383 [root@node1 ~]# redis-cli -a 123456 --no-auth-warning cluster nodes 540516862020d0488f3a7b1ed5b188b624527f4a 10.0.0.157:6379@16379 master - 0 1675951324000 1 connected 5462-10922 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 10.0.0.158:6379@16379 master - 0 1675951324000 2 connected 10923-16383 e37097952378288720c436aebf4f01e4096005c0 10.0.0.156:6379@16379 myself,master - 0 1675951322000 0 connected 0-5461 cc22270e6c3ca563c669f3d8f8ba3bcc06f13e28 10.0.0.172:6379@16379 master - 0 1675951324715 5 connected 83bf279100fd098932f737f40f1b6f1a3e69474d 10.0.0.171:6379@16379 master - 0 1675951323708 4 connected b328f1aef7d514f8760c10d2e13c5466ed9f4dfc 10.0.0.159:6379@16379 master - 0 1675951320000 3 connected [root@node1 ~]# redis-cli -h 10.0.0.159 -a 123456 --no-auth-warning cluster replicate e37097952378288720c436aebf4f01e4096005c0 OK [root@node1 ~]# redis-cli -h 10.0.0.171 -a 123456 --no-auth-warning cluster replicate 540516862020d0488f3a7b1ed5b188b624527f4a OK [root@node1 ~]# redis-cli -h 10.0.0.172 -a 123456 --no-auth-warning cluster replicate 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 OK [root@node1 ~]# redis-cli -a 123456 --no-auth-warning cluster nodes 540516862020d0488f3a7b1ed5b188b624527f4a 10.0.0.157:6379@16379 master - 0 1675952591000 1 connected 5462-10922 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 10.0.0.158:6379@16379 master - 0 1675952592487 2 connected 10923-16383 e37097952378288720c436aebf4f01e4096005c0 10.0.0.156:6379@16379 myself,master - 0 1675952589000 0 connected 0-5461 cc22270e6c3ca563c669f3d8f8ba3bcc06f13e28 10.0.0.172:6379@16379 slave 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 0 1675952588455 2 connected 83bf279100fd098932f737f40f1b6f1a3e69474d 10.0.0.171:6379@16379 slave 540516862020d0488f3a7b1ed5b188b624527f4a 0 1675952591478 1 connected b328f1aef7d514f8760c10d2e13c5466ed9f4dfc 10.0.0.159:6379@16379 slave e37097952378288720c436aebf4f01e4096005c0 0 1675952591000 0 connected [root@node1 ~]# redis-cli -a 123456 --no-auth-warning cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:5 cluster_my_epoch:0 cluster_stats_messages_ping_sent:10882 cluster_stats_messages_pong_sent:11148 cluster_stats_messages_meet_sent:5 cluster_stats_messages_sent:22035 cluster_stats_messages_ping_received:11148 cluster_stats_messages_pong_received:10887 cluster_stats_messages_received:22035
新版本创建集群
搭建后集群后分配槽点 [root@centos7 ~]# redis-cli -a 123456 --cluster create 10.0.0.156:6379 10.0.0.157:6379 10.0.0.158:6379 10.0.0.159:6379 10.0.0.171:6379 10.0.0.172:6379 --cluster-replicas 1 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 10.0.0.171:6379 to 10.0.0.156:6379 Adding replica 10.0.0.172:6379 to 10.0.0.157:6379 Adding replica 10.0.0.159:6379 to 10.0.0.158:6379 M: 284e9791656dbac33cb1d6e83568a60e8d84d230 10.0.0.156:6379 slots:[0-5460] (5461 slots) master M: c3f495b7803eb75db4f2c50e6042c5688a09d194 10.0.0.157:6379 slots:[5461-10922] (5462 slots) master M: 5ce656697e443c63dcc731129373ba0bac6a5747 10.0.0.158:6379 slots:[10923-16383] (5461 slots) master S: 1690099cd947202933875611ca2901c2ddb8597b 10.0.0.159:6379 replicates 5ce656697e443c63dcc731129373ba0bac6a5747 S: 36bcd366bf14b8122777accb55d7bd0d014e8230 10.0.0.171:6379 replicates 284e9791656dbac33cb1d6e83568a60e8d84d230 S: 0c4ffe8052798d05a2e6c14be40d73b3445bacba 10.0.0.172:6379 replicates c3f495b7803eb75db4f2c50e6042c5688a09d194 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 10.0.0.156:6379) M: 284e9791656dbac33cb1d6e83568a60e8d84d230 10.0.0.156:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 0c4ffe8052798d05a2e6c14be40d73b3445bacba 10.0.0.172:6379 slots: (0 slots) slave replicates c3f495b7803eb75db4f2c50e6042c5688a09d194 M: c3f495b7803eb75db4f2c50e6042c5688a09d194 10.0.0.157:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 36bcd366bf14b8122777accb55d7bd0d014e8230 10.0.0.171:6379 slots: (0 slots) slave replicates 284e9791656dbac33cb1d6e83568a60e8d84d230 M: 5ce656697e443c63dcc731129373ba0bac6a5747 10.0.0.158:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 1690099cd947202933875611ca2901c2ddb8597b 10.0.0.159:6379 slots: (0 slots) slave replicates 5ce656697e443c63dcc731129373ba0bac6a5747 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. [root@centos7 ~]# redis-cli -a 123456 cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 0c4ffe8052798d05a2e6c14be40d73b3445bacba 10.0.0.172:6379@16379 slave c3f495b7803eb75db4f2c50e6042c5688a09d194 0 1675956965000 2 connected 284e9791656dbac33cb1d6e83568a60e8d84d230 10.0.0.156:6379@16379 myself,master - 0 1675956964000 1 connected 0-5460 c3f495b7803eb75db4f2c50e6042c5688a09d194 10.0.0.157:6379@16379 master - 0 1675956966000 2 connected 5461-10922 36bcd366bf14b8122777accb55d7bd0d014e8230 10.0.0.171:6379@16379 slave 284e9791656dbac33cb1d6e83568a60e8d84d230 0 1675956966212 1 connected 5ce656697e443c63dcc731129373ba0bac6a5747 10.0.0.158:6379@16379 master - 0 1675956965206 3 connected 10923-16383 1690099cd947202933875611ca2901c2ddb8597b 10.0.0.159:6379@16379 slave 5ce656697e443c63dcc731129373ba0bac6a5747 0 1675956964000 3 connected [root@centos7 ~]# redis-cli -a 123456 cluster info Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:91 cluster_stats_messages_pong_sent:99 cluster_stats_messages_sent:190 cluster_stats_messages_ping_received:94 cluster_stats_messages_pong_received:91 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:190
redis集群扩容
把新的节点加入集群中 [root@centos7 ~]# redis-cli -a 123456 --cluster add-node 10.0.0.173:6379 10.0.0.156:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Adding node 10.0.0.173:6379 to cluster 10.0.0.156:6379 >>> Performing Cluster Check (using node 10.0.0.156:6379) M: e37097952378288720c436aebf4f01e4096005c0 10.0.0.156:6379 slots:[0-5461] (5462 slots) master 1 additional replica(s) M: 540516862020d0488f3a7b1ed5b188b624527f4a 10.0.0.157:6379 slots:[5462-10922] (5461 slots) master 1 additional replica(s) M: 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 10.0.0.158:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: cc22270e6c3ca563c669f3d8f8ba3bcc06f13e28 10.0.0.172:6379 slots: (0 slots) slave replicates 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 S: 83bf279100fd098932f737f40f1b6f1a3e69474d 10.0.0.171:6379 slots: (0 slots) slave replicates 540516862020d0488f3a7b1ed5b188b624527f4a S: b328f1aef7d514f8760c10d2e13c5466ed9f4dfc 10.0.0.159:6379 slots: (0 slots) slave replicates e37097952378288720c436aebf4f01e4096005c0 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 10.0.0.173:6379 to make it join the cluster. [OK] New node added correctly. 重新分配槽位 [root@node1 ~]# redis-cli -a 123456 --cluster reshard 10.0.0.156:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing Cluster Check (using node 10.0.0.156:6379) M: d6e2eca6b338b717923f64866bd31d42e52edc98 10.0.0.156:6379 slots: (0 slots) master M: d34da8666a6f587283a1c2fca5d13691407f9462 10.0.0.157:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: d04e524daec4d8e22bdada7f21a9487c2d3e1057 10.0.0.158:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 99720241248ff0e4c6fa65c2385e92468b3b5993 10.0.0.159:6379 slots: (0 slots) slave replicates d04e524daec4d8e22bdada7f21a9487c2d3e1057 M: f67f1c02c742cd48d3f48d8c362f9f1b9aa31549 10.0.0.173:6379 slots: (0 slots) master S: f9adcfb8f5a037b257af35fa548a26ffbadc852d 10.0.0.172:6379 slots: (0 slots) slave replicates cb028b83f9dc463d732f6e76ca6bbcd469d948a7 S: 9875b50925b4e4f29598e6072e5937f90df9fc71 10.0.0.171:6379 slots: (0 slots) slave replicates d34da8666a6f587283a1c2fca5d13691407f9462 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. How many slots do you want to move (from 1 to 16384)?4096 #新分配多少个槽位 =16384/master个数 What is the receiving node ID? d6e2eca6b338b717923f64866bd31d42e52edc98 #新的 master的ID Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1: all #将哪些源主机的槽位分配给新的节点,all是自动在所有的redis node选择划 分,如果是从redis cluster删除某个主机可以使用此方式将指定主机上的槽位全部移动到别的redis主机 ...... Do you want to proceed with the proposed reshard plan (yes/no)? yes #确认分配 ...... Moving slot 12274 from 10.0.0.158:6379 to 10.0.0.173:6379: Moving slot 12275 from 10.0.0.158:6379 to 10.0.0.173:6379: Moving slot 12276 from 10.0.0.158:6379 to 10.0.0.173:6379: Moving slot 12277 from 10.0.0.158:6379 to 10.0.0.173:6379: Moving slot 12278 from 10.0.0.158:6379 to 10.0.0.173:6379: Moving slot 12279 from 10.0.0.158:6379 to 10.0.0.173:6379: Moving slot 12280 from 10.0.0.158:6379 to 10.0.0.173:6379: Moving slot 12281 from 10.0.0.158:6379 to 10.0.0.173:6379: Moving slot 12282 from 10.0.0.158:6379 to 10.0.0.173:6379: Moving slot 12283 from 10.0.0.158:6379 to 10.0.0.173:6379: Moving slot 12284 from 10.0.0.158:6379 to 10.0.0.173:6379: Moving slot 12285 from 10.0.0.158:6379 to 10.0.0.173:6379: Moving slot 12286 from 10.0.0.158:6379 to 10.0.0.173:6379: Moving slot 12287 from 10.0.0.158:6379 to 10.0.0.173:6379: [root@node1 ~]# redis-cli -a 123456 cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 540516862020d0488f3a7b1ed5b188b624527f4a 10.0.0.157:6379@16379 master - 0 1676560040914 1 connected 6827-10922 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 10.0.0.158:6379@16379 master - 0 1676560040000 2 connected 12288-16383 e37097952378288720c436aebf4f01e4096005c0 10.0.0.156:6379@16379 myself,master - 0 1676560037000 0 connected 1366-5461 cc22270e6c3ca563c669f3d8f8ba3bcc06f13e28 10.0.0.172:6379@16379 slave 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 0 1676560037844 2 connected 83bf279100fd098932f737f40f1b6f1a3e69474d 10.0.0.171:6379@16379 slave 540516862020d0488f3a7b1ed5b188b624527f4a 0 1676560039895 1 connected b328f1aef7d514f8760c10d2e13c5466ed9f4dfc 10.0.0.159:6379@16379 slave e37097952378288720c436aebf4f01e4096005c0 0 1676560040000 0 connected adf674e50130ccef555dbcee10a94fd44b4cd10a 10.0.0.173:6379@16379 master - 0 1676560041935 6 connected 0-1365 5462-6826 10923-12287 新加一个从节点进集群 [root@node1 ~]# redis-cli -a 123456 --cluster add-node 10.0.0.174:6379 10.0.0.156:6379 --cluster-slave --cluster-master-id adf674e50130ccef555dbcee10a94fd44b4cd10a Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Adding node 10.0.0.174:6379 to cluster 10.0.0.156:6379 >>> Performing Cluster Check (using node 10.0.0.156:6379) M: e37097952378288720c436aebf4f01e4096005c0 10.0.0.156:6379 slots:[1366-5461] (4096 slots) master 1 additional replica(s) M: 540516862020d0488f3a7b1ed5b188b624527f4a 10.0.0.157:6379 slots:[6827-10922] (4096 slots) master 1 additional replica(s) M: 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 10.0.0.158:6379 slots:[12288-16383] (4096 slots) master 1 additional replica(s) S: cc22270e6c3ca563c669f3d8f8ba3bcc06f13e28 10.0.0.172:6379 slots: (0 slots) slave replicates 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 S: 83bf279100fd098932f737f40f1b6f1a3e69474d 10.0.0.171:6379 slots: (0 slots) slave replicates 540516862020d0488f3a7b1ed5b188b624527f4a S: b328f1aef7d514f8760c10d2e13c5466ed9f4dfc 10.0.0.159:6379 slots: (0 slots) slave replicates e37097952378288720c436aebf4f01e4096005c0 M: adf674e50130ccef555dbcee10a94fd44b4cd10a 10.0.0.173:6379 slots:[0-1365],[5462-6826],[10923-12287] (4096 slots) master [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 10.0.0.174:6379 to make it join the cluster. Waiting for the cluster to join >>> Configure node as replica of 10.0.0.173:6379. [OK] New node added correctly. [root@node1 ~]# redis-cli -a 123456 cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 540516862020d0488f3a7b1ed5b188b624527f4a 10.0.0.157:6379@16379 master - 0 1676560596000 1 connected 6827-10922 b04b6bae61504ed41c0f8ee4d213c020eb44f07c 10.0.0.174:6379@16379 slave adf674e50130ccef555dbcee10a94fd44b4cd10a 0 1676560598000 6 connected 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 10.0.0.158:6379@16379 master - 0 1676560597000 2 connected 12288-16383 e37097952378288720c436aebf4f01e4096005c0 10.0.0.156:6379@16379 myself,master - 0 1676560594000 0 connected 1366-5461 cc22270e6c3ca563c669f3d8f8ba3bcc06f13e28 10.0.0.172:6379@16379 slave 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 0 1676560597299 2 connected 83bf279100fd098932f737f40f1b6f1a3e69474d 10.0.0.171:6379@16379 slave 540516862020d0488f3a7b1ed5b188b624527f4a 0 1676560597000 1 connected b328f1aef7d514f8760c10d2e13c5466ed9f4dfc 10.0.0.159:6379@16379 slave e37097952378288720c436aebf4f01e4096005c0 0 1676560598307 0 connected adf674e50130ccef555dbcee10a94fd44b4cd10a 10.0.0.173:6379@16379 master - 0 1676560594246 6 connected 0-1365 5462-6826 10923-12287 [root@node1 ~]# redis-cli -a 123456 cluster info Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:8 cluster_size:4 cluster_current_epoch:7 cluster_my_epoch:0 cluster_stats_messages_ping_sent:21544 cluster_stats_messages_pong_sent:21880 cluster_stats_messages_meet_sent:5 cluster_stats_messages_fail_sent:5 cluster_stats_messages_update_sent:6 cluster_stats_messages_sent:43440 cluster_stats_messages_ping_received:21878 cluster_stats_messages_pong_received:25640 cluster_stats_messages_meet_received:2 cluster_stats_messages_received:47520
迁移
#所以节点清空密码 [root@node1 ~]# redis-cli -p 6379 -a 123456 --no-auth-warning CONFIG SET requirepass "" OK [root@node1 ~]# redis-cli --cluster import 10.0.0.156:6379 --cluster-from 10.0.0.173:6379 --cluster-copy --cluster-replace >>> Importing data from 10.0.0.173:6379 to cluster 10.0.0.156:6379 >>> Performing Cluster Check (using node 10.0.0.156:6379) M: e37097952378288720c436aebf4f01e4096005c0 10.0.0.156:6379 slots:[0-1364],[1366-5461] (5461 slots) master 1 additional replica(s) M: 540516862020d0488f3a7b1ed5b188b624527f4a 10.0.0.157:6379 slots:[1365],[5462-6825],[6827-10922] (5461 slots) master 1 additional replica(s) M: 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 10.0.0.158:6379 slots:[6826],[10923-16383] (5462 slots) master 1 additional replica(s) S: cc22270e6c3ca563c669f3d8f8ba3bcc06f13e28 10.0.0.172:6379 slots: (0 slots) slave replicates 258bf4841b23c2a6a0ce87ce6cd5a688e1848b20 S: 83bf279100fd098932f737f40f1b6f1a3e69474d 10.0.0.171:6379 slots: (0 slots) slave replicates 540516862020d0488f3a7b1ed5b188b624527f4a S: b328f1aef7d514f8760c10d2e13c5466ed9f4dfc 10.0.0.159:6379 slots: (0 slots) slave replicates e37097952378288720c436aebf4f01e4096005c0 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. *** Importing 100 keys from DB 0 Migrating c48 to 10.0.0.158:6379: OK Migrating c10 to 10.0.0.156:6379: OK Migrating c94 to 10.0.0.156:6379: OK Migrating c58 to 10.0.0.156:6379: OK Migrating c79 to 10.0.0.158:6379: OK . . . Migrating c61 to 10.0.0.156:6379: OK Migrating c1 to 10.0.0.158:6379: OK Migrating c46 to 10.0.0.157:6379: OK Migrating c67 to 10.0.0.158:6379: OK Segmentation fault
4. zabbix原理?
zabbix由几个主要的软件组件构成
server: zabbix server是一个核心组件,agent可以向它报告可用性和完整性信息和统计数据。该server所有的配置,数据和业务数据都存在数据库中。
数据库存储:所有的配置信息包括采集的数据都被zabbix存储在数据库中。
Web界面: 通过一个基于web的平台,我们可以从任何一个地方访问zabbix。这个web是zabbix server的一部分,通常跟server运行在同一台物理机上。(SQLite必须得配置在同一台物理机上)
Proxy: proxy可以代替server收集性能和可用性的数据。proxy是一个可选的部分,但它对于减弱zabbix server的负载非常有用。
agent: zabbix agent被部署在监控的目标上,主机监控本地的资源和应用并汇报数据给zabbix server。
5. zabbix server/proxy和agent配置文件详解。
#server配置文件 root@zabbix-server:~# grep "^[a-Z]" /apps/zabbix_server/etc/zabbix_server.conf LogType=file LogFile=/apps/zabbix_server/logs/zabbix_server.log LogFileSize=500 DebugLevel=3 PidFile=/apps/zabbix_server/run/zabbix_server.pid SocketDir=/apps/zabbix_server/run/ DBHost=10.0.0.164 DBName=zabbix_server DBUser=zabbix DBPassword=123456 SNMPTrapperFile=/apps/zabbix_server/run/zabbix_traps.tmp StartSNMPTrapper=1 HousekeepingFrequency=4 MaxHousekeeperDelete=1000000 CacheSize=126M CacheUpdateFrequency=60 StartDBSyncers=6 HistoryCacheSize=128M HistoryIndexCacheSize=32M TrendCacheSize=16M ValueCacheSize=16M Timeout=30 UnavailableDelay=60 FpingLocation=/usr/bin/fping LogSlowQueries=3000 TmpDir=/apps/zabbix_server/run/ ProxyConfigFrequency=60 AllowRoot=1 User=rootroot@zabbix-server:~# grep "^[a-Z]" /apps/zabbix_server/etc/zabbix_server.conf LogType=file LogFile=/apps/zabbix_server/logs/zabbix_server.log LogFileSize=500 DebugLevel=3 PidFile=/apps/zabbix_server/run/zabbix_server.pid SocketDir=/apps/zabbix_server/run/ DBHost=10.0.0.164 DBName=zabbix_server DBUser=zabbix DBPassword=123456 SNMPTrapperFile=/apps/zabbix_server/run/zabbix_traps.tmp StartSNMPTrapper=1 HousekeepingFrequency=4 MaxHousekeeperDelete=1000000 CacheSize=126M CacheUpdateFrequency=60 StartDBSyncers=6 HistoryCacheSize=128M HistoryIndexCacheSize=32M TrendCacheSize=16M ValueCacheSize=16M Timeout=30 UnavailableDelay=60 FpingLocation=/usr/bin/fping LogSlowQueries=3000 TmpDir=/apps/zabbix_server/run/ ProxyConfigFrequency=60 AllowRoot=1 User=root
#proxy-active配置文件 root@zabbix-proxy-active:~# grep '^[a-Z]' /etc/zabbix/zabbix_proxy.conf ProxyMode=0 Server=10.0.0.161 ServerPort=10051 Hostname=zabbix-proxy-active LogFile=/var/log/zabbix/zabbix_proxy.log LogFileSize=0 PidFile=/var/run/zabbix/zabbix_proxy.pid SocketDir=/var/run/zabbix DBHost=10.0.0.164 DBName=zabbix_proxy_active DBUser=proxy DBPassword=123456 ProxyLocalBuffer=720 ProxyOfflineBuffer=720 HeartbeatFrequency=60 ConfigFrequency=60 DataSenderFrequency=30 StartPollers=5 StartPollersUnreachable=3 StartTrappers=5 StartPingers=3 JavaGateway=10.0.0.162 JavaGatewayPort=10052 StartJavaPollers=5 SNMPTrapperFile=/var/log/snmptrap/snmptrap.log ListenIP=0.0.0.0 Timeout=30 ExternalScripts=/usr/lib/zabbix/externalscripts FpingLocation=/usr/bin/fping Fping6Location=/usr/bin/fping6 LogSlowQueries=3000
#proxy-passive配置文件 root@zabbix-proxy-passive:~# grep '^[a-Z]' /etc/zabbix/zabbix_proxy.conf ProxyMode=1 Server=10.0.0.161 Hostname=zabbix-proxy-passive ListenPort=10051 LogFile=/var/log/zabbix/zabbix_proxy.log LogFileSize=0 PidFile=/var/run/zabbix/zabbix_proxy.pid SocketDir=/var/run/zabbix DBHost=10.0.0.164 DBName=zabbix_proxy_passive DBUser=proxy DBPassword=123456 ProxyLocalBuffer=720 ProxyOfflineBuffer=720 HeartbeatFrequency=60 ConfigFrequency=60 StartPollers=5 StartPollersUnreachable=5 StartTrappers=5 StartPingers=3 JavaGateway=10.0.0.162 JavaGatewayPort=10052 StartJavaPollers=5 SNMPTrapperFile=/var/log/snmptrap/snmptrap.log ListenIP=0.0.0.0 HousekeepingFrequency=1 CacheSize=16M StartDBSyncers=4 HistoryCacheSize=16M Timeout=30 ExternalScripts=/usr/lib/zabbix/externalscripts FpingLocation=/usr/bin/fping Fping6Location=/usr/bin/fping6 LogSlowQueries=3000
#agent配置文件 root@zabbix-web1:~# grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Server=10.0.0.161 ListenPort=10050 ListenIP=0.0.0.0 StartAgents=3 ServerActive=127.0.0.1 Hostname=10.0.0.166 Include=/etc/zabbix/zabbix_agentd.d/*.conf
6. zabbix监控常用服务 nginx,redis, tomcat, haproxy,mysql。
#监控Tomcat root@zabbix-web1:~# apt install openjdk-8-jdk root@zabbix-web1:/apps/apache-tomcat-8.5.73# tar xvf apache-tomcat-8.5.73.tar.gz root@zabbix-web1:/apps/apache-tomcat-8.5.73# vim bin/catalina.sh CATALINA_OPTS="$CATALINA_OPTS -Docm.sun.management.jmxremote -Docm.sun.management.jmxremote.port=12345 -Docm.sun.management.jmxremote.authenticate=false -Docm.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=10.0.0.166" root@zabbix-web1:/apps/apache-tomcat-8.5.73# /apps/apache-tomcat-8.5.73/bin/catalina.sh start Using CATALINA_BASE: /apps/apache-tomcat-8.5.73 Using CATALINA_HOME: /apps/apache-tomcat-8.5.73 Using CATALINA_TMPDIR: /apps/apache-tomcat-8.5.73/temp Using JRE_HOME: /usr Using CLASSPATH: /apps/apache-tomcat-8.5.73/bin/bootstrap.jar:/apps/apache-tomcat-8.5.73/bin/tomcat-juli.jar Using CATALINA_OPTS: -Docm.sun.management.jmxremote -Docm.sun.management.jmxremote.port=12345 -Docm.sun.management.jmxremote.authenticate=false -Docm.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=10.0.0.166 Tomcat started. root@zabbix-web1:/apps/apache-tomcat-8.5.73/bin# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:10050 0.0.0.0:* LISTEN 0 128 127.0.0.53%lo:53 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 50 *:41119 *:* LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:* LISTEN 0 100 *:8080 *:* LISTEN 0 50 *:46483 *:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 50 *:12345 *:*
root@zabbix-server:~# vim /apps/zabbix_server/sbin/zabbix_java/settings.sh root@zabbix-server:~# grep "^[a-Z]" /apps/zabbix_server/sbin/zabbix_java/settings.sh LISTEN_IP="0.0.0.0" LISTEN_PORT=10052 PID_FILE="/tmp/zabbix_java.pid" START_POLLERS=5 TIMEOUT=30 root@zabbix-server:~# /apps/zabbix_server/sbin/zabbix_java/startup.sh root@zabbix-server:~# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 64 0.0.0.0:32981 0.0.0.0:* LISTEN 0 128 127.0.0.53%lo:53 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:54489 0.0.0.0:* LISTEN 0 128 0.0.0.0:57757 0.0.0.0:* LISTEN 0 64 0.0.0.0:2049 0.0.0.0:* LISTEN 0 128 0.0.0.0:10050 0.0.0.0:* LISTEN 0 128 0.0.0.0:10051 0.0.0.0:* LISTEN 0 128 0.0.0.0:111 0.0.0.0:* LISTEN 0 128 0.0.0.0:40465 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 64 [::]:41849 [::]:* LISTEN 0 128 [::]:52603 [::]:* LISTEN 0 64 [::]:2049 [::]:* LISTEN 0 50 *:10052 *:* LISTEN 0 128 [::]:38255 [::]:* LISTEN 0 128 [::]:111 [::]:* LISTEN 0 128 *:80 *:* LISTEN 0 128 [::]:50257 [::]:* root@zabbix-server:~# vim /apps/zabbix_server/etc/zabbix_server.conf JavaGateway=10.0.0.161 JavaGatewayPort=10052 StartJavaPollers=5 root@zabbix-server:~# systemctl restart zabbix-server.service
#nginx location /nginx_status { stub_status; allow 10.0.0.161/16; allow 127.0.0.1; deny all; } ###: root@zabbix-web2:~# vim /etc/zabbix/zabbix_agentd.d/test.conf UserParameter=nginx_status[*],/bin/bash /etc/zabbix/zabbix_agentd.d/nginx_status.sh $1 root@zabbix-web2:/etc/zabbix/zabbix_agentd.d# vim nginx_status.sh #!/bin/bash Check_Writing (){ /usr/bin/curl "http://127.0.0.1:80/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}' } Check_Reading(){ /usr/bin/curl "http://127.0.0.1:80/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $2}' } Check_Waiting(){ /usr/bin/curl "http://127.0.0.1:80/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $6}' } Check_Active(){ /usr/bin/curl "http://127.0.0.1:80/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}' } Check_accepts(){ /usr/bin/curl "http://127.0.0.1:80/nginx_status/" 2>/dev/null| awk NR==3 |awk '{print $1}' } Check_handled(){ /usr/bin/curl "http://127.0.0.1:80/nginx_status/" 2>/dev/null| awk NR==3 |awk '{print $2}' } Check_requests(){ /usr/bin/curl "http://127.0.0.1:80/nginx_status/" 2>/dev/null| awk NR==3 |awk '{print $3}' } case $1 in write) Check_Writing; ;; reading) Check_Reading; ;; active) Check_active; ;; waiting) Check_Waiting; ;; accept) Check_accepts; ;; handled) Check_handled; ;; requests) Check_requests; esac 测试 root@zabbix-server:/etc/apt# /usr/bin/zabbix_get -s 192.168.150.17 -p 10050 -k"nginx_status[handled]" 27 root@zabbix-server:/etc/apt# /usr/bin/zabbix_get -s 192.168.150.17 -p 10050 -k"nginx_status[accept]" 25 root@zabbix-server:/etc/apt# /usr/bin/zabbix_get -s 192.168.150.17 -p 10050 -k"nginx_status[write]" 1
redis root@zabbix-web2:/etc/zabbix/zabbix_agentd.d# cat redis_monitor.sh #!bin/bash redis_status(){ R_PORT=$1 R_COMMAND=$2 (echo -en "INFO\r\n";) | ncat 127.0.0.1 "$R_PORT" > /tmp/redis_"$R_PORT".tmp REDIS_STAT_VALUE=$(grep ""$R_COMMAND":" /tmp/redis_"$R_PORT".tmp | cut -d ':' -f2) echo $REDIS_STAT_VALUE } help(){ echo "${0} + redis_status + PORT +COMMAND" } main(){ case $1 in redis_status) redis_status $2 $3 ;; *) help ;; esac } main $1 $2 $3 root@zabbix-web2:/etc/zabbix/zabbix_agentd.d# cat all.conf UserParameter=redis_monitor[*],/bin/bash /etc/zabbix/zabbix_agentd.d/redis_monitor.sh $1 $2 $3 root@zabbix-server:~# /apps/zabbix_server/bin/zabbix_get -s 10.0.0.167 -p 10050 -k "redis_monitor["redis_status","6379","connected_clients"]" 1
7. zabbix 使用api批量添加主机,脚本支持读csv文件,csv文件可以传递主机名,主机地址,模板名,主机PORT。读取csv遍历每1行添加主机到zabbix web。
root@zabbix-web2:/opt# curl -s -X POST -H 'Content-Type:application/json' -d ' > { > "jsonrpc": "2.0", > "method": "user.login", "params": { > "user": "Admin", > "password": "zabbix" > }, > "id": 1 > }' http://10.0.0.167/zabbix/api_jsonrpc.php | python3 -m json.tool { "jsonrpc": "2.0", "result": "35449513e525509881628285990735ef", "id": 1 } ###获取gid、templete-id、将要加入的主机zabbix-agent配置好 root@zabbix-web2:/opt#curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "host.create", "params": { "host": "API Add Host Test", "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": "10.0.0.168", "dns": "", "port": "10050" } ], "groups": [ { "groupid": "18" } ], "templates": [ { "templateid": "10001" } ] }, "auth": "f1b7a0cb25ed9d7f60595d63fb660a4f", "id": 1 }' http://10.0.0.167/zabbix/api_jsonrpc.php | python -m json.tool { "id": 1, "jsonrpc": "2.0", "result": { "hostids": [ "10439" ] } }
8。实现任意一种告警通知,短信,邮箱,企业微信
1)先在QQ邮箱打开该功能,然后生成授权码并保存
2)在zabbix web 配置报警媒介
3)给用户添加报警媒介
4)创建动作并添加