502 Bad Gateway Error with Angular app in Kubernetes

4/12/2021

Versions and references are noted below.

I am attempting to migrate my Angular client application and my Node/Express API from a Heroku virtualized server to DigitalOcean's Managed Kubernetes (DOKS). Largely following the Ingress Tutorial (see References, below), I have successfully:

  • Used Docker to containerize both the Angular and the API,
  • Setup a Kubernetes cluster and container registry on DOKS,
  • Setup nginx-ingress,
  • Setup jetstack/cert-manager and Let's Encrypt,
  • Configured my domain name register and DOKS DNS,
  • Deployed both the Angular and API to pods,
  • Connected to the API from the Internet.

The pertinent YAML configuration files are attached below (see Configurations, below). I'd be glad to add others if it would help.

Where I have failed is connecting to the Angular from the Internet. I receive an HTTP 502 Bad Gateway when connecting.

To ensure my Angular code was not causing the problem, I have created a default Angular app, following the Default Angular Tutorial (see References).

I followed Troubleshooting Deployments (see References) and determined the following:

  • Using kubectl port-forward, I am able to connect to the Angular app's pod at port 4200,
  • Using kubectl port-forward, I am able to connect to the Angular app's Cluster IP service at port 80,
  • I am not able to connect to the Angular app's service via Ingress (502 Bad Gateway),
  • Reviewing the ingress logs using the ingress-nginx plugin to kubectl, I was able to find an error code.

The error code in the ingress log is:

2021/04/12 06:08:43 [error] 423#423: *1158356 connect() failed (111: Connection refused) while connecting to upstream, client: XXX.XXX.XXX.XXX, server: pvg.example.com, request: "GET / HTTP/1.1", upstream: "http://XXX.XXX.XXX.XXX:4200/", host: "pvg.example.com"

This is where I am stuck. I would be grateful for any insights from this expert community.

Thank you!

Disclaimer: I have also asked this question on DigitalOcean's Community website with no responses over the last week (see References, below). When answered, I will link the two together.

Configurations

Configuration for Service and Deployment of Angular:

---
apiVersion: v1
kind: Service
metadata:
  name: my-app-cip
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: 4200
  selector:
    app: my-app
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: nginx
        image: registry.digitalocean.com/XXXXXXXX/my-app:0.1.0
        ports:
        - containerPort: 4200
          protocol: TCP

Configuration for Ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: hello-kubernetes-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
  tls:
  - hosts:
    - api.example.com    # This is working
    - pvg.example.com    # This is NOT working
    secretName: hello-kubernetes-tls
  rules:
  - host: "api.example.com"  # This is working
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: my-api-cip
            port:
              number: 80
  - host: "pvg.example.com"  # This is NOT working
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: my-app-cip
            port:
              number: 80

Versions

  • node.js 10.19.0
  • npm 7.7.6
  • yarn 1.22.10
  • ng 11.2.2
  • docker 19.03.8
  • kubectl 1.20.1
  • doctl 1.57.0
  • kubernetes 1.20.2-do.0
  • helm 3.5.3
  • nginx controller 0.45.0
  • jetstack 1.3.0

References

Ingress Tutorial

Default Angular Tutorial:

Troubleshooting Deployments

Question as asked on DigitalOcean Community website:

EDIT: Fixed markdown mistakes; added comment to YAML for clarification; fixed typo in Ingress YAML file.

-- MarkWilx
angular
docker
http-status-code-502
kubernetes
nginx-ingress

0 Answers