我一直在为我的工作部署设置 Kubernetes 集群。我将 MongoDB 托管在外部 Droplet 上,并且想要配置外部服务访问。
我正在关注这个tutorial 。 当我使用 kubectl 应用我的配置时,一切似乎都运行良好。
我的设置服务和端点
kind: Service
apiVersion: v1
metadata:
name: mongo
spec:
ports:
- name: mongodb
port: xxxx
------
kind: Endpoints
apiVersion: v1
metadata:
name: mongo
subsets:
- addresses:
- ip: 159.89.x.x
ports:
- port: xxx
我正在使用 typescript 进行开发,这就是我目前设置数据库连接的方式
const MONGO_URI = `mongodb://${config.mongourl}:${config.mongoport}/${config.collection}?authSource=admin`;
const options ={
user:`${config.mngusername}`,
pass:`${config.mngpassword}`,
keepAlive: true,
keepAliveInitialDelay: 300000,
useNewUrlParser: true,
useCreateIndex: true
}
mongoose.connect(MONGO_URI, options);
我的问题是,如何使用代码中的设置服务/部署。
谢谢
最佳答案
使用外部服务的方式有很多种,例如:
- 对应用中服务的 IP:端口进行硬编码。
- 在您的应用中使用环境变量并通过 configmaps 注入(inject)它们.
- 创建服务/端点,ExternalName服务,或带有 External IPs 的服务。这允许您的应用程序使用 Kubernetes'Service Discovery机制。
根据您的情况,您可以使用:
const MONGO_URI = 'mongodb://mongo/${config.collection}?authSource=admin';
因为名称 mongo
将映射到 159.89.x.x:xxx
关于mongodb - 如何在 Kubernetes 中使用外部 MongoDB 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57824094/