简体   繁体   中英

How to configure NGINX to proxy API calls to backend deployed on Kubernetes?

I have two deployments one for backend and other for frontend both deployed on the Kubernetes cluster.

BackendDeployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend-deployement
  labels:
    app: backend-kube
spec:
  replicas: 1
  selector:
    matchLabels:
      app: backend-app-kube
  template:
    metadata:
      labels:
        app: backend-app-kube
    spec:
      containers:
        - name: backend-kube-image
          image: fxx/frox-backend-kube
          ports:
            - containerPort: 8111

BackendService.yaml

apiVersion: v1
kind: Service
metadata:
  name: backend-app-kube-service
spec:
  selector:
    app: backend-app-kube
  ports:
    - protocol: TCP
      port: 8111
      targetPort: 8111

Backend is getting deployed perfectly

FrontendDeployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend-deployement
  labels:
    app: frontend-kube
spec:
  replicas: 1
  selector:
    matchLabels:
      app: fronted-app-kube
  template:
    metadata:
      labels:
        app: fronted-app-kube
    spec:
      containers:
        - name: fronted-kube-image
          image: fxx/frox-front-kube
          ports:
            - containerPort: 80

FrontendService.yaml

apiVersion: v1
kind: Service
metadata:
  name: fronted-app-kube-service
spec:
  selector:
    app: fronted-app-kube
  type: NodePort
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Kubernetes documentation suggested Now that you have your backend, you can create a frontend that connects to the backend. The frontend connects to the backend worker Pods by using the DNS name given to the backend Service.

https://kubernetes.io/docs/tasks/access-application-cluster/connecting-frontend-backend/

nginx.conf

server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri /index.html;                 
    }
    location /api {
    proxy_pass http://backend-app-kube-service;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection ‘upgrade’;
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

Now when i am creating the frontend deployment it gives me error in container construction [emerg] 1#1: host not found in upstream

Can somebody please give the solution How can I make frontend to talk to backend using Nginx

You don't need Nginx to talk to the backend.

Front end can talk to backend directly by using the backend address as http://backend-app-kube-service:8111 if they are in the same namespace.

If they are in different namespaces you need to give the FQDN which will be like http://backend-app-kube-service.namespace.svc.cluster.local:8111

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM