无法在远程 Kubernetes 中访问我的服务

Can't access my service in a remote Kubernetes

我一直在寻找类似的问题,但我没有找到。我有一个远程 Kubernetes 集群,其架构为一主二工。安装的版本如下: 库伯内斯:1.15.1-0 Docker:18.09.1-3.el7 我正在尝试部署并公开 Spring 项目的一个 JAR 文件,该文件具有一个 REST 端点。

Deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: microservices-deployment
  labels:
    app: microservices-deployment
spec:
  replicas: 3
  template:
    metadata:
      name: microservices-deployment
      labels:
        app: microservices-deployment
    spec:
      containers:
        - name: microservices-deployment
          image: **my_repo**/*repo_name*:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8085
      restartPolicy: Always
  selector:
    matchLabels:
      app: microservices-deployment

service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: microservices-service
spec:
  selector:
    app: microservices-deployment
  ports:
    - port: 8085
      targetPort: 8085
  type: NodePort

我的application.properties:

server.port=8085

Docker文件:

FROM openjdk:8
ADD target/microservices.jar microservices.jar
EXPOSE 8085
ENTRYPOINT ["java", "-jar", "microservices.jar"]

看起来我的 pods 已经准备好了,一切看起来都很好,但是我什至无法从主人的终端访问我公开的服务。 有人有什么主意吗? 提前致谢。

更新

我可以从我的主机远程登录到我的节点上的端口 30000(在我将 30000 指定为我的节点端口之后),以及远程登录到我的 pods 端口 8085 上。当我尝试从 master 远程登录到 nodes\pods 中的任何其他端口我都被拒绝,所以我认为这是一个好的开始。尽管如此,我仍然无法访问我指定的其余端点,尽管它正在本地工作 Docker : docker 运行 -p 8085:8085 IMAGE_NAME

您定义了:

 - port: 8085
   targetPort: 8085

有关信息:targetPort 是容器化应用程序使用的端口,port 是集群 IP(内部集群 IP)的端口。
但是你没有为 nodePort 定义一个值,这意味着 K8s will allocate it for you :

If you set the type field to NodePort, the Kubernetes control plane allocates a port from a range specified by --service-node-port-range flag (default: 30000-32767).

但您也可以指定该端口:

If you want a specific port number, you can specify a value in the nodePort field. The control plane will either allocate you that port or report that the API transaction failed. This means that you need to take care of possible port collisions yourself. You also have to use a valid port number, one that's inside the range configured for NodePort use.

无论如何,你可以通过 kubectl -n YOUR_NAMESPACE describe service YOUR_SERVICE.
get/check K8s 选择的(或你选择的)nodePort 的值 通常,您应该滥用 describe 子命令来 diagnostic/debug K8s 部署。

然后从集群中的任何节点或集群外部的任何节点(因为端口的类型为 nodePort),在分配的端口(生成的或未生成的)上请求集群的任何节点,然后你应该能够从集群节点请求您的服务。

如果您想自己定义该外部端口,请明确定义
(注意:nodePort 在集群中必须是唯一的)例如:

ports:
  - port: 8085
    targetPort: 8085
    nodePort: 8085

这样,每个集群节点都会暴露8085端口的服务。

问题是网络问题。从其中一名工作人员访问端点就可以了。谢谢大家。