【问题标题】:Unable to access exposed port on kubernetes无法访问 kubernetes 上的暴露端口
【发布时间】:2019-01-10 19:10:40
【问题描述】:

我已经构建了一个自定义 tcserver 映像,公开端口 80 8080 和 8443。基本上你有一个 apache,并且在配置中你有一个代理传递将它转发到 tcserver tomcat。

EXPOSE 80 8080 8443

之后,我创建了一个 kubernetes yaml 来构建仅暴露 80 端口的 pod。

apiVersion: v1
kind: Pod
metadata:
  name: tcserver
  namespace: default
spec:
  containers:
  - name: tcserver
    image: tcserver-test:v1
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 80

以及随之而来的服务。

apiVersion: v1
kind: Service
metadata:
  name: tcserver-svc
  labels:
    app: tcserver
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30080
  selector:
    app: tcserver

但问题是我无法访问它。
如果我登录到 pod (kubectl exec -it tcserver -- /bin/bash),我可以执行 curl -k -v http://localhost,它会回复。

我认为我的服务有问题,但我不知道是什么。
任何帮助将不胜感激。

SVC 变化
根据 sfgroups 的建议,我将 targetPort: 80 添加到 svc 中,但仍然无法正常工作。

当我尝试卷曲 IP 时,我得到一个 No route to host

[root@testmaster tcserver]# curl -k -v http://172.30.62.162:30080/
* About to connect() to 172.30.62.162 port 30080 (#0)
*   Trying 172.30.62.162...
* No route to host
* Failed connect to 172.30.62.162:30080; No route to host
* Closing connection 0
curl: (7) Failed connect to 172.30.62.162:30080; No route to host

这是来自 svc 的描述:

[root@testmaster tcserver]# kubectl describe svc tcserver-svc
Name:                   tcserver-svc
Namespace:              default
Labels:                 app=tcserver
Annotations:            <none>
Selector:               app=tcserver
Type:                   NodePort
IP:                     172.30.62.162
Port:                   <unset> 80/TCP
NodePort:               <unset> 30080/TCP
Endpoints:              <none>
Session Affinity:       None
Events:                 <none>

【问题讨论】:

  • 在您的服务中,我看到选择器是 app: tcserver,但我在 Pod 定义中没有看到标签。

标签: kubernetes kubernetes-service


【解决方案1】:

当您查看 kubectl describe service 输出时,您会发现它实际上并未附加到任何 pod:

Endpoints:              <none>

那是因为您在服务规范中说该服务将附加到带有app: tcserver

标签的 pod
spec:
  selector:
    app: tcserver

但是,在 pod 规范的元数据中,您根本没有指定任何标签

metadata:
  name: tcserver
  namespace: default
  # labels: {}

所以这里的解决方法是在 pod 规范中添加适当的标签

metadata:
  labels:
    app: tcserver

另请注意,在实践中部署裸 pod 有点不寻常。通常它们被包裹在一个更高级别的控制器中,通常是一个部署,它实际上创建了 Pod。部署规范有一个模板 pod 规范,重要的是 pod 的标签。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tcserver
  # Labels here are useful, but the service doesn't look for them
spec:
  template:
    metadata:
      labels:
        # These labels are what the service cares about
        app: tcserver
    spec:
      containers: [...]

【讨论】:

  • 我将它部署为 pod,因为我正在测试它。当我知道一切正常时,这个想法在未来会更高。谢谢
【解决方案2】:

我看到目标帖子丢失了,你可以添加 traget 端口并测试吗?

apiVersion: v1
kind: Service
metadata:
  name: tcserver-svc
  labels:
    app: tcserver
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30080
    targetPort: 80
  selector:
    app: tcserver

【讨论】:

  • 根据文档 kubernetes.io/docs/concepts/services-networking/service , Note that a Service can map an incoming port to any targetPort. By default the targetPort will be set to the same value as the port field. 。因此没有必要明确定义nodePort。
  • @JasonStanley,同意不需要节点端口。正如大卫指出的标签选择器问题。
猜你喜欢
  • 2014-10-23
  • 1970-01-01
  • 2017-01-03
  • 2018-11-21
  • 2019-09-10
  • 2020-02-21
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
相关资源
最近更新 更多