Kubernetes 后端合并?

Kubernetes Backends Combining?

我正在部署一个微服务应用程序,我在后端层有两个服务。它们是登录名和食谱。当我尝试调用食谱服务时,我得到了登录服务。

Forwarded URL from service recipes-service: http://localhost:8082
Forwarded URL from service login-service: http://localhost:8081

如果我访问http://localhost:8082,就是登录服务。后端只能分配一个服务吗?如果是这样,我怎样才能拥有多个服务(不用说,入口)?

程序结构如下:

kubernetes-manifests:
--- frontend_service.deployment.yaml
--- frontend_service.service.yaml 
--- login_service.deployment.yaml
--- login_service.service.yaml
--- recipes_service.deployment.yaml
--- recipes_service.service.yaml

login_service.service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: login-service
  labels:
    app: smart-grocer-nodejs
    tier: backend
spec:
  type: LoadBalancer
  selector:
    app: smart-grocer-nodejs
    tier: backend
  ports:
  - port: 8081
    targetPort: http-server

recipes_service.service.yaml

apiVersion: v1
kind: Service
metadata:
  name: recipes-service
  labels:
    app: smart-grocer-nodejs
    tier: backend
spec:
  type: LoadBalancer
  selector:
    app: smart-grocer-nodejs
    tier: backend
  ports:
  - port: 8082
    targetPort: http-server

此外,如果我调用 kubectl logs,我会收到以下错误:

npm ERR! code ELIFECYCLE
npm ERR! errno 137
npm ERR! recipes@1.0.0 start: `node app.js`
npm ERR! Exit status 137

虽然食谱的 src 在 Kubernetes 之外工作(有和没有 docker 文件)

然后说:

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/cnb/.npm/_logs/2020-11-18T18_08_27_622Z-debug.log

虽然我不知道如何找到那个文件

Kubernetes 服务是一种抽象,它在集群中的某处定义了一组逻辑 Pods 运行,它们都提供相同的功能。 Pods 可以配置为与服务对话,并且知道与服务的通信将自动负载平衡到作为服务成员的某个 pod。

在你的两个服务中,你有相同的 selectors,为了区分将不同的标签放入部署中,然后将相应的 selectors 添加到服务中。例如:

login_service.deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: your-login-deployment-name
spec:
  selector:
    matchLabels:
      app: smart-grocer-nodejs-login
  replicas: number-of-replicas
  template:
    metadata:
      labels:
        app: smart-grocer-nodejs-login
    spec:
      containers:
      - name: ...
        ...

对应服务login_service.service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: login-service
  labels:
    app: smart-grocer-nodejs-login
spec:
  type: LoadBalancer
  selector:
    app: smart-grocer-nodejs-login
  ports:
  - port: 8081
    targetPort: http-server

此规范将创建一个服务,该服务以带有 app: smart-grocer-nodejs-login 标签的部署的任何 Pod 上的端口 http-server 为目标,并将其公开在一个抽象的服务端口上(targetPort:是容器接受流量的端口,port:是抽象的服务端口,可以是其他 pods 用于访问服务的任何端口)。当节点在静态端口“selects”pods 上收到标签 app 设置为 smart-grocer-nodejs-login 的请求时,将请求转发给其中一个。

对于recipes_service.deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: your-recipes-deployment-name
spec:
  selector:
    matchLabels:
      app: smart-grocer-nodejs-recipes
  replicas: number-of-replicas
  template:
    metadata:
      labels:
        app: smart-grocer-nodejs-recipes
    spec:
      containers:
      - ...
        ...

对应服务recipes_service.service.yaml

apiVersion: v1
kind: Service
metadata:
  name: recipes-service
  labels:
    app: smart-grocer-nodejs-recipes
spec:
  type: LoadBalancer
  selector:
    app: smart-grocer-nodejs-recipes
  ports:
  - port: 8082
    targetPort: http-server

此规范将创建一个服务,该服务以带有 app: smart-grocer-nodejs-recipes 标签的部署的任何 Pod 上的 TCP 端口 http-server 为目标,并将其暴露在抽象的服务端口上(targetPort:是容器接受流量的端口,port:是抽象的服务端口,可以是其他 pods 用于访问服务的任何端口)。当节点在静态端口“select”pods上收到标签app设置为smart-grocer-nodejs-recipes的请求时,将请求转发给其中一个。

看看:application-connection-to-service.