zoukankan      html  css  js  c++  java
  • 作业第二十一周

    1、搭建一个 redis 哨兵集群

    Master服务器 IP:10.0.0.8

    [root@redis-master~]#yum -y install redis ##安装redis
    [root@redis-master~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456'  /etc/redis.conf  ##修改配置文件
    [root@redis-master~]#systemctl enable --now redis  #启动redis服务

    Slave1服务器 IP:10.0.0.18

    [root@redis-slave1~]#yum -y install redis ##安装redis
    [root@redis-slave1~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456'  /etc/redis.conf  ##修改配置文件
    [root@redis-slave1~]#systemctl enable --now redis  #启动redis服务
    [root@redis-slave1 ~]#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> REPLICAOF 10.0.0.8 6379 #设置10.0.0.8为master
    OK
    127.0.0.1:6379> INFO replication #查看复制信息
    # Replication
    role:slave
    master_host:10.0.0.8
    master_port:6379
    master_link_status:up
    master_last_io_seconds_ago:4
    master_sync_in_progress:0
    slave_repl_offset:140
    slave_priority:100
    slave_read_only:1
    connected_slaves:0
    master_replid:8fdca730a2ae48fb9c8b7e739dcd2efcc76794f3
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:140
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:99
    repl_backlog_histlen:42

    Slave2服务器 IP:10.0.0.28

    [root@redis-slave2~]#yum -y install redis ##安装redis
    [root@redis-slave2~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456'  /etc/redis.conf  ##修改配置文件
    [root@redis-slave2~]#systemctl enable --now redis  #启动redis服务
    [root@redis-slave2 ~]#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> REPLICAOF 10.0.0.8 6379 #设置10.0.0.8为master
    OK
    127.0.0.1:6379> INFO replication
    # Replication
    role:slave
    master_host:10.0.0.8
    master_port:6379
    master_link_status:up
    master_last_io_seconds_ago:3
    master_sync_in_progress:0
    slave_repl_offset:182
    slave_priority:100
    slave_read_only:1
    connected_slaves:0
    master_replid:8fdca730a2ae48fb9c8b7e739dcd2efcc76794f3
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:182
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:15
    repl_backlog_histlen:168
    127.0.0.1:6379>

    编辑配置文件Sentinel.conf

    三个哨后服务器的配置都如下

    [root@redis-master ~]#grep -vE "^#|^$" /etc/redis-sentinel.conf 
    port 26379
    daemonize no
    pidfile "/var/run/redis-sentinel.pid"
    logfile "/var/log/redis/sentinel.log"
    dir "/tmp"
    sentinel myid 50547f34ed71fd48c197924969937e738a39975b  #此行每个哨兵主机必须唯一
    sentinel deny-scripts-reconfig yes
    sentinel monitor mymaster 10.0.0.8 6379 2           #修改此行
    sentinel down-after-milliseconds mymaster 3000      #修改此行
    sentinel auth-pass mymaster 123456                  #增加此行
    sentinel config-epoch mymaster 0
    
    #以下自动生成,不需要修改
    protected-mode no
    supervised systemd
    sentinel leader-epoch mymaster 0
    sentinel known-replica mymaster 10.0.0.28 6379
    sentinel known-replica mymaster 10.0.0.18 6379
    sentinel current-epoch 0
    
    [root@redis-master ~]#scp /etc/redis-sentinel.conf redis-slave1:/etc/
    [root@redis-master ~]#scp /etc/redis-sentinel.conf redis-slave2:/etc/
    
    sentinel myid 50547f34ed71fd48c197924969937e738a39975b 

    启动哨兵

    三台哨兵服务器都要启动

    #确保每个哨兵主机myid不同
    [root@redis-slave1 ~]#vim /etc/redis-sentinel.conf
    sentinel myid 50547f34ed71fd48c197924969937e738a39975c 
    [root@redis-slave2 ~]#vim /etc/redis-sentinel.conf
    sentinel myid 50547f34ed71fd48c197924969937e738a39975d 
    
    [root@redis-master ~]#systemctl restart redis-sentinel.service
    [root@redis-slave1 ~]#systemctl restart redis-sentinel.service
    [root@redis-slave2 ~]#systemctl restart redis-sentinel.service

    验证哨兵端口

    [root@redis-master ~]#ss -ntl
    State        Recv-Q        Send-Q      Local Address:Port     Peer Address:Port        
    LISTEN       0             128               0.0.0.0:22            0.0.0.0:*           
    LISTEN       0             128               0.0.0.0:26379         0.0.0.0:*           
    LISTEN       0             128               0.0.0.0:6379          0.0.0.0:*           
    LISTEN       0             128                  [::]:22               [::]:*           
    LISTEN       0             128                  [::]:26379            [::]:*           
    LISTEN       0             128                  [::]:6379             [::]:* 

    2、实现 redis cluster 得部署

    实验环境

    10.0.0.8
    10.0.0.18
    10.0.0.28
    10.0.0.38
    10.0.0.48
    10.0.0.58

    所有6台主机都执行:[root@centos8 ~]#dnf -y install redis

    每个节点修改redis配置,必须开启cluster功能的参数

     #手动修改配置文件
      [root@redis-node1 ~]vim /etc/redis.conf
      masterauth 123456   #建议配置,否则后期的master和slave无法成功,还需再配置
      requirepass 123456
      cluster-enabled yes #必须开启集群状态,开启后redis 进程会有cluster显示 
      cluster-config-file nodes-6379.conf #此文件有redis cluster集群自动创建和维护 
     cluster-require-full-coverage no
    
      #或者执行下面命令,批量修改
      [root@redis-node1 ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf
      [root@redis-node1 ~]#systemctl enable --now redis

    验证当前Redis服务状态

     #开启了16379的cluster的端口
      [root@centos8 ~]#ss -ntl
      State        Recv-Q        Send-Q  Local Address:Port     Peer Address:Port        
      LISTEN       0             128           0.0.0.0:22            0.0.0.0:*           
      LISTEN       0             100         127.0.0.1:25            0.0.0.0:*           
      LISTEN       0             128           0.0.0.0:16379  #多了一个16379端口        0.0.0.0:*           
      LISTEN       0             128           0.0.0.0:6379          0.0.0.0:*           
      LISTEN       0             128              [::]:22               [::]:*           
      LISTEN       0             100             [::1]:25               [::]:*           
    
      #注意进程的状态
      [root@centos8 ~]#ps -ef|grep redis
      redis   1939    1  0 10:54 ?    00:00:00 /usr/bin/redis-server 0.0.0.0:6379 [cluster]
      #出现cluster
      root    1955   1335  0 10:57 pts/0    00:00:00 grep --color=auto redis

    创建集群

    # --cluster-replicas 1 表示每个master对应一个slave节点
    [root@redis-node1 ~]#redis-cli -a 123456  --cluster create 10.0.0.8:6379   10.0.0.18:6379    10.0.0.28:6379   10.0.0.38:6379   10.0.0.48:6379   10.0.0.58: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.38:6379 to 10.0.0.8:6379
    Adding replica 10.0.0.48:6379 to 10.0.0.18:6379
    Adding replica 10.0.0.58:6379 to 10.0.0.28:6379
    M: cb028b83f9dc463d732f6e76ca6bbcd469d948a7 10.0.0.8:6379   #带M的为master
       slots:[0-5460] (5461 slots) master                        #当前master的槽位起始和结束位
    M: 99720241248ff0e4c6fa65c2385e92468b3b5993 10.0.0.18:6379
       slots:[5461-10922] (5462 slots) master
    M: d34da8666a6f587283a1c2fca5d13691407f9462 10.0.0.28:6379
       slots:[10923-16383] (5461 slots) master
    S: f9adcfb8f5a037b257af35fa548a26ffbadc852d 10.0.0.38:6379  #带S的slave
       replicates cb028b83f9dc463d732f6e76ca6bbcd469d948a7      
    S: d04e524daec4d8e22bdada7f21a9487c2d3e1057 10.0.0.48:6379
       replicates 99720241248ff0e4c6fa65c2385e92468b3b5993
    S: 9875b50925b4e4f29598e6072e5937f90df9fc71 10.0.0.58:6379
       replicates d34da8666a6f587283a1c2fca5d13691407f9462
    Can I set the above configuration? (type 'yes' to accept): yes #输入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.8:6379)
    M: cb028b83f9dc463d732f6e76ca6bbcd469d948a7 10.0.0.8:6379
       slots:[0-5460] (5461 slots) master   #已经分配的槽位
       1 additional replica(s)              #分配了一个slave
    S: 9875b50925b4e4f29598e6072e5937f90df9fc71 10.0.0.58:6379
       slots: (0 slots) slave               #slave没有分配槽位
       replicates d34da8666a6f587283a1c2fca5d13691407f9462  #对应的master的10.0.0.28的ID
    S: f9adcfb8f5a037b257af35fa548a26ffbadc852d 10.0.0.38:6379
       slots: (0 slots) slave
       replicates cb028b83f9dc463d732f6e76ca6bbcd469d948a7 #对应的master的10.0.0.8的ID
    S: d04e524daec4d8e22bdada7f21a9487c2d3e1057 10.0.0.48:6379
       slots: (0 slots) slave
       replicates 99720241248ff0e4c6fa65c2385e92468b3b5993 #对应的master的10.0.0.18的ID
    M: 99720241248ff0e4c6fa65c2385e92468b3b5993 10.0.0.18:6379
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    M: d34da8666a6f587283a1c2fca5d13691407f9462 10.0.0.28:6379
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    [OK] All nodes agree about slots configuration.  #所有节点槽位分配完成
    >>> Check for open slots... #检查打开的槽位
    >>> Check slots coverage... #检查插槽覆盖范围
    [OK] All 16384 slots covered. #所有槽位(16384个)分配完成
    [root@redis-node1 ~]#
    
    观察以上结果,可以看到3组master/slave
    master:10.0.0.8---slave:10.0.0.38
    master:10.0.0.18---slave:10.0.0.48
    master:10.0.0.28---slave:10.0.0.58

    查看主从状态

    <!-- wp:code -->
    <pre class="wp-block-code"><code>&#91;root@redis-node1 ~]#redis-cli -a 123456 INFO replication
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    # Replication
    role:master
    connected_slaves:1
    slave0:ip=10.0.0.38,port=6379,state=online,offset=896,lag=1
    master_replid:3a388865080d779180ff240cb75766e7e57877da
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:896
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:896
    
    &#91;root@redis-node2 ~]#redis-cli -a 123456 INFO replication
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    # Replication
    role:master
    connected_slaves:1
    slave0:ip=10.0.0.48,port=6379,state=online,offset=980,lag=1
    master_replid:b9066d3cbf0c5fecc7f4d1d5cb2433999783fa3f
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:980
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:980
    
    &#91;root@redis-node3 ~]#redis-cli -a 123456 INFO replication
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    # Replication
    role:master
    connected_slaves:1
    slave0:ip=10.0.0.58,port=6379,state=online,offset=980,lag=0
    master_replid:53208e0ed9305d721e2fb4b3180f75c689217902
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:980
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:980
    
    &#91;root@redis-node4 ~]#redis-cli -a 123456 INFO replication
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    # Replication
    role:slave
    master_host:10.0.0.8
    master_port:6379
    master_link_status:up
    master_last_io_seconds_ago:1
    master_sync_in_progress:0
    slave_repl_offset:1036
    slave_priority:100
    slave_read_only:1
    connected_slaves:0
    master_replid:3a388865080d779180ff240cb75766e7e57877da
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:1036
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:1036
    
    &#91;root@redis-node5 ~]#redis-cli -a 123456 INFO replication
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    # Replication
    role:slave
    master_host:10.0.0.18
    master_port:6379
    master_link_status:up
    master_last_io_seconds_ago:2
    master_sync_in_progress:0
    slave_repl_offset:1064
    slave_priority:100
    slave_read_only:1
    connected_slaves:0
    master_replid:b9066d3cbf0c5fecc7f4d1d5cb2433999783fa3f
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:1064
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:1064
    
    &#91;root@redis-node6 ~]#redis-cli -a 123456 INFO replication
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    # Replication
    role:slave
    master_host:10.0.0.28
    master_port:6379
    master_link_status:up
    master_last_io_seconds_ago:7
    master_sync_in_progress:0
    slave_repl_offset:1078
    slave_priority:100
    slave_read_only:1
    connected_slaves:0
    master_replid:53208e0ed9305d721e2fb4b3180f75c689217902
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:1078
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:1078</code></pre>
    <!-- /wp:code -->

    验证集群状态

    [root@redis-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:6                 #节点数
    cluster_size:3                        #三个集群
    cluster_current_epoch:6
    cluster_my_epoch:1
    cluster_stats_messages_ping_sent:837
    cluster_stats_messages_pong_sent:811
    cluster_stats_messages_sent:1648
    cluster_stats_messages_ping_received:806
    cluster_stats_messages_pong_received:837
    cluster_stats_messages_meet_received:5
    cluster_stats_messages_received:1648
    
    #查看任意节点的集群状态
    [root@redis-node1 ~]#redis-cli -a 123456 --cluster info 10.0.0.38:6379
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    10.0.0.18:6379 (99720241...) -> 0 keys | 5462 slots | 1 slaves.
    10.0.0.28:6379 (d34da866...) -> 0 keys | 5461 slots | 1 slaves.
    10.0.0.8:6379 (cb028b83...) -> 0 keys | 5461 slots | 1 slaves.
    [OK] 0 keys in 3 masters.
    0.00 keys per slot on average.

    查看集群node对应关系

    [root@redis-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.
    9875b50925b4e4f29598e6072e5937f90df9fc71 10.0.0.58:6379@16379 slave d34da8666a6f587283a1c2fca5d13691407f9462 0 1582344815790 6 connected
    f9adcfb8f5a037b257af35fa548a26ffbadc852d 10.0.0.38:6379@16379 slave cb028b83f9dc463d732f6e76ca6bbcd469d948a7 0 1582344811000 4 connected
    d04e524daec4d8e22bdada7f21a9487c2d3e1057 10.0.0.48:6379@16379 slave 99720241248ff0e4c6fa65c2385e92468b3b5993 0 1582344815000 5 connected
    99720241248ff0e4c6fa65c2385e92468b3b5993 10.0.0.18:6379@16379 master - 0 1582344813000 2 connected 5461-10922
    d34da8666a6f587283a1c2fca5d13691407f9462 10.0.0.28:6379@16379 master - 0 1582344814780 3 connected 10923-16383
    cb028b83f9dc463d732f6e76ca6bbcd469d948a7 10.0.0.8:6379@16379 myself,master - 0 1582344813000 1 connected 0-5460
    
    [root@redis-node1 ~]#redis-cli -a 123456 --cluster check 10.0.0.38:6379
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    10.0.0.18:6379 (99720241...) -> 0 keys | 5462 slots | 1 slaves.
    10.0.0.28:6379 (d34da866...) -> 0 keys | 5461 slots | 1 slaves.
    10.0.0.8:6379 (cb028b83...) -> 0 keys | 5461 slots | 1 slaves.
    [OK] 0 keys in 3 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 10.0.0.38:6379)
    S: f9adcfb8f5a037b257af35fa548a26ffbadc852d 10.0.0.38:6379
       slots: (0 slots) slave
       replicates cb028b83f9dc463d732f6e76ca6bbcd469d948a7
    S: d04e524daec4d8e22bdada7f21a9487c2d3e1057 10.0.0.48:6379
       slots: (0 slots) slave
       replicates 99720241248ff0e4c6fa65c2385e92468b3b5993
    M: 99720241248ff0e4c6fa65c2385e92468b3b5993 10.0.0.18:6379
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: 9875b50925b4e4f29598e6072e5937f90df9fc71 10.0.0.58:6379
       slots: (0 slots) slave
       replicates d34da8666a6f587283a1c2fca5d13691407f9462
    M: d34da8666a6f587283a1c2fca5d13691407f9462 10.0.0.28:6379
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    M: cb028b83f9dc463d732f6e76ca6bbcd469d948a7 10.0.0.8:6379
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.

    验证集群写入key

    #经过算法计算,当前key的槽位需要写入指定的node
    [root@redis-node1 ~]#redis-cli -a 123456 -h 10.0.0.8  SET key1 values1
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    (error) MOVED 9189 10.0.0.18:6379    #槽位不在当前node所以无法写入
    [root@redis-node1 ~]#redis-cli -a 123456 -h 10.0.0.18  SET key1 values1
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    OK
    #指定node可写入
    [root@redis-node1 ~]#redis-cli -a 123456 -h 10.0.0.18  GET  key1
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    "values1"
    #对应的slave节点可以KEYS *,但GET key1失败
    [root@redis-node1 ~]#redis-cli -a 123456 -h 10.0.0.48  KEYS "*"
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    1) "key1"
    [root@redis-node1 ~]#redis-cli -a 123456 -h 10.0.0.48 GET key1
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    (error) MOVED 9189 10.0.0.18:6379

    3、部署一个 tomcat 服务

    #安装JDK
    tar xvf   jdk-8u271-linux-x64.tar.gz -C /usr/local
    cd  /usr/local 
    ln -s jdk1.8.* jdk
    cat >  /etc/profile.d/jdk.sh <<EOF
    export JAVA_HOME=$JDK_DIR/jdk
    export JRE_HOME=$JAVA_HOME/jre
    export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib/
    export PATH=$PATH:$JAVA_HOME/bin
    EOF
    .  /etc/profile.d/jdk.sh
    
    #安装Tomcat
    tar xf  apache-tomcat-8.5.59.tar.gz -C /usr/local
    cd /usr/local
    ln -s apache-tomcat-*/  tomcat
    echo "PATH=$TOMCAT_DIR/tomcat/bin:"'$PATH' > /etc/profile.d/tomcat.sh
    ./etc/profile.d/tomcat.sh
    id tomcat &> /dev/null || useradd -r -s /sbin/nologin tomcat
    cat > usr/local/tomcat/conf/tomcat.conf <<EOF
    JAVA_HOME=usr/local/jdk
    JRE_HOME=$JAVA_HOME/jre
    EOF
    chown -R tomcat.tomcat/usr/lcoal/tomcat/
    cat > /lib/systemd/system/tomcat.service  <<EOF
    [Unit]
    Description=Tomcat
    #After=syslog.target network.target remote-fs.target nss-lookup.target
    After=syslog.target network.target 
    
    [Service]
    Type=forking
    EnvironmentFile=usr/local/tomcat/conf/tomcat.conf
    ExecStart=usr/local/tomcat/bin/startup.sh
    ExecStop=usr/local/tomcat/bin/shutdown.sh
    PrivateTmp=true
    User=tomcat
    
    [Install]
    WantedBy=multi-user.target
    EOF
    systemctl daemon-reload
    systemctl enable --now tomcat.service
  • 相关阅读:
    外部存储 使用详解
    内部存储 使用详解
    SQLite 使用详解
    SharePerference 使用详解
    Preference 使用详解
    Notification 使用详解
    PopupWindow 使用详解
    Fragment 使用详解
    Dialog 使用详解
    ListView 使用详解
  • 原文地址:https://www.cnblogs.com/tz66/p/13884237.html
Copyright © 2011-2022 走看看