mongodb conection string for kubernetes
The idea is pretty straight foward. With kubernetes you create your own network / cluster. Then u started to put in your database service and give it a name so it can be recognized by apps running on the same network, for example, you want to call your database "mongoserver".
Here we call our service mogoserver, doesnt really matter. But the label DOES. It will based on this criteria to look for your server (under the configuration called, labels (yeap this is important, besides the port number) It has gotta to match.
Service configuration file.
apiVersion: v1
kind: Service
metadata:
labels:
name : mongoserver
name: mongoserver
spec:
ports:
- port: 27017
protocol: TCP
targetPort: 27017
selector:
name : mongoserver
Database pod configuration file
kind: Pod
apiVersion: v1
metadata:
labels:
name : mongoserver
name: mongoserver
spec:
volumes:
- name: mongo-pv-storage
persistentVolumeClaim:
claimName: mongo-pv-claim
containers:
- name: mongoserver
image: mongo
ports:
- containerPort: 27017
hostPort: 27017
name : mongoserver
volumeMounts:
- mountPath: "/data/db"
name: mongo-pv-storage
If your service is configure right, you can use the following command to check..
kubectl describe service/mongoserver and should see something below (look at the endpoints) :-
This shows that your endpoint has been successfully resolved. So if your mongo db connection string is "mongo://mongoserver:27017", it will get redirect to 172.17.9.5.
Comments