apiVersion: v1
kind: Service
metadata:
name: mongo
labels:
name: mongo
spec:
ports:
- port: 27017
targetPort: 27017
clusterIP: None
selector:
name: mongo
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: mongo
spec:
serviceName: "mongo"
replicas: 1
template:
metadata:
labels:
name: mongo
# environment: test
spec:
terminationGracePeriodSeconds: 10
volumes:
- name: mongo-pv-storage
persistentVolumeClaim:
claimName: mongo-pv-claim
containers:
- name: mongo
image: mongo:4.0.12-xenial
command:
- mongod
- "--bind_ip"
- 0.0.0.0
- "--smallfiles"
- "--noprealloc"
ports:
- containerPort: 27017
name: mongo
volumeMounts:
- name: mongo-pv-storage
mountPath: /data/db
我用了上面的yaml。使用kubectl exec命令可以正常检查Mongo Db。下面的yaml用于部署Spring Boot应用程序。
apiVersion: apps/v1
kind: Deployment
metadata:
name: imageprocessor-app-backend
labels:
app: imageprocessor-app-backend
spec:
# modify replicas according to your case
selector:
matchLabels:
tier: imageprocessor-app-backend
template:
metadata:
labels:
tier: imageprocessor-app-backend
spec:
containers:
- name: imageprocessor-app-backend
image: imageprocessor-app-backend:v1
ports:
- containerPort: 8099
env:
- name: spring.data.mongodb.host
value: mongo-0.mongo
- name: spring.data.mongodb.port
value: "27017"
- name: spring.data.mongodb.database
value: testdb
---
apiVersion: v1
kind: Service
metadata:
name: imageprocessor-app-backend
spec:
type: NodePort
ports:
- port: 8099
nodePort: 31471
selector:
tier: imageprocessor-app-backend
我得到的异常(exception)是
2019-09-24 12:27:04.902 INFO 1 --- [o-0.mongo:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server mongo-0.mongo:27017
com.mongodb.MongoSocketException: mongo-0.mongo: Try again
at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:188) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar:na]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
Caused by: java.net.UnknownHostException: mongo-0.mongo: Try again
at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_212]
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:929) ~[na:1.8.0_212]
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324) ~[na:1.8.0_212]
at java.net.InetAddress.getAllByName0(InetAddress.java:1277) ~[na:1.8.0_212]
at java.net.InetAddress.getAllByName(InetAddress.java:1193) ~[na:1.8.0_212]
at java.net.InetAddress.getAllByName(InetAddress.java:1127) ~[na:1.8.0_212]
at java.net.InetAddress.getByName(InetAddress.java:1077) ~[na:1.8.0_212]
at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:186) ~[mongodb-driver-core-3.8.2.jar:na]
... 5 common frames omitted
如何在我的应用程序中连接无头mongo服务。我尝试使用-名称:spring.data.mongodb.host
值:mongo-0.mongo //和值:mongo
最佳答案
您需要使用服务名称作为主机名。在您的示例中,它是mongo
。我在上面的YAML上部署了mongo,并且可以从同一命名空间中的另一个pod成功连接到它。
如果您在imageprocessor-app-backend
之外的其他 namespace 中运行mongo
,则必须将运行mongo
的 namespace 添加到主机名:mongo.<namespace>
,例如mongo.mongo
。
关于spring - 如何在Kubernetes中连接 headless Mongo Service,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58080573/