Kubernetes 中的后端/前端服务之间没有通信
Posted
技术标签:
【中文标题】Kubernetes 中的后端/前端服务之间没有通信【英文标题】:No communication between backend/frontend services in Kubernetes 【发布时间】:2020-12-12 09:54:59 【问题描述】:我正在尝试创建一个非常简单的 Kubernetes 项目,其中包括用 Reactjs + nginx 编写的前端客户端和用 Java + Spring boot 编写的后端服务器之间的通信。
我可以在本地与 docker-compose 进行通信,但是在将其部署到 gke 时,我得到:失败(111:连接被拒绝)
在我的后端:
控制器:
@RestController
@RequestMapping("/msgs")
@CrossOrigin
public class MsgController
@GetMapping("/getMsg")
public String getMsg()
return "hello from backend";
Dockerfile
FROM adoptopenjdk/openjdk11:alpine-jre
WORKDIR /opt/app
ARG JAR_FILE=target/*.jar
COPY $JAR_FILE app.jar
# java -jar /opt/app/app.jar
ENTRYPOINT ["java","-jar","app.jar"]
部署 yml:
Version: apps/v1
kind: Deployment
metadata:
name: server-demo
spec:
selector:
matchLabels:
app: server-demo
tier: backend
track: stable
replicas: 1
template:
metadata:
labels:
app: server-demo
tier: backend
track: stable
spec:
containers:
- name: hello
image: "gcr.io/gcp-kub-course/server-demo:latest"
ports:
- name: http
containerPort: 4420
---
apiVersion: v1
kind: Service
metadata:
name: server-demo
spec:
selector:
app: hello
tier: backend
ports:
- protocol: TCP
port: 4420
targetPort: 4420
在前端,我有
const [msg, setMsg] = useState('');
useEffect(() =>
fetch('/msgs/getMsg')
.then(response => response.text())
.then(m =>
// console.log(JSON.stringify(m))
setMsg(m)
);
);
return <div>msg</div>
Dockerfile:
FROM node:10-alpine as a builder
COPY package.json package-lock.json ./
RUN npm install && mkdir /react-ui && mv ./node_modules ./react-ui
WORKDIR /react-ui
COPY . .
# Build the project and copy the files
RUN npm run build
FROM nginx:alpine
#!/bin/sh
COPY ./nginx/nginx.conf /etc/nginx/nginx.conf
RUN rm -rf /usr/share/nginx/html/*
COPY --from=builder /react-ui/build /usr/share/nginx/html
EXPOSE 3000 80
ENTRYPOINT ["nginx", "-g", "daemon off;"]
nginx.conf:
worker_processes 5; ## Default: 1
worker_rlimit_nofile 8192;
events
worker_connections 4096; ## Default: 1024
http
upstream client
server
listen 80;
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;
location /msgs
proxy_pass http://server-demo:4420;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
和部署 yml:
apiVersion: v1
kind: Service
metadata:
name: frontend-service
labels:
app: frontend-service
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: frontend-service
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend-service
namespace: default
labels:
app: frontend-service
spec:
replicas: 1
selector:
matchLabels:
app: frontend-service
template:
metadata:
labels:
app: frontend-service
spec:
containers:
- name: frontend-service
image: gcr.io/gcp-kub-course/frontend-service:latest
imagePullPolicy: "Always"
ports:
- name: http
containerPort: 80
查看服务时:
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend-service LoadBalancer 10.24.15.122 34.121.100.70 80:32506/TCP 57m
kubernetes ClusterIP 10.24.0.1 <none> 443/TCP 25h
server-demo ClusterIP 10.24.4.49 <none> 4420/TCP 57m
查看吊舱时:
frontend-service-bf9b4ccfd-jcjvm 1/1 Running 0 58m
server-demo-84df7f57c6-blgxq 1/1 Running 0 58m
最后在查看前端服务日志时,我看到了:
2020/08/23 16:05:11 [error] 6#6: *28 connect() failed (111: Connection refused) while connecting to upstream, client: 10.128.0.7, server: , request: "GET /msgs/getMsg HTTP/1.1", upstream: "http://10.24.4.49:4420/msgs/getMsg", host: "34.121.100.70", referrer: "http://34.121.100.70/"
10.128.0.7 - - [23/Aug/2020:16:05:11 +0000] "GET /msgs/getMsg HTTP/1.1" 502 559 "http://34.121.100.70/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36"
我可以看到 nginx 完成了它的工作,因为它将 GET /msgs/getMsg 代理到 10.24.4.49 这是演示服务器 IP,它们都在默认命名空间上,我迷路了
【问题讨论】:
【参考方案1】:我发布它的第二个我看到了我的错误: 在 server-demo 部署 yaml 结束时
我错过了配置:
selector:
app: hello
必须是:
selector:
app: server-demo
【讨论】:
以上是关于Kubernetes 中的后端/前端服务之间没有通信的主要内容,如果未能解决你的问题,请参考以下文章