如何使用NodeJS和Express为OpenShift应用程序配置路由 [英] How to configure a route for an OpenShift app with nodeJS and express
问题描述
我是OpenShift的新手,我希望获得一些帮助,以便在OpenShift上为我的应用配置路由.
I am new to OpenShift and I would like some help in order to configure the route for my app on OpenShift.
这是我要部署的NodeJS-Express应用程序中的端口和ip配置:
This is my port and ip configuration in the NodeJS - Express app that I'm trying to deploy:
const server_port = process.env.OPENSHIFT_NODEJS_PORT || process.env.OPENSHIFT_INTERNAL_PORT || process.env.PORT || 5000;
const server_ip = process.env.OPENSHIFT_NODEJS_IP || process.env.OPENSHIFT_INTERNAL_IP || '0.0.0.0';
server.listen(server_port, server_ip, () => {
console.log(`Server running on port ${server_port}`);
});
具有完全相同的配置,应用程序可以在Heroku中的 localhost:5000
中成功运行.任何帮助将不胜感激.
With exactly the same configuration the application runs successfully in localhost:5000
in Heroku. Any help will be appreciated.
更新:
如上所述,我更改了项目的端口和ip,这是该路由的YAML:
I changed my project's port and ip as shown above and this is my YAML for the route:
kind: Route
apiVersion: route.openshift.io/v1
metadata:
name: *****-gitlab-io-be
namespace: *****gitlabiobe
selfLink: >-
/apis/route.openshift.io/v1/namespaces/*****gitlabiobe/routes/*****-gitlab-io-be
uid: 34fef86d-4051-11ea-bd30-0a580a810070
resourceVersion: '155488429'
creationTimestamp: '2020-01-26T15:33:31Z'
labels:
app: *****-gitlab-io-be
app.kubernetes.io/component: *****-gitlab-io-be
app.kubernetes.io/instance: *****-gitlab-io-be
app.kubernetes.io/name: nodejs
app.kubernetes.io/part-of: *****-gitlab-io-be
app.openshift.io/runtime: nodejs
app.openshift.io/runtime-version: latest
annotations:
app.openshift.io/vcs-ref: master
app.openshift.io/vcs-uri: 'https://gitlab.com/*****/*****.gitlab.io.be.git'
openshift.io/host.generated: 'true'
spec:
host: >-
************gitlabiobe.apps.ca-central-1.starter.openshift-online.com
subdomain: ''
to:
kind: Service
name: *****-gitlab-io-be
weight: 100
port:
targetPort: 5000-tcp
wildcardPolicy: None
status:
ingress:
- host: >-
*****gitlabiobe.apps.ca-central-1.starter.openshift-online.com
routerName: default
conditions:
- type: Admitted
status: 'True'
lastTransitionTime: '2020-01-26T15:33:31Z'
wildcardPolicy: None
routerCanonicalHostname: apps.ca-central-1.starter.openshift-online.com
推荐答案
有几项要检查:
- 吊舱应该可用(所有容器已准备就绪,所有探针均已通过,等等)
- NodeJS服务器必须侦听0.0.0.0地址(不是本地主机)
-
OpenShift服务应转发8080端口,并且其选择器标识所需的Pod,例如:
- The pod should be available (all containers are ready, all probes are passed etc)
- NodeJS server have to listen to 0.0.0.0 address (not localhost)
OpenShift Service should forward 8080 port and it's selector identifies required pod, e.g.:
kind: Service
...
spec:
selector:
app: xxx
ports:
- name: 8080-tcp
port: 8080
protocol: TCP
targetPort: 8080
路由必须映射到所需的OpenShift服务及其目标端口,例如:
The Route have to be mapped to required OpenShift Service and the its target port, e.g.:
kind: Route
...
spec:
port:
targetPort: 8080-tcp
to:
kind: Service
name: xxx
...
然后,您可以使用路线的主人到达豆荚.
Then you can use the route's host to reach the pod.
这篇关于如何使用NodeJS和Express为OpenShift应用程序配置路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!