我想通过一个80端口的入口公开我的两个API服务
apiVersion: v1
kind: Service
metadata:
name: my-api
spec:
selector:
app: my-api
ports:
- name: api1
port: 3000
targetPort: 3000
- name: api2
port: 4000
targetPort: 4000
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
tls:
- hosts:
- example.com
secretName: app-tls
rules:
- host: example.com
http:
paths:
- path: /my-api(/|$)(.*)
backend:
serviceName: my-api
servicePort: 80
但当我尝试到达https://example.com/my-api
时,它总是返回503状态代码。
servicePort: 80
并不意味着nginx入口在端口80
上服务。这实际上是后端服务上的端口,听起来有两个端口:3000
和4000
。
nginx入口控制器默认在端口80
上服务,如果您也启用了TLS或/和443
。在您的情况下,如果您想同时为这两个API提供服务,您可以简单地分离路径。
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
tls:
- hosts:
- example.com
secretName: app-tls
rules:
- host: example.com
http:
paths:
- path: /my-api1(/|$)(.*)
backend:
serviceName: my-api
servicePort: 3000
- path: /my-api2(/|$)(.*)
backend:
serviceName: my-api
servicePort: 4000
✌️