在 Type=LoadBalancer 的服务上指定 nodePort
Specifying nodePort on a Service of Type=LoadBalancer
我们公司有一项外部服务需要连接到我们其中一个 Kubernetes 集群中的应用程序 运行。外部服务 必须 通过 IP 地址(它不能使用主机名)与静态端口(端口 80)连接。这并不理想,但我们无法控制外部服务。
我们集群中唯一对外暴露的服务运行 HAProxy,它将流量路由到集群中的所有内部(未公开暴露)pods/services。 HAProxy 的服务部署为 LoadBalancer 类型,它位于 AWS ELB 后面用于 SSL 终止。
当我尝试使用静态端口部署 HAProxy 时出现问题。 According to the documentation,我应该能够在服务类型=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" 部分下的不同位置,但这只会引发解析错误。
提前致谢!我完全不知所措。
我相信如果你不尝试配置 nodePort
它会做你想做的事。
port
和 targetPort
很重要:它们在您的情况下指定端口 443(在 ELB 或 Kubernetes 服务端点 haproxy.default.svc.cluster.local
上)将转发到端口 80在豆荚里。在您描述的 AWS 环境中,nodePort
主要是一个副作用:您将使用 ClusterIP
服务在 Pods 和 LoadBalancer
服务之间进行通信以获取您想要的东西暴露在直接集群之外,并且通常不直接连接到节点本身。
有a more specific limitation,nodePort
必须在30000到32767之间;您不能使用它从节点发布任意端口,这就是您收到错误的原因。
我们公司有一项外部服务需要连接到我们其中一个 Kubernetes 集群中的应用程序 运行。外部服务 必须 通过 IP 地址(它不能使用主机名)与静态端口(端口 80)连接。这并不理想,但我们无法控制外部服务。
我们集群中唯一对外暴露的服务运行 HAProxy,它将流量路由到集群中的所有内部(未公开暴露)pods/services。 HAProxy 的服务部署为 LoadBalancer 类型,它位于 AWS ELB 后面用于 SSL 终止。
当我尝试使用静态端口部署 HAProxy 时出现问题。 According to the documentation,我应该能够在服务类型=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" 部分下的不同位置,但这只会引发解析错误。
提前致谢!我完全不知所措。
我相信如果你不尝试配置 nodePort
它会做你想做的事。
port
和 targetPort
很重要:它们在您的情况下指定端口 443(在 ELB 或 Kubernetes 服务端点 haproxy.default.svc.cluster.local
上)将转发到端口 80在豆荚里。在您描述的 AWS 环境中,nodePort
主要是一个副作用:您将使用 ClusterIP
服务在 Pods 和 LoadBalancer
服务之间进行通信以获取您想要的东西暴露在直接集群之外,并且通常不直接连接到节点本身。
有a more specific limitation,nodePort
必须在30000到32767之间;您不能使用它从节点发布任意端口,这就是您收到错误的原因。