MiniKube ExternalName服务无法访问本地资源问题排查求助
问题:Minikube Pod无法访问本地机器服务,ExternalName服务解析失败
已查阅资源(一周未解决)
- StackOverflow上所有ExternalName访问相关问题
- Kubernetes ExternalName官方文档
- 多个关于ExternalName的YouTube视频
- 多个Kubernetes网络相关YouTube视频
- Medium上关于Pod访问外部数据库的文章
环境与问题详情
本地环境配置
- 本地机器运行Docker容器
nginx,使用nginx:stable-perl镜像,端口映射为8090,Chrome访问http://192.168.56.1:8090可正常显示"Welcome to nginx!"页面,192.168.56.1为本地机器IP。 - 本地通过
minikube start --driver=hyperv启动Minikube,其中运行helloworldPod,镜像为bhargavshah86/kube-test:v0.1:
PS C:\data\code\unleash> kubectl get pods NAME READY STATUS RESTARTS AGE helloworld-8654c996c4-thjrf 1/1 Running 0 9m48s
- 执行过端口转发命令:
kubectl --namespace default port-forward helloworld-8654c996c4-thjrf 8090:8090
- 本地
etc\hosts配置:
192.168.56.1 my.web
Pod内测试结果
nslookup执行结果(注:命令格式有误,误将端口作为DNS服务器参数,但解析到了目标IP):
/usr/share/nginx/html # nslookup 192.168.56.1 8090 Server: 0.0.31.154 Address 1: 0.0.31.154 Name: 192.168.56.1 Address 1: 192.168.56.1 my.web
curl本地IP超时(注:命令格式有误,多空格导致访问80端口而非8090):
/usr/share/nginx/html # curl 192.168.56.1 8090 curl: (7) Failed to connect to 192.168.56.1 port 80: Operation timed out
curlExternalName服务my-web无法解析主机:
/usr/share/nginx/html # curl http://my-web:8090 curl: (6) Could not resolve host: my-web
- 使用服务完整域名访问同样失败:
/usr/share/nginx/html # curl my-web.default.svc.cluster.local:8090 curl: (6) Could not resolve host: my-web.default.svc.cluster.local
telnet连接超时:
/usr/share/nginx/html # busybox-extras telnet 192.168.56.1 8090 telnet: can't connect to remote host (192.168.56.1): Operation timed out
- 安装
postgresql-client后访问本地Postgres(192.168.56.1:5432)报错:
psql -h 192.168.56.1 -U postgres
- Pod内
curl www.google.com可正常访问。
相关资源配置
my-web服务描述
PS C:\data\code\unleash> kubectl describe svc/my-web Name: my-web Namespace: default Labels: <none> Annotations: <none> Selector: <none> Type: ExternalName IP Families: <none> IP: IPs: <none> External Name: 192.168.56.1 Port: <unset> 8090/TCP TargetPort: 8090/TCP Endpoints: <none> Session Affinity: None Events: <none>
minikube tunnel执行报错
PS C:\data\code\unleash> minikube tunnel --bind-address=192.168.56.1 W0424 19:34:25.670477 20364 main.go:291] Unable to resolve the current Docker CLI context "default": context "default": context not found: open C:\Users\srhusain\.docker\contexts\meta\37a8eec1ce19687d132fe29051dca629d164e2c4958ba141d5f4133a33f0688f\meta.json: The system cannot find the path specified. E0424 19:34:29.903003 20364 out.go:453] unable to execute error getting ssh port: get port 22 for "minikube": hyperv container inspect -f "'{{(index (index .NetworkSettings.Ports \"22/tcp\") 0).HostPort}}'" minikube: exec: "hyperv": executable file not found in %PATH% stdout: stderr: : template: error getting ssh port: get port 22 for "minikube": hyperv container inspect -f "'{{(index (index .NetworkSettings.Ports \"22/tcp\") 0).HostPort}}'" minikube: exec: "hyperv": executable file not found in %!P(string=index)ATH&{%! (string=index of untyped nil)}stdout: stderr: :1:92: executing "error getting ssh port: get port 22 for \"minikube\": hyperv container inspect -f \"'{{(index (index .NetworkSettings.Ports \"22/tcp\") 0).HostPort}}'\" minikube: exec: \"hyperv\": executable file not found in %PATH%\nstdout:\n\nstderr:\n" at <index .NetworkSettings.Ports "22/tcp">: error calling %!s(MISSING): %!w(MISSING) - returning raw string. ❌ Exiting due to DRV_PORT_FORWARD: error getting ssh port: get port 22 for "minikube": hyperv container inspect -f "'{{(index (index .NetworkSettings.Ports \"22/tcp\" ) 0).HostPort}}'" minikube: exec: "hyperv": executable file not found in %PATH% stdout: stderr: ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ 😿 If the above advice does not help, please let us know: │ │ 👉 https://github.com/kubernetes/minikube/issues/new/choose │ │ │ │ Please run `minikube logs --file=logs.txt` and attach logs.txt to the GitHub issue. │ │ Please also attach the following file to the GitHub issue: │ │ - C:\Users\srhusain\AppData\Local\Temp\minikube_tunnel_df9325734b3fbaf9471c9c4a9cc1524d4e32b11a_0.log │ │ │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
已尝试的ExternalName配置修改
spec: type: ExternalName externalName: my.web # --- spec: type: ExternalName externalName: 192.168.56.1
完整YAML配置
apiVersion: apps/v1 kind: Deployment metadata: name: helloworld labels: app: helloworld spec: replicas: 1 selector: matchLabels: app: helloworld template: metadata: labels: app: helloworld spec: containers: - name: helloworld image: bhargavshah86/kube-test:v0.1 resources: limits: memory: 512Mi cpu: "1" requests: memory: 256Mi cpu: "0.2" ports: - containerPort: 8080 --- apiVersion: v1 kind: Service metadata: name: my-web spec: type: ExternalName externalName: 192.168.56.1 ports: - port: 8090 protocol: TCP targetPort: 8090
Endpoints信息
PS C:\data\code\unleash> kubectl get endpoints NAME ENDPOINTS AGE helloworld 10.244.0.11:8080 3h4m kubernetes 172.18.112.240:8443 4h3m
Pod信息
PS C:\data\code\unleash> kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES helloworld-8654c996c4-mm4m2 1/1 Running 0 20m 10.244.0.11 minikube <none> <none>
期望结果
Pod内执行curl http://192.168.56.1:8090或curl http://my-web:8090能正常返回"Welcome to nginx!"页面,而非超时或解析失败。
内容的提问来源于stack exchange,提问作者Syed Rafey Husain
相关产品推荐
相关产品推荐

