gpt4 book ai didi

reactjs - 如何在k8s集群内部前后连接(连接被拒绝)

转载 作者:行者123 更新时间:2023-12-02 11:39:00 26 4
gpt4 key购买 nike

尝试将 React 前端 Web 连接到 nodejs express api 服务器到 kubernetes 集群时出错。

可以在浏览器中导航到 http:localhost:3000和网站是好的。

但无法导航到 http:localhost:3008符合预期(不应该暴露)

我的目标是将 REACT_APP_API_URL 环境变量传递给前端以设置 axios baseURL并能够在front和它的api服务器之间建立通信。

部署-front.yml

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: gbpd-front
spec:
selector:
matchLabels:
app: gbpd-api
tier: frontend
track: stable
replicas: 2
template:
metadata:
labels:
app: gbpd-api
tier: frontend
track: stable
spec:
containers:
- name: react
image: binomio/gbpd-front:k8s-3
ports:
- containerPort: 3000
resources:
limits:
memory: "150Mi"
requests:
memory: "100Mi"
imagePullPolicy: Always

服务前端.yaml

apiVersion: v1
kind: Service
metadata:
name: gbpd-front
spec:
selector:
app: gbpd-api
tier: frontend
ports:
- protocol: "TCP"
port: 3000
targetPort: 3000
type: LoadBalancer


部署-back.yaml

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: gbpd-api
spec:
selector:
matchLabels:
app: gbpd-api
tier: backend
track: stable
replicas: 3 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: gbpd-api
tier: backend
track: stable
spec:
containers:
- name: gbpd-api
image: binomio/gbpd-back:dev
ports:
- name: http
containerPort: 3008

服务返回.yaml

apiVersion: v1
kind: Service
metadata:
name: gbpd-api
spec:
selector:
app: gbpd-api
tier: backend
ports:
- protocol: "TCP"
port: 3008
targetPort: http

我尝试了很多组合,还尝试将“LoadBalancer”添加到后端服务,但没有...

我可以将完美连接到 localhost:3000 并使用前端,但前端无法连接到后端服务。

问题一 :为了正确传递 REACT_APP_API_URL 到前端,要使用的 ip/name 是什么?
问题2 : 为什么 curl localhost:3008 没有响应?

经过 2 天尝试 k8s 官方文档中的几乎所有内容......无法弄清楚这里发生了什么,所以任何帮助将不胜感激。

