[TOC]

Docker 快速部署单实例 redis 数据库

Shell 命令示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
mkdir -vp /app/redis/data
tee /app/redis/redis.conf <<'EOF'
# Author: WeiyiGeek
# blog: https://blog.weiyigeek.top
# 绑定任意接口、服务端口、后台运行。
bind 0.0.0.0
port 6379
# 容器里必须设置为no
daemonize no
supervised auto

# redis服务pid进程文件名
pidfile "/var/run/redis.pid"

# 关闭保护模式,并配置使用密码访问
protected-mode no
requirepass "123456"
# echo -e "weiyigeek"|sha256sum
# requirepass 097575a79efcd7ea7b1efa2bcda78a4fc7cbd0820736b2f2708e72c3d21f8b61

# 数据文件保存路径,rdb/AOF文件也保存在这里
dir "/data"

# 日志文件记录文件(notice / verbose)
# logfile "/logs/redis.log"
# loglevel verbose

# 最大客户端连接数
maxclients 10000

# 客户端连接空闲多久后断开连接,单位秒,0表示禁用
timeout 60
tcp-keepalive 60

# Redis 数据持久化(rdb/aof)配置
# RDB 文件名
dbfilename "dump.rdb"
# 数据自动保存脚本条件例如300s中有10key发生变化
save 300 10
save 60 10000
# 对RDB文件进行压缩,建议以(磁盘)空间换(CPU)时间。
rdbcompression yes
# 版本5的RDB有一个CRC64算法的校验和放在了文件的最后。这将使文件格式更加可靠。
rdbchecksum yes
# RDB自动触发策略是否启用,默认为yes
rdb-save-incremental-fsync yes

# AOF开启
appendonly yes
# AOF文件名
appendfilename "appendonly.aof"
# 可选值 always, everysec,no,建议设置为everysec
appendfsync everysec

# Redis风险命令重命名
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
rename-command FLUSHDB b840fc02d524045429941cc15f59e41cb7be6c53
rename-command FLUSHALL b840fc02d524045429941cc15f59e41cb7be6c54
rename-command EVAL b840fc02d524045429941cc15f59e41cb7be6c55
rename-command DEBUG b840fc02d524045429941cc15f59e41cb7be6c56
# rename-command SHUTDOWN SHUTDOWN
EOF

# docker 环境
$ docker run -d -p 6379:6379 \
-v /app/redis/redis.conf:/etc/redis/redis.conf \
-v /app/redis/data:/data \
--name redis-server redis:6.2.6-alpine3.15 redis-server /etc/redis/redis.conf

# nerdctl + containerd 环境
$ nerdctl run -d -p 6379:6379 \
-v /app/redis/redis.conf:/etc/redis/redis.conf \
-v /app/redis/data:/data \
--name redis-server redis:6.2.6-alpine3.15 redis-server /etc/redis/redis.conf
5e854a58087ae1bba5a661b2941474560cbecc37f54c7f4e7a28afbaed6aebf0

执行结果:

1
2
3
4
5
6
7
8
9
# 查看容器是否运行
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2919b8a9478a redis:6.2.6-alpine3.15 "docker-entrypoint.s…" 20 seconds ago Up 19 seconds 0.0.0.0:6379->6379/tcp redis-server

# 连接redis数据库是否正常
$ docker exec -it redis-server redis-cli -a 123456 ping
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
PONG


Kubernetes 快速部署单实例 redis 数据库

步骤01.创建configMap 资源清单配置Redis.conf (此处还是采用上述 /app/redis/redis.conf )

1
2
3
4
kubectl create configmap -n database redis-single-conf --from-file=/app/redis/redis.conf
kubectl get configmap -n database redis-single-conf
# NAME DATA AGE
# redis-single-conf 1 30s


步骤02.StatefulSet 与 SVC 资源清单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
tee redis-single-server.yaml <<'EOF'
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: __APPNAME__
namespace: __APPNAMESPACE__
spec:
replicas: 1
serviceName: __APPNAME__
selector:
matchLabels:
app: __APPNAME__
template:
metadata:
labels:
app: __APPNAME__
spec:
containers:
- name: redis
image: redis:6.2.6-alpine3.15
imagePullPolicy: IfNotPresent
ports:
- containerPort: 6379
name: server
command: [ "redis-server", "/conf/redis.conf"]
volumeMounts:
# 从configmap获取的配置文件,挂载到指定文件中【https://blog.weiyigeek.top】.
- name: conf
mountPath: /conf/redis.conf
subPath: redis.conf
- name: data
mountPath: /data
# - name: logs
# mountPath: /logs
# 时区设置
- name: timezone
mountPath: /etc/localtime
volumes:
- name: conf
# 配置文件采用configMap
configMap:
name: redis-single-conf
defaultMode: 0755
# 日志采用hostPath卷
- name: logs
hostPath:
type: DirectoryOrCreate
path: /nfsdisk-31/datastore/redis/demo1/logs
# 时区定义
- name: timezone
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "__STORAGECLASSNAME__"
resources:
requests:
storage: 2Gi
---
apiVersion: v1
kind: Service
metadata:
name: __APPNAME__
namespace: __APPNAMESPACE__
spec:
type: ClusterIP
ports:
- port: 6379
targetPort: 6379
name: tcp
selector:
app: __APPNAME__
EOF

# 注意 替换 storageClassName
sed -i -e 's/__APPNAME__/redis-single/g' -e 's/__APPNAMESPACE__/database/g' -e 's/__STORAGECLASSNAME__/managed-nfs-storage/' redis-single-server.yaml


步骤 03.部署资源清单到Kubernetes集群

1
2
3
kubectl apply -f redis-single-server.yaml
# statefulset.apps/redis-single created
# service/redis-single created


步骤 04.验证部署应用资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
kubectl get pod -n database -l app=redis-single
# NAME READY STATUS RESTARTS AGE
# redis-single-0 1/1 Running 0 16m

kubectl get svc -n database redis-single
# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
# redis-single ClusterIP 11.19.196.169 <none> 6379/TCP 15m

# 连接到redis数据库
$ telnet 11.19.196.169 6379
# Trying 11.19.196.169...
# Connected to 11.19.196.169.
# Escape character is '^]'.
# auth 123456
# +OK
# ping
# +PONG
# info
# $4241
# # Server
# redis_version:6.2.6
# redis_git_sha1:00000000
# redis_git_dirty:0
# redis_build_id:63421500bb103677
# redis_mode:standalone
# os:Linux 4.20.13-1.el7.elrepo.x86_64 x86_64
# arch_bits:64
# multiplexing_api:epoll
# atomicvar_api:atomic-builtin
# gcc_version:10.3.1
# process_id:1
# process_supervised:no
# run_id:677b6d0188b564670f26ce47154b70c8aa6c3f04
# tcp_port:6379
# server_time_usec:1650962303340701

温馨提示: 集群中的其它应用,我们可以通过svc的地址或者集群dns域名(redis-single.database-推荐)来访问该pod。