繁体   English   中英

Redis 部署在 AWS - 本地主机 SpringBoot 应用程序连接超时

[英]Redis deployed in AWS - Connection time out from localhost SpringBoot app

关于部署在 AWS(不是 AWS Elastic Cache)中的 Redis 的小问题以及连接到它的问题。

这是部署在 AWS 中的 Redis 的设置:(仅粘贴 Kube.netes StatefulSet 和服务)

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
spec:
  serviceName: redis
  replicas: 3
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      initContainers:
        - name: config
          image: redis:7.0.5-alpine
          command: [ "sh", "-c" ]
          args:
            - |
              cp /tmp/redis/redis.conf /etc/redis/redis.conf
              
              echo "finding master..."
              MASTER_FDQN=`hostname  -f | sed -e 's/redis-[0-9]\./redis-0./'`
              if [ "$(redis-cli -h sentinel -p 5000 ping)" != "PONG" ]; then
                echo "master not found, defaulting to redis-0"
              
                if [ "$(hostname)" = "redis-0" ]; then
                  echo "this is redis-0, not updating config..."
                else
                  echo "updating redis.conf..."
                  echo "slaveof $MASTER_FDQN 6379" >> /etc/redis/redis.conf
                fi
              else
                echo "sentinel found, finding master"
                MASTER="$(redis-cli -h sentinel -p 5000 sentinel get-master-addr-by-name mymaster | grep -E '(^redis-\d{1,})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})')"
                echo "master found : $MASTER, updating redis.conf"
                echo "slaveof $MASTER 6379" >> /etc/redis/redis.conf
              fi
          volumeMounts:
            - name: redis-config
              mountPath: /etc/redis/
            - name: config
              mountPath: /tmp/redis/
      containers:
        - name: redis
          image: redis:7.0.5-alpine
          command: ["redis-server"]
          args: ["/etc/redis/redis.conf"]
          ports:
            - containerPort: 6379
              name: redis
          volumeMounts:
            - name: data
              mountPath: /data
            - name: redis-config
              mountPath: /etc/redis/
      volumes:
        - name: redis-config
          emptyDir: {}
        - name: config
          configMap:
            name: redis-config
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: [ "ReadWriteOnce" ]
        storageClassName: nfs-1
        resources:
          requests:
            storage: 50Mi
---
apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  ports:
    - port: 6379
      targetPort: 6379
      name: redis
  selector:
    app: redis
  type: LoadBalancer

Pod 很健康,我可以执行它并正常执行操作。 这是全部:

NAME          READY   STATUS    RESTARTS   AGE
pod/redis-0   1/1     Running   0          22h
pod/redis-1   1/1     Running   0          22h
pod/redis-2   1/1     Running   0          22h

NAME            TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/redis   LoadBalancer   192.168.45.55   10.51.5.2     6379:30315/TCP   26h

NAME                     READY   AGE
statefulset.apps/redis   3/3     22h

这是服务的描述:

Name:                     redis
Namespace:                Namespace
Labels:                   <none>
Annotations:              <none>
Selector:                 app=redis
Type:                     LoadBalancer
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       192.168.22.33
IPs:                      192.168.22.33
LoadBalancer Ingress:     10.51.5.2
Port:                     redis  6379/TCP
TargetPort:               6379/TCP
NodePort:                 redis  30315/TCP
Endpoints:                192.xxx:6379,192.xxx:6379,192.xxx:6379
Session Affinity:         None
External Traffic Policy:  Cluster
Events:
  Type    Reason        Age                From                Message
  ----    ------        ----               ----                -------
  Normal  IPAllocated   68s                metallb-controller  Assigned IP ["10.51.5.2"]
  Normal  nodeAssigned  58s (x5 over 66s)  metallb-speaker     announcing from node "someaddress.com" with protocol "bgp"
  Normal  nodeAssigned  58s (x5 over 66s)  metallb-speaker     announcing from node "someaddress.com" with protocol "bgp"

然后我尝试连接到它,即使用非常简单的 Spring Boot 应用程序插入一些数据。 该应用程序没有业务逻辑,只是试图插入数据。 以下是相关部分:


@Configuration
public class RedisConfiguration {

    @Bean
    public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {
        return new LettuceConnectionFactory("10.51.5.2", 30315);
    }

@Repository
public class RedisRepository {

    private final ReactiveRedisOperations<String, String> reactiveRedisOperations;

