You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Desktop本地K8s集群LoadBalancer服务部署与访问故障排查

问题:Docker Desktop本地K8s集群中Nginx服务无法通过localhost:32090访问

在Docker Desktop创建的本地K8s集群中部署了Nginx Deployment及LoadBalancer类型的Service,但无法通过localhost:32090访问服务。

相关配置及状态信息

Service配置(nginxsvc)

#nginxsvc
#service
apiVersion: v1
kind: Service
metadata:
  name: nginxsvc
  labels:
    app: nginx-app
spec:
  selector:
    app: nginx-app
  type: LoadBalancer
  ports:
  - port: 8080 #service port
    targetPort: 8080 #container port
    protocols: TCP

Deployment配置

#Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-app
  template:
    metadata:
      labels:
        app: nginx-app
    spec: 
      containers:
      - name: nginx-controller
        image: nginx:latest
        ports:
        - containerPort: 8080

服务状态

NAME                 TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE   SELECTOR
service/kubernetes   ClusterIP      10.96.0.1       <none>        443/TCP          15m   <none>
service/nginxsvc     LoadBalancer   10.98.148.173   localhost     8080:32090/TCP   15m   app=nginx-app

问题原因及解决方法

核心问题

官方nginx:latest镜像默认监听80端口,但当前Deployment和Service配置中都指定容器端口为8080,导致流量转发到了容器内无监听的端口,因此无法访问服务。

解决步骤

  1. 修改配置文件

    • 更新Deployment的containerPort为80:
      containers:
      - name: nginx-controller
        image: nginx:latest
        ports:
        - containerPort: 80
      
    • 更新Service的targetPort为80(port可保持8080不变,或也改为80):
      ports:
      - port: 8080 #service port
        targetPort: 80 #container port
        protocols: TCP
      
  2. 重新应用配置
    执行命令重新部署:

    kubectl apply -f <deployment配置文件路径>
    kubectl apply -f <service配置文件路径>
    
  3. 验证服务可用性

    • 查看Pod状态是否正常:kubectl get pods
    • 查看Pod日志确认Nginx启动正常:kubectl logs <nginx-pod-name>
    • 再次尝试访问localhost:32090

额外排查点

若修改后仍无法访问,可检查:

  • Docker Desktop的K8s集群是否正常运行(右上角Docker图标→Settings→Kubernetes确认已启用且状态正常)
  • 本地防火墙是否拦截了32090端口
  • 执行kubectl describe service nginxsvc查看服务事件是否存在异常

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 21:20:25