问题描述
我已经为两台机器安装了 docker 和 kubeadm 以在 Kubernetes 中部署应用程序,并添加了 ingress-Nginx 控制器。
应用已使用节点端口类型成功部署,并且能够使用 IP 和端口号公开。
和 DNS
testNginx.com
是为该 IP 创建的,因此我可以公开应用程序:
testNginx.com:3001
部署和服务的配置如下:
apiVersion: apps/v1
kind: Deployment
Metadata:
name: tool-deployment
namespace: bot
labels:
app: tool
spec:
replicas: 1
selector:
matchLabels:
app: tool
template:
Metadata:
labels:
app: tool
spec:
hostNetwork: true
restartPolicy: Always
containers:
- name: tool
image: myimage:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3001
---
apiVersion: v1
kind: Service
Metadata:
name: tool-service
namespace: bot
spec:
type: NodePort
selector:
app: tool
ports:
- protocol: TCP
port: 3001
targetPort: 3001
nodePort: 32001
接下来,我尝试将 ingress 部署到服务,以便我可以在没有端口的情况下公开应用程序。
ingress 的 YAML 配置如下
apiVersion: networking.k8s.io/v1
kind: Ingress
Metadata:
name: tool-ingress
namespace: bot
annotations:
Nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: testNginx.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: tool-service
port:
number: 32001
但是入口不起作用:
This page isn’t working
testNginx.com didn’t send any data.
ERR_EMPTY_RESPONSE
解决方法
首先,如果您想使用 ingress 公开您的应用程序,那么您应该使用 ClusterIP 服务。 那么你的清单文件应该是这样的。
apiVersion: v1
kind: Service
metadata:
name: tool-service
namespace: bot
spec:
type: ClusterIP
selector:
app: tool
ports:
- protocol: TCP
port: 3001
targetPort: 3001
您还必须更改入口清单文件。
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: tool-ingress
namespace: bot
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: testnginx.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: tool-service
port:
number: 3001