Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Kubernetes service on port 80

Tags:

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. 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?

like image 394
kellanburket Avatar asked Nov 02 '16 18:11

kellanburket


People also ask

How do you expose Kubernetes service?

From the Service type drop-down list, select Cluster IP. Click Expose. When your Service is ready, the Service details page opens, and you can see details about your Service. Under Cluster IP, make a note of the IP address that Kubernetes assigned to your Service.

How do I access a Kubernetes service from outside?

In case you want to access an external service, you need to: create a Service without any selector - Kubernetes will NOT create an Endpoints object. manually create the Endpoints resource corresponding to your Service (with the same name) and IP/port of the service you want to access.

What port is Kubernetes running on?

By default, the Kubernetes API server listens on port 6443 on the first non-localhost network interface, protected by TLS. In a typical production Kubernetes cluster, the API serves on port 443. The port can be changed with the --secure-port , and the listening IP address with the --bind-address flag.


2 Answers

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.

{
    "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

like image 62
aborilov Avatar answered Sep 28 '22 08:09

aborilov


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:

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.

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

like image 27
Camil Avatar answered Sep 28 '22 09:09

Camil