kubectl 描述 svc gbpd-api
回复:
kubectl describe svc gbpd-api
Name: gbpd-api
Namespace: default
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"gbpd-api","namespace":"default"},"spec":{"ports":[{"port":3008,"p...
Selector: app=gbpd-api,tier=backend
Type: LoadBalancer
IP: 10.107.145.227
LoadBalancer Ingress: localhost
Port: <unset> 3008/TCP
TargetPort: http/TCP
NodePort: <unset> 31464/TCP
Endpoints: 10.1.1.48:3008,10.1.1.49:3008,10.1.1.50:3008
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>

最佳答案

我测试了您的环境,并且在使用 Nginx 镜像时可以正常工作,让我们回顾一下环境:

  • 正面部署描述正确。
  • 前台服务将其公开为 loadbalancer ,这意味着您的前端可以从外部访问,非常完美。
  • 后面的部署也被正确描述了。
  • 后端服务保留为 ClusterIP,以便只能从集群内部访问,非常棒。

  • 下面我将演示前端和后端之间的通信。
  • 我正在使用您提供的相同 yaml,只是出于示例目的将图像更改为 Nginx,并且由于它是一个 http 服务器,因此我将容器端口更改为 80。

  • Question 1: What's is the ip/name to use in order to pass REACT_APP_API_URL to fronten correctly?


  • 我根据要求将 ENV 变量添加到了前端部署中,我也将使用它来演示。您必须使用服务名称来 curl,我使用的是短版本,因为我们在同一个命名空间中工作。您也可以使用全名:http://gbpd-api.default.svc.cluster.local:3008


  • 转载:
  • 创建 yaml 并应用它们:
  • $ cat deploy-front.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: gbpd-front
    spec:
    selector:
    matchLabels:
    app: gbpd-api
    tier: frontend
    track: stable
    replicas: 2
    template:
    metadata:
    labels:
    app: gbpd-api
    tier: frontend
    track: stable
    spec:
    containers:
    - name: react
    image: nginx
    env:
    - name: REACT_APP_API_URL
    value: http://gbpd-api:3008
    ports:
    - containerPort: 80
    resources:
    limits:
    memory: "150Mi"
    requests:
    memory: "100Mi"
    imagePullPolicy: Always

    $ cat service-front.yaml
    cat: cat: No such file or directory
    apiVersion: v1
    kind: Service
    metadata:
    name: gbpd-front
    spec:
    selector:
    app: gbpd-api
    tier: frontend
    ports:
    - protocol: "TCP"
    port: 3000
    targetPort: 80
    type: LoadBalancer

    $ cat deploy-back.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: gbpd-api
    spec:
    selector:
    matchLabels:
    app: gbpd-api
    tier: backend
    track: stable
    replicas: 3
    template:
    metadata:
    labels:
    app: gbpd-api
    tier: backend
    track: stable
    spec:
    containers:
    - name: gbpd-api
    image: nginx
    ports:
    - name: http
    containerPort: 80

    $ cat service-back.yaml
    apiVersion: v1
    kind: Service
    metadata:
    name: gbpd-api
    spec:
    selector:
    app: gbpd-api
    tier: backend
    ports:
    - protocol: "TCP"
    port: 3008
    targetPort: http

    $ kubectl apply -f deploy-front.yaml
    deployment.apps/gbpd-front created
    $ kubectl apply -f service-front.yaml
    service/gbpd-front created
    $ kubectl apply -f deploy-back.yaml
    deployment.apps/gbpd-api created
    $ kubectl apply -f service-back.yaml
    service/gbpd-api created
  • 请记住,在 Kubernetes 中,通信是 designed to be made between services ,因为当部署发生变化或 pod 失败时,总是会重新创建 pod。
  • $ kubectl get all
    NAME READY STATUS RESTARTS AGE
    pod/gbpd-api-dc5b4b74b-kktb9 1/1 Running 0 41m
    pod/gbpd-api-dc5b4b74b-mzpbg 1/1 Running 0 41m
    pod/gbpd-api-dc5b4b74b-t6qxh 1/1 Running 0 41m
    pod/gbpd-front-66b48f8b7c-4zstv 1/1 Running 0 30m
    pod/gbpd-front-66b48f8b7c-h58ds 1/1 Running 0 31m

    NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    service/gbpd-api ClusterIP 10.0.10.166 <none> 3008/TCP 40m
    service/gbpd-front LoadBalancer 10.0.11.78 35.223.4.218 3000:32411/TCP 42m
  • Pod 是工作人员,由于它们本质上是可替换的,因此我们将连接到前端 Pod 以模拟他的行为并尝试连接到后端服务(这是将流量引导到后端 Pod 之一的网络层)。
  • curl没有自带nginx镜像预装的,所以我必须安装它以进行演示:
  • $ kubectl exec -it pod/gbpd-front-66b48f8b7c-4zstv -- /bin/bash
    root@gbpd-front-66b48f8b7c-4zstv:/# apt update && apt install curl -y
    done.

    root@gbpd-front-66b48f8b7c-4zstv:/# curl gbpd-api:3008
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    ...
  • 现在让我们尝试使用定义的环境变量:
  • root@gbpd-front-66b48f8b7c-4zstv:/# printenv | grep REACT
    REACT_APP_API_URL=http://gbpd-api:3008
    root@gbpd-front-66b48f8b7c-4zstv:/# curl $REACT_APP_API_URL
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    ...

    注意事项:

    Question 2: Why is curl localhost:3008 not answering?


  • 由于正确描述了所有 yaml,您必须检查 image: binomio/gbpd-back:dev按预期在端口 3008 上正确服务。
  • 由于不是公开镜像,我无法测试,所以我给你排查步骤:
  • 就像我们在前端 pod 中登录一样,您必须登录到这个后端 pod 并测试 curl localhost:3008 .
  • 如果它基于带有 apt-get 的 linux 发行版,您可以像我在演示中那样运行命令:
  • 从后端部署获取 pod 名称(例如:gbpd-api-6676c7695c-6bs5n)
  • 运行 kubectl exec -it pod/<POD_NAME> -- /bin/bash
  • 然后运行 ​​apt update && apt install curl -y
  • 并测试curl localhost:3008
  • 如果没有答案运行 `apt update && apt install net-tools
  • 并测试netstat -nlpt ,它将必须向您显示正在运行的服务的输出和相应的端口,例如:
  • root@gbpd-api-585df9cb4d-xr6nk:/# netstat -nlpt
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
    tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master pro
  • 如果 pod 即使采用这种方法也没有返回任何内容,则您必须检查图像中的代码。

  • 之后如果您需要帮助,请告诉我!

    关于reactjs - 如何在k8s集群内部前后连接(连接被拒绝),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60986557/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com