赞
踩
在当今信息化的时代,数据的安全性和稳定性显得尤为重要。数据库是许多企业和组织存储和管理数据的核心,因此如何保证数据库的高可用性和数据的同步性是一个非常关键的问题。而基于主从同步的技术可以有效地解决这个问题。本文将介绍如何在 Docker 环境下搭建 MS SQL Server 的主从同步,帮助读者了解主从同步的原理和实现方式,进而提高数据的可靠性和稳定性。
主从同步是一种常用的技术,用于在多个 SQL Server 实例之间保持数据同步。在主从同步中,一个 SQL Server 实例被用作数据的源,而另一个或多个 SQL Server 实例则作为数据的接收端。当主节点上的数据发生更改时,这些更改将被捕获并保存到一个事务日志中。从节点会定期检查主节点的事务日志,并将主节点上的更改应用到自己的数据库中,从而保持两个数据库之间的数据同步。
在开始安装之前,需要确保 CentOS 上已经安装了 Docker 和 Docker Compose。可以通过以下命令来进行安装:
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
按照下列步骤依次进行安装,中间过程直接略过
# 1.安装需要的软件包:
sudo yum install -y yum-utils
# 2.设置docker的阿里库
yum-config-manager --add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 3.安装docker:
sudo yum install -y docker-ce docker-ce-cli containerd.io
# 4.启动docker服务:
sudo systemctl start docker
# 5.设置开机自启动docker服务:
sudo systemctl enable docker
sudo docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
在进行主从搭建之前,需要先创建一个 Docker 网络。这个网络用于连接所有的 SQL Server 容器。
在创建网络之前,需要先确认已经启动了 Docker:
sudo systemctl start docker
然后,可以使用以下命令来创建一个名为 sync-net 的 Docker 网络:
[root@hecs-bluetata ~]# docker network create -d bridge sync-net
538c142757e91c0b798ce0e45dc02b6038f00adaf37cfe3b09659dea8c950c93
其中参数 -d 指定了网络的类型,bridge 指的是桥接网络,sync-net 指的是新创建的网络的名称。
创建 SQL Server 容器之前,需要先准备 SQL Server 的 Docker 镜像。可以通过以下命令来获取 SQL Server 2019 的 Docker 镜像:
docker pull mcr.microsoft.com/mssql/server:2019-latest
获取到 Docker 镜像之后,就可以创建 SQL Server 容器了。可以通过以下命令来创建 2 个 SQL Server 容器,分别命名为 sqlserver-master、和 sqlserver-slave,并加入所创建的 Docker 网络中。
docker run --name sqlserver-master --hostname sqlserver-master --network sync-net \
-p 1433:1433 \
-e 'ACCEPT_EULA=Y' \
-e 'SA_PASSWORD= P@ssw0rd01 ' \
-e "MSSQL_AGENT_ENABLED=True" \
-e "MSSQL_PID=Developer" \
-d mcr. Microsoft. Com/mssql/server: 2019-latest
Docker run --name sqlserver-slave --hostname sqlserver-slave --network sync-net \
-p 1434:1433 \
-e 'ACCEPT_EULA=Y' \
-e 'SA_PASSWORD= P@ssw0rd02 ' \
-e "MSSQL_AGENT_ENABLED=True" \
-e "MSSQL_PID=Developer" \
-d mcr. Microsoft. Com/mssql/server: 2019-latest
针对上述命令,相关参数的解释:
请确确保在创建上述 Docker 的过程中没有错误。
如果在创建过程中出现端口占用,或者名称占用等错误,可以查看相应容器,选择性的删除容器后,重新创建,相关命令:
Docker ps -a
Docker rm d 3 d 3 a 4712 b 5 f
Docker stop d 3 d 3 a 4712 b 5 f
进入 SQL Server 主节点容器,并创建主从同步端点:
Docker exec -it sqlserver-master /opt/mssql-tools/bin/sqlcmd \
-S localhost -U SA -P P@ssw0rd01 \
-Q "CREATE ENDPOINT endpoint_mirroring STATE = STARTED AS TCP (LISTENER_PORT=7022) FOR DATABASE_MIRRORING (ROLE=PARTNER)"
进入 SQL Server 从节点容器,并创建主从同步端点:
Docker exec -it sqlserver-slave /opt/mssql-tools/bin/sqlcmd \
-S localhost -U SA -P P@ssw0rd02 \
-Q "CREATE ENDPOINT endpoint_mirroring STATE = STARTED AS TCP (LISTENER_PORT=7022) FOR DATABASE_MIRRORING (ROLE=PARTNER)"
回到 SQL Server 主节点容器,并创建主从同步数据库:
Docker exec -it sqlserver-master /opt/mssql-tools/bin/sqlcmd \
-S localhost -U SA -P P@ssw0rd01 \
-Q "CREATE DATABASE mydb" \
-Q "BACKUP DATABASE mydb TO DISK='/var/opt/mssql/data/mydb. Bak'" \
-Q "RESTORE DATABASE mydb WITH NORECOVERY" \
-Q "ALTER DATABASE mydb SET PARTNER = 'TCP://sqlserver-slave: 7022'"
回到 SQL Server 从节点容器,并创建主从同步数据库:
docker exec -it sqlserver-slave /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P P@ssw0rd02
docker exec -it sqlserver-slave /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P P@ssw0rd02 ' \
-Q "CREATE DATABASE mydb" \
-Q "RESTORE DATABASE mydb FROM DISK='/var/opt/mssql/data/mydb. Bak' WITH NORECOVERY" \
-Q "ALTER DATABASE mydb SET PARTNER = 'TCP://sqlserver-master: 7022'"
回到 SQL Server 主节点容器,并启动主从同步:
Docker exec -it sqlserver-master /opt/mssql-tools/bin/sqlcmd `
-S localhost -U SA -P P@ssw0rd01 \
-Q "ALTER DATABASE mydb SET PARTNER SAFETY OFF" \
-Q "ALTER DATABASE mydb SET PARTNER SAFETY ON" \
回到 SQL Server 主节点容器,并查看主从同步状态:
Docker exec -it sqlserver-master /opt/mssql-tools/bin/sqlcmd \
-S localhost -U SA -P P@ssw0rd01 \
-Q "SELECT database_id, synchronization_state_desc FROM sys. Database_mirroring WHERE database_id = DB_ID ('mydb')"
可以看到以下的输出结果:
Database_id synchronization_state_desc
----------- ------------------------
5 SYNCHRONIZED
这表示主从同步已经成功地建立,并且 mydb 数据库已经在主从节点之间同步。
注意:这里我这里使用了开发版的 SQL Server 镜像,如果你在生产环境中使用 SQL Server,请使用适当版本的镜像,并根据需要进行调整。
本文介绍了在 Docker 环境下搭建 MS SQL Server 的主从同步,并演示了如何进行配置和管理。通过本文的学习,你可以了解主从同步技术的实现原理和具体操作方法,并为提高数据可靠性和稳定性提供了一种有效的解决方案。同时,也需要认真考虑主从同步的一些限制和要求,并根据实际情况进行配置和管理。
Mkdir -p /opt/docker/redis/conf
Mkdir -p /opt/docker/redis/data
Chmod 777 -R /opt/docker/redis
#默认配置文件redis .conf 全文 # Redis configuration file example. # # Note that in order to read the configuration file, Redis must be # Started with the file path as first argument: # # ./redis-server /path/to/redis. Conf # Note on units: when memory size is needed, it is possible to specify # It in the usual form of 1 k 5 GB 4 M and so forth: # # 1 k => 1000 bytes # 1 kb => 1024 bytes # 1 m => 1000000 bytes # 1 mb => 1024*1024 bytes # 1 g => 1000000000 bytes # 1 gb => 1024*1024*1024 bytes # # Units are case insensitive so 1 GB 1 Gb 1 gB are all the same. ################################## INCLUDES ################################### # Include one or more other config files here. This is useful if you # Have a standard template that goes to all Redis servers but also need # To customize a few per-server settings. Include files can include # Other files, so use this wisely. # # Note that option "include" won't be rewritten by command "CONFIG REWRITE" # From admin or Redis Sentinel. Since Redis always uses the last processed # Line as value of a configuration directive, you'd better put includes # At the beginning of this file to avoid overwriting config change at runtime. # # If instead you are interested in using includes to override configuration # Options, it is better to use include as the last line. # # Included paths may contain wildcards. All files matching the wildcards will # Be included in alphabetical order. # Note that if an include path contains a wildcards but no files match it when # The server is started, the include statement will be ignored and no error will # Be emitted. It is safe, therefore, to include wildcard files from empty # Directories. # # Include /path/to/local. Conf # Include /path/to/other. Conf # Include /path/to/fragments/*. Conf # ################################## MODULES ##################################### # Load modules at startup. If the server is not able to load modules # It will abort. It is possible to use multiple loadmodule directives. # # Loadmodule /path/to/my_module. So # Loadmodule /path/to/other_module. So ################################## NETWORK ##################################### # By default, if no "bind" configuration directive is specified, Redis listens # For connections from all available network interfaces on the host machine. # It is possible to listen to just one or multiple selected interfaces using # The "bind" configuration directive, followed by one or more IP addresses. # Each address can be prefixed by "-", which means that redis will not fail to # Start if the address is not available. Being not available only refers to # Addresses that does not correspond to any network interface. Addresses that # Are already in use will always fail, and unsupported protocols will always BE # Silently skipped. # # Examples: # # Bind 192.168.1.100 10.0.0.1 # listens on two specific IPv 4 addresses # Bind 127.0.0.1 :: 1 # listens on loopback IPv 4 and IPv 6 # Bind * -::* # like the default, all available interfaces # # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the # Internet, binding to all the interfaces is dangerous and will expose the # Instance to everybody on the internet. So by default we uncomment the # Following bind directive, that will force Redis to listen only on the # IPv 4 and IPv 6 (if available) loopback interface addresses (this means Redis # Will only be able to accept client connections from the same host that it is # Running on). # # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # COMMENT OUT THE FOLLOWING LINE. # # You will also need to set a password unless you explicitly disable protected # Mode. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Bind 127.0.0.1 -::1 # By default, outgoing connections (from replica to master, from Sentinel to # Instances, cluster bus, etc.) are not bound to a specific local address. In # Most cases, this means the operating system will handle that based on routing # And the interface through which the connection goes out. # # Using bind-source-addr it is possible to configure a specific address to bind # To, which may also affect how the connection gets routed. # # Example: # # Bind-source-addr 10.0.0.1 # Protected mode is a layer of security protection, in order to avoid that # Redis instances left open on the internet are accessed and exploited. # # When protected mode is on and the default user has no password, the server # Only accepts local connections from the IPv 4 address (127.0.0.1), IPv 6 address # (::1) or Unix domain sockets. # # By default protected mode is enabled. You should disable it only if # You are sure you want clients from other hosts to connect to Redis # Even if no authentication is configured. Protected-mode yes # Redis uses default hardened security configuration directives to reduce the # Attack surface on innocent users. Therefore, several sensitive configuration # Directives are immutable, and some potentially-dangerous commands are blocked. # # Configuration directives that control files that Redis writes to (e.g., 'dir' # And 'dbfilename') and that aren't usually modified during runtime # Are protected by making them immutable. # # Commands that can increase the attack surface of Redis and that aren't usually # Called by users are blocked by default. # # These can be exposed to either all connections or just local ones by setting # Each of the configs listed below to either of these values: # # No - Block for any connection (remain immutable) # Yes - Allow for any connection (no protection) # Local - Allow only for local connections. Ones originating from the # IPv 4 address (127.0.0.1), IPv 6 address (::1) or Unix domain sockets. # # Enable-protected-configs no # Enable-debug-command no # Enable-module-command no # Accept connections on the specified port, default is 6379 (IANA #815344 ). # If port 0 is specified Redis will not listen on a TCP socket. Port 6379 # TCP listen () backlog. # # In high requests-per-second environments you need a high backlog in order # To avoid slow clients connection issues. Note that the Linux kernel # Will silently truncate it to the value of /proc/sys/net/core/somaxconn so # Make sure to raise both the value of somaxconn and tcp_max_syn_backlog # In order to get the desired effect. Tcp-backlog 511 # Unix socket. # # Specify the path for the Unix socket that will be used to listen for # Incoming connections. There is no default, so Redis will not listen # On a unix socket when not specified. # # Unixsocket /run/redis. Sock # Unixsocketperm 700 # Close the connection after a client is idle for N seconds (0 to disable) Timeout 0 # TCP keepalive. # # If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence # Of communication. This is useful for two reasons: # # 1) Detect dead peers. # 2) Force network equipment in the middle to consider the connection to be # alive. # # On Linux, the specified value (in seconds) is the period used to send ACKs. # Note that to close the connection the double of the time is needed. # On other kernels the period depends on the kernel configuration. # # A reasonable value for this option is 300 seconds, which is the new # Redis default starting with Redis 3.2.1. Tcp-keepalive 300 # Apply OS-specific mechanism to mark the listening socket with the specified # ID, to support advanced routing and filtering capabilities. # # On Linux, the ID represents a connection mark. # On FreeBSD, the ID represents a socket cookie ID. # On OpenBSD, the ID represents a route table ID. # # The default value is 0, which implies no marking is required. # Socket-mark-id 0 ################################# TLS/SSL ##################################### # By default, TLS/SSL is disabled. To enable it, the "tls-port" configuration # Directive can be used to define TLS-listening ports. To enable TLS on the # Default port, use: # # Port 0 # Tls-port 6379 # Configure a X.509 certificate and private key to use for authenticating the # Server to connected clients, masters or cluster peers. These files should be # PEM formatted. # # Tls-cert-file redis. Crt # Tls-key-file redis. Key # # If the key file is encrypted using a passphrase, it can be included here # As well. # # Tls-key-file-pass secret # Normally Redis uses the same certificate for both server functions (accepting # Connections) and client functions (replicating from a master, establishing # Cluster bus connections, etc.). # # Sometimes certificates are issued with attributes that designate them as # Client-only or server-only certificates. In that case it may be desired to use # Different certificates for incoming (server) and outgoing (client) # Connections. To do that, use the following directives: # # Tls-client-cert-file client. Crt # Tls-client-key-file client. Key # # If the key file is encrypted using a passphrase, it can be included here # As well. # # Tls-client-key-file-pass secret # Configure a DH parameters file to enable Diffie-Hellman (DH) key exchange, # Required by older versions of OpenSSL (<3.0). Newer versions do not require # This configuration and recommend against it. # # Tls-dh-params-file redis. Dh # Configure a CA certificate (s) bundle or directory to authenticate TLS/SSL # Clients and peers. Redis requires an explicit configuration of at least one # Of these, and will not implicitly use the system wide configuration. # # Tls-ca-cert-file ca. Crt # Tls-ca-cert-dir /etc/ssl/certs # By default, clients (including replica servers) on a TLS port are required # To authenticate using valid client side certificates. # # If "no" is specified, client certificates are not required and not accepted. # If "optional" is specified, client certificates are accepted and must be # Valid if provided, but are not required. # # Tls-auth-clients no # Tls-auth-clients optional # By default, a Redis replica does not attempt to establish a TLS connection # With its master. # # Use the following directive to enable TLS on replication links. # # Tls-replication yes # By default, the Redis Cluster bus uses a plain TCP connection. To enable # TLS for the bus protocol, use the following directive: # # Tls-cluster yes # By default, only TLSv 1.2 and TLSv 1.3 are enabled and it is highly recommended # That older formally deprecated versions are kept disabled to reduce the attack surface. # You can explicitly specify TLS versions to support. # Allowed values are case insensitive and include "TLSv 1", "TLSv 1.1", "TLSv 1.2", # "TLSv 1.3" (OpenSSL >= 1.1.1) or any combination. # To enable only TLSv 1.2 and TLSv 1.3, use: # # Tls-protocols "TLSv 1.2 TLSv 1.3" # Configure allowed ciphers. See the ciphers (1 ssl) manpage for more information # About the syntax of this string. # # Note: this configuration applies only to <= TLSv 1.2. # # Tls-ciphers DEFAULT:! MEDIUM # Configure allowed TLSv 1.3 ciphersuites. See the ciphers (1 ssl) manpage for more # Information about the syntax of this string, and specifically for TLSv 1.3 # Ciphersuites. # # Tls-ciphersuites TLS_CHACHA 20_POLY 1305_SHA 256 # When choosing a cipher, use the server's preference instead of the client # Preference. By default, the server follows the client's preference. # # Tls-prefer-server-ciphers yes # By default, TLS session caching is enabled to allow faster and less expensive # Reconnections by clients that support it. Use the following directive to disable # Caching. # # Tls-session-caching no # Change the default number of TLS sessions cached. A zero value sets the cache # To unlimited size. The default size is 20480. # # Tls-session-cache-size 5000 # Change the default timeout of cached TLS sessions. The default timeout is 300 # Seconds. # # Tls-session-cache-timeout 60 ################################# GENERAL ##################################### # By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis. Pid when daemonized. # When Redis is supervised by upstart or systemd, this parameter has no impact. Daemonize no # If you run Redis from upstart or systemd, Redis can interact with your # Supervision tree. Options: # supervised no - no supervision interaction # supervised upstart - signal upstart by putting Redis into SIGSTOP mode # requires "expect stop" in your upstart job config # supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET # on startup, and updating Redis status on a regular # basis. # supervised auto - detect upstart or systemd method based on # UPSTART_JOB or NOTIFY_SOCKET environment variables # Note: these supervision methods only signal "process is ready." # They do not enable continuous pings back to your supervisor. # # The default is "no". To run under upstart/systemd, you can simply uncomment # The line below: # # Supervised auto # If a pid file is specified, Redis writes it where specified at startup # And removes it at exit. # # When the server runs non daemonized, no pid file is created if none is # Specified in the configuration. When the server is daemonized, the pid file # Is used even if not specified, defaulting to "/var/run/redis. Pid". # # Creating a pid file is best effort: if Redis is not able to create it # Nothing bad happens, the server will start and run normally. # # Note that on modern Linux systems "/run/redis. Pid" is more conforming # And should be used instead. Pidfile /var/run/redis_6379. Pid # Specify the server verbosity level. # This can be one of: # Debug (a lot of information, useful for development/testing) # Verbose (many rarely useful info, but not a mess like the debug level) # Notice (moderately verbose, what you want in production probably) # Warning (only very important / critical messages are logged) Loglevel notice # Specify the log file name. Also the empty string can be used to force # Redis to log on the standard output. Note that if you use standard # Output for logging but daemonize, logs will be sent to /dev/null Logfile "" # To enable logging to the system logger, just set 'syslog-enabled' to yes, # And optionally update the other syslog parameters to suit your needs. # Syslog-enabled no # Specify the syslog identity. # Syslog-ident redis # Specify the syslog facility. Must be USER or between LOCAL 0-LOCAL 7. # Syslog-facility local 0 # To disable the built in crash log, which will possibly produce cleaner core # Dumps when they are needed, uncomment the following: # # Crash-log-enabled no # To disable the fast memory check that's run as part of the crash log, which # Will possibly let redis terminate sooner, uncomment the following: # # Crash-memcheck-enabled no # Set the number of databases. The default database is DB 0, you can select # a different one on a per-connection basis using SELECT <dbid> where # Dbid is a number between 0 and 'databases'-1 Databases 16 # By default Redis shows an ASCII art logo only when started to log to the # Standard output and if the standard output is a TTY and syslog logging is # Disabled. Basically this means that normally a logo is displayed only in # Interactive sessions. # # However it is possible to force the pre-4.0 behavior and always show a # ASCII art logo in startup logs by setting the following option to yes. Always-show-logo no # By default, Redis modifies the process title (as seen in 'top' and 'ps') to # Provide some runtime information. It is possible to disable this and leave # The process name as executed by setting the following to no. Set-proc-title yes # When changing the process title, Redis uses the following template to construct # The modified title. # # Template variables are specified in curly brackets. The following variables are # Supported: # # {title} Name of process as executed if parent, or type of child process. # {listen-addr} Bind address or '*' followed by TCP or TLS port listening on, or # Unix socket if only that's available. # {server-mode} Special mode, i.e. "[sentinel]" or "[cluster]". # {port} TCP port listening on, or 0. # {tls-port} TLS port listening on, or 0. # {unixsocket} Unix domain socket listening on, or "". # {config-file} Name of configuration file used. # Proc-title-template "{title} {listen-addr} {server-mode}" ################################ SNAPSHOTTING ################################ # Save the DB to disk. # # save <seconds> <changes> [<seconds> <changes> ...] # # Redis will save the DB if the given number of seconds elapsed and it # Surpassed the given number of write operations against the DB. # # Snapshotting can be completely disabled with a single empty string argument # As in following example: # # Save "" # # Unless specified otherwise, by default Redis will save the DB: # * After 3600 seconds (an hour) if at least 1 change was performed # * After 300 seconds (5 minutes) if at least 100 changes were performed # * After 60 seconds if at least 10000 changes were performed # # You can set these explicitly by uncommenting the following line. # # Save 3600 1 300 100 60 10000 # By default Redis will stop accepting writes if RDB snapshots are enabled # (at least one save point) and the latest background save failed. # This will make the user aware (in a hard way) that data is not persisting # On disk properly, otherwise chances are that no one will notice and some # Disaster will happen. # # If the background saving process will start working again Redis will # Automatically allow writes again. # # However if you have setup your proper monitoring of the Redis server # And persistence, you may want to disable this feature so that Redis will # Continue to work as usual even if there are problems with disk, # Permissions, and so forth. Stop-writes-on-bgsave-error yes # Compress string objects using LZF when dump .rdb databases? # By default compression is enabled as it's almost always a win. # If you want to save some CPU in the saving child set it to 'no' but # The dataset will likely be bigger if you have compressible values or keys. Rdbcompression yes # Since version 5 of RDB a CRC 64 checksum is placed at the end of the file. # This makes the format more resistant to corruption but there is a performance # Hit to pay (around 10%) when saving and loading RDB files, so you can disable it # For maximum performances. # # RDB files created with checksum disabled have a checksum of zero that will # Tell the loading code to skip the check. Rdbchecksum yes # Enables or disables full sanitization checks for ziplist and listpack etc when # Loading an RDB or RESTORE payload. This reduces the chances of a assertion or # Crash later on while processing commands. # Options: # no - Never perform full sanitization # yes - Always perform full sanitization # clients - Perform full sanitization only for user connections. # Excludes: RDB files, RESTORE commands received from the master # connection, and client connections which have the # skip-sanitize-payload ACL flag. # The default should be 'clients' but since it currently affects cluster # Resharding via MIGRATE, it is temporarily set to 'no' by default. # # Sanitize-dump-payload no # The filename where to dump the DB Dbfilename dump. Rdb # Remove RDB files used by replication in instances without persistence # Enabled. By default this option is disabled, however there are environments # Where for regulations or other security concerns, RDB files persisted on # Disk by masters in order to feed replicas, or stored on disk by replicas # In order to load them for the initial synchronization, should be deleted # ASAP. Note that this option ONLY WORKS in instances that have both AOF # And RDB persistence disabled, otherwise is completely ignored. # # An alternative (and sometimes better) way to obtain the same effect is # To use diskless replication on both master and replicas instances. However # In the case of replicas, diskless is not always an option. Rdb-del-sync-files no # The working directory. # # The DB will be written inside this directory, with the filename specified # Above using the 'dbfilename' configuration directive. # # The Append Only File will also be created inside this directory. # # Note that you must specify a directory here, not a file name. Dir ./ ################################# REPLICATION ################################# # Master-Replica replication. Use replicaof to make a Redis instance a copy of # Another Redis server. A few things to understand ASAP about Redis replication. # # +------------------+ +---------------+ # | Master | ---> | Replica | # | (receive writes) | | (exact copy) | # +------------------+ +---------------+ # # 1) Redis replication is asynchronous, but you can configure a master to # stop accepting writes if it appears to be not connected with at least # a given number of replicas. # 2) Redis replicas are able to perform a partial resynchronization with the # master if the replication link is lost for a relatively small amount of # time. You may want to configure the replication backlog size (see the next # sections of this file) with a sensible value depending on your needs. # 3) Replication is automatic and does not need user intervention. After a # network partition replicas automatically try to reconnect to masters # and resynchronize with them. # # replicaof <masterip> <masterport> # If the master is password protected (using the "requirepass" configuration # Directive below) it is possible to tell the replica to authenticate before # Starting the replication synchronization process, otherwise the master will # Refuse the replica request. # # masterauth <master-password> # # However this is not enough if you are using Redis ACLs (for Redis version # 6 or greater), and the default user is not capable of running the PSYNC # Command and/or other commands needed for replication. In this case it's # Better to configure a special user to use with replication, and specify the # Masteruser configuration as such: # # masteruser <username> # # When masteruser is specified, the replica will authenticate against its # master using the new AUTH form: AUTH <username> <password>. # When a replica loses its connection with the master, or when the replication # Is still in progress, the replica can act in two different ways: # # 1) if replica-serve-stale-data is set to 'yes' (the default) the replica will # still reply to client requests, possibly with out of date data, or the # data set may just be empty if this is the first synchronization. # # 2) If replica-serve-stale-data is set to 'no' the replica will reply with error # "MASTERDOWN Link with MASTER is down and replica-serve-stale-data is set to 'no'" # to all data access commands, excluding commands such as: # INFO, REPLICAOF, AUTH, SHUTDOWN, REPLCONF, ROLE, CONFIG, SUBSCRIBE, # UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, COMMAND, POST, # HOST and LATENCY. # Replica-serve-stale-data yes # You can configure a replica instance to accept writes or not. Writing against # A replica instance may be useful to store some ephemeral data (because data # Written on a replica will be easily deleted after resync with the master) but # May also cause problems if clients are writing to it because of a # Misconfiguration. # # Since Redis 2.6 by default replicas are read-only. # # Note: read only replicas are not designed to be exposed to untrusted clients # On the internet. It's just a protection layer against misuse of the instance. # Still a read only replica exports by default all the administrative commands # Such as CONFIG, DEBUG, and so forth. To a limited extent you can improve # Security of read only replicas using 'rename-command' to shadow all the # Administrative / dangerous commands. Replica-read-only yes # Replication SYNC strategy: disk or socket. # # New replicas and reconnecting replicas that are not able to continue the # Replication process just receiving differences, need to do what is called a # "full synchronization". An RDB file is transmitted from the master to the # Replicas. # # The transmission can happen in two different ways: # # 1) Disk-backed: The Redis master creates a new process that writes the RDB # file on disk. Later the file is transferred by the parent # process to the replicas incrementally. # 2) Diskless: The Redis master creates a new process that directly writes the # RDB file to replica sockets, without touching the disk at all. # # With disk-backed replication, while the RDB file is generated, more replicas # Can be queued and served with the RDB file as soon as the current child # Producing the RDB file finishes its work. With diskless replication instead # Once the transfer starts, new replicas arriving will be queued and a new # Transfer will start when the current one terminates. # # When diskless replication is used, the master waits a configurable amount of # Time (in seconds) before starting the transfer in the hope that multiple # Replicas will arrive and the transfer can be parallelized. # # With slow disks and fast (large bandwidth) networks, diskless replication # Works better. Repl-diskless-sync yes # When diskless replication is enabled, it is possible to configure the delay # The server waits in order to spawn the child that transfers the RDB via socket # To the replicas. # # This is important since once the transfer starts, it is not possible to serve # New replicas arriving, that will be queued for the next RDB transfer, so the # Server waits a delay in order to let more replicas arrive. # # The delay is specified in seconds, and by default is 5 seconds. To disable # It entirely just set it to 0 seconds and the transfer will start ASAP. Repl-diskless-sync-delay 5 # When diskless replication is enabled with a delay, it is possible to let # The replication start before the maximum delay is reached if the maximum # Number of replicas expected have connected. Default of 0 means that the # Maximum is not defined and Redis will wait the full delay. Repl-diskless-sync-max-replicas 0 # ----------------------------------------------------------------------------- # WARNING: RDB diskless load is experimental. Since in this setup the replica # Does not immediately store an RDB on disk, it may cause data loss during # Failovers. RDB diskless load + Redis modules not handling I/O reads may also # Cause Redis to abort in case of I/O errors during the initial synchronization # Stage with the master. Use only if you know what you are doing. # ----------------------------------------------------------------------------- # # Replica can load the RDB it reads from the replication link directly from the # Socket, or store the RDB to a file and read that file after it was completely # Received from the master. # # In many cases the disk is slower than the network, and storing and loading # The RDB file may increase replication time (and even increase the master's # Copy on Write memory and replica buffers). # However, parsing the RDB file directly from the socket may mean that we have # To flush the contents of the current database before the full rdb was # Received. For this reason we have the following options: # # "disabled" - Don't use diskless load (store the rdb file to the disk first) # "on-empty-db" - Use diskless load only when it is completely safe. # "swapdb" - Keep current db contents in RAM while parsing the data directly # from the socket. Replicas in this mode can keep serving current # data set while replication is in progress, except for cases where # they can't recognize master as having a data set from same # replication history. # Note that this requires sufficient memory, if you don't have it, # you risk an OOM kill. Repl-diskless-load disabled # Master send PINGs to its replicas in a predefined interval. It's possible to # Change this interval with the repl_ping_replica_period option. The default # Value is 10 seconds. # # Repl-ping-replica-period 10 # The following option sets the replication timeout for: # # 1) Bulk transfer I/O during SYNC, from the point of view of replica. # 2) Master timeout from the point of view of replicas (data, pings). # 3) Replica timeout from the point of view of masters (REPLCONF ACK pings). # # It is important to make sure that this value is greater than the value # Specified for repl-ping-replica-period otherwise a timeout will be detected # Every time there is low traffic between the master and the replica. The default # Value is 60 seconds. # # Repl-timeout 60 # Disable TCP_NODELAY on the replica socket after SYNC? # # If you select "yes" Redis will use a smaller number of TCP packets and # Less bandwidth to send data to replicas. But this can add a delay for # The data to appear on the replica side, up to 40 milliseconds with # Linux kernels using a default configuration. # # If you select "no" the delay for data to appear on the replica side will # Be reduced but more bandwidth will be used for replication. # # By default we optimize for low latency, but in very high traffic conditions # Or when the master and replicas are many hops away, turning this to "yes" may # Be a good idea. Repl-disable-tcp-nodelay no # Set the replication backlog size. The backlog is a buffer that accumulates # Replica data when replicas are disconnected for some time, so that when a # Replica wants to reconnect again, often a full resync is not needed, but a # Partial resync is enough, just passing the portion of data the replica # Missed while disconnected. # # The bigger the replication backlog, the longer the replica can endure the # Disconnect and later be able to perform a partial resynchronization. # # The backlog is only allocated if there is at least one replica connected. # # Repl-backlog-size 1 mb # After a master has no connected replicas for some time, the backlog will be # Freed. The following option configures the amount of seconds that need to # Elapse, starting from the time the last replica disconnected, for the backlog # Buffer to be freed. # # Note that replicas never free the backlog for timeout, since they may be # Promoted to masters later, and should be able to correctly "partially # Resynchronize" with other replicas: hence they should always accumulate backlog. # # A value of 0 means to never release the backlog. # # Repl-backlog-ttl 3600 # The replica priority is an integer number published by Redis in the INFO # Output. It is used by Redis Sentinel in order to select a replica to promote # Into a master if the master is no longer working correctly. # # A replica with a low priority number is considered better for promotion, so # For instance if there are three replicas with priority 10, 100, 25 Sentinel # Will pick the one with priority 10, that is the lowest. # # However a special priority of 0 marks the replica as not able to perform the # Role of master, so a replica with priority of 0 will never be selected by # Redis Sentinel for promotion. # # By default the priority is 100. Replica-priority 100 # The propagation error behavior controls how Redis will behave when it is # Unable to handle a command being processed in the replication stream from a master # Or processed while reading from an AOF file. Errors that occur during propagation # Are unexpected, and can cause data inconsistency. However, there are edge cases # In earlier versions of Redis where it was possible for the server to replicate or persist # Commands that would fail on future versions. For this reason the default behavior # Is to ignore such errors and continue processing commands. # # If an application wants to ensure there is no data divergence, this configuration # Should be set to 'panic' instead. The value can also be set to 'panic-on-replicas' # To only panic when a replica encounters an error on the replication stream. One of # These two panic values will become the default value in the future once there are # Sufficient safety mechanisms in place to prevent false positive crashes. # # Propagation-error-behavior ignore # Replica ignore disk write errors controls the behavior of a replica when it is # Unable to persist a write command received from its master to disk. By default, # This configuration is set to 'no' and will crash the replica in this condition. # It is not recommended to change this default, however in order to be compatible # With older versions of Redis this config can be toggled to 'yes' which will just # Log a warning and execute the write command it got from the master. # # Replica-ignore-disk-write-errors no # ----------------------------------------------------------------------------- # By default, Redis Sentinel includes all replicas in its reports. A replica # Can be excluded from Redis Sentinel's announcements. An unannounced replica # will be ignored by the 'sentinel replicas <master>' command and won't be # Exposed to Redis Sentinel's clients. # # This option does not change the behavior of replica-priority. Even with # Replica-announced set to 'no', the replica can be promoted to master. To # Prevent this behavior, set replica-priority to 0. # # Replica-announced yes # It is possible for a master to stop accepting writes if there are less than # N replicas connected, having a lag less or equal than M seconds. # # The N replicas need to be in "online" state. # # The lag in seconds, that must be <= the specified value, is calculated from # The last ping received from the replica, that is usually sent every second. # # This option does not GUARANTEE that N replicas will accept the write, but # Will limit the window of exposure for lost writes in case not enough replicas # Are available, to the specified number of seconds. # # For example to require at least 3 replicas with a lag <= 10 seconds use: # # Min-replicas-to-write 3 # Min-replicas-max-lag 10 # # Setting one or the other to 0 disables the feature. # # By default min-replicas-to-write is set to 0 (feature disabled) and # Min-replicas-max-lag is set to 10. # A Redis master is able to list the address and port of the attached # Replicas in different ways. For example the "INFO replication" section # Offers this information, which is used, among other tools, by # Redis Sentinel in order to discover replica instances. # Another place where this info is available is in the output of the # "ROLE" command of a master. # # The listed IP address and port normally reported by a replica is # Obtained in the following way: # # IP: The address is auto detected by checking the peer address # of the socket used by the replica to connect with the master. # # Port: The port is communicated by the replica during the replication # handshake, and is normally the port that the replica is using to # listen for connections. # # However when port forwarding or Network Address Translation (NAT) is # Used, the replica may actually be reachable via different IP and port # Pairs. The following two options can be used by a replica in order to # Report to its master a specific set of IP and port, so that both INFO # And ROLE will report those values. # # There is no need to use both the options if you need to override just # The port or the IP address. # # Replica-announce-ip 5.5.5.5 # Replica-announce-port 1234 ############################### KEYS TRACKING ################################# # Redis implements server assisted support for client side caching of values. # This is implemented using an invalidation table that remembers, using # A radix key indexed by key name, what clients have which keys. In turn # This is used in order to send invalidation messages to clients. Please # Check this page to understand more about the feature: # # https://redis.io/topics/client-side-caching # # When tracking is enabled for a client, all the read only queries are assumed # To be cached: this will force Redis to store information in the invalidation # Table. When keys are modified, such information is flushed away, and # Invalidation messages are sent to the clients. However if the workload is # Heavily dominated by reads, Redis could use more and more memory in order # To track the keys fetched by many clients. # # For this reason it is possible to configure a maximum fill value for the # Invalidation table. By default it is set to 1 M of keys, and once this limit # Is reached, Redis will start to evict keys in the invalidation table # Even if they were not modified, just to reclaim memory: this will in turn # Force the clients to invalidate the cached values. Basically the table # Maximum size is a trade off between the memory you want to spend server # Side to track information about who cached what, and the ability of clients # To retain cached objects in memory. # # If you set the value to 0, it means there are no limits, and Redis will # Retain as many keys as needed in the invalidation table. # In the "stats" INFO section, you can find information about the number of # Keys in the invalidation table at every given moment. # # Note: when key tracking is used in broadcasting mode, no memory is used # In the server side so this setting is useless. # # Tracking-table-max-keys 1000000 ################################## SECURITY ################################### # Warning: since Redis is pretty fast, an outside user can try up to # 1 million passwords per second against a modern box. This means that you # Should use very strong passwords, otherwise they will be very easy to break. # Note that because the password is really a shared secret between the client # And the server, and should not be memorized by any human, the password # Can be easily a long string from /dev/urandom or whatever, so by using a # Long and unguessable password no brute force attack will be possible. # Redis ACL users are defined in the following format: # # user <username> ... Acl rules ... # # For example: # # user worker +@list +@connection ~jobs:* on >ffa 9203 c 493 aa 99 # # The special username "default" is used for new connections. If this user # Has the "nopass" rule, then new connections will be immediately authenticated # As the "default" user without the need of any password provided via the # AUTH command. Otherwise if the "default" user is not flagged with "nopass" # The connections will start in not authenticated state, and will require # AUTH (or the HELLO command AUTH option) in order to be authenticated and # Start to work. # # The ACL rules that describe what a user can do are the following: # # on Enable the user: it is possible to authenticate as this user. # off Disable the user: it's no longer possible to authenticate # with this user, however the already authenticated connections # will still work. # skip-sanitize-payload RESTORE dump-payload sanitization is skipped. # sanitize-payload RESTORE dump-payload is sanitized (default). # +<command> Allow the execution of that command. # May be used with `|` for allowing subcommands (e.g "+config|get") # -<command> Disallow the execution of that command. # May be used with `|` for blocking subcommands (e.g "-config|set") # +@<category> Allow the execution of all the commands in such category # with valid categories are like @admin, @set, @sortedset, ... # and so forth, see the full list in the server. C file where # the Redis command table is described and defined. # The special category @all means all the commands, but currently # present in the server, and that will be loaded in the future # via modules. # +<command>|first-arg Allow a specific first argument of an otherwise # disabled command. It is only supported on commands with # no sub-commands, and is not allowed as negative form # like -SELECT|1, only additive starting with "+". This # feature is deprecated and may be removed in the future. # allcommands Alias for +@all. Note that it implies the ability to execute # all the future commands loaded via the modules system. # nocommands Alias for -@all. # ~<pattern> Add a pattern of keys that can be mentioned as part of # commands. For instance ~* allows all the keys. The pattern # is a glob-style pattern like the one of KEYS. # It is possible to specify multiple patterns. # %R~<pattern> Add key read pattern that specifies which keys can be read # from. # %W~<pattern> Add key write pattern that specifies which keys can be # written to. # allkeys Alias for ~* # resetkeys Flush the list of allowed keys patterns. # &<pattern> Add a glob-style pattern of Pub/Sub channels that can be # accessed by the user. It is possible to specify multiple channel # patterns. # allchannels Alias for &* # resetchannels Flush the list of allowed channel patterns. # ><password> Add this password to the list of valid password for the user. # For example >mypass will add "mypass" to the list. # This directive clears the "nopass" flag (see later). # <<password> Remove this password from the list of valid passwords. # nopass All the set passwords of the user are removed, and the user # is flagged as requiring no password: it means that every # password will work against this user. If this directive is # used for the default user, every new connection will be # immediately authenticated with the default user without # any explicit AUTH command required. Note that the "resetpass" # directive will clear this condition. # resetpass Flush the list of allowed passwords. Moreover removes the # "nopass" status. After "resetpass" the user has no associated # passwords and there is no way to authenticate without adding # some password (or setting it as "nopass" later). # reset Performs the following actions: resetpass, resetkeys, off, # -@all. The user returns to the same state it has immediately # after its creation. # (<options>) Create a new selector with the options specified within the # parentheses and attach it to the user. Each option should be # space separated. The first character must be ( and the last # character must be ). # Clearselectors Remove all of the currently attached selectors. # Note this does not change the "root" user permissions, # which are the permissions directly applied onto the # user (outside the parentheses). # # ACL rules can be specified in any order: for instance you can start with # Passwords, then flags, or key patterns. However note that the additive # And subtractive rules will CHANGE MEANING depending on the ordering. # For instance see the following example: # # user alice on +@all -DEBUG ~* >somepassword # # This will allow "alice" to use all the commands with the exception of the # DEBUG command, since +@all added all the commands to the set of the commands # Alice can use, and later DEBUG was removed. However if we invert the order # Of two ACL rules the result will be different: # # user alice on -DEBUG +@all ~* >somepassword # # Now DEBUG was removed when alice had yet no commands in the set of allowed # Commands, later all the commands are added, so the user will be able to # Execute everything. # # Basically ACL rules are processed left-to-right. # # The following is a list of command categories and their meanings: # * keyspace - Writing or reading from keys, databases, or their metadata # in a type agnostic way. Includes DEL, RESTORE, DUMP, RENAME, EXISTS, DBSIZE, # KEYS, EXPIRE, TTL, FLUSHALL, etc. Commands that may modify the keyspace, # key or metadata will also have `write` category. Commands that only read # the keyspace, key or metadata will have the `read` category. # * read - Reading from keys (values or metadata). Note that commands that don't # interact with keys, will not have either `read` or `write`. # * write - Writing to keys (values or metadata) # * admin - Administrative commands. Normal applications will never need to use # these. Includes REPLICAOF, CONFIG, DEBUG, SAVE, MONITOR, ACL, SHUTDOWN, etc. # * dangerous - Potentially dangerous (each should be considered with care for # various reasons). This includes FLUSHALL, MIGRATE, RESTORE, SORT, KEYS, # CLIENT, DEBUG, INFO, CONFIG, SAVE, REPLICAOF, etc. # * connection - Commands affecting the connection or other connections. # This includes AUTH, SELECT, COMMAND, CLIENT, ECHO, PING, etc. # * blocking - Potentially blocking the connection until released by another # command. # * fast - Fast O (1) commands. May loop on the number of arguments, but not the # number of elements in the key. # * slow - All commands that are not Fast. # * pubsub - PUBLISH / SUBSCRIBE related # * transaction - WATCH / MULTI / EXEC related commands. # * scripting - Scripting related. # * set - Data type: sets related. # * sortedset - Data type: zsets related. # * list - Data type: lists related. # * hash - Data type: hashes related. # * string - Data type: strings related. # * bitmap - Data type: bitmaps related. # * hyperloglog - Data type: hyperloglog related. # * geo - Data type: geo related. # * stream - Data type: streams related. # # For more information about ACL configuration please refer to # the Redis web site at https://redis.io/topics/acl # ACL LOG # # The ACL Log tracks failed commands and authentication events associated # With ACLs. The ACL Log is useful to troubleshoot failed commands blocked # By ACLs. The ACL Log is stored in memory. You can reclaim memory with # ACL LOG RESET. Define the maximum entry length of the ACL Log below. Acllog-max-len 128 # Using an external ACL file # # Instead of configuring users here in this file, it is possible to use # A stand-alone file just listing users. The two methods cannot be mixed: # If you configure users here and at the same time you activate the external # ACL file, the server will refuse to start. # # The format of the external ACL user file is exactly the same as the # Format that is used inside redis. Conf to describe users. # # Aclfile /etc/redis/users. Acl # IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility # Layer on top of the new ACL system. The option effect will be just setting # The password for the default user. Clients will still authenticate using # AUTH <password> as usually, or more explicitly with AUTH default <password> # If they follow the new protocol: both will work. # # The requirepass is not compatible with aclfile option and the ACL LOAD # Command, these will cause requirepass to be ignored. # # Requirepass foobared Requirepass 123456 # New users are initialized with restrictive permissions by default, via the # equivalent of this ACL rule 'off resetkeys -@all '. Starting with Redis 6.2, it # Is possible to manage access to Pub/Sub channels with ACL rules as well. The # Default Pub/Sub channels permission if new users is controlled by the # Acl-pubsub-default configuration directive, which accepts one of these values: # # Allchannels: grants access to all Pub/Sub channels # Resetchannels: revokes access to all Pub/Sub channels # # From Redis 7.0, acl-pubsub-default defaults to 'resetchannels' permission. # # Acl-pubsub-default resetchannels # Command renaming (DEPRECATED). # # ------------------------------------------------------------------------ # WARNING: avoid using this option if possible. Instead use ACLs to remove # Commands from the default user, and put them only in some admin user you # Create for administrative purposes. # ------------------------------------------------------------------------ # # It is possible to change the name of dangerous commands in a shared # Environment. For instance the CONFIG command may be renamed into something # Hard to guess so that it will still be available for internal-use tools # But not available for general clients. # # Example: # # Rename-command CONFIG b 840 fc 02 d 524045429941 cc 15 f 59 e 41 cb 7 be 6 c 52 # # It is also possible to completely kill a command by renaming it into # An empty string: # # Rename-command CONFIG "" # # Please note that changing the name of commands that are logged into the # AOF file or transmitted to replicas may cause problems. ################################### CLIENTS #################################### # Set the max number of connected clients at the same time. By default # This limit is set to 10000 clients, however if the Redis server is not # Able to configure the process file limit to allow for the specified limit # The max number of allowed clients is set to the current file limit # Minus 32 (as Redis reserves a few file descriptors for internal uses). # # Once the limit is reached Redis will close all the new connections sending # An error 'max number of clients reached'. # # IMPORTANT: When Redis Cluster is used, the max number of connections is also # Shared with the cluster bus: every node in the cluster will use two # Connections, one incoming and another outgoing. It is important to size the # Limit accordingly in case of very large clusters. # # Maxclients 10000 ############################## MEMORY MANAGEMENT ################################ # Set a memory usage limit to the specified amount of bytes. # When the memory limit is reached Redis will try to remove keys # According to the eviction policy selected (see maxmemory-policy). # # If Redis can't remove keys according to the policy, or if the policy is # Set to 'noeviction', Redis will start to reply with errors to commands # That would use more memory, like SET, LPUSH, and so on, and will continue # To reply to read-only commands like GET. # # This option is usually useful when using Redis as an LRU or LFU cache, or to # Set a hard memory limit for an instance (using the 'noeviction' policy). # # WARNING: If you have replicas attached to an instance with maxmemory on, # The size of the output buffers needed to feed the replicas are subtracted # From the used memory count, so that network problems / resyncs will # Not trigger a loop where keys are evicted, and in turn the output # Buffer of replicas is full with DELs of keys evicted triggering the deletion # Of more keys, and so forth until the database is completely emptied. # # In short... If you have replicas attached it is suggested that you set a lower # Limit for maxmemory so that there is some free RAM on the system for replica # Output buffers (but this is not needed if the policy is 'noeviction'). # # maxmemory <bytes> # MAXMEMORY POLICY: how Redis will select what to remove when maxmemory # Is reached. You can select one from the following behaviors: # # Volatile-lru -> Evict using approximated LRU, only keys with an expire set. # Allkeys-lru -> Evict any key using approximated LRU. # Volatile-lfu -> Evict using approximated LFU, only keys with an expire set. # Allkeys-lfu -> Evict any key using approximated LFU. # Volatile-random -> Remove a random key having an expire set. # Allkeys-random -> Remove a random key, any key. # Volatile-ttl -> Remove the key with the nearest expire time (minor TTL) # Noeviction -> Don't evict anything, just return an error on write operations. # # LRU means Least Recently Used # LFU means Least Frequently Used # # Both LRU, LFU and volatile-ttl are implemented using approximated # Randomized algorithms. # # Note: with any of the above policies, when there are no suitable keys for # Eviction, Redis will return an error on write operations that require # More memory. These are usually commands that create new keys, add data or # Modify existing keys. A few examples are: SET, INCR, HSET, LPUSH, SUNIONSTORE, # SORT (due to the STORE argument), and EXEC (if the transaction includes any # Command that requires memory). # # The default is: # # Maxmemory-policy noeviction # LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated # Algorithms (in order to save memory), so you can tune it for speed or # Accuracy. By default Redis will check five keys and pick the one that was # Used least recently, you can change the sample size using the following # Configuration directive. # # The default of 5 produces good enough results. 10 Approximates very closely # True LRU but costs more CPU. 3 is faster but not very accurate. # # Maxmemory-samples 5 # Eviction processing is designed to function well with the default setting. # If there is an unusually large amount of write traffic, this value may need to # Be increased. Decreasing this value may reduce latency at the risk of # Eviction processing effectiveness # 0 = minimum latency, 10 = default, 100 = process without regard to latency # # Maxmemory-eviction-tenacity 10 # Starting from Redis 5, by default a replica will ignore its maxmemory setting # (unless it is promoted to master after a failover or manually). It means # That the eviction of keys will be just handled by the master, sending the # DEL commands to the replica as keys evict in the master side. # # This behavior ensures that masters and replicas stay consistent, and is usually # What you want, however if your replica is writable, or you want the replica # To have a different memory setting, and you are sure all the writes performed # To the replica are idempotent, then you may change this default (but be sure # To understand what you are doing). # # Note that since the replica by default does not evict, it may end using more # Memory than the one set via maxmemory (there are certain buffers that may # Be larger on the replica, or data structures may sometimes take more memory # And so forth). So make sure you monitor your replicas and make sure they # Have enough memory to never hit a real out-of-memory condition before the # Master hits the configured maxmemory setting. # # Replica-ignore-maxmemory yes # Redis reclaims expired keys in two ways: upon access when those keys are # Found to be expired, and also in background, in what is called the # "active expire key". The key space is slowly and interactively scanned # Looking for expired keys to reclaim, so that it is possible to free memory # Of keys that are expired and will never be accessed again in a short time. # # The default effort of the expire cycle will try to avoid having more than # Ten percent of expired keys still in memory, and will try to avoid consuming # More than 25% of total memory and to add latency to the system. However # It is possible to increase the expire "effort" that is normally set to # "1", to a greater value, up to the value "10". At its maximum value the # System will use more CPU, longer cycles (and technically may introduce # More latency), and will tolerate less already expired keys still present # In the system. It's a tradeoff between memory, CPU and latency. # # Active-expire-effort 1 ############################# LAZY FREEING #################################### # Redis has two primitives to delete keys. One is called DEL and is a blocking # Deletion of the object. It means that the server stops processing new commands # In order to reclaim all the memory associated with an object in a synchronous # Way. If the key deleted is associated with a small object, the time needed # In order to execute the DEL command is very small and comparable to most other # O (1) or O (log_N) commands in Redis. However if the key is associated with an # Aggregated value containing millions of elements, the server can block for # A long time (even seconds) in order to complete the operation. # # For the above reasons Redis also offers non blocking deletion primitives # Such as UNLINK (non blocking DEL) and the ASYNC option of FLUSHALL and # FLUSHDB commands, in order to reclaim memory in background. Those commands # Are executed in constant time. Another thread will incrementally free the # Object in the background as fast as possible. # # DEL, UNLINK and ASYNC option of FLUSHALL and FLUSHDB are user-controlled. # It's up to the design of the application to understand when it is a good # Idea to use one or the other. However the Redis server sometimes has to # Delete keys or flush the whole database as a side effect of other operations. # Specifically Redis deletes objects independently of a user call in the # Following scenarios: # # 1) On eviction, because of the maxmemory and maxmemory policy configurations, # in order to make room for new data, without going over the specified # memory limit. # 2) Because of expire: when a key with an associated time to live (see the # EXPIRE command) must be deleted from memory. # 3) Because of a side effect of a command that stores data on a key that may # already exist. For example the RENAME command may delete the old key # content when it is replaced with another one. Similarly SUNIONSTORE # or SORT with STORE option may delete existing keys. The SET command # itself removes any old content of the specified key in order to replace # it with the specified string. # 4) During replication, when a replica performs a full resynchronization with # its master, the content of the whole database is removed in order to # load the RDB file just transferred. # # In all the above cases the default is to delete objects in a blocking way, # Like if DEL was called. However you can configure each case specifically # In order to instead release memory in a non-blocking way like if UNLINK # Was called, using the following configuration directives. Lazyfree-lazy-eviction no Lazyfree-lazy-expire no Lazyfree-lazy-server-del no Replica-lazy-flush no # It is also possible, for the case when to replace the user code DEL calls # With UNLINK calls is not easy, to modify the default behavior of the DEL # Command to act exactly like UNLINK, using the following configuration # Directive: Lazyfree-lazy-user-del no # FLUSHDB, FLUSHALL, SCRIPT FLUSH and FUNCTION FLUSH support both asynchronous and synchronous # Deletion, which can be controlled by passing the [SYNC|ASYNC] flags into the # Commands. When neither flag is passed, this directive will be used to determine # If the data should be deleted asynchronously. Lazyfree-lazy-user-flush no ################################ THREADED I/O ################################# # Redis is mostly single threaded, however there are certain threaded # Operations such as UNLINK, slow I/O accesses and other things that are # Performed on side threads. # # Now it is also possible to handle Redis clients socket reads and writes # In different I/O threads. Since especially writing is so slow, normally # Redis users use pipelining in order to speed up the Redis performances per # Core, and spawn multiple instances in order to scale more. Using I/O # Threads it is possible to easily speedup two times Redis without resorting # To pipelining nor sharding of the instance. # # By default threading is disabled, we suggest enabling it only in machines # That have at least 4 or more cores, leaving at least one spare core. # Using more than 8 threads is unlikely to help much. We also recommend using # Threaded I/O only if you actually have performance problems, with Redis # Instances being able to use a quite big percentage of CPU time, otherwise # There is no point in using this feature. # # So for instance if you have a four cores boxes, try to use 2 or 3 I/O # Threads, if you have a 8 cores, try to use 6 threads. In order to # Enable I/O threads use the following configuration directive: # # Io-threads 4 # # Setting io-threads to 1 will just use the main thread as usual. # When I/O threads are enabled, we only use threads for writes, that is # To thread the write (2) syscall and transfer the client buffers to the # Socket. However it is also possible to enable threading of reads and # Protocol parsing using the following configuration directive, by setting # It to yes: # # Io-threads-do-reads no # # Usually threading reads doesn't help much. # # NOTE 1: This configuration directive cannot be changed at runtime via # CONFIG SET. Also, this feature currently does not work when SSL is # Enabled. # # NOTE 2: If you want to test the Redis speedup using redis-benchmark, make # Sure you also run the benchmark itself in threaded mode, using the # --threads option to match the number of Redis threads, otherwise you'll not # Be able to notice the improvements. ############################ KERNEL OOM CONTROL ############################## # On Linux, it is possible to hint the kernel OOM killer on what processes # Should be killed first when out of memory. # # Enabling this feature makes Redis actively control the oom_score_adj value # For all its processes, depending on their role. The default scores will # Attempt to have background child processes killed before all others, and # Replicas killed before masters. # # Redis supports these options: # # No: Don't make changes to oom-score-adj (default). # Yes: Alias to "relative" see below. # Absolute: Values in oom-score-adj-values are written as is to the kernel. # Relative: Values are used relative to the initial value of oom_score_adj when # the server starts and are then clamped to a range of -1000 to 1000. # Because typically the initial value is 0, they will often match the # absolute values. Oom-score-adj no # When oom-score-adj is used, this directive controls the specific values used # For master, replica and background child processes. Values range -2000 to # 2000 (higher means more likely to be killed). # # Unprivileged processes (not root, and without CAP_SYS_RESOURCE capabilities) # Can freely increase their value, but not decrease it below its initial # Settings. This means that setting oom-score-adj to "relative" and setting the # Oom-score-adj-values to positive values will always succeed. Oom-score-adj-values 0 200 800 #################### KERNEL transparent hugepage CONTROL ###################### # Usually the kernel Transparent Huge Pages control is set to "madvise" or # Or "never" by default (/sys/kernel/mm/transparent_hugepage/enabled), in which # Case this config has no effect. On systems in which it is set to "always", # Redis will attempt to disable it specifically for the redis process in order # To avoid latency problems specifically with fork (2) and CoW. # If for some reason you prefer to keep it enabled, you can set this config to # "no" and the kernel global to "always". Disable-thp yes ############################## APPEND ONLY MODE ############################### # By default Redis asynchronously dumps the dataset on disk. This mode is # Good enough in many applications, but an issue with the Redis process or # A power outage may result into a few minutes of writes lost (depending on # The configured save points). # # The Append Only File is an alternative persistence mode that provides # Much better durability. For instance using the default data fsync policy # (see later in the config file) Redis can lose just one second of writes in a # Dramatic event like a server power outage, or a single write if something # Wrong with the Redis process itself happens, but the operating system is # Still running correctly. # # AOF and RDB persistence can be enabled at the same time without problems. # If the AOF is enabled on startup Redis will load the AOF, that is the file # With the better durability guarantees. # # Please check https://redis.io/topics/persistence for more information. Appendonly no # The base name of the append only file. # # Redis 7 and newer use a set of append-only files to persist the dataset # And changes applied to it. There are two basic types of files in use: # # - Base files, which are a snapshot representing the complete state of the # dataset at the time the file was created. Base files can be either in # the form of RDB (binary serialized) or AOF (textual commands). # - Incremental files, which contain additional commands that were applied # to the dataset following the previous file. # # In addition, manifest files are used to track the files and the order in # Which they were created and should be applied. # # Append-only file names are created by Redis following a specific pattern. # The file name's prefix is based on the 'appendfilename' configuration # Parameter, followed by additional information about the sequence and type. # # For example, if appendfilename is set to appendonly. Aof, the following file # Names could be derived: # # - appendonly. Aof. 1. Base. Rdb as a base file. # - appendonly. Aof. 1. Incr. Aof, appendonly. Aof. 2. Incr. Aof as incremental files. # - appendonly. Aof. Manifest as a manifest file. Appendfilename "appendonly. Aof" # For convenience, Redis stores all persistent append-only files in a dedicated # Directory. The name of the directory is determined by the appenddirname # Configuration parameter. Appenddirname "appendonlydir" # The fsync () call tells the Operating System to actually write data on disk # Instead of waiting for more data in the output buffer. Some OS will really flush # Data on disk, some other OS will just try to do it ASAP. # # Redis supports three different modes: # # No: don't fsync, just let the OS flush the data when it wants. Faster. # Always: fsync after every write to the append only log. Slow, Safest. # Everysec: fsync only one time every second. Compromise. # # The default is "everysec", as that's usually the right compromise between # Speed and data safety. It's up to you to understand if you can relax this to # "no" that will let the operating system flush the output buffer when # It wants, for better performances (but if you can live with the idea of # Some data loss consider the default persistence mode that's snapshotting), # Or on the contrary, use "always" that's very slow but a bit safer than # Everysec. # # More details please check the following article: # http://antirez.com/post/redis-persistence-demystified.html # # If unsure, use "everysec". # Appendfsync always Appendfsync everysec # Appendfsync no # When the AOF fsync policy is set to always or everysec, and a background # Saving process (a background save or AOF log background rewriting) is # Performing a lot of I/O against the disk, in some Linux configurations # Redis may block too long on the fsync () call. Note that there is no fix for # This currently, as even performing fsync in a different thread will block # Our synchronous write (2) call. # # In order to mitigate this problem it's possible to use the following option # That will prevent fsync () from being called in the main process while a # BGSAVE or BGREWRITEAOF is in progress. # # This means that while another child is saving, the durability of Redis is # The same as "appendfsync no". In practical terms, this means that it is # Possible to lose up to 30 seconds of log in the worst scenario (with the # Default Linux settings). # # If you have latency problems turn this to "yes". Otherwise leave it as # "no" that is the safest pick from the point of view of durability. No-appendfsync-on-rewrite no # Automatic rewrite of the append only file. # Redis is able to automatically rewrite the log file implicitly calling # BGREWRITEAOF when the AOF log size grows by the specified percentage. # # This is how it works: Redis remembers the size of the AOF file after the # Latest rewrite (if no rewrite has happened since the restart, the size of # The AOF at startup is used). # # This base size is compared to the current size. If the current size is # Bigger than the specified percentage, the rewrite is triggered. Also # You need to specify a minimal size for the AOF file to be rewritten, this # Is useful to avoid rewriting the AOF file even if the percentage increase # Is reached but it is still pretty small. # # Specify a percentage of zero in order to disable the automatic AOF # Rewrite feature. Auto-aof-rewrite-percentage 100 Auto-aof-rewrite-min-size 64 mb # An AOF file may be found to be truncated at the end during the Redis # Startup process, when the AOF data gets loaded back into memory. # This may happen when the system where Redis is running # Crashes, especially when an ext 4 filesystem is mounted without the # Data=ordered option (however this can't happen when Redis itself # Crashes or aborts but the operating system still works correctly). # # Redis can either exit with an error when this happens, or load as much # Data as possible (the default now) and start if the AOF file is found # To be truncated at the end. The following option controls this behavior. # # If aof-load-truncated is set to yes, a truncated AOF file is loaded and # The Redis server starts emitting a log to inform the user of the event. # Otherwise if the option is set to no, the server aborts with an error # And refuses to start. When the option is set to no, the user requires # To fix the AOF file using the "redis-check-aof" utility before to restart # The server. # # Note that if the AOF file will be found to be corrupted in the middle # The server will still exit with an error. This option only applies when # Redis will try to read more data from the AOF file but not enough bytes # Will be found. Aof-load-truncated yes # Redis can create append-only base files in either RDB or AOF formats. Using # The RDB format is always faster and more efficient, and disabling it is only # Supported for backward compatibility purposes. Aof-use-rdb-preamble yes # Redis supports recording timestamp annotations in the AOF to support restoring # The data from a specific point-in-time. However, using this capability changes # The AOF format in a way that may not be compatible with existing AOF parsers. Aof-timestamp-enabled no ################################ SHUTDOWN ##################################### # Maximum time to wait for replicas when shutting down, in seconds. # # During shut down, a grace period allows any lagging replicas to catch up with # The latest replication offset before the master exists. This period can # Prevent data loss, especially for deployments without configured disk backups. # # The 'shutdown-timeout' value is the grace period's duration in seconds. It is # Only applicable when the instance has replicas. To disable the feature, set # The value to 0. # # Shutdown-timeout 10 # When Redis receives a SIGINT or SIGTERM, shutdown is initiated and by default # An RDB snapshot is written to disk in a blocking operation if save points are configured. # The options used on signaled shutdown can include the following values: # Default: Saves RDB snapshot only if save points are configured. # Waits for lagging replicas to catch up. # Save: Forces a DB saving operation even if no save points are configured. # Nosave: Prevents DB saving operation even if one or more save points are configured. # Now: Skips waiting for lagging replicas. # Force: Ignores any errors that would normally prevent the server from exiting. # # Any combination of values is allowed as long as "save" and "nosave" are not set simultaneously. # Example: "nosave force now" # # Shutdown-on-sigint default # Shutdown-on-sigterm default ################ NON-DETERMINISTIC LONG BLOCKING COMMANDS ##################### # Maximum time in milliseconds for EVAL scripts, functions and in some cases # Modules' commands before Redis can start processing or rejecting other clients. # # If the maximum execution time is reached Redis will start to reply to most # Commands with a BUSY error. # # In this state Redis will only allow a handful of commands to be executed. # For instance, SCRIPT KILL, FUNCTION KILL, SHUTDOWN NOSAVE and possibly some # Module specific 'allow-busy' commands. # # SCRIPT KILL and FUNCTION KILL will only be able to stop a script that did not # Yet call any write commands, so SHUTDOWN NOSAVE may be the only way to stop # The server in the case a write command was already issued by the script when # The user doesn't want to wait for the natural termination of the script. # # The default is 5 seconds. It is possible to set it to 0 or a negative value # To disable this mechanism (uninterrupted execution). Note that in the past # This config had a different name, which is now an alias, so both of these do # The same: # Lua-time-limit 5000 # Busy-reply-threshold 5000 ################################ REDIS CLUSTER ############################### # Normal Redis instances can't be part of a Redis Cluster; only nodes that are # Started as cluster nodes can. In order to start a Redis instance as a # Cluster node enable the cluster support uncommenting the following: # # Cluster-enabled yes # Every cluster node has a cluster configuration file. This file is not # Intended to be edited by hand. It is created and updated by Redis nodes. # Every Redis Cluster node requires a different cluster configuration file. # Make sure that instances running in the same system do not have # Overlapping cluster configuration file names. # # Cluster-config-file nodes-6379. Conf # Cluster node timeout is the amount of milliseconds a node must be unreachable # For it to be considered in failure state. # Most other internal time limits are a multiple of the node timeout. # # Cluster-node-timeout 15000 # The cluster port is the port that the cluster bus will listen for inbound connections on. When set # To the default value, 0, it will be bound to the command port + 10000. Setting this value requires # You to specify the cluster bus port when executing cluster meet. # Cluster-port 0 # A replica of a failing master will avoid to start a failover if its data # Looks too old. # # There is no simple way for a replica to actually have an exact measure of # Its "data age", so the following two checks are performed: # # 1) If there are multiple replicas able to failover, they exchange messages # in order to try to give an advantage to the replica with the best # replication offset (more data from the master processed). # Replicas will try to get their rank by offset, and apply to the start # of the failover a delay proportional to their rank. # # 2) Every single replica computes the time of the last interaction with # its master. This can be the last ping or command received (if the master # is still in the "connected" state), or the time that elapsed since the # disconnection with the master (if the replication link is currently down). # If the last interaction is too old, the replica will not try to failover # at all. # # The point "2" can be tuned by user. Specifically a replica will not perform # The failover if, since the last interaction with the master, the time # Elapsed is greater than: # # (node-timeout * cluster-replica-validity-factor) + repl-ping-replica-period # # So for example if node-timeout is 30 seconds, and the cluster-replica-validity-factor # Is 10, and assuming a default repl-ping-replica-period of 10 seconds, the # Replica will not try to failover if it was not able to talk with the master # For longer than 310 seconds. # # A large cluster-replica-validity-factor may allow replicas with too old data to failover # A master, while a too small value may prevent the cluster from being able to # Elect a replica at all. # # For maximum availability, it is possible to set the cluster-replica-validity-factor # To a value of 0, which means, that replicas will always try to failover the # Master regardless of the last time they interacted with the master. # (However they'll always try to apply a delay proportional to their # Offset rank). # # Zero is the only value able to guarantee that when all the partitions heal # The cluster will always be able to continue. # # Cluster-replica-validity-factor 10 # Cluster replicas are able to migrate to orphaned masters, that are masters # That are left without working replicas. This improves the cluster ability # To resist to failures as otherwise an orphaned master can't be failed over # In case of failure if it has no working replicas. # # Replicas migrate to orphaned masters only if there are still at least a # Given number of other working replicas for their old master. This number # Is the "migration barrier". A migration barrier of 1 means that a replica # Will migrate only if there is at least 1 other working replica for its master # And so forth. It usually reflects the number of replicas you want for every # Master in your cluster. # # Default is 1 (replicas migrate only if their masters remain with at least # One replica). To disable migration just set it to a very large value or # Set cluster-allow-replica-migration to 'no'. # A value of 0 can be set but is useful only for debugging and dangerous # In production. # # Cluster-migration-barrier 1 # Turning off this option allows to use less automatic cluster configuration. # It both disables migration to orphaned masters and migration from masters # That became empty. # # Default is 'yes' (allow automatic migrations). # # Cluster-allow-replica-migration yes # By default Redis Cluster nodes stop accepting queries if they detect there # Is at least a hash slot uncovered (no available node is serving it). # This way if the cluster is partially down (for example a range of hash slots # Are no longer covered) all the cluster becomes, eventually, unavailable. # It automatically returns available as soon as all the slots are covered again. # # However sometimes you want the subset of the cluster which is working, # To continue to accept queries for the part of the key space that is still # Covered. In order to do so, just set the cluster-require-full-coverage # Option to no. # # Cluster-require-full-coverage yes # This option, when set to yes, prevents replicas from trying to failover its # Master during master failures. However the replica can still perform a # Manual failover, if forced to do so. # # This is useful in different scenarios, especially in the case of multiple # Data center operations, where we want one side to never be promoted if not # In the case of a total DC failure. # # Cluster-replica-no-failover no # This option, when set to yes, allows nodes to serve read traffic while the # Cluster is in a down state, as long as it believes it owns the slots. # # This is useful for two cases. The first case is for when an application # Doesn't require consistency of data during node failures or network partitions. # One example of this is a cache, where as long as the node has the data it # Should be able to serve it. # # The second use case is for configurations that don't meet the recommended # Three shards but want to enable cluster mode and scale later. A # Master outage in a 1 or 2 shard configuration causes a read/write outage to the # Entire cluster without this option set, with it set there is only a write outage. # Without a quorum of masters, slot ownership will not change automatically. # # Cluster-allow-reads-when-down no # This option, when set to yes, allows nodes to serve pubsub shard traffic while # The cluster is in a down state, as long as it believes it owns the slots. # # This is useful if the application would like to use the pubsub feature even when # The cluster global stable state is not OK. If the application wants to make sure only # One shard is serving a given channel, this feature should be kept as yes. # # Cluster-allow-pubsubshard-when-down yes # Cluster link send buffer limit is the limit on the memory usage of an individual # Cluster bus link's send buffer in bytes. Cluster links would be freed if they exceed # This limit. This is to primarily prevent send buffers from growing unbounded on links # toward slow peers (E.g. PubSub messages being piled up). # This limit is disabled by default. Enable this limit when 'mem_cluster_links' INFO field # And/or 'send-buffer-allocated' entries in the 'CLUSTER LINKS` command output continuously increase. # Minimum limit of 1 gb is recommended so that cluster link buffer can fit in at least a single # PubSub message by default. (client-query-buffer-limit default value is 1 gb) # # Cluster-link-sendbuf-limit 0 # Clusters can configure their announced hostname using this config. This is a common use case for # Applications that need to use TLS Server Name Indication (SNI) or dealing with DNS based # Routing. By default this value is only shown as additional metadata in the CLUSTER SLOTS # Command, but can be changed using 'cluster-preferred-endpoint-type' config. This value is # Communicated along the clusterbus to all nodes, setting it to an empty string will remove # The hostname and also propagate the removal. # # Cluster-announce-hostname "" # Clusters can advertise how clients should connect to them using either their IP address, # A user defined hostname, or by declaring they have no endpoint. Which endpoint is # Shown as the preferred endpoint is set by using the cluster-preferred-endpoint-type # Config with values 'ip', 'hostname', or 'unknown-endpoint'. This value controls how # The endpoint returned for MOVED/ASKING requests as well as the first field of CLUSTER SLOTS. # If the preferred endpoint type is set to hostname, but no announced hostname is set, a '?' # Will be returned instead. # # When a cluster advertises itself as having an unknown endpoint, it's indicating that # The server doesn't know how clients can reach the cluster. This can happen in certain # Networking situations where there are multiple possible routes to the node, and the # Server doesn't know which one the client took. In this case, the server is expecting # The client to reach out on the same endpoint it used for making the last request, but use # The port provided in the response. # # Cluster-preferred-endpoint-type ip # In order to setup your cluster make sure to read the documentation # available at https://redis.io web site. ########################## CLUSTER DOCKER/NAT support ######################## # In certain deployments, Redis Cluster nodes address discovery fails, because # Addresses are NAT-ted or because ports are forwarded (the typical case is # Docker and other containers). # # In order to make Redis Cluster working in such environments, a static # Configuration where each node knows its public address is needed. The # Following four options are used for this scope, and are: # # * cluster-announce-ip # * cluster-announce-port # * cluster-announce-tls-port # * cluster-announce-bus-port # # Each instructs the node about its address, client ports (for connections # Without and with TLS) and cluster message bus port. The information is then # Published in the header of the bus packets so that other nodes will be able to # Correctly map the address of the node publishing the information. # # If cluster-tls is set to yes and cluster-announce-tls-port is omitted or set # To zero, then cluster-announce-port refers to the TLS port. Note also that # Cluster-announce-tls-port has no effect if cluster-tls is set to no. # # If the above options are not used, the normal Redis Cluster auto-detection # Will be used instead. # # Note that when remapped, the bus port may not be at the fixed offset of # Clients port + 10000, so you can specify any port and bus-port depending # On how they get remapped. If the bus-port is not set, a fixed offset of # 10000 will be used as usual. # # Example: # # Cluster-announce-ip 10.1.1.5 # Cluster-announce-tls-port 6379 # Cluster-announce-port 0 # Cluster-announce-bus-port 6380 ################################## SLOW LOG ################################### # The Redis Slow Log is a system to log queries that exceeded a specified # Execution time. The execution time does not include the I/O operations # Like talking with the client, sending the reply and so forth, # But just the time needed to actually execute the command (this is the only # Stage of command execution where the thread is blocked and can not serve # Other requests in the meantime). # # You can configure the slow log with two parameters: one tells Redis # What is the execution time, in microseconds, to exceed in order for the # Command to get logged, and the other parameter is the length of the # Slow log. When a new command is logged the oldest one is removed from the # Queue of logged commands. # The following time is expressed in microseconds, so 1000000 is equivalent # To one second. Note that a negative number disables the slow log, while # A value of zero forces the logging of every command. Slowlog-log-slower-than 10000 # There is no limit to this length. Just be aware that it will consume memory. # You can reclaim memory used by the slow log with SLOWLOG RESET. Slowlog-max-len 128 ################################ LATENCY MONITOR ############################## # The Redis latency monitoring subsystem samples different operations # At runtime in order to collect data related to possible sources of # Latency of a Redis instance. # # Via the LATENCY command this information is available to the user that can # Print graphs and obtain reports. # # The system only logs operations that were performed in a time equal or # Greater than the amount of milliseconds specified via the # Latency-monitor-threshold configuration directive. When its value is set # To zero, the latency monitor is turned off. # # By default latency monitoring is disabled since it is mostly not needed # If you don't have latency issues, and collecting data has a performance # Impact, that while very small, can be measured under big load. Latency # Monitoring can easily be enabled at runtime using the command # "CONFIG SET latency-monitor-threshold <milliseconds>" if needed. Latency-monitor-threshold 0 ################################ LATENCY TRACKING ############################## # The Redis extended latency monitoring tracks the per command latencies and enables # Exporting the percentile distribution via the INFO latencystats command, # And cumulative latency distributions (histograms) via the LATENCY command. # # By default, the extended latency monitoring is enabled since the overhead # Of keeping track of the command latency is very small. # Latency-tracking yes # By default the exported latency percentiles via the INFO latencystats command # Are the p 50, p 99, and p 999. # Latency-tracking-info-percentiles 50 99 99.9 ############################# EVENT NOTIFICATION ############################## # Redis can notify Pub/Sub clients about events happening in the key space. # This feature is documented at https://redis.io/topics/notifications # # For instance if keyspace events notification is enabled, and a client # Performs a DEL operation on key "foo" stored in the Database 0, two # Messages will be published via Pub/Sub: # # PUBLISH __keyspace@0__ : foo del # PUBLISH __keyevent@0__ : del foo # # It is possible to select the events that Redis will notify among a set # Of classes. Every class is identified by a single character: # # K Keyspace events, published with __keyspace@<db>__ prefix. # E Keyevent events, published with __keyevent@<db>__ prefix. # g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ... # $ String commands # l List commands # s Set commands # h Hash commands # z Sorted set commands # x Expired events (events generated every time a key expires) # e Evicted events (events generated when a key is evicted for maxmemory) # n New key events (Note: not included in the 'A' class) # t Stream commands # d Module key type events # m Key-miss events (Note: It is not included in the 'A' class) # A Alias for g$lshzxetd, so that the "AKE" string means all the events # (Except key-miss events which are excluded from 'A' due to their # unique nature). # # The "notify-keyspace-events" takes as argument a string that is composed # of zero or multiple characters. The empty string means that notifications # are disabled. # # Example: to enable list and generic events, from the point of view of the # event name, use: # # notify-keyspace-events Elg # # Example 2: to get the stream of the expired keys subscribing to channel # name __keyevent@0__ : expired use: # # notify-keyspace-events Ex # # By default all notifications are disabled because most users don't need # this feature and the feature has some overhead. Note that if you don't # specify at least one of K or E, no events will be delivered. Notify-keyspace-events "" ############################### ADVANCED CONFIG ############################### # Hashes are encoded using a memory efficient data structure when they have a # Small number of entries, and the biggest entry does not exceed a given # Threshold. These thresholds can be configured using the following directives. Hash-max-listpack-entries 512 Hash-max-listpack-value 64 # Lists are also encoded in a special way to save a lot of space. # The number of entries allowed per internal list node can be specified # As a fixed maximum size or a maximum number of elements. # For a fixed maximum size, use -5 through -1, meaning: # -5: max size: 64 Kb <-- not recommended for normal workloads # -4: max size: 32 Kb <-- not recommended # -3: max size: 16 Kb <-- probably not recommended # -2: max size: 8 Kb <-- good # -1: max size: 4 Kb <-- good # Positive numbers mean store up to _exactly_ that number of elements # Per list node. # The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size), # But if your use case is unique, adjust the settings as necessary. List-max-listpack-size -2 # Lists may also be compressed. # Compress depth is the number of quicklist ziplist nodes from *each* side of # The list to *exclude* from compression. The head and tail of the list # Are always uncompressed for fast push/pop operations. Settings are: # 0: disable all list compression # 1: depth 1 means "don't start compressing until after 1 node into the list, # going from either the head or tail" # So: [head]->node->node->...->node->[tail] # [head], [tail] will always be uncompressed; inner nodes will compress. # 2: [head]->[next]->node->node->...->node->[prev]->[tail] # 2 here means: don't compress head or head->next or tail->prev or tail, # but compress all nodes between them. # 3: [head]->[next]->[next]->node->node->...->node->[prev]->[prev]->[tail] # Etc. List-compress-depth 0 # Sets have a special encoding in just one case: when a set is composed # Of just strings that happen to be integers in radix 10 in the range # Of 64 bit signed integers. # The following configuration setting sets the limit in the size of the # Set in order to use this special memory saving encoding. Set-max-intset-entries 512 # Similarly to hashes and lists, sorted sets are also specially encoded in # Order to save a lot of space. This encoding is only used when the length and # Elements of a sorted set are below the following limits: Zset-max-listpack-entries 128 Zset-max-listpack-value 64 # HyperLogLog sparse representation bytes limit. The limit includes the # 16 bytes header. When an HyperLogLog using the sparse representation crosses # This limit, it is converted into the dense representation. # # A value greater than 16000 is totally useless, since at that point the # Dense representation is more memory efficient. # # The suggested value is ~ 3000 in order to have the benefits of # The space efficient encoding without slowing down too much PFADD, # Which is O (N) with the sparse encoding. The value can be raised to # ~ 10000 when CPU is not a concern, but space is, and the data set is # Composed of many HyperLogLogs with cardinality in the 0 - 15000 range. Hll-sparse-max-bytes 3000 # Streams macro node max size / items. The stream data structure is a radix # Tree of big nodes that encode multiple items inside. Using this configuration # It is possible to configure how big a single node can be in bytes, and the # Maximum number of items it may contain before switching to a new node when # Appending new stream entries. If any of the following settings are set to # Zero, the limit is ignored, so for instance it is possible to set just a # Max entries limit by setting max-bytes to 0 and max-entries to the desired # Value. Stream-node-max-bytes 4096 Stream-node-max-entries 100 # Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in # Order to help rehashing the main Redis hash table (the one mapping top-level # Keys to values). The hash table implementation Redis uses (see dict. C) # Performs a lazy rehashing: the more operation you run into a hash table # That is rehashing, the more rehashing "steps" are performed, so if the # Server is idle the rehashing is never complete and some more memory is used # By the hash table. # # The default is to use this millisecond 10 times every second in order to # Actively rehash the main dictionaries, freeing memory when possible. # # If unsure: # Use "activerehashing no" if you have hard latency requirements and it is # Not a good thing in your environment that Redis can reply from time to time # To queries with 2 milliseconds delay. # # Use "activerehashing yes" if you don't have such hard requirements but # Want to free memory asap when possible. Activerehashing yes # The client output buffer limits can be used to force disconnection of clients # That are not reading data from the server fast enough for some reason (a # Common reason is that a Pub/Sub client can't consume messages as fast as the # Publisher can produce them). # # The limit can be set differently for the three different classes of clients: # # Normal -> normal clients including MONITOR clients # Replica -> replica clients # Pubsub -> clients subscribed to at least one pubsub channel or pattern # # The syntax of every client-output-buffer-limit directive is the following: # # client-output-buffer-limit <class> <hard limit> <soft limit> <soft seconds> # # A client is immediately disconnected once the hard limit is reached, or if # The soft limit is reached and remains reached for the specified number of # Seconds (continuously). # So for instance if the hard limit is 32 megabytes and the soft limit is # 16 megabytes / 10 seconds, the client will get disconnected immediately # If the size of the output buffers reach 32 megabytes, but will also get # Disconnected if the client reaches 16 megabytes and continuously overcomes # The limit for 10 seconds. # # By default normal clients are not limited because they don't receive data # Without asking (in a push way), but just after a request, so only # Asynchronous clients may create a scenario where data is requested faster # Than it can read. # # Instead there is a default limit for pubsub and replica clients, since # Subscribers and replicas receive data in a push fashion. # # Note that it doesn't make sense to set the replica clients output buffer # Limit lower than the repl-backlog-size config (partial sync will succeed # And then replica will get disconnected). # Such a configuration is ignored (the size of repl-backlog-size will be used). # This doesn't have memory consumption implications since the replica client # Will share the backlog buffers memory. # # Both the hard or the soft limit can be disabled by setting them to zero. Client-output-buffer-limit normal 0 0 0 Client-output-buffer-limit replica 256 mb 64 mb 60 Client-output-buffer-limit pubsub 32 mb 8 mb 60 # Client query buffers accumulate new commands. They are limited to a fixed # Amount by default in order to avoid that a protocol desynchronization (for # Instance due to a bug in the client) will lead to unbound memory usage in # The query buffer. However you can configure it here if you have very special # Needs, such us huge multi/exec requests or alike. # # Client-query-buffer-limit 1 gb # In some scenarios client connections can hog up memory leading to OOM # Errors or data eviction. To avoid this we can cap the accumulated memory # Used by all client connections (all pubsub and normal clients). Once we # Reach that limit connections will be dropped by the server freeing up # Memory. The server will attempt to drop the connections using the most # Memory first. We call this mechanism "client eviction". # # Client eviction is configured using the maxmemory-clients setting as follows: # 0 - client eviction is disabled (default) # # A memory value can be used for the client eviction threshold, # For example: # Maxmemory-clients 1 g # # A percentage value (between 1% and 100%) means the client eviction threshold # Is based on a percentage of the maxmemory setting. For example to set client # Eviction at 5% of maxmemory: # Maxmemory-clients 5% # In the Redis protocol, bulk requests, that are, elements representing single # Strings, are normally limited to 512 mb. However you can change this limit # Here, but must be 1 mb or greater # # Proto-max-bulk-len 512 mb # Redis calls an internal function to perform many background tasks, like # Closing connections of clients in timeout, purging expired keys that are # Never requested, and so forth. # # Not all tasks are performed with the same frequency, but Redis checks for # Tasks to perform according to the specified "hz" value. # # By default "hz" is set to 10. Raising the value will use more CPU when # Redis is idle, but at the same time will make Redis more responsive when # There are many keys expiring at the same time, and timeouts may be # Handled with more precision. # # The range is between 1 and 500, however a value over 100 is usually not # A good idea. Most users should use the default of 10 and raise this up to # 100 only in environments where very low latency is required. Hz 10 # Normally it is useful to have an HZ value which is proportional to the # Number of clients connected. This is useful in order, for instance, to # Avoid too many clients are processed for each background task invocation # In order to avoid latency spikes. # # Since the default HZ value by default is conservatively set to 10, Redis # Offers, and enables by default, the ability to use an adaptive HZ value # Which will temporarily raise when there are many connected clients. # # When dynamic HZ is enabled, the actual configured HZ will be used # As a baseline, but multiples of the configured HZ value will be actually # Used as needed once more clients are connected. In this way an idle # Instance will use very little CPU time while a busy instance will be # More responsive. Dynamic-hz yes # When a child rewrites the AOF file, if the following option is enabled # The file will be fsync-ed every 4 MB of data generated. This is useful # In order to commit the file to the disk more incrementally and avoid # Big latency spikes. Aof-rewrite-incremental-fsync yes # When redis saves RDB file, if the following option is enabled # The file will be fsync-ed every 4 MB of data generated. This is useful # In order to commit the file to the disk more incrementally and avoid # Big latency spikes. Rdb-save-incremental-fsync yes # Redis LFU eviction (see maxmemory setting) can be tuned. However it is a good # Idea to start with the default settings and only change them after investigating # How to improve the performances and how the keys LFU change over time, which # Is possible to inspect via the OBJECT FREQ command. # # There are two tunable parameters in the Redis LFU implementation: the # Counter logarithm factor and the counter decay time. It is important to # Understand what the two parameters mean before changing them. # # The LFU counter is just 8 bits per key, it's maximum value is 255, so Redis # Uses a probabilistic increment with logarithmic behavior. Given the value # Of the old counter, when a key is accessed, the counter is incremented in # This way: # # 1. A random number R between 0 and 1 is extracted. # 2. A probability P is calculated as 1/(old_value*lfu_log_factor+1). # 3. The counter is incremented only if R < P. # # The default lfu-log-factor is 10. This is a table of how the frequency # Counter changes with a different number of accesses with different # Logarithmic factors: # # +--------+------------+------------+------------+------------+------------+ # | factor | 100 hits | 1000 hits | 100 K hits | 1 M hits | 10 M hits | # +--------+------------+------------+------------+------------+------------+ # | 0 | 104 | 255 | 255 | 255 | 255 | # +--------+------------+------------+------------+------------+------------+ # | 1 | 18 | 49 | 255 | 255 | 255 | # +--------+------------+------------+------------+------------+------------+ # | 10 | 10 | 18 | 142 | 255 | 255 | # +--------+------------+------------+------------+------------+------------+ # | 100 | 8 | 11 | 49 | 143 | 255 | # +--------+------------+------------+------------+------------+------------+ # # NOTE: The above table was obtained by running the following commands: # # redis-benchmark -n 1000000 incr foo # redis-cli object freq foo # # NOTE 2: The counter initial value is 5 in order to give new objects a chance # To accumulate hits. # # The counter decay time is the time, in minutes, that must elapse in order # For the key counter to be divided by two (or decremented if it has a value # Less <= 10). # # The default value for the lfu-decay-time is 1. A special value of 0 means to # Decay the counter every time it happens to be scanned. # # Lfu-log-factor 10 # Lfu-decay-time 1 ########################### ACTIVE DEFRAGMENTATION ####################### # # What is active defragmentation? # ------------------------------- # # Active (online) defragmentation allows a Redis server to compact the # Spaces left between small allocations and deallocations of data in memory, # Thus allowing to reclaim back memory. # # Fragmentation is a natural process that happens with every allocator (but # Less so with Jemalloc, fortunately) and certain workloads. Normally a server # Restart is needed in order to lower the fragmentation, or at least to flush # Away all the data and create it again. However thanks to this feature # Implemented by Oran Agra for Redis 4.0 this process can happen at runtime # In a "hot" way, while the server is running. # # Basically when the fragmentation is over a certain level (see the # Configuration options below) Redis will start to create new copies of the # Values in contiguous memory regions by exploiting certain specific Jemalloc # Features (in order to understand if an allocation is causing fragmentation # And to allocate it in a better place), and at the same time, will release the # Old copies of the data. This process, repeated incrementally for all the keys # Will cause the fragmentation to drop back to normal values. # # Important things to understand: # # 1. This feature is disabled by default, and only works if you compiled Redis # to use the copy of Jemalloc we ship with the source code of Redis. # This is the default with Linux builds. # # 2. You never need to enable this feature if you don't have fragmentation # issues. # # 3. Once you experience fragmentation, you can enable this feature when # needed with the command "CONFIG SET activedefrag yes". # # The configuration parameters are able to fine tune the behavior of the # Defragmentation process. If you are not sure about what they mean it is # A good idea to leave the defaults untouched. # Active defragmentation is disabled by default # Activedefrag no # Minimum amount of fragmentation waste to start active defrag # Active-defrag-ignore-bytes 100 mb # Minimum percentage of fragmentation to start active defrag # Active-defrag-threshold-lower 10 # Maximum percentage of fragmentation at which we use maximum effort # Active-defrag-threshold-upper 100 # Minimal effort for defrag in CPU percentage, to be used when the lower # Threshold is reached # Active-defrag-cycle-min 1 # Maximal effort for defrag in CPU percentage, to be used when the upper # Threshold is reached # Active-defrag-cycle-max 25 # Maximum number of set/hash/zset/list fields that will be processed from # The main dictionary scan # Active-defrag-max-scan-fields 1000 # Jemalloc background thread for purging will be enabled by default Jemalloc-bg-thread yes # It is possible to pin different threads and processes of Redis to specific # CPUs in your system, in order to maximize the performances of the server. # This is useful both in order to pin different Redis threads in different # CPUs, but also in order to make sure that multiple Redis instances running # In the same host will be pinned to different CPUs. # # Normally you can do this using the "taskset" command, however it is also # Possible to this via Redis configuration directly, both in Linux and FreeBSD. # # You can pin the server/IO threads, bio threads, aof rewrite child process, and # The bgsave child process. The syntax to specify the cpu list is the same as # The taskset command: # # Set redis server/io threads to cpu affinity 0,2,4,6: # server_cpulist 0-7:2 # # Set bio threads to cpu affinity 1,3: # Bio_cpulist 1,3 # # Set aof rewrite child process to cpu affinity 8,9,10,11: # Aof_rewrite_cpulist 8-11 # # Set bgsave child process to cpu affinity 1,10,11 # Bgsave_cpulist 1,10-11 # In some cases redis will emit warnings and even refuse to start if it detects # That the system is in bad state, it is possible to suppress these warnings # By setting the following config which takes a space delimited list of warnings # To suppress # # Ignore-warnings ARM 64-COW-BUG
docker run --name redis -p 6379:6379 \
-v /home/looper/docker/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /home/looper/docker/redis/data:/data \
--restart=always \
--privileged=true \
-d redis redis-server /usr/local/etc/redis/redis.conf
Docker exec -it 容器 id redis-cli
我一直以为 redis 配置文件中的 bind 的作用是:限制 redis 服务器用来接收来自哪些服务器(IP 地址)的 redis 连接请求, 只有在 bind 指定的 IP 地址的计算机才可以访问这个 redis 服务器。事实证明,上面的结论大错特错。 这是对 Redis 中 bind 理解的一个误区。 例如: Bind 127.0.0.1 就是用来限制只有本机可以连接 redis 服务连接 Bind 0.0.0.0 就是用来允许任意计算机都可以连接 redis 服务连接。 注意:以上的理解都是错误的。他们正好是特例,对我们产生了一种错觉。 不信的的话你们可以试一试:(最好试一试) Bind 10.0.0.1(或者除了 127.0.0.1 和 0.0.0.0 之外的任何 IP 地址) 然后重启 redis,就会发现启动不起来。 对于为什么启动不起来,你们知道了 bind 的真正意思之后,就会明白启动不起来的原因。 对于 Redis 中 bind 的正确的理解是: Bind:是绑定本机的 IP 地址,(准确的是:本机的网卡对应的 IP 地址,每一个网卡都有一个 IP 地址),而不是 redis 允许来自其他计算机的 IP 地址。 如果指定了 bind,则说明只允许来自指定网卡的 Redis 请求。如果没有指定,就说明可以接受来自任意一个网卡的 Redis 请求。 举个例子:如果 redis 服务器(本机)上有两个网卡,每一个网卡对应一个 IP 地址,例如 IP 1 和 IP 2。(注意这个 IP 1 和 IP 2 都是本机的 IP 地址)。 我们的配置文件:bind IP 1。只有我们通过 IP 1 来访问 redis 服务器,才允许连接 Redis 服务器,如果我们通过 IP 2 来访问 Redis 服务器,就会连不上 Redis。 结论: Bind 并不是指定 redis 中可以接受来自哪些服务器请求的 IP 地址,而是:bind 用于指定本机网卡对应的 IP 地址。 如果我们想限制只有指定的主机可以连接到 redis 中,我们只能通过防火墙来控制,而不能通过 redis 中的 bind 参数来限制。 使用阿里云的安全组,来限制指定的主机连接 6379 端口。 Redis 中的【protected-mode】的理解: Redis 本身无法限制【只有指定主机】连接到 redis 中,就像我上面说的一样,bind 指定只是用来设置接口地址(interfaces)。 1、如果你的 bind 设置为:bind 127.0.0.1,这是非常安全的,因为只有本台主机可以连接到 redis,就算不设置密码,也是安全的,除非有人登入到你的服务器上。 2、如果你的 bind 设置为:bind 0.0.0.0,表示所有主机都可以连接到 redis。(前提:你的服务器必须开放 redis 的端口)。这时设置密码,就会多一层保护,只有知道密码的才可以访问。也就是任何知道密码的主机都可以访问到你的 redis。 Protected-mode 是 redis 本身的一个安全层,这个安全层的作用:就是只有【本机】可以访问 redis,其他任何都不可以访问 redis。这个安全层开启必须满足三个条件,不然安全层处于关闭状态: (1)protected-mode yes(处于开启) (2)没有 bind 指令。原文:The server is not binding explicitly to a set of addresses using the "bind" directive. (3)没有设置密码。原文:No password is configured。 这时 redis 的保护机制就会开启。开启之后,只有本机才可以访问 redis。如果上面三个条件任何一个不满足,就不会开启保护机制。
从docker仓库里拉取最新的镜像
docker pull nginx
需要先在宿主机创建Nginx外部挂载的配置文件( /usr/local/docker/nginx.conf)
(Nginx本身容器只存在/etc/nginx 目录 , 本身就不创建 nginx.conf 文件,当服务器和容器都不存在 nginx.conf 文件时, 执行启动命令的时候 docker会将nginx.conf 作为目录创建 , 这并不是我们想要的结果 。)
创建挂载目录
mkdir -p /home/looper/docker/nginx/conf
mkdir -p /home/looper/docker/nginx/log
mkdir -p /home/looper/docker/nginx/html
然后运行nginx容器,并将容器里的文件复制到宿主机对应的文件夹
# 生成容器 docker run --name nginx -p 80:80 -d nginx # 将容器nginx.conf文件复制到宿主机 docker cp nginx:/etc/nginx/nginx.conf /home/looper/docker/nginx/conf/nginx.conf # 将容器conf.d文件夹下内容复制到宿主机 docker cp nginx:/etc/nginx/conf.d /home/looper/docker/nginx/conf/conf.d # 将容器中的html文件夹复制到宿主机 docker cp nginx:/usr/share/nginx/html /home/looper/docker/nginx/ 复制完容器里对应的文件后,将容器删除重新生成 关闭该容器 docker stop nginx ## 删除该容器 docker rm nginx ### 或使用此命令,删除正在运行的nginx容器 docker rm -f nginx
3、启动容器
使用如下命令启动容器 docker run \ -p 80:80 \ --name nginx \ --restart=always \ -v /home/looper/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \ -v /home/looper/docker/nginx/conf/conf.d:/etc/nginx/conf.d \ -v /home/looper/docker/nginx/log:/var/log/nginx \ -v /home/looper/docker/nginx/html:/usr/share/nginx/html \ -d nginx:latest 命令 描述 -p 80:80 将容器的 80(后面那个) 端口映射到主机的 80(前面那个) 端口 –name nginx 容器的名字 –restart=always 在Docker重启时,自动重启容器 -v /usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf 挂载nginx.conf配置文件 -v /usr/local/docker/nginx/conf/conf.d:/etc/nginx/conf.d 挂载nginx配置文件 -v /usr/local/docker/nginx/log:/var/log/nginx 挂载nginx日志文件 -v /usr/local/docker/nginx/html:/usr/share/nginx/html 挂载nginx内容 -d nginx:latest 本地运行的版本
## 重启docker服务
sudo systemctl restart docker.service
## 启动容器
docker start nginx
至此,端口添加成功,可以配置端口进行访问啦!!!
#拉取镜像
docker pull rabbitmq:3-management
#运行RabbitMq
docker run \
-e RABBITMQ_DEFAULT_USER=looper\
-e RABBITMQ_DEFAULT_PASS=151523\
--name mq \
--hostname mq1 \
-p 15672:15672 \
-p 5672:5672 \
-d \
rabbitmq:3-management
接下来,我们看看如何安装RabbitMQ的集群。
在RabbitMQ的官方文档中,讲述了两种集群的配置方式:
我们先来看普通模式集群,我们的计划部署3节点的mq集群:
主机名 | 控制台端口 | amqp通信端口 |
---|---|---|
mq1 | 8081 —> 15672 | 8071 —> 5672 |
mq2 | 8082 —> 15672 | 8072 —> 5672 |
mq3 | 8083 —> 15672 | 8073 —> 5672 |
集群中的节点标示默认都是:rabbit@[hostname]
,因此以上三个节点的名称分别为:
RabbitMQ底层依赖于Erlang,而Erlang虚拟机就是一个面向分布式的语言,默认就支持集群模式。集群模式中的每个RabbitMQ 节点使用 cookie 来确定它们是否被允许相互通信。
要使两个节点能够通信,它们必须具有相同的共享秘密,称为Erlang cookie。cookie 只是一串最多 255 个字符的字母数字字符。
每个集群节点必须具有相同的 cookie。实例之间也需要它来相互通信。
我们先在之前启动的mq容器中获取一个cookie值,作为集群的cookie。执行下面的命令:
docker exec -it mq cat /var/lib/rabbitmq/.erlang.cookie
可以看到cookie值如下:
GIIKNSACLCUKKXCZEHQD
接下来,停止并删除当前的mq容器,我们重新搭建集群。
docker rm -f mq
在/tmp目录新建一个配置文件 rabbitmq.conf:
cd /tmp
# 创建文件
touch rabbitmq.conf
文件内容如下:
loopback_users.guest = false
listeners.tcp.default = 5672
cluster_formation.peer_discovery_backend = rabbit_peer_discovery_classic_config
cluster_formation.classic_config.nodes.1 = rabbit@mq1
cluster_formation.classic_config.nodes.2 = rabbit@mq2
cluster_formation.classic_config.nodes.3 = rabbit@mq3
再创建一个文件,记录cookie
cd /tmp
# 创建cookie文件
touch .erlang.cookie
# 写入cookie
echo "GIIKNSACLCUKKXCZEHQD" > .erlang.cookie
# 修改cookie文件的权限
chmod 600 .erlang.cookie
准备三个目录,mq1、mq2、mq3:
cd /tmp
# 创建目录
mkdir mq1 mq2 mq3
然后拷贝rabbitmq.conf、cookie文件到mq1、mq2、mq3:
# 进入/tmp
cd /tmp
# 拷贝
cp rabbitmq.conf mq1
cp rabbitmq.conf mq2
cp rabbitmq.conf mq3
cp .erlang.cookie mq1
cp .erlang.cookie mq2
cp .erlang.cookie mq3
创建一个网络:
docker network create mq-net
docker volume create
运行命令
docker run -d --net mq-net \
-v ${PWD}/mq1/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf \
-v ${PWD}/.erlang.cookie:/var/lib/rabbitmq/.erlang.cookie \
-e RABBITMQ_DEFAULT_USER=guest \
-e RABBITMQ_DEFAULT_PASS=guest \
--name mq1 \
--hostname mq1 \
-p 8071:5672 \
-p 8081:15672 \
docker.io/rabbitmq:3.8-management
docker logs -f mq1 #查看日志
docker run -d --net mq-net \
-v ${PWD}/mq2/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf \
-v ${PWD}/.erlang.cookie:/var/lib/rabbitmq/.erlang.cookie \
-e RABBITMQ_DEFAULT_USER=guest \
-e RABBITMQ_DEFAULT_PASS=guest \
--name mq2 \
--hostname mq2 \
-p 8072:5672 \
-p 8082:15672 \
docker.io/rabbitmq:3.8-management
docker run -d --net mq-net \
-v ${PWD}/mq3/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf \
-v ${PWD}/.erlang.cookie:/var/lib/rabbitmq/.erlang.cookie \
-e RABBITMQ_DEFAULT_USER=guest \
-e RABBITMQ_DEFAULT_PASS=guest \
--name mq3 \
--hostname mq3 \
-p 8073:5672 \
-p 8083:15672 \
docker.io/rabbitmq:3.8-management
在mq1这个节点上添加一个队列:
如图,在mq2和mq3两个控制台也都能看到:
点击这个队列,进入管理页面:
然后利用控制台发送一条消息到这个队列:
结果在mq2、mq3上都能看到这条消息:
我们让其中一台节点mq1宕机:
docker stop mq1
然后登录mq2或mq3的控制台,发现simple.queue也不可用了:
说明数据并没有拷贝到mq2和mq3。
在刚刚的案例中,一旦创建队列的主机宕机,队列就会不可用。不具备高可用能力。如果要解决这个问题,必须使用官方提供的镜像集群方案。
官方文档地址:https://www.rabbitmq.com/ha.html
默认情况下,队列只保存在创建该队列的节点上。而镜像模式下,创建队列的节点被称为该队列的主节点,队列还会拷贝到集群中的其它节点,也叫做该队列的镜像节点。
但是,不同队列可以在集群中的任意节点上创建,因此不同队列的主节点可以不同。甚至,一个队列的主节点可能是另一个队列的镜像节点。
用户发送给队列的一切请求,例如发送消息、消息回执默认都会在主节点完成,如果是从节点接收到请求,也会路由到主节点去完成。镜像节点仅仅起到备份数据作用。
当主节点接收到消费者的ACK时,所有镜像都会删除节点中的数据。
总结如下:
镜像模式的配置有3种模式:
ha-mode | ha-params | 效果 |
---|---|---|
准确模式exactly | 队列的副本量count | 集群中队列副本(主服务器和镜像服务器之和)的数量。count如果为1意味着单个副本:即队列主节点。count值为2表示2个副本:1个队列主和1个队列镜像。换句话说:count = 镜像数量 + 1。如果群集中的节点数少于count,则该队列将镜像到所有节点。如果有集群总数大于count+1,并且包含镜像的节点出现故障,则将在另一个节点上创建一个新的镜像。 |
all | (none) | 队列在群集中的所有节点之间进行镜像。队列将镜像到任何新加入的节点。镜像到所有节点将对所有群集节点施加额外的压力,包括网络I / O,磁盘I / O和磁盘空间使用情况。推荐使用exactly,设置副本数为(N / 2 +1)。 |
nodes | node names | 指定队列创建到哪些节点,如果指定的节点全部不存在,则会出现异常。如果指定的节点在集群中存在,但是暂时不可用,会创建节点到当前客户端连接到的节点。 |
这里我们以rabbitmqctl命令作为案例来讲解配置语法。
语法示例:
rabbitmqctl set_policy ha-two "^two\." '{"ha-mode":"exactly","ha-params":2,"ha-sync-mode":"automatic"}'
rabbitmqctl set_policy
:固定写法ha-two
:策略名称,自定义"^two\."
:匹配队列的正则表达式,符合命名规则的队列才生效,这里是任何以two.
开头的队列名称'{"ha-mode":"exactly","ha-params":2,"ha-sync-mode":"automatic"}'
: 策略内容"ha-mode":"exactly"
:策略模式,此处是exactly模式,指定副本数量"ha-params":2
:策略参数,这里是2,就是副本数量为2,1主1镜像"ha-sync-mode":"automatic"
:同步策略,默认是manual,即新加入的镜像节点不会同步旧的消息。如果设置为automatic,则新加入的镜像节点会把主节点中所有消息都同步,会带来额外的网络开销rabbitmqctl set_policy ha-all "^all\." '{"ha-mode":"all"}'
ha-all
:策略名称,自定义"^all\."
:匹配所有以all.
开头的队列名'{"ha-mode":"all"}'
:策略内容"ha-mode":"all"
:策略模式,此处是all模式,即所有节点都会称为镜像节点rabbitmqctl set_policy ha-nodes "^nodes\." '{"ha-mode":"nodes","ha-params":["rabbit@nodeA", "rabbit@nodeB"]}'
rabbitmqctl set_policy
:固定写法ha-nodes
:策略名称,自定义"^nodes\."
:匹配队列的正则表达式,符合命名规则的队列才生效,这里是任何以nodes.
开头的队列名称'{"ha-mode":"nodes","ha-params":["rabbit@nodeA", "rabbit@nodeB"]}'
: 策略内容"ha-mode":"nodes"
:策略模式,此处是nodes模式"ha-params":["rabbit@mq1", "rabbit@mq2"]
:策略参数,这里指定副本所在节点名称我们使用exactly模式的镜像,因为集群节点数量为3,因此镜像数量就设置为2.
运行下面的命令:
docker exec -it mq1 rabbitmqctl set_policy ha-two "^two\." '{"ha-mode":"exactly","ha-params":2,"ha-sync-mode":"automatic"}'
下面,我们创建一个新的队列:
在任意一个mq控制台查看队列:
给two.queue发送一条消息:
然后在mq1、mq2、mq3的任意控制台查看消息:
现在,我们让two.queue的主节点mq1宕机:
docker stop mq1
查看集群状态:
查看队列状态:
发现依然是健康的!并且其主节点切换到了rabbit@mq2上
从RabbitMQ 3.8版本开始,引入了新的仲裁队列,他具备与镜像队里类似的功能,但使用更加方便。
在任意控制台添加一个队列,一定要选择队列类型为Quorum类型。
在任意控制台查看队列:
可以看到,仲裁队列的 + 2字样。代表这个队列有2个镜像节点。
因为仲裁队列默认的镜像数为5。如果你的集群有7个节点,那么镜像数肯定是5;而我们集群只有3个节点,因此镜像数量就是3.
可以参考对镜像集群的测试,效果是一样的。
1)启动一个新的MQ容器:
docker run -d --net mq-net \
-v ${PWD}/.erlang.cookie:/var/lib/rabbitmq/.erlang.cookie \
-e RABBITMQ_DEFAULT_USER=itcast \
-e RABBITMQ_DEFAULT_PASS=123321 \
--name mq4 \
--hostname mq5 \
-p 8074:15672 \
-p 8084:15672 \
rabbitmq:3.8-management
2)进入容器控制台:
docker exec -it mq4 bash
3)停止mq进程
rabbitmqctl stop_app
4)重置RabbitMQ中的数据:
rabbitmqctl reset
5)加入mq1:
rabbitmqctl join_cluster rabbit@mq1
6)再次启动mq进程
rabbitmqctl start_app
我们先查看下quorum.queue这个队列目前的副本情况,进入mq1容器:
docker exec -it mq1 bash
执行命令:
rabbitmq-queues quorum_status "quorum.queue"
结果:
现在,我们让mq4也加入进来:
rabbitmq-queues add_member "quorum.queue" "rabbit@mq4"
结果:
再次查看:
rabbitmq-queues quorum_status "quorum.queue"
查看控制台,发现quorum.queue的镜像数量也从原来的 +2 变成了 +3:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。