内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
风哥提示:
本文档介绍缓存服务器的部署和配置方法。
Part01-Redis缓存部署
1.1 安装Redis
[root@redis-server ~]# dnf install -y redis
Updating Subscription Management repositories.
Last metadata expiration check: 0:05:23 ago on Fri Apr 4 22:40:00 2026.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
redis x86_64 7.0.5-1.el9 appstream 1.0 M
Transaction Summary
================================================================================
Install 1 Package
Total download size: 1.0 M
Installed size: 3.0 M
Downloading Packages:
redis-7.0.5-1.el9.更多学习教程公众号风哥教程itpux_comx86_64.rpm 2.0 MB/s | 1.0 MB 00:00
——————————————————————————–
Total 2.0 MB/s | 1.0 MB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : redis-7.0.5-1.el9.x86_64 1/1
Running scriptlet: redis-7.0.5-1.el9.x86_64 1/1
Verifying : redis-7.0.5-1.el9.x86_64 1/1
Installed:
redis-7.0.5-1.更多视频教程www.fgedu.net.cnel9.x86_64
Complete!
# 配置Redis
[root@redis-server ~]# cat > /etc/redis/redis.conf << 'EOF'
# 绑定地址
bind 0.0.0.0
# 端口
port 6379
# 守护进程
daemonize no
# 密码
requirepass Redis@123456
# 最大内存
maxmemory 2gb
maxmemory-policy allkeys-lru
# 持久化配置
save 900 1
save 300 10
save 60 10000
# AOF配置
appendonly yes
appendfsync everysec
# 日志
loglevel notice
logfile /var/log/redis/redis.log
# 慢查询日志
slowlog-log-slower-than 10000
slowlog-max-len 128
EOF
# 启动Redis
[root@redis-server ~]# systemctl enable --now redis
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.
# 测试连接
[root@redis-server ~]# redis-cli -a 'Redis@123456' ping
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
PONG
# 设置测试数据
[root@redis-server ~]# redis-cli -a 'Redis@123456'
redis 127.0.0.1:6379> SET test “Hello Redis”
OK
redis 127.0.0.1:6379> GET test
“Hello Redis”
redis 127.0.0.1:6379> SET counter 100
OK
redis 127.0.0.1:6379> INCR counter
(integer) 101
redis 127.0.0.1:6379> LPUSH mylist item1 item2 item3
(integer) 3
redis 127.0.0.1:6379> LRANGE mylist 0 -1
1) “item3”
2) “item2”
3) “item1”
1.2 配置Redis集群
[root@redis1 ~]# cat > /etc/redis/redis.conf << 'EOF' bind 0.0.0.0 port 6379 daemonize no requirepass Redis@123456 masterauth Redis@123456 # 集群配置 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-announce-ip 192.168.1.11 cluster-announce-port 6379 cluster-announce-bus-port 16379 # 持久化 appendonly yes EOF # 启动Redis [root@redis1 ~]# systemctl enable --now redis # 创建集群 [root@redis1 ~]# redis-cli --cluster create \ 192.168.1.11:6379 192.168.1.12:6379 192.168.1.13:6379 \ 192.168.1.14:6379 192.168.1.15:6379 192.168.1.16:6379 \ --cluster-replicas 1 -a 'Redis@123456' Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing hash slots allocation on 6 nodes…
Master[0] -> Slots 0 – 5460
Master[1] -> Slots 5461 – 10922
Master[2] -> Slots 10923 – 16383
Adding replica 192.168.1.15:6379 to 192.168.1.11:6379
Adding replica 192学习交流加群风哥QQ113257174.168.1.16:6379 to 192.168.1.12:6379
Adding replica 192.168.1.14:6379 to 192.168.1.13:6379
M: 1234567890abcdef 192.168.1.11:6379
slots:[0-5460] (5461 slots) master
M: 2345678901abcdef 192.168.1.12:6379
slotfrom PG视频:www.itpux.coms:[5461-10922] (5462 slots) master
M: 3456789012abcdef 192.168.1.13:6379
slots:[10923-16383] (5461 slots) master
S: 4567890123abcdef 192.168.1.14:6379
replicates 1234567890abcdef
S: 5678901234abcdef 192.168.1.15:6379
replicates 2345678901abcdef
S: 6789012345abcdef 192.168.1.16:6379
replicates 3456789012abcdef
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 192.168.1.11:6379)
M: 1234567890abcdef 192.168.1.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 4567890123abcdef 192.168.1.14:6379
slots: (0 slots) slave
replicates 1234567890abcdef
M: 2345678901abcdef 192.168.1.12:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 5678901234abcdef 192.168.1.15:6379
slots: (0 slots) slave
replicates 2345678901abcdef
M: 3456789012abcdef 192.168.1.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 6789012345abcdef 192.168.1.16:6379
slots: (0 slots) slave
replicates 3456789012abcdef
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
# 测试集群
[root@redis1 ~]# redis-cli -c -a ‘Redis@123456’
redis 127.0.0.1:6379> SET key1 “value1”
-> Redirected to slot [9189] located at 192.168.1.12:6379
OK
redis 192.168.1.12:6379> GET key1
“value1”
Part02-Memcached部署
学习交流加群风哥微信: itpux-com
2.1 安装Memcached
[root@memcached ~]# dnf install -y memcached
# 配置Memcached
[root@memcached ~]# cat > /etc/sysconfig/memcached << 'EOF'
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="1024"
OPTIONS="-l 0.0.0.0"
EOF
# 启动Memcached
[root@memcached ~]# systemctl enable --now memcached
Created symlink /etc/systemd/system/multi-user.target.wants/memcached.service → /usr/lib/systemd/system/memcached.service.
# 测试连接
[root@memcached ~]# telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
stats
STAT pid 12345
STAT uptime 100
STAT time 1712246400
STAT version 1.6.9
STAT libevent 2.1.12-stable
STAT pointer_size 64
STAT rusage_user 0.001234
STAT rusage_system 0.002345
STAT max_connections 1024
STAT curr_connections 1
STAT total_connections 2
STAT connection_structures 2
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT bytes_read 7
STAT bytes_written 0
STAT limit_maxbytes 1073741824
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT slab_reassign_rescues 0
STAT slab_reassign_chunk_rescues 0
STAT slab_reassign_evictions_nomem 0
STAT slab_reassign_inline_reclaim 0
STAT slab_reassign_busy_items 0
STAT slab_reassign_busy_deletes 0
STAT slab_reassign_running 0
STAT slabs_moved 0
STAT lru_crawler_running 0
STAT lru_crawler_starts 0
STAT lru_maintainer_juggles 0
END
# 存储数据
set test 0 0 5
hello
STORED
get test
VALUE test 0 5
hello
END
quit
- 根据业务选择合适的缓存系统
- 配置合理的内存大小
- 启用持久化保护数据
- 监控缓存命中率
- 配置主从或集群提高可用性
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
