首页 文章

在Type = LoadBalancer的服务上指定nodePort

提问于
浏览
0

我们公司有一个外部服务,需要连接到我们的Kubernetes集群中运行的应用程序 . 外部服务必须通过IP地址(不能使用主机名)与静态端口(端口80)连接 . 这并不理想,但我们无法控制外部服务 .

我们集群中唯一外部公开的服务运行HAProxy,它将流量路由到集群中的所有内部(未公开)的pod /服务 . HAProxy的服务部署为类型LoadBalancer,它位于AWS ELB后面,用于SSL终止 .

当我尝试使用静态端口部署HAProxy时出现问题 . According to the documentation,我应该能够在ports部分为服务类型= NodePort或LoadBalancer指定"nodePort":

nodePort:
The port on each node on which this service is exposed when type=NodePort or 
LoadBalancer. Usually assigned by the system. If specified, it will be 
allocated to the service if unused or else creation of the service will fail. 
Default is to auto-allocate a port if the ServiceType of this Service 
requires one.

Note that this Service will be visible as both <NodeIP>:spec.ports[*].nodePort and .spec.clusterIP:spec.ports[*].port. (If the --nodeport-addresses flag in kube-proxy is set, would be filtered NodeIP(s).)

所以当然,我尝试使用以下k8s配置文件:

apiVersion: v1
kind      : Service
metadata:
    name: haproxy
    labels:
        app : haproxy
        env : {{ .Values.env | lower }}
    annotations:
        dns.alpha.kubernetes.io/external: "{{ .Values.externalUrl | lower }}"
        service.beta.kubernetes.io/aws-load-balancer-ssl-cert                : {{ .Values.sslcert | lower }}
        service.beta.kubernetes.io/aws-load-balancer-ssl-ports               : "443"
        service.beta.kubernetes.io/aws-load-balancer-backend-protocol        : http
        service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout : "500"
        service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags: "Environment={{ .Values.env | lower }},Type=k8s"
spec:
    type : LoadBalancer
    ports:
        - name      : https
          port      : 443
          targetPort: 80
          nodePort  : 80
    selector:
        app : haproxy
        env : {{ .Values.env | lower }}

使用Helm部署时会产生以下错误:

Error: UPGRADE FAILED: Service "haproxy" is invalid: spec.ports[0].nodePort: Invalid value: 80:

我只需要通过在我的浏览器中输入节点的ip来启动haproxy服务,但也许我错放了nodePort配置密钥 . 它应该在配置文件中的其他地方吗?我已经尝试将它移动到“ports”部分下的各个地方,但这只会引发解析错误 .

提前致谢!我完全失去了 .

1 回答

  • 1

    我相信如果你只是不尝试配置 nodePort 它会做你想要的 .

    porttargetPort 很重要:它们在您的情况下指定端口443(在ELB或Kubernetes服务 endpoints haproxy.default.svc.cluster.local 上)将转发到容器中的端口80 . 在您描述的AWS环境中, nodePort 主要是一个副作用:您使用 ClusterIP 服务在Pod之间进行通信,并使用 LoadBalancer 服务来处理您希望在直接集群外部公开的内容,并且通常不会直接连接到节点本身 .

    a more specific limitationnodePort 必须介于30000和32767之间;你可以't use it to publish arbitrary ports from nodes, and that'为什么你得到了 .

相关问题