Docker Builds Redis Master-Slave Mode (Sentinel Monitoring) & SpringBoot 2.0 Integrated Redis Service (Lettuce Connection Pool)

1. Docker sets up Redis Master+Sentinel Monitoring

Set up environment: Ubuntu 18.04.3 Docker 18.09.7

  1. Download Mirror
    docker pull redis (default redis:latest)

  2. Get the configuration file and modify it

    wget http://download.redis.io/redis-stable/redis.conf
    wget http://download.redis.io/redis-stable/sentinel.conf
    
    # Primary four slave port:6379~6383
    cp redis.conf redis_6379.conf
    # Three Sentinels port:26379~26381
    cp sentinel.conf sentinel_26379.conf

    master primary configuration parameters:

    # Protected mode with bind and requirepass on
    protected-mode no
    port 6379
    # Specify accessible IP, test environment can be set up this way or commented out directly for debugging convenience, or specified online for security
    bind 0.0.0.0
    # Run in the background as a daemon
    daemonize yes
    # Process number is written to file when daemonize opens
    pidfile "/var/run/redis_6379.pid"
    # Log level, options are debug, verbose, notice, warning
    loglevel notice
    # Logs are written to files, and relative paths are generated in the working directory dir
    logfile "redis_6379.log"
    # snapshot file
    dbfilename dump_6379.rdb
    # Working Directory Default. /
    dir /redis
    # Open from server read-only mode
    replica-read-only yes
    # Password
    requirepass 123456
    # Open aof log, default no
    appendonly yes
    # aof log file
    appendfilename "appendonly_6379.aof"
    # The fsync function is called once per second to force the AOF log to be flushed from the kernel cache to disk. always is called for every modification instruction. no never
    appendfsync everysec
    # RDB AOF mixed persistence after Redis4.0, writing snapshot files to the beginning of the AOF file
    aof-use-rdb-preamble yes
    # Slow log execution time, 10000 microseconds = 10 milliseconds
    slowlog-log-slower-than 10000
    # Priority when upgrading master, default 100, 0:will never be promoted to master
    slave-priority 100
    
    # Schizophrenia: When the primary node hangs up, all synchronization messages have not yet been received from the secondary node, causing this part of the message to be lost
    # Sentinel cannot guarantee that messages will not be lost at all, but it can also ensure that messages will be lost as little as possible. There are two options to limit the master slave delay from being too large
    # Primary node must have at least three slave nodes replicating normally, otherwise the write-to-write service will stop and become unavailable
    min-replicas-to-write 3
    # If no feedback from the node is received within 10 seconds, it means that the synchronization from the node is abnormal (abnormal replication)
    min-replicas-max-lag 10

    Replica (old slave) is modified on the basis of master:

    port 6380
    # The older version is slaveof master_host master_port
    # Because my Springboot application and redis service are on different servers, I write my IP directly here
    # Note the difference between IP in docker container and host IP if it is on the same machine
    replicaof xxx.xxx.xxx.xx 6379
    # Primary Server Password
    masterauth 123456

    sentinel main configuration parameters:

    port 26379
    daemonize yes
    pidfile "/var/run/redis-sentinel_26379.pid"
    logfile "sentinel_26379.log"
    
    # myid is generated automatically. If you find that there is only one sentinel in the info information after starting multiple sentinels, you should be aware that the same id is in the replication profile
    # sentinel myid 0620901873e8b5175b6d0882700b04acb40b4bb4
    
    # sentinel marks master as the number of milliseconds it takes to be subjectively offline (no ping or error returned within a given time)
    sentinel down-after-milliseconds <master-name> <milliseconds>
    
    # Monitored master node, you can add more than one master to monitor at the same time, distinguished by name below, here is mymaster
    # 2 Represents the number of sentinel s required to determine master's objective offline (subjective offline), failover failover after decision
    sentinel monitor mymaster xxx.xxx.xxx.xx 6380 2
    
    # failover limits the number of slave nodes that initiate replication to the new primary node
    sentinel parallel-syncs mymaster 2
    
    # Primary Node Password
    sentinel auth-pass mymaster 123456
    
    
  3. New and Start Container & Start redis+sentinel Service
    Main Command Interpretation:

    Docker run (new and start container) parameter:
    -d Run the container in the background and print the container ID
    -i Interactive operation
    -t terminal
    -p Specifies the port mapping in the allowable format: ip:hostPort:containerPort
                                ip::containerPort
                                hostPort:containerPort
                                containerPort
    --name Sets the container name
    --network specifies the network mode, optional parameter: --network=bridge default, connect to default Bridge
                                     --network=host container uses host's network
                                     --network=container:NAME_or_ID Makes the new container use the network configuration of the existing container
                                     --network=none does not configure the container's network, user can customize the network configuration
    -v, --volume mounts the host directory to the container directory
                  For example: -v/usr/local/redis/redis_6379.conf:/redis/redis_6379.conf
                       Colon':'precedes host directory and follows container directory
                       If redis configures dir to. / then the current directory is the / redis directory
    --link adds a soft link to the container name or ID
            For example, --link redis-6379, so configuring replica and sentinel monitor can replace IP with redis-6379
            --link redis-6379:master can set aliases
    /bin/bash is followed by the command, where we want an interactive Shell, so use / bin/bash or direct bash (already configured in the system environment variable)
    Enter Container command (either works or docker run-it enters directly at startup):
    docker attach container_name/id
    docker exec -it container_name/id bash
    Example: docker exec-it redis-6379 Bash
    Exit container:
    exit closes the container before exiting
    Ctrl+P+Q won't, so this shortcut is usually used
    redis-cli enters redis interactive interface
    Optional parameters:
    -h host
    -p port
    -a password
    shutdown stop service
    Example: Stop sentinel service redis-cli-p 26379-a 123456 shutdown

    Start containers and services:

    docker run -it --name redis-6379 -p 6379:6379 -v /usr/local/redis/redis_6379.conf:/redis/redis_6379.conf redis bash
    # Enter container directly after startup, current directory/data
    cd /redis
    # Ls-la can see a file redis_6379.conf in the / redis directory
    # Start the redis service
    redis-server redis_6379.conf
    # view log
    cat redis_6379.log
    # redis-cli enters the interactive interface
    # Sign in
    auth 123456
    # View information about server runtime environment parameters/master-slave replication
    info [server/replication]
    exit Sign out redis-cli
    
    # exit then closes the container and exits
    # So press Ctrl+P+Q to exit the container


    (This is a later screenshot, switched from server, role:slave)
    Start Sentinel:

    docker run -it --name redis-sentinel-26379 -p 26379:26379 -v /usr/local/redis/sentinel_26379.conf redis bash
    cd ../redis
    # Start Sentinel Service
    redis-sentinel sentinel_26379.conf
    cat sentinel_26379.log
    # redis-cli info sentinel can view monitoring information




    After all starts:

 

2. Integrating SpringBoot 2.0

Keywords: Programming Redis Docker network snapshot

Added by virtualdevl on Thu, 09 Apr 2020 08:54:36 +0300