我在kubernetes中使用带有minikube和kubectl的Hyper-V,无法通过mongo服务和2种不同的部署将Nestjs api连接到Mongo Pod。
我的具有服务的Mongo.yml文件:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
labels:
app: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo
ports:
- containerPort: 27017
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
spec:
selector:
app: mongodb
ports:
- protocol: TCP
port: 27017
targetPort: 27017
我的Api acp-api.yml文件:
apiVersion: apps/v1
kind: Deployment
metadata:
name: acp-api-deployment
labels:
app: acp-api
spec:
replicas: 1
selector:
matchLabels:
app: acp-api
template:
metadata:
labels:
app: acp-api
spec:
containers:
- name: acp-api
image: acp/acp-rest-api
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3000
我必须给mongodb uri进行连接的Api配置:
import { Injectable } from '@nestjs/common'
import { MongooseModuleOptions, MongooseOptionsFactory } from '@nestjs/mongoose'
@Injectable()
export class MongooseConfigService implements MongooseOptionsFactory {
private username: string = ''
private password: string = ''
constructor() {}
createMongooseOptions():
| Promise<MongooseModuleOptions>
| MongooseModuleOptions {
return {
useCreateIndex: true,
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true,
uri: 'mongodb://mongodb-service:27017/acp--db'
}
}
}
我尝试使用带和不带端口,pod ip的mongo db服务名称 但无法连接