Kubernetes服务未获取到外部IP。

11

我正在本地运行 Kubernetes 集群,使用 KubeAdm 进行初始化。 我配置了 flannel 网络插件。

当我将服务公开为 NodePort 时,我无法接收外部 IP。 我错过了什么?

enter image description here

我的 deployment yaml 文件如下:

apiVersion: v1
kind: Service
metadata:
  name: testapp
  labels:
    run: testapp
spec:
  type: NodePort
  ports:
  - port: 8080
    targetPort: 80
    protocol: TCP
    name: http
  - port: 443
    protocol: TCP
    name: https
  selector:
    run: testapp
---------
apiVersion: apps/v1
kind: Deployment
metadata:
  name: testapp
spec:
  selector:
    matchLabels:
      run: testapp
  replicas: 2
  template:
    metadata:
      name: testapp
      labels:
        run: testapp
    spec:
      containers:
        - image: [omitted]
          name: testapp
          ports:
          - containerPort: 80
             livenessProbe:
               httpGet:
               path: /api/health
               port: 80

环境详细信息:

Kubernetes 版本:

Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.3", GitCommit:"d2835416544f298c919e2ead3be3d0864b52323b", GitTreeState:"clean", BuildDate:"2018-02-07T12:22:21Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.3", GitCommit:"d2835416544f298c919e2ead3be3d0864b52323b", GitTreeState:"clean", BuildDate:"2018-02-07T11:55:20Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}
在本地运行的 vSphere 虚拟机上安装了 Ubuntu Server 16.04。
3个回答

17

将服务暴露为NodePort时,您将无法获得外部IP。在节点端口上公开服务意味着您的服务将在集群中任何节点的NodeIP上以30000-32767(默认行为)之间的随机端口对外提供服务。

在您的情况下,您的服务所暴露的端口是31727。

集群中的每个节点都会将该端口(每个节点上相同的端口号)代理到启动服务的Pod中。

最简单的方法是使用:

kubectl describe service <service-name>

检查上面结果中Nodeport的详细信息。

然后使用以下方式获取集群中任何节点的节点IP

kubectl get nodes -o wide

现在您可以通过<Node-IP>:<Node-Port>来外部访问您的服务。

此外,如果您想要固定的Node端口,可以在yaml文件中指定。

注意:请确保在节点上添加安全规则,允许特定端口的流量通过。


如果我们使用 <Pod-IP>:<Node-Port> 会发生什么? - user1319182

4
如果您使用节点端口,则不会在此处看到“External-IP”值。
从文档中得知:(出处):“如果将类型字段设置为“NodePort”,Kubernetes主节点将从配置的范围内(默认值:30000-32767)分配一个端口,并且每个节点都将代理该端口(每个节点上的相同端口号)进入您的服务。该端口将在您的服务的spec.ports [*] .nodePort字段中报告。”
因此,您没有一个可以显示在那里的IP地址。您可以连接到任何定义的NodePort上的任何一个节点。

2
根据文档:“EXTERNAL-IP列中的IP地址是公共互联网上可用的IP地址。CLUSTER-IP仅在您的集群/私有云网络内部可用。”...“对于应用程序的某些部分,您可能希望将服务暴露到外部IP地址上。Kubernetes支持两种方法:NodePorts和LoadBalancers。”摘自:https://kubernetes.io/docs/concepts/services-networking/connect-applications-service#exposing-the-service - Illidan
1
那么呢?当您使用LoadBalancer模式而不是NodePort时,您将看到External-IP。在NodePort中,您没有专用的外部IP可用于您的服务,所有节点都将处理与您的服务的连接。 - Anton Kostenko

1
在NodePort中,你不会得到一个外部IP, 尝试使用type:LoadBalancer代替NodePort来获取外部IP,这才是你真正想要的,
apiVersion: v1
kind: Service metadata:
name: my-service spec:
selector:
app.kubernetes.io/name: MyApp

端口:
- protocol: TCP

  port: 80

  targetPort: 9376

clusterIP: 10.0.171.239

类型: LoadBalancer

状态:

负载均衡器:

ingress:

- ip: 192.0.2.127

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接