angular application not reachable by nginx on kubernetes

2020-02-14 nginx kubernetes

After creating my application, creating the image through the Dockerfile, I proceeded to configure it in a pod of kubernetes. However, I can't reach it via IP address. In fact, I noticed that there is no ip assigned.

NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes            ClusterIP   XX.XX.X.X       <none>        443/TCP   2d1h
app                   ClusterIP   XX.XX.XXX.XX   <none>        80/TCP    1h

I have tried to edited the file yaml to set the selector run:ning, but I do not see the external ip.

This is the situation of the services:

NAMESPACE     NAME                                       TYPE           
default       service/kubernetes                         ClusterIP      
default       service/app                                ClusterIP      
kube-system   service/kube-dns                           ClusterIP      
kube-system   service/nginx-ingress-controller           LoadBalancer   
kube-system   service/nginx-ingress-controller-metrics   ClusterIP      
kube-system   service/nginx-ingress-default-backend      ClusterIP   

Answers

You don't need to have an external IP when you use ingress because the traffic will come into the nginx ingress and will be routed to the app service based on the rules defined in ingress resource.The app service will just be a clusterIP type service .You need to define an ingress resource as below:

apiVersion: extensions/v1beta1
kind: Ingress
metadata: 
  name: app-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - http:
      paths:
        - path: /
          backend:
            serviceName: app
            servicePort: 80 # port of the service 

After you create above you should able to access the service by hitting the LoadBalancer IP or DNS of service nginx-ingress-controller.

ingress traffic pattern

To access your service from outside you have multiple options:

  • LoadBalancer service: You can create a Loadbalancer service (supported on few environments).

  • NodePort service: Change the type of your service to NodePort and you can access your service using any node's IP address.

  • Ingress: Create an ingress resource (as nginx is already running in your cluster) and you can access your service via Ingress controller (in your case nginx).

  • HostNetwork: You can run your pod on host network by adding the field hostNetwork: true in your pod template. Using this you can directly reach your pod without using service. This is not a recommended approach though.

Related