K8s Ingress for nodeport. Example for beginners

10/8/2019

Can you help me? I want to deploy Ingres for nodeport. But I can't understand if this possible?

I tried to find some information in Google but I got Ingress for load balancing or some difficult examples Ingress Ruby on rails and etc.

-- noute
kubernetes
kubernetes-ingress

2 Answers

10/8/2019

The idea behind ingress is you need to use an ingress controller, how you expose the ingress controller is completely up to you as you would use a Kubernetes services to expose it, for example, if you wanted to use ingress on a nodeport your structure would look similar to:

service exposing nodeport ==> ingress controller ==> web app service (via ingress) ==> web app

This is a general flow as it doesn't matter how you expose your ingress controller (LoadBalancer, NodePort, etc) the routing will generally be base on hostname i.e if my NodePort is exposed on 172.64.0.25:30965 then I would point my domain at that IP and the ingress routes based on the host that it gets routed with. There is a lot of documentation on this that can be found here (this is for the Nginx ingress controller but all ingress routing is very similar)

-- Spazzy757
Source: StackOverflow

10/8/2019

I'll try to provide the simplest example that I can think of below. I will use the nginxdemos/hello docker image for my example. Locally this works as this:

$docker run -p 80:80 -d nginxdemos/hello    
...
$curl -I http://localhost

HTTP/1.1 200 OK
Server: nginx/1.13.8
Date: Tue, 08 Oct 2019 06:14:52 GMT
Content-Type: text/html
Connection: keep-alive
Expires: Tue, 08 Oct 2019 06:14:51 GMT
Cache-Control: no-cache

Cool. Here is our backend deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-backend
  namespace: java2days
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx-server
  template:
    metadata:
      labels:
        app: nginx-server
    spec:
      containers:
        - name: nginx-server
          image: nginxdemos/hello
          ports:
            - containerPort: 80
              name: server-port
          livenessProbe:
            httpGet:
              path: /
              port: 80
            initialDelaySeconds: 15
            periodSeconds: 15
            timeoutSeconds: 3
          readinessProbe:
            httpGet:
              path: /
              port: 80
            initialDelaySeconds: 15
            periodSeconds: 15
            timeoutSeconds: 3

Shortly we will have 2 replicas of an nginx server. Up and running on nodes somewhere in the cloud:

$kubectl get pods

NAME                            READY   STATUS    RESTARTS   AGE
nginx-backend-dfcdb9797-dnx7x   1/1     Running   0          21m
nginx-backend-dfcdb9797-pnrhn   1/1     Running   0          21m

Let's create a NodePort service now. Here is the service yaml:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  namespace: java2days
spec:
  ports:
    - port: 80
      protocol: TCP
      targetPort: 80
      name: http
  selector:
    app: nginx-server
  type: NodePort

Note the selector, it matches the backend service. Now it is time for the ingress controller.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
  namespace: java2days
spec:
  rules:
    - http:
        paths:
          - backend:
              serviceName: nginx-service
              servicePort: 80
            path: /*

You will have to wait for about 5-10 minutes until GCloud provisions an IP address for this Ingress. Finally it is done, something like this:

$kubectl get ingress

NAME            HOSTS   ADDRESS         PORTS   AGE
nginx-ingress   *       x.y.z.p         80      15m

From my local machine now:

$curl -I http://x.y.z.p

HTTP/1.1 200 OK

Great it is working. If you open it in the browser and refresh multiple times you will see that the server ID changes and load balancing works. Additional entry point for reading - here.

Do not forget to clean up the resources when you finish experimenting.

-- Lachezar Balev
Source: StackOverflow