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
验证修复步骤
- 重新应用修改后的配置文件:
kubectl apply -f hello-app.yaml - 查看Service的后端关联状态,确认已绑定Pod:
在输出的kubectl describe service hello-app-serviceEndpoints字段中,应能看到对应Deployment 2个副本的Pod IP。 - 再次curl该Service的URL,即可正常返回结果。
内容的提问来源于stack exchange,提问作者ginywiny
相关产品推荐
相关产品推荐

