[Azure Redis Cache] Linux VM使用6380端口(SSL方式)连接Azure Redis (redis cli & stunnel) - LuBu0505/My-Code GitHub Wiki

问题描述

在Azure Redis的官方文档中,介绍了在Windows下,如何通过redis-cli.exe连接Redis, 包含如何配置stunnel使得通过 6380,SSL方式连接到Redis Cache。本文面临的情况为

使用Linux VM 如何连接到Azure Redis, 通过SSL方式(6380)或非SSL方式(6379)

准备条件

  • Azure Redis
  • Linux VM
  • redis-cli
  • stunnel4

操作步骤

第一步:安装Redis-cli

sudo apt-get update

sudo apt-get install redis-tools

注:redis-tools 中包含了redis-cli工具及其他reids工具

第二步:安装stunnel4

因为redis-cli.exe 工具不支持 TLS(通过6380端口连接),所以使用stunnel 来连接到Redis的6380端口. 当前使用的是第4版本的stunnel,在Linux中的安装命令如下:

sudo apt-get install stunnel4

注意:

如果要使用非SSL端口6379运行Redis,在这种情况下,您无需使用stunnel,并且可以在Azure Redis缓存中开启非SSL端口的情况下直接使用以下命令进行访问:

redis-cli -p 6379 -a <access key> -h <yourcachename.redis.cache.chinacloudapi.cn>

第三步:开启Stunnel服务,在VM启动时自动启动

修改/etc/default/stunnel4配置文件,修改文件中ENABLED的值为1。使用nano编辑文件,命令为:

sudo nano /etc/default/stunnel4

通过键盘方向键移动到下图箭头处,修改ENABLED为1后,使用CRTL + X后,输入Y,来保存当前的修改。然后回车回到Linux命令窗口。

第四步:在stunnel中添加redis-cli的配置项

在Stunnel中添加redis的配置文件,使用如下命令添加文件并且在文件中配置Redis-cli的链接信息 sudo nano /etc/stunnel/redis.conf

打开文件后,输入配置内容:

[redis-cli]

client = yes

accept = 127.0.0.1:6380

connect = yourredisname.redis.cache.chinacloudapi.cn:6380

使用CTRL + X, 输入Y,回车保存

第五步:重启Stunnel服务,加载Redis配置,使用命令为

sudo systemctl restart stunnel4.service

第六步:检查stunnel是否已经监听6380端口,使用命令

sudo netstat -plunt

第七步:使用Redis-cli通过6380(SSL方式)连接到Redis,连接命令

redis-cli -p 6380 -a <your redis access key>

此时,就可以看见连接成功并开始管理Redis。

[以上,希望对您有所帮助。]

排查连接错误

在连接到Redis的时候,如果遇见连接错误,如何来检查Redis的网络情况及端口是否正常ping通。可以使用以下方式。

一:使用telnet 命令,连接到Redis

telnet yourredisname.redis.cache.chinacloudapi.cn port

如:

二:使用tcpping命令长ping redis服务,查看网络情况

由于TCPPING命令需要在Linux中安装。命令如下:

####To install tcptraceroute on Debian/Ubuntu:

$ sudo apt-get install tcptraceroute

####To install tcptraceroute on CentOS/REHL, first set up RepoForge on your system, and then:

$ sudo yum install tcptraceroute

####Finally, download tcpping from the web.

$ cd /usr/bin $ sudo wget http://www.vdberg.org/~richard/tcpping $ sudo chmod 755 tcpping

####To measure network latency by using tcpping, simply run it as follows.

$ tcpping yourredisname.redis.cache.chinacloudapi.cn 6379

seq 0: tcp response from 139.219.6.4 [open] 31.382 ms

seq 1: tcp response from 139.219.6.4 [open] 26.407 ms

seq 2: tcp response from 139.219.6.4 [open] 34.900 ms

seq 3: tcp response from 139.219.6.4 [open] 38.130 ms

seq 4: tcp response from 139.219.6.4 [open] 37.641 ms

seq 5: tcp response from 139.219.6.4 [open] 29.953 ms

seq 6: tcp response from 139.219.6.4 [open] 31.179 ms

seq 7: tcp response from 139.219.6.4 [open] 26.353 ms

seq 8: tcp response from 139.219.6.4 [open] 24.195 ms

seq 9: tcp response from 139.219.6.4 [open] 29.222 ms

参考资料:

**使用 Redis 命令行工具进行连接: **https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-how-to-redis-cli-tool#enable-access-for-redis-cliexe

Connect to Azure Cache for Redis using SSL Port 6380 from Linux VM: https://techcommunity.microsoft.com/t5/azure-paas-blog/connect-to-azure-cache-for-redis-using-ssl-port-6380-from-linux/ba-p/1186109

How to install tcpping on Linux: https://gist.github.com/cnDelbert/5fb06ccf10c19dbce3a7

⚠️ **GitHub.com Fallback** ⚠️