需要帮助连接节点与Kubernetes的Neo4j数据库

目前,我正在获取节点无法连接到数据库的ECONNREFUSED 。 我不知道数据库主机应该在哪里结束。 我应该使用localhost:7474 7474,127.0.0.1:7474,0.0.0.0 localhost:7474 ,还是我应该使用一些主机,当我得到我的Kubernetes豆荚,我可以以某种方式传入? 例如像$(minikube ip) = 192.168.90.100:7474 ,但为我的数据库? 有一个ENV包含数据库主机,我应该从某个地方拉?

 const neo4jDatabase = connect({ server: 'http://<what goes here!?>:7474', user: process.env.DB_USER, pass: process.env.DB_PASS, }); 

我似乎已经得到与下面的.ymlconfiguration运行豆荚,但不知道如果neo4j一个是正确的。

 NAME READY STATUS RESTARTS AGE neo4j-4166717986-8qbwq 1/1 Running 0 41m node-481322091-g27md 1/1 Running 0 11m 

node.yml

 apiVersion: extensions/v1beta1 kind: Deployment metadata: name: node spec: replicas: 1 template: metadata: labels: app: node tier: backend track: stable spec: containers: - name: node image: "myapp" ports: - name: nodeport containerPort: 8080 env: - name: DB_USER valueFrom: configMapKeyRef: name: config key: db_user - name: DB_PASS valueFrom: configMapKeyRef: name: config key: db_pass --- apiVersion: v1 kind: Service metadata: name: node spec: selector: app: node tier: backend ports: - protocol: TCP port: 80 targetPort: type: LoadBalancer 

neo4j.yml

 apiVersion: extensions/v1beta1 kind: Deployment metadata: name: neo4j spec: template: metadata: labels: run: neo4j spec: containers: - name: neo4j image: "my-neo4j" ports: - containerPort: 7474 --- apiVersion: v1 kind: Service metadata: name: neo4j labels: run: neo4j spec: selector: run: neo4j ports: - port: 7474 targetPort: 7474 protocol: TCP 

也许Kubernetes鲜为人知的一个特点是,一些神奇的环境variables被注入到正在运行的pod中。

在你的特定情况下,pod对于命名空间中的每个服务都有一个环境variables。 格式如下:

 <your service>_SERVICE_HOST <your service name>_SERVICE_PORT_EXPOSED_PORT 

您可以通过附加到正在运行的pod和kubectl exec -ti <your pod id> sh并发出printenv命令来validation这是否正确。

请注意,如果服务是在pod之后创build的,则必须使用kubectl delete pod <your pod id>以强制重新创build(和注入)环境variables。

在你的情况下,最终的代码将如下所示:

 const serviceHost = process.ENV.NEO4J_SERVICE_HOST; const servicePort = process.ENV.NEO4J_SERVICE_PORT_EXPOSED_PORT; const neo4jDatabase = connect({ server: `http://${serviceHost}:${servicePort}`, user: process.env.DB_USER, pass: process.env.DB_PASS, });