skip to Main Content

enter image description hereI am trying to bring up angular application on custom local domain blog.dev. The angular application is working fine of https://localhost:4200. But on the blog.dev domain, it is not working.

My angular deployment file is given below

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: client-depl
spec: 
  replicas: 1
  selector: 
    matchLabels:
      app: client
  template: 
    metadata:
      labels:
        app: client 
    spec:
      containers:
        - name: client
          image: pinakinc/client
          ports:
          - containerPort: 4200
---
apiVersion: v1
kind: Service
metadata:
  name: client-srv
spec:
  selector:
    app: client
  type: LoadBalancer
  ports: 
    - name: client
      protocol: TCP
      port: 80
      targetPort: 4200
      nodePort: 31000

The nginx.conf is given below

worker_processes  1;

events {
  worker_connections  1024;
}

http {
  server {
    listen 80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;
    include /etc/nginx/mime.types;

    gzip on;
    gzip_min_length 1000;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css application/json application/javascript 
    application/x-javascript text/xml application/xml application/xml+rss 
    text/javascript;

    location / {
      try_files $uri $uri/ /index.html;
    }
  }
}

The angular.json is given below

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "Angular-Mean": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/Angular-Mean",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "options": {
            "host": "blog.dev",
            "port": 80
          },
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "Angular-Mean:build:production"
            },
            "development": {
              "browserTarget": "Angular-Mean:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            
            "browserTarget": "Angular-Mean:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "./node_modules/bootstrap/dist/css/bootstrap.css",
              "src/styles.css"
            ],
            "scripts": []
          }
        }
      }
    }
  },
  "defaultProject": "Angular-Mean"
}

The host file entry is given below

127.0.5.1 blog.dev

I am using minikube

2

Answers


  1. Chosen as BEST ANSWER

    I was able to resolve the issue. Things that were done to resolve the issue

    1. nginx config file was changed as follows:

       worker_processes  1;
      
       events {
         worker_connections  1024;
       }
      
       http {
         server {
           listen 80;
           server_name  localhost;
      
           root   /usr/share/nginx/html;
           index  index.html index.htm;
           include /etc/nginx/mime.types;
      
           gzip on;
           gzip_min_length 1000;
           gzip_proxied expired no-cache no-store private auth;
           gzip_types text/plain text/css application/json 
           application/javascript 
           application/x-javascript text/xml application/xml 
           application/xml+rss 
           text/javascript;
      
       location / {
         try_files $uri $uri/ /index.html;
       }
         }
       }
      

    The deployment file is given below

    apiVersion: apps/v1
    kind: Deployment
    metadata: 
      name: client-depl
    spec: 
      replicas: 1
      selector: 
        matchLabels:
          app: client
      template: 
        metadata:
          labels:
            app: client 
        spec:
          containers:
            - name: client
              image: pinakinc/client
              ports:
              - containerPort: 4200
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: client-srv
    spec:
      selector:
        app: client
      type: LoadBalancer
      ports: 
        - name: client
          protocol: TCP
          port: 80
          targetPort: 80
          nodePort: 32000
    

  2. Your application running on port 4200 so the target port should be 4200 in service also.

    So traffic flow like service port 80 > target port 4200 > container port 4200

    Target point and container port should get matched.

    - name: client
              image: pinakinc/client
              ports:
              - containerPort: 4200
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: client-srv
    spec:
      selector:
        app: client
      type: LoadBalancer
      ports: 
        - name: client
          protocol: TCP
          port: 80
          targetPort: 4200
          nodePort: 31000
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search