Windows环境下Vagrant搭建的裸金属K8s集群中MongoDB Compass连接问题及账号密码咨询
First, let's tackle the credentials question, then walk through two reliable connection methods for your Vagrant-based bare-metal cluster.
Step 1: Locate MongoDB Credentials (Username/Password)
Most Kubernetes deployments store database credentials in Secrets—here's how to retrieve them:
- List all secrets in your namespace (add
-n <namespace>if you're not using the default):
Look for a secret tied to MongoDB, likekubectl get secretsmongodb,guestbook-mongodb, ormongo-db-credentials. - Decode the base64-encoded username:
kubectl get secret <your-mongo-secret-name> -o jsonpath='{.data.username}' | base64 -d - Decode the password the same way:
kubectl get secret <your-mongo-secret-name> -o jsonpath='{.data.password}' | base64 -d
Note for Official GuestBook Example
If you used the standard Kubernetes GuestBook demo, the MongoDB deployment doesn't enable authentication by default—so you can skip the credentials step entirely. Verify this by checking the config inside the container:
# Inside the MongoDB container cat /etc/mongod.conf | grep authorization
If there's no authorization: enabled line, auth is turned off.
Step 2: Connect with MongoDB Compass
You have two solid options here—port forwarding (recommended for temporary testing) or exposing the service via NodePort (for longer-term access).
Option 1: Kubectl Port Forwarding (Quick & Secure)
This maps your local machine's port directly to the cluster's MongoDB service, no public exposure needed:
- Find your MongoDB service name:
kubectl get svc | grep mongo - Run the port forward command (keep your terminal open while it runs):
kubectl port-forward svc/<your-mongo-service-name> 27017:27017 - Open MongoDB Compass and use one of these connection strings:
- With authentication:
mongodb://<username>:<password>@localhost:27017/guestbook?authSource=admin - Without authentication (default GuestBook setup):
mongodb://localhost:27017/guestbook
- With authentication:
Option 2: Expose MongoDB via NodePort (Permanent Access)
If you want persistent access without keeping a port forward running:
- Edit your MongoDB service to change its type to NodePort:
kubectl edit svc/<your-mongo-service-name> - Replace
type: ClusterIPwithtype: NodePort, then save and exit the editor. - Get the assigned NodePort and your Vagrant node's IP:
- NodePort:
Look for the port mapping likekubectl get svc <your-mongo-service-name>27017:30XXX/TCP—the second number is your NodePort. - Vagrant node IP:
Use thekubectl get nodes -o wideINTERNAL-IPof the node where your MongoDB pod is running (you found this earlier withkubectl get pods -o wide).
- NodePort:
- In MongoDB Compass, use this connection string:
- With authentication:
mongodb://<username>:<password>@<vagrant-node-ip>:<node-port>/guestbook?authSource=admin - Without authentication:
mongodb://<vagrant-node-ip>:<node-port>/guestbook
- With authentication:
内容的提问来源于stack exchange,提问作者VivekDev

