以下是我的nodejs微服务模式:
// api.ts
import { Router } from 'express';
const router = Router();
router.get(':id', ...doSomething);
router.post(':id', ...doSomething);
export default router;
// index.ts
import * as Express from 'express';
import API from './api.js';
basePath = process.env.basePath; // usually is project name
const app = Express();
// handle external call
app.use(basePath, API) // wish to remove this line
// handle internal call from microservices
app.use(API) // preferred to be like this
...continue
下面是我的kubedeploy文件从同事那里继承
apiVersion: apps/v1
kind: Deployment
metadata:
name: $CI_PROJECT_NAME
namespace: $KUBE_NAMESPACE
spec:
replicas: 1
selector:
matchLabels:
app: $CI_PROJECT_NAME
template:
metadata:
labels:
app: $CI_PROJECT_NAME
spec:
imagePullSecrets:
- name: gitlabcred
containers:
- image: registry.gitlab.com/$GROUP_NAME/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME
imagePullPolicy: Always
name: $CI_PROJECT_NAME
ports:
- containerPort: $PORT
env:
- name: basePath
value: "$URL_PATH"
resources: $KUBE_RESOURCES
livenessProbe: $KUBE_LIVENESS
---
apiVersion: v1
kind: Service
metadata:
name: $CI_PROJECT_NAME
namespace: $KUBE_NAMESPACE
spec:
ports:
- port: $PORT
protocol: TCP
name: http
selector:
app: $CI_PROJECT_NAME
sessionAffinity: ClientIP
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: $CI_PROJECT_NAME
namespace: $KUBE_NAMESPACE
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: $KUBE_DNS_NAME
http:
paths:
- path: /$URL_PATH
backend:
serviceName: $CI_PROJECT_NAME
servicePort: $PORT
- http:
paths:
- backend:
serviceName: $CI_PROJECT_NAME
servicePort: $PORT
上面的代码和设置在内部和外部呼叫中正常工作,如下所示:
http://publicurl.com/projecta/someid///some//外部呼叫,Microservice接收请求。Pathas&quot as&quota/projecta/someid"
'http://publicurl.com/projectb/someid//外部呼叫,Microservice接收请求。Pathas&quot as&quotb/projectB/someid"
'http://projecta/someid//内部呼叫,微服务接收请求。
http://projectb/someid//内部呼叫,微服务接收请求。
我希望删除" app.use(basepath,api("从我的微服务中以使其环境独立。
无论如何,我是否可以更改我的kubedeploy,以使其更改我从外部呼叫中收到的小路径,以"/some quot"?
更新:以下是DevOps
更新的最新KubedeployapiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: $CI_PROJECT_NAME
namespace: $KUBE_NAMESPACE
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- host: $KUBE_DNS_NAME
http:
paths:
- path: /$URL_PATH(/|$)(.*)
backend:
serviceName: $CI_PROJECT_NAME
servicePort: $PORT
- http:
paths:
- backend:
serviceName: $CI_PROJECT_NAME
servicePort: $PORT
我在上面尝试过,但我不明白它从浏览器侧重定向。示例:我在浏览器上打开http://publicurl.com/projecta/help时,以某种方式在浏览器地址栏上http://publicurl.com/help在http://publicurl.com/help上显示为"默认后端-404"由于入口无法找到匹配路径。
您可以使用重写注释,但请记住,这些是NGINX控制器的自定义扩展,并且不适合所有其他实现。
来自他们的示例:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: rewrite
namespace: default
spec:
rules:
- host: rewrite.bar.com
http:
paths:
- backend:
serviceName: http-svc
servicePort: 80
path: /something(/|$)(.*)