我创建了两个服务,每个服务都有相应的部署。所有这些都在同一个PC。应用YAML后,服务和部署都正常运行。
sudo npm kubectl apply -f deployment.yaml
现在,如果我尝试卷曲到入口IP地址,那么我得到404页未找到作为响应。
sudo kubectl get ingress -o wide
sudo curl <IP address of ingress>
如果我尝试curl到员工服务或员工部署的IP地址,它会给出连接在等待一段时间后被拒绝。我为员工服务和部署提到的任何端口我尝试,我总是得到错误而不是正确的响应。
社区有人能帮忙吗?我需要从员工pod调用myshop pod的/emplist
endpoint。
myshop. js
const express = require('express')
const app = express()
const port = 3000
app.get('/emplist', (req, res) => {
res.status(200).send( empListObj )
});
app.listen(port, '0.0.0.0', () => {
console.log(`Listening on port ${port}`)
})
js
const axios = require('axios').default;
const express = require('express')
const app = express()
const port = 3001
app.get('/', (req, res) => {
axios({
method: 'get',
url: 'http://myshopservice/emplist'
})
.then(function (response) {
res.status(200).send(response.data.empList)
})
.catch(function (error) {
res.status(200).send(`Error because ${error}`);
})
})
app.listen(port, '0.0.0.0', () => {
console.log(`Listening on port ${port}`)
})
现在对应的部署. yaml是:
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myshop-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: myshop.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: myshopservice
port:
number: 4000
---
apiVersion: v1
kind: Service
metadata:
name: myshopservice
spec:
selector:
app: myshop
ports:
- protocol: "TCP"
port: 4000
targetPort: 3000
nodePort: 30000
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myshop
spec:
replicas: 1
selector:
matchLabels:
app: myshop
template:
metadata:
labels:
app: myshop
spec:
containers:
- name: myshop
image: mydockerhubid/myshop:1
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: employeeservice
spec:
selector:
app: employee
ports:
- protocol: "TCP"
port: 4001
targetPort: 3001
nodePort: 30001
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: employee
spec:
replicas: 1
selector:
matchLabels:
app: employee
template:
metadata:
labels:
app: employee
spec:
containers:
- name: hud
image: mydockerhubid/employee:1
ports:
- containerPort: 3001
---
看起来主要是入口有问题。我建议首先检查服务状态,如果它正在运行或不在
您的应用程序正在侦听一个特定的路径,但您没有在curl中请求该路径。如果您想这样做,您也可以这样做,但我建议您先尝试简单的方式,然后再使用基于路由/路径的路由。
另外,您的入口正在myshop服务上发送请求,但它只监听或有路由: /emplist
因此,尝试一次入口,在本地删除注释和主机。
如果你想保留主机,你必须在本地添加条目到/etc/host
用于主机和入口IP映射,然后你应该使用或尝试curl on domain而不是入口IP。
您是否正在运行任何入口控制器?Nginx左右?:https://kubernetes.github.io/ingress-nginx/
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myshop-ingress
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: myshopservice
port:
number: 4000
检查这个非常简单的入口路由和控制器设置示例:https://dev.to/xaviergeerinck/creating-a-kubernetes-nginx-ingress-controller-and-create-a-rule-to-a-sample-application-4and