Expose service on local kubernetes

For those still looking for an answer. I've managed to achieve this by adding another Kube service just to expose my app to localhost calls (via browser or Postman):

kind: Service
apiVersion: v1
metadata:
  name: apaches-published
spec:
  ports:
    - name: http
      port: 8080
      targetPort: 80
      protocol: TCP
  selector:
    app: web
  type: LoadBalancer

Try it now on: http://localhost:8080


There are several solutions to expose services in kubernetes: http://alesnosek.com/blog/2017/02/14/accessing-kubernetes-pods-from-outside-of-the-cluster/

Here are my solutions according to alesnosek for a local kubernetes bundled with docker:

1. hostNetwork

hostNetwork: true

Dirty (the host network should not be shared for security reasons) => I did not check this solution.

2. hostPort

hostPort: 8086

Does not apply to services => I did not check this solution.

3. NodePort

Expose the service by defining a nodePort:

apiVersion: v1
kind: Service
metadata:
  name: apaches
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30000
  selector:
    app: apache

4. LoadBalancer

EDIT @MathObsessed posted the solution in his anwer.

5. Ingress

a. Install Ingress Controller

git clone https://github.com/jnewland/local-dev-with-docker-for-mac-kubernetes.git

kubectl apply -f nginx-ingress/namespaces/nginx-ingress.yaml -Rf nginx-ingress

b. Configure Ingress

kubectl apply -f apache-ing.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: apache-ingress
spec:
  rules:
  - host: localhost
    http:
      paths:
      - path: /
        backend:
          serviceName: apaches
          servicePort: 80

Now I can access my apache deployed with kubernetes by calling http://localhost/

Remarks for using local-dev-with-docker-for-mac-kubernetes

  • The repo simplifies the deployment of the offical ingress-nginx controller
  • For production use I would follow the official guide.
  • The repos ships with a tiny full featured ingress example. Very useful for getting quickly a working example application.

Further documentation

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

Tags:

Kubernetes