我使用LoadBalancer类型在端口30000上公开公开了Kubernetes服务(Python Flask应用程序)(所有Kubernetes NodePorts必须在我所理解的30000-32767范围内)。我需要在标准的HTTP端口80上访问面向公众的服务。这样做的最佳方法是什么?
答案 0 :(得分:9)
如果您不使用任何云提供商,您只需在服务中设置externalIPs
选项并在节点上启用此IP,并且kube-proxy会将来自此IP的流量路由到您的pod。
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "my-service"
},
"spec": {
"selector": {
"app": "MyApp"
},
"ports": [
{
"name": "http",
"protocol": "TCP",
"port": 80,
"targetPort": 9376
}
],
"externalIPs" : [
"80.11.12.10"
]
}
}
https://kubernetes.io/docs/concepts/services-networking/service/#external-ips
答案 1 :(得分:1)
如果您想使用云提供商的LB,假设您的应用程序在端口8080上公开,并且您希望在端口80上公开公开它,那么配置的外观如下:
apiVersion: v1
kind: Service
metadata:
name: flask_app
labels:
run: flask_app
namespace: default
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
protocol: TCP
name: http
selector:
run: flask_app
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: flask_app
namespace: default
spec:
replicas: 1
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
run: flask_app
spec:
restartPolicy: Always
terminationGracePeriodSeconds: 60
containers:
- name: flask_app
image: repo/flask_app:latest
ports:
- containerPort: 8080
imagePullPolicy: Always
另一种选择是使用Ingress Controller,例如Nginx。
https://kubernetes.io/docs/concepts/services-networking/ingress/