Kubernetes Nginx Ingress Controller expose Nginx Webserver

9/20/2018

I basically want to access the Nginx-hello page externally by URL. I've made a (working) A-record for a subdomain to my v-server running kubernetes and Nginx ingress: vps.my-domain.com

I installed Kubernetes via kubeadm on CoreOS as a single-node cluster using these tutorials: https://kubernetes.io/docs/setup/independent/install-kubeadm/, https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/, and nginx-ingress using https://kubernetes.github.io/ingress-nginx/deploy/#bare-metal.

I also added the following entry to the /etc/hosts file:

31.214.xxx.xxx vps.my-domain.com

(xxx was replaced with the last three digits of the server IP)

I used the following file to create the deployment, service, and ingress:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - name: http
          containerPort: 80
          protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: my-nginx
  labels:
    run: my-nginx
spec:
  type: ClusterIP
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: http
  selector:
    run: my-nginx
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-nginx
  annotations:
    kubernetes.io/ingress.class: "nginx"
    ingress.kubernetes.io/ssl-redirect: "False"
spec:
  rules:
  - host: vps.my-domain.com
    http:
      paths:
      - backend:
          serviceName: my-nginx
          servicePort: 80

Output of describe ing:

core@vps ~/k8 $ kubectl describe ing
Name:             my-nginx
Namespace:        default
Address:
Default backend:  default-http-backend:80 (<none>)
Rules:
  Host                  Path  Backends
  ----                  ----  --------
  vps.my-domain.com
                           my-nginx:80 (<none>)
Annotations:
  kubectl.kubernetes.io/last-applied-configuration:      
{"apiVersion":"extensions/v1beta1",...}

  kubernetes.io/ingress.class:         nginx
  ingress.kubernetes.io/ssl-redirect:  False
Events:
  Type    Reason  Age                From                      Message
  ----    ------  ----               ----                      -------
  Normal  UPDATE  49m (x2 over 56m)  nginx-ingress-controller  Ingress default/my-nginx

While I can curl the Nginx hello page using the nodeip and port 80 it doesn't work from outside the VM. Failed to connect to vps.my-domain.com port 80: Connection refused

Did I forgot something or is the configuration just wrong? Any help or tips would be appreciated!

Thank you

EDIT:

Visiting "vps.my-domain.com:30519` gives me the nginx welcome page. But in the config I specified port :80. I got the port from the output of get services:

core@vps ~/k8 $ kubectl get services --all-namespaces | grep "my-nginx"
default         my-nginx               ClusterIP   10.107.5.14      <none>        80/TCP                       1h

I also got it to work on port :80 by adding

externalIPs:
  - 31.214.xxx.xxx

to the my-nginx service. But this is not how it's supposed to work, right? In the tutorials and examples for kubernetes and ingress-nginx, it worked always without externalIPs. Also the ingress rules doesn't work now (e.g. if I set the path to /test).

-- michidk
docker
kubernetes
kubernetes-ingress
nginx
nginx-ingress

1 Answer

9/20/2018

So apparently I was missing one part: the load balancer. I'm not sure why this wasn't mentioned in those instructions as a requirement. But i followed this tutorial: https://kubernetes.github.io/ingress-nginx/deploy/baremetal/#a-pure-software-solution-metallb and now everything works.

Since metallb requires multiple ip addresses, you have to list your single ip-adress with the subnet \32: 31.214.xxx.xxx\32

-- michidk
Source: StackOverflow