无法通过K8s Service访问HTTP REST API(Windows+Minikube环境)
Windows Minikube部署服务无法访问问题
我在Windows机器上用Minikube部署了Deployment和Service,但无法访问服务,相关信息如下:
当前资源状态
Deployment状态
kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE helloworld-dep 3/3 3 3 9h
Pod状态
kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES greet-api-demo 1/1 Running 2 (23m ago) 6d9h 172.17.0.5 minikube <none> <none> helloworld-dep-f8586dd84-28dxg 1/1 Running 1 (23m ago) 9h 172.17.0.6 minikube <none> <none> helloworld-dep-f8586dd84-p4pg9 1/1 Running 1 (23m ago) 9h 172.17.0.7 minikube <none> <none> helloworld-dep-f8586dd84-tgmcj 1/1 Running 1 (23m ago) 9h 172.17.0.8 minikube <none> <none>
Service状态
kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE helloworld-service NodePort 10.96.44.199 <none> 80:30008/TCP 4m1s kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 191d
Pod应用日志
Pod日志显示Spring Boot应用已在容器内8080端口正常启动:
kubectl logs helloworld-dep-f8586dd84-28dxg Setting Active Processor Count to 8 Calculating JVM memory based on 11276448K available memory For more information on this calculation, see https://paketo.io/docs/reference/java-reference/#memory-calculator Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -Xmx10889346K -XX:MaxMetaspaceSize=79901K -XX:ReservedCodeCacheSize=240M -Xss1M (Total Memory: 11276448K, Thread Count: 50, Loaded Class Count: 11693, Headroom: 0%) Enabling Java Native Memory Tracking Adding 124 container CA certificates to JVM truststore Spring Cloud Bindings Enabled Picked up JAVA_TOOL_OPTIONS: -Djava.security.properties=/layers/paketo-buildpacks_bellsoft-liberica/java-security-properties/java-security.properties -XX:+ExitOnOutOfMemoryError -XX:ActiveProcessorCount=8 -XX:MaxDirectMemorySize=10M -Xmx10889346K -XX:MaxMetaspaceSize=79901K -XX:ReservedCodeCacheSize=240M -Xss1M -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary -XX:+PrintNMTStatistics -Dorg.springframework.cloud.bindings.boot.enable=true . ____ _ __ _ _ /\/ ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.0.0) 2022-12-23T03:38:53.397Z INFO 1 --- [ main] com.playground.k8s.K8sDemoApplication : Starting K8sDemoApplication using Java 17.0.5 with PID 1 (/workspace/BOOT-INF/classes started by cnb in /workspace) 2022-12-23T03:38:53.403Z INFO 1 --- [ main] com.playground.k8s.K8sDemoApplication : No active profile set, falling back to 1 default profile: "default" 2022-12-23T03:39:14.389Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-12-23T03:39:14.648Z INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-12-23T03:39:14.650Z INFO 1 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.1] 2022-12-23T03:39:15.883Z INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-12-23T03:39:15.888Z INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 21396 ms 2022-12-23T03:39:19.693Z INFO 1 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index 2022-12-23T03:39:23.699Z INFO 1 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator' 2022-12-23T03:39:25.262Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-12-23T03:39:25.397Z INFO 1 --- [ main] com.playground.k8s.K8sDemoApplication : Started K8sDemoApplication in 35.178 seconds (process running for 38.144)
访问尝试结果
我尝试通过Minikube IP、ClusterIP和Pod IP访问服务,均出现连接超时:
# curl http://192.168.49.2:30008/actuator curl: (28) Failed to connect to 192.168.49.2 port 30008 after 21061 ms: Timed out # curl http://10.96.44.199:30008/actuator curl: (28) Failed to connect to 10.96.44.199 port 30008 after 21034 ms: Timed out # curl http://172.17.0.6:30008/actuator curl: (28) Failed to connect to 172.17.0.6 port 30008 after 21052 ms: Timed out
排查步骤
- 检查Service端口映射是否正确:从配置看,Service的端口映射是
80:30008/TCP,但Pod内应用监听8080端口。执行kubectl describe service helloworld-service查看TargetPort是否为8080,若不是则修改Service配置,将targetPort指定为8080。 - 验证Pod内部端口可达性:先执行
minikube ssh进入Minikube节点,再在节点内执行curl http://172.17.0.6:8080/actuator,确认Pod内应用是否可访问。 - 检查Windows防火墙规则:临时关闭防火墙测试,或添加允许Minikube网段(通常为192.168.49.0/24)访问30008端口的规则。
- 使用Minikube内置命令暴露服务:执行
minikube service helloworld-service,该命令会自动处理端口转发并打开可访问的服务URL。 - 注意ClusterIP访问限制:ClusterIP是集群内部虚拟IP,仅能在集群内(Pod或Minikube节点)访问,Windows主机无法直接路由到该IP段。
内容的提问来源于stack exchange,提问作者Nital
相关产品推荐
相关产品推荐