    public RedisRepository(ReactiveRedisOperations<String, String> reactiveRedisOperations) {
        this.reactiveRedisOperations = reactiveRedisOperations;
    }

    public Mono<RedisPojo> save(RedisPojo redisPojo) {
        return reactiveRedisOperations.opsForValue().set(redisPojo.getInput(), redisPojo.getOutput()).map(__ -> redisPojo);
    }

每次我尝试写入数据时,都会收到此异常:

2022-12-02T20:20:08.015+08:00 ERROR 1184 --- [ctor-http-nio-3] a.w.r.e.AbstractErrorWebExceptionHandler : [8f16a752-1]  500 Server Error for HTTP POST "/save"

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.translateException(LettuceConnectionFactory.java:1602) ~[spring-data-redis-3.0.0.jar:3.0.0]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    *__checkpoint ⇢ Handler com.redis.controller.RedisController#test(RedisRequest) [DispatcherHandler]
    *__checkpoint ⇢ HTTP POST "/save" [ExceptionHandlingWebHandler]
Original Stack Trace:
        at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.translateException(LettuceConnectionFactory.java:1602) ~[spring-data-redis-3.0.0.jar:3.0.0]

Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to 10.51.5.2/<unresolved>:30315
    at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78) ~[lettuce-core-6.2.1.RELEASE.jar:6.2.1.RELEASE]
    at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56) ~[lettuce-core-6.2.1.RELEASE.jar:6.2.1.RELEASE]
    at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:350) ~[lettuce-core-6.2.1.RELEASE.jar:6.2.1.RELEASE]
    at io.lettuce.core.RedisClient.connect(RedisClient.java:216) ~[lettuce-core-6.2.1.RELEASE.jar:6.2.1.RELEASE]

Caused by: io.netty.channel.ConnectTimeoutException: connection timed out: /10.51.5.2:30315
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe$1.run(AbstractNioChannel.java:261) ~[netty-transport-4.1.85.Final.jar:4.1.85.Final]
    at io.netty.util.concurrent.PromiseTask.runTask(PromiseTask.java:98) ~[netty-common-4.1.85.Final.jar:4.1.85.Final]

这特别令人费解,因为我非常确定 Spring Boot 应用程序的代码正在运行。 当我更改return new LettuceConnectionFactory("10.51.5.2", 30315); : 到

  1. 我笔记本电脑上的常规 Redis(“localhost”,6379),
  2. 在我的笔记本电脑上 dockerized Redis,
  3. 一个 dockerized Redis on prem,一切正常。

因此,我很困惑我在AWS中设置这个Redis做错了什么。 我应该怎么做才能正确连接到它。 我可以得到一些帮助吗?

谢谢

默认情况下,Redis将自身绑定到 IP 地址127.0.0.1::1 ,并且不接受针对非本地接口的连接。 这是您的主要问题的可能性很高,您可能需要查看redis.conf文件以将 Redis 绑定到您需要的接口或通用* -::* ,如配置文件本身的注释中所述(我已经在上面链接了)。

话虽如此,如果默认用户没有密码,Redis 也不接受非本地接口上的连接——一个名为Protected mode的安全层。 因此,您应该为默认用户提供密码或在redis.conf文件中禁用保护模式

不确定这是否适用于您的情况,但作为旁注,我建议始终避免将 Redis 暴露给 Inte.net。

你正在混合两件事。 要为不同命名空间中的 pod 启用此服务,您不需要外部负载均衡器,您可以尝试使用 redis.namespace-name:6379 dns 名称,它就会正常工作。 这样的 dns 对于你创建的每个服务都有(但只在 kube.netes 内部工作)Kube.netes 将确保你的流量将被路由到正确的 pod(假设有多个)。

如果你想从 kube.netes 外部公开 redis 那么你需要确保有来自外部的连接然后你需要.network 负载均衡器将流量转发到你的 kube.netes 服务(在你的情况下节点端口,所以您需要以 eks 工作节点:30315 作为目标的 NLB)

如果您的工作节点有公共 IP 并且它们的 SecurityGroups 允许直接连接到它们,您可以尝试直接连接到工作节点的 IP 只是为了测试(没有 LB)。

不管你的设置如何,你总是可以通过 kubectl 创建代理

kubectl port-forward -n redisNS svc/redis 6379:6379

并从 spring 启动应用程序连接到本地主机:6379

在最终设置中,您希望如何从应用程序连接到 redis?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM