I am learning about Kubernetes am trying move from docker-compose to use Kubernetes but having problem with connecting to MongoDB running on localhost.
I don't have any problems connecting using following docker-compose.yaml by using 'network_mode: "host"'
//docker-compose.yaml
version: '3'
services:
eureka:
image: sage-eureka
ports:
- "8080:8080"
network_mode: "host"
But I'm having issues in Kubernetes. I used Kompose to convert the docker-compose.
//application.properties
spring.data.mongodb.host=localhost (tried adding mongo instead of localhost when used ExternalName and ClusterIP)
spring.data.mongodb.port=27017
spring.data.mongodb.database=MyMongo
// eureka-service.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.21.0 (992df58d8)
creationTimestamp: null
labels:
io.kompose.service: eureka
name: eureka
spec:
ports:
- name: "8080"
port: 8080
targetPort: 8080
- name: "27017"
port: 27017
targetPort: 27017
selector:
io.kompose.service: eureka
status:
loadBalancer: {}
//eureka-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.21.0 (992df58d8)
creationTimestamp: null
labels:
io.kompose.service: eureka
name: eureka
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: eureka
strategy: {}
template:
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.21.0 (992df58d8)
creationTimestamp: null
labels:
io.kompose.service: eureka
spec:
hostNetwork: true
dnsPolicy: Default
containers:
- args:
- --spring.profiles.active=local
image: sage-eureka
imagePullPolicy: Never
name: sageeureka
ports:
- containerPort: 8080
- containerPort: 27017
resources: {}
restartPolicy: Always
serviceAccountName: ""
volumes: null
status: {}
I'm also getting on external ip address when I've added type to LoadBalencer and I've also tried creating Service type ExternalName and NodePort for Mongo as shown in Kubernetes best practices: mapping external services:
eureka ClusterIP 10.102.22.91 <none> 8080/TCP,27017/TCP 45h
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d2h
mongo ExternalName <none> host.docker.internal <none> 45h
I've referred to similar posts here but I don't know exactly what needs to be done and following these post are also not working for me as I don't know what I'm doing wrong:
- com.mongodb.MongoSocketOpenException: Exception opening socket(MongoDB, Docker)
- During local development with Kubernetes/minikube, how should I connect to postgres database running on localhost?
I also tried creating the mongo-serivce as in 2nd post like this:
kind: Service
apiVersion: v1
metadata:
name: mongo
spec:
type: ExternalName
externalName: host.docker.internal
Please Help!. Thank you.