You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

在Kubernetes部署无状态Go应用与Redis时遇TCP连接超时故障

Troubleshooting "dial tcp: i/o timeout" Between Go App and Redis in Kubernetes

Alright, let's break down this timeout issue—super common when dealing with inter-Pod communication in Kubernetes, so we'll work through the most likely causes step by step.

1. Verify Redis Service Configuration (Critical!)

In Kubernetes, your app should always connect to Redis via its Service, not directly to the Pod IP (since Pod IPs are ephemeral). Let's check if the Service is set up correctly:

  • List all Services in your namespace to confirm the Redis Service exists:
    kubectl get svc -n <your-namespace>
    
  • Describe the Redis Service to ensure its label selector matches the Redis Pod's labels, and the port configuration is correct (default Redis port is 6379):
    kubectl describe svc <redis-service-name> -n <your-namespace>
    
    Look for the Selector field—this should exactly match the labels on your running Redis Pod (check with kubectl get pods <redis-pod-name> -o jsonpath='{.metadata.labels}').

2. Confirm App is Using Service Name for Redis Connection

Double-check your Go app's configuration: it should use the Redis Service name (e.g., redis:6379) as the connection address, not a hardcoded Pod IP or external URL. Kubernetes' internal DNS resolves Service names to their ClusterIPs automatically.

To test DNS resolution from your app Pod:

kubectl exec -it <app-pod-name> -- nslookup <redis-service-name>

If this fails, your cluster's DNS addon (like CoreDNS) might have issues, but that's less common than misconfigured Services.

3. Check for NetworkPolicy Restrictions

If you're using NetworkPolicies in your cluster, they might be blocking traffic between your app Pod and Redis:

  • List all NetworkPolicies in the Redis namespace:
    kubectl get networkpolicy -n <redis-namespace>
    
  • Ensure there's a policy that allows ingress traffic from your app's Pods (or namespace) to Redis on port 6379. If no policies exist, traffic is allowed by default—but if there are policies, they need to explicitly permit this communication.

4. Test Direct Network Connectivity

Let's rule out low-level network issues:

  • From your app Pod, test connectivity to the Redis Service:
    kubectl exec -it <app-pod-name> -- nc -zv <redis-service-name> 6379
    
  • If that times out, try connecting directly to the Redis Pod's IP (get it with kubectl get pods <redis-pod-name> -o jsonpath='{.status.podIP}'):
    kubectl exec -it <app-pod-name> -- nc -zv <redis-pod-ip> 6379
    
    • If connecting to the Pod IP works, the problem is with the Redis Service (go back to step 1).
    • If it also times out, check if your cluster's node network allows Pod-to-Pod communication, or if there are node-level firewall rules blocking traffic.

5. Ensure Redis is Listening on All Interfaces

Sometimes Redis is configured to only listen on 127.0.0.1 (localhost), which means it won't accept connections from other Pods:

  • Exec into the Redis Pod and check the bind configuration:
    kubectl exec -it <redis-pod-name> -- redis-cli config get bind
    
    If the result is 127.0.0.1, you need to update your Redis deployment to set bind 0.0.0.0 in the configuration file—this allows connections from any IP.

6. Check Go App's Connection Timeout Settings

Finally, verify that your Go app's Redis client isn't using an overly short timeout. For example, when creating a Redis client, ensure you set a reasonable DialTimeout:

client := redis.NewClient(&redis.Options{
    Addr:     "redis:6379",
    Password: "", // no password set
    DB:       0,  // use default DB
    DialTimeout: 5 * time.Second, // Increase if needed
})

A timeout that's too short (like 1 second) can cause false positives if there's minor network latency.

Start with steps 1 and 2—those are the most frequent culprits here. Let me know what you uncover!

内容的提问来源于stack exchange,提问作者shivam

火山引擎 最新活动