我正在尝试使用Kubernetes部署我的简单Web应用程序。我完成了Kubernetes集群的制作,并使用入口成功公开了我的react应用。但似乎从清单文件的“ env”字段接收的后端服务的域URL不起作用。
以下是React应用程序的清单文件。
kind: Service
apiVersion: v1
metadata:
name: recofashion-client
labels:
app: recofashion-client
spec:
type: NodePort
selector:
app: recofashion-client
ports:
- name: http
port: 80
targetPort: 3000
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: recofashion-client
labels:
name: recofashion-client
spec:
replicas: 2
selector:
matchLabels:
app: recofashion-client
template:
metadata:
labels:
app: recofashion-client
spec:
containers:
- name: web
image: recofashion/client-runtime
imagePullPolicy: Always
ports:
- containerPort: 3000
env:
- name: NODE_ENV
value: production
- name: REACT_APP_API_V1_ENDPOINT
value: http://recofashion-api/api/v1
而且我认为k8s DNS本身没有问题。我尝试在我的“ recofashion-client”窗格中使用curl发送请求,它似乎按预期工作。
curl http://recofashion-api/api/v1/user/me
{"timestamp":"2020-02-03T06:55:20.748+0000","status":403,"error":"Forbidden","message":"Access Denied","path":"/api/v1/user/me"}
但是,当我尝试在浏览器中发送请求时,失败如下:
我正在像这样从k8s接收react app中的外部环境变量:
const response = await getWithAuth(`${process.env.REACT_APP_API_V1_ENDPOINT}/user/me`)
所以有什么问题???我在互联网上搜索了很多,但找不到合适的答案...
++入口清单文件
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: ingress
spec:
rules:
- http:
paths:
- path: /*
backend:
serviceName: recofashion-client
servicePort: 80
根据提供的所有信息,我设法使用GKE重现了您的方案。
TL; DR:
ClusterIP
LoadBalancer
,文档在这里。您的ENV REACT_APP_API_V1_ENDPOINT
必须指向Api服务地址,而不是部署地址或Pod地址。(即:value: http://recofashion-api-svc
)
您不能在外部使用群集DNS。
由于我没有您的react应用,因此我使用echo-app来模拟通信的两个部分。通过这种方式,我可以手动重现您的应用程序将自己执行的操作。
internet
和之间recofashion-client
recofashion-client
和之间recofashion-api
。recofashion-client
-前端:
apiVersion: apps/v1
kind: Deployment
metadata:
name: recofashion-client
labels:
name: recofashion-client
spec:
replicas: 2
selector:
matchLabels:
app: recofashion-client
template:
metadata:
labels:
app: recofashion-client
spec:
containers:
- name: web
image: mendhak/http-https-echo
ports:
- name: http
containerPort: 80
env:
- name: NODE_ENV
value: production
- name: REACT_APP_API_V1_ENDPOINT
value: http://recofashion-api-svc
---
apiVersion: v1
kind: Service
metadata:
name: recofashion-cli-svc
labels:
app: recofashion-client
spec:
type: LoadBalancer
selector:
app: recofashion-client
ports:
- name: http
port: 3000
targetPort: 80
recofashion-api
-后端API:
kind: Deployment
apiVersion: apps/v1
metadata:
name: recofashion-api
labels:
name: recofashion-api
spec:
replicas: 2
selector:
matchLabels:
app: recofashion-api
template:
metadata:
labels:
app: recofashion-api
spec:
containers:
- name: api-web
image: mendhak/http-https-echo
imagePullPolicy: Always
ports:
- containerPort: 80
env:
- name: NODE_ENV
value: production
---
kind: Service
apiVersion: v1
metadata:
name: recofashion-api-svc
labels:
app: recofashion-api
spec:
selector:
app: recofashion-api
ports:
- name: http
port: 80
targetPort: 80
注意:我保持您的Ingress完好无损。
现在到终端:
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
gke-cluster-1-default-pool-e0523823-06jt Ready <none> 2d v1.15.7-gke.23
gke-cluster-1-default-pool-e0523823-vklh Ready <none> 2d v1.15.7-gke.23
$ kubectl apply -f recofashion-full.yaml
deployment.apps/recofashion-client created
service/recofashion-cli-svc created
deployment.apps/recofashion-api created
service/recofashion-api-svc created
ingress.extensions/reco-ingress created
$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/recofashion-api-784b4d9897-9256q 1/1 Running 0 12m
pod/recofashion-api-784b4d9897-ljkfs 1/1 Running 0 12m
pod/recofashion-client-75579c8499-wd5vj 1/1 Running 0 12m
pod/recofashion-client-75579c8499-x766s 1/1 Running 0 12m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 2d
service/recofashion-api-svc ClusterIP 10.0.4.73 <none> 80/TCP 12m
service/recofashion-cli-svc LoadBalancer 10.0.3.133 35.239.58.188 3000:31814/TCP 12m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/recofashion-api 2/2 2 2 13m
deployment.apps/recofashion-client 2/2 2 2 13m
NAME DESIRED CURRENT READY AGE
replicaset.apps/recofashion-api-784b4d9897 2 2 2 13m
replicaset.apps/recofashion-client-75579c8499 2 2 2 13m
$ curl http://35.239.58.188:3000
{
"path": "/",
"headers": {
"host": "35.239.58.188:3000",
"user-agent": "curl/7.66.0",
},
"method": "GET",
"body": "",
"hostname": "35.239.58.188",
"ip": "::ffff:10.8.1.1",
"protocol": "http",
"os": {
"hostname": "recofashion-client-75579c8499-x766s"
}
}
到目前为止没有问题,我们能够curl
从外面到recofashion-client
。
现在,让我们连接到内部,recofashion-client
并尝试recofashion-api
使用部署期间创建的ENV进行连接:
❯ kubectl exec recofashion-client-75579c8499-x766s -it sh
/app # apk update && apk add curl
OK: 10 MiB in 20 packages
/app # env
REACT_APP_API_V1_ENDPOINT=http://recofashion-api-svc
NODE_ENV=production
/app # curl $REACT_APP_API_V1_ENDPOINT
{
"path": "/",
"headers": {
"host": "recofashion-api-svc",
"user-agent": "curl/7.61.1",
"accept": "*/*"
},
"method": "GET",
"body": "",
"hostname": "recofashion-api-svc",
"ip": "::ffff:10.8.1.21",
"protocol": "http",
"os": {
"hostname": "recofashion-api-784b4d9897-9256q"
}
}
/app # nslookup recofashion-api-svc
Name: recofashion-api-svc
Address 1: 10.0.4.73 recofashion-api-svc.default.svc.cluster.local
当我们api-service
在ENV中使用该名称时value
,它会解析DNS,因为该服务负责将负载定向到POD。
编辑:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句