Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing a service in Kubernetes using nginx reverse proxy

I am new to Kubernetes and wanted to understand how I can expose a service running in Kubernetes to the outside world. I have exposed it using a NodePort on the cluster. So, for example: A service exposes port 31234 on the host and I can get to the service from another server through https://kubeserverIP:31234.

What I want to achieve is serve this service through nginx (on a different server, out of Kube control) via a url,say, http://service.example.com. I have tried deploying nginx with an upstream pointing to the service but that is not working and get a bad gateway error.

Is there something which I am missing here? Or is there a neater way of achieving this.

I have a baremetal installation of Kubernetes cluster and have no access to gce load balancer or other vendor LBs.

Thanks

like image 625
devops84uk Avatar asked Jul 11 '18 14:07

devops84uk


People also ask

Is Kubernetes service a reverse proxy?

An ingress controller acts as a reverse proxy and load balancer. It implements a Kubernetes Ingress. The ingress controller adds a layer of abstraction to traffic routing, accepting traffic from outside the Kubernetes platform and load balancing it to Pods running inside the platform.

How do you expose API in Kubernetes?

If you would like to query the API without an official client library, you can run kubectl proxy as the command of a new sidecar container in the Pod. This way, kubectl proxy will authenticate to the API and expose it on the localhost interface of the Pod, so that other containers in the Pod can use it directly.

Can nginx be used as reverse proxy?

Nginx is an open source web server that can also serve as a reverse proxy. Apart from being used to host websites, it's also one of the most widely used reverse proxy and load balancing solutions.

Can I use nginx with Kubernetes?

NGINX provides a suite of products which run within Kubernetes environments: NGINX Plus – A reverse proxy and load balancer that can perform multiple roles: Sidecar in NGINX Service Mesh. Ingress controller for Kubernetes clusters managing both ingress and egress traffic.


2 Answers

Thanks for pointing in the right direction.

Essential steps broadly were:

  1. Create an app and its service definition.

  2. Create a namespace for ingress.

  3. Create a default backend deployment and service for redirecting all requests not defined in Ingress rules. Create these in the ingress space
  4. Create the nginx ingress controller deployment.
  5. Create RBAC rules.
  6. Finally create the ingress rule for the applications with the paths and the ports.

Found a very useful guide which explained things in details: https://akomljen.com/kubernetes-nginx-ingress-controller/

like image 120
devops84uk Avatar answered Sep 26 '22 03:09

devops84uk


You're almost there! Your next step will be to setup a ingress controller. There is an NGINX Ingress controller plugin that you can checkout here.

Edit: Here's an example configuration: https://github.com/nginxinc/kubernetes-ingress/tree/master/examples/complete-example

like image 26
jonfriesen Avatar answered Sep 26 '22 03:09

jonfriesen