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

Kubernetes Service YAML文件连接被拒,CLI暴露部署正常,求排查

问题诊断:YAML创建的Service无法连接,CLI方式正常

核心错误:Service的Selector与Pod标签不匹配

你的YAML配置中,Service的选择器和Deployment定义的Pod标签完全不一致,导致Service无法找到对应的后端Pod,因此curl时出现连接拒绝。

具体标签对比:

  • Deployment的Pod标签(位于template.metadata.labels):
    labels:
      run: hello-world-example
    
  • YAML中Service的Selector:
    selector:
      app.kubernetes.io/name: hello-world
    

而使用kubectl expose deployment hello-world --type=LoadBalancer --port=8080时,Kubernetes会自动复用Deployment的Selector规则(即run: hello-world-example),所以新创建的hello-world Service能正确关联Pod,curl请求正常。

修复方案

修改YAML中Service的Selector,使其与Deployment的Pod标签一致:

apiVersion: v1
kind: Service
metadata:
  name: hello-app-service
spec:
  type: LoadBalancer
  selector:
    run: hello-world-example  # 改为与Deployment Pod匹配的标签
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 8080

验证修复步骤

  1. 重新应用修改后的配置文件:
    kubectl apply -f hello-app.yaml
    
  2. 查看Service的后端关联状态,确认已绑定Pod:
    kubectl describe service hello-app-service
    
    在输出的Endpoints字段中,应能看到对应Deployment 2个副本的Pod IP。
  3. 再次curl该Service的URL,即可正常返回结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 13:33:18