找回密码
 立即注册
首页 业界区 安全 Redis SSL安装

Redis SSL安装

吉芷雁 7 天前
一、概述

因为业务需求,mysql8必须部署在机房服务器,不能使用阿里云。
因客户安全性要求,必须开启SSL连接。
二、制作Redis SSL镜像

下载redis源码
  1. wget https://download.redis.io/releases/redis-6.2.6.tar.gz
  2. tar zxvf redis-6.2.6.tar.gz
  3. cd redis-6.2.6
复制代码
过滤redis.conf配置文件,去除以#开头的,以空行开头的。
  1. cat redis.conf|grep -v "^#"|grep -v "^$" > redis.conf.new
复制代码
生成ssl证书
  1. mkdir -p /opt/redis/tls
  2. cd /opt/redis/tls
复制代码
生成 CA 根证书,有效期100年
  1. openssl genrsa -out ca.key 2048
  2. openssl req -x509 -new -nodes -sha256 -key ca.key -days 36500 -subj '/O=Redis Test/CN=Certificate Authority' -out ca.crt
复制代码
生成 Redis 服务器证书,有效期100年
  1. openssl genrsa -out redis.key 2048
  2. openssl req -new -sha256 -key redis.key -subj '/O=Redis Test/CN=Server' | openssl x509 -req -sha256 -CA ca.crt -CAkey ca.key -CAserial ca.txt -CAcreateserial -days 36500 -out redis.crt
  3. openssl dhparam -out redis.dh 2048
复制代码
生成Redis SSL镜像

创建一个 Dockerfile,基于官方 Redis 镜像
  1. FROM redis:6.2.17-alpine
  2. # 安装 OpenSSL
  3. RUN apk add --no-cache openssl
  4. # 复制证书文件
  5. COPY tls/redis.crt /tls/redis.crt
  6. COPY tls/redis.key /tls/redis.key
  7. COPY tls/ca.crt /tls/ca.crt
  8. COPY tls/redis.dh /tls/redis.dh
  9. # 复制 Redis 配置文件
  10. COPY redis.conf /usr/local/etc/redis/redis.conf
  11. RUN chown redis:redis -R /tls/
  12. # 启动 Redis
  13. CMD ["redis-server", "/usr/local/etc/redis/redis.conf"]
复制代码
拷贝redis.conf 文件
  1. cp /opt/redis-6.2.6/redis.conf.new redis.conf
复制代码
修改redis.conf,增加tls配置
  1. port 0
  2. ################################## TLS 配置 ###################################
  3. tls-port 6380
  4. tls-cert-file /tls/redis.crt
  5. tls-key-file /tls/redis.key
  6. tls-ca-cert-file /tls/ca.crt
  7. tls-dh-params-file /tls/redis.dh
  8. tls-auth-clients no
  9. #########################################
复制代码
redis.conf,完整内容如下:
  1. bind 0.0.0.0protected-mode yesport 0
  2. ################################## TLS 配置 ###################################
  3. tls-port 6380
  4. tls-cert-file /tls/redis.crt
  5. tls-key-file /tls/redis.key
  6. tls-ca-cert-file /tls/ca.crt
  7. tls-dh-params-file /tls/redis.dh
  8. tls-auth-clients no
  9. #########################################requirepass 12345678save 900 1save 300 10save 60 10000maxmemory-policy noevictiontcp-backlog 511timeout 0tcp-keepalive 300daemonize nopidfile /var/run/redis_6379.pidloglevel noticelogfile ""databases 16always-show-logo noset-proc-title yesproc-title-template "{title} {listen-addr} {server-mode}"stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbrdb-del-sync-files nodir /datareplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-diskless-load disabledrepl-disable-tcp-nodelay noreplica-priority 100acllog-max-len 128lazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush nolazyfree-lazy-user-del nolazyfree-lazy-user-flush nooom-score-adj nooom-score-adj-values 0 200 800disable-thp yesappendonly noappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yesjemalloc-bg-thread yes
复制代码
在默认配置文件基础上,主要修改了以下这些
  1. bind 0.0.0.0protected-mode yesport 0
  2. ################################## TLS 配置 ###################################
  3. tls-port 6380
  4. tls-cert-file /tls/redis.crt
  5. tls-key-file /tls/redis.key
  6. tls-ca-cert-file /tls/ca.crt
  7. tls-dh-params-file /tls/redis.dh
  8. tls-auth-clients no
  9. #########################################requirepass 12345678save 900 1save 300 10save 60 10000maxmemory-policy noevictiondir /data
复制代码
参数解释:
bind,这个参数必须要改成0.0.0.0,否则java连接无法连接redisport 0,表示禁用默认的6379端口tls-auth-clients no,必须设置成no,java代码,不需要双向认证requirepass,redis登录密码save 900 1,这些都是rdb的保持策略maxmemory-policy noeviction,过期策略,不做删除,永久保留dir /data,redis数据统一在/data里面 编译镜像
  1. docker build -f Dockerfile -t redis:6.2.17-alpine-ssl .
复制代码
测试运行镜像,是否正常
  1. docker run -it redis:6.2.17-alpine-ssl
复制代码
没有报错,就说明成功了。
 
三、正式运行
  1. mkdir -p /data/redis-prod-ssl
  2. cd /data/redis-prod-ssl
复制代码
拷贝tlscp -r /opt/redis/tls .vi redis.conf只需要修改密码即可,修改requirepass后面的值 编辑docker-compose.yaml
  1. services:
  2.   redis-prod-ssl:
  3.     image: redis:6.2.17-alpine-ssl
  4.     container_name: redis-prod-ssl
  5.     ports:
  6.       - "6380:6380"
  7.     environment:
  8.       TZ: Asia/Shanghai
  9.     volumes:
  10.       - ./redis-data:/data
  11.       - ./redis.conf:/usr/local/etc/redis/redis.conf
  12.     restart: always
  13. volumes:
  14.   redis-data:
复制代码
启动服务
  1. docker-compose up -d
复制代码
 
四、navicat连接

使用navicat软件连接
1.png

 注意要开启ssl,并指定证书
2.png

点击测试连接,提示连接成功,就可以了
3.png

 
 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册