kubernetes 无法使Angular 在本地域上工作

sczxawaw  于 2023-02-11  发布在  Kubernetes
关注(0)|答案(1)|浏览(87)

我正在尝试在自定义本地域www.example.com上启动Angular 应用程序。Angular 应用程序在https://localhost:4200上运行良好。但在blog.dev域上,它无法运行。blog.dev. The angular application is working fine of https://localhost:4200. But on the blog.dev domain, it is not working.
我的Angular 部署文件如下所示

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: 31000

nginx. conf如下所示

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;
    }
  }
}

angular.json如下所示

{
  "$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"
}

主机文件条目如下所示
127.0.5.1 blog.dev
我正在使用Docker桌面

ggazkfy8

ggazkfy81#

您的应用程序在端口4200上运行,因此目标端口也应该是服务中的4200
所以像service port 80 > target port 4200 > container port 4200这样的流量
目标点和集装箱港口应该匹配。

- 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

相关问题