Kubernetes精细并行处理教程Redis连接问题求助
Hey there, let's get this sorted out quickly! The root cause of your DNS resolution error is that Kubernetes DNS resolves Service names, not Pod names. When you ran redis-cli -h redis, you were trying to reach a Service named redis—but you only created a Pod named temp, with no corresponding Service to map that hostname to.
Here's how to fix it step by step:
1. Deploy Redis properly with a Service (per the tutorial)
The official guide expects you to set up a Redis Deployment and Service first, not just a one-off Pod. Run these commands first:
# Create a Redis Deployment to manage the Redis Pod kubectl create deployment redis --image=redis # Expose the Deployment as a Service so DNS can resolve "redis" kubectl expose deployment redis --port=6379 --name=redis
2. Recreate your temp Pod and connect
Now that the redis Service exists, spin up your temporary Pod again:
kubectl run -i --tty temp --image redis --command "/bin/sh"
Once inside the Pod, run this command again—it should work now:
redis-cli -h redis
3. Quick test workaround (skip Service, not recommended)
If you just want a quick test without setting up a Service, you can connect directly to the Pod's IP:
- First, get the Pod's IP address:
kubectl get pod temp -o jsonpath='{.status.podIP}'
- Then use that IP in
redis-cli:
redis-cli -h <your-pod-ip-here>
Note: This is unstable because Pod IPs change when the Pod restarts—using a Service is the proper, production-ready way.
Why this happened
To clarify: Kubernetes DNS assigns a domain name to every Service in the format <service-name>.<namespace>.svc.cluster.local. When you use redis-cli -h redis, it automatically resolves to the redis Service's IP in your current namespace. Since you didn't have that Service created earlier, DNS had no record to look up, hence the error.
内容的提问来源于stack exchange,提问作者magladde

