简体   繁体   English

在端口80上访问Kubernetes服务

[英]Accessing Kubernetes service on port 80

I have a Kubernetes service (a Python Flask application) exposed publicly on port 30000 (All Kubernetes NodePorts have to be in the range 30000-32767 from what I understand) using the LoadBalancer type. 我有一个Kubernetes服务(一个Python Flask应用程序)使用LoadBalancer类型在端口30000上公开公开(所有Kubernetes NodePorts必须在我理解的范围内,在30000-32767范围内)。 I need for my public-facing service to be accessible on the standard HTTP port 80. What's the best way to go about doing this? 我需要在标准的HTTP端口80上访问面向公众的服务。这样做的最佳方法是什么?

If you don't use any cloudproviders, you can just set externalIPs option in service and make this IP up on node, and kube-proxy will route traffic from this IP to your pod for you. 如果您不使用任何云提供商,您只需在服务中设置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 https://kubernetes.io/docs/concepts/services-networking/service/#external-ips

If you want to use cloud provider's LB, assuming your app expose on port 8080 and you want to publicly expose it on port 80, here is how the configuration should look: 如果您想使用云提供商的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

Another option is to use a Ingress Controller, for example Nginx. 另一种选择是使用Ingress控制器,例如Nginx。

https://kubernetes.io/docs/concepts/services-networking/ingress/ https://kubernetes.io/docs/concepts/services-networking/ingress/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM