gpt4 book ai didi

mongodb - 如何使用指南针连接到 mongodb replicaset (k8s)

转载 作者:行者123 更新时间:2023-12-05 03:16:52 30 4
gpt4 key购买 nike

我使用 bitnami helm chart 在我的 k8s 集群上将 mongodb 安装为具有 3 个副本的副本集。

所以我得到了这些 pod:

mongodb-0.mongodb-headless.mongodb.svc.cluster.local:27017
mongodb-1.mongodb-headless.mongodb.svc.cluster.local:27017
mongodb-2.mongodb-headless.mongodb.svc.cluster.local:27017

现在我想使用 mongodb compass 进行访问。

我设置了一个端口转发(在 27017 我正在运行本地 mongodb)

kubectl port-forward svc/mongodb-headless -n mongodb 27018:27017

并尝试将指南针与 uri 连接

mongodb://localhost:27018

但这给了我错误

getaddrinfo ENOTFOUND mongodb-0.mongodb-headless.mongodb.svc.cluster.local

使用指南针连接到我的 k8s 集群 mongodb 我做错了什么?


更新

% kubectl get all -n mongodb

NAME READY STATUS RESTARTS AGE
pod/mongodb-0 1/1 Running 0 25h
pod/mongodb-1 1/1 Running 0 25h
pod/mongodb-2 1/1 Running 0 25h
pod/mongodb-arbiter-0 1/1 Running 0 2d14h

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/mongodb-arbiter-headless ClusterIP None <none> 27017/TCP 2d14h
service/mongodb-headless ClusterIP None <none> 27017/TCP 2d14h

NAME READY AGE
statefulset.apps/mongodb 3/3 2d14h
statefulset.apps/mongodb-arbiter 1/1 2d14h

bitnami helm chart 的 values.yaml

image:
registry: docker.io
repository: bitnami/mongodb
digest: "sha256:916202d7af766dd88c2fff63bf711162c9d708ac7a3ffccd2aa812e3f03ae209" # tag: 4.4.15
pullPolicy: IfNotPresent
architecture: replicaset
replicaCount: 2
updateStrategy:
type: RollingUpdate
containerPorts:
mongodb: 27017
auth:
enabled: true
rootUser: root
rootPassword: "password"
usernames: ["user"]
passwords: ["userpass"]
databases: ["db"]

service:
portName: mongodb
ports:
mongodb: 27017

persistence:
enabled: true
accessModes:
- ReadWriteOnce
size: 8Gi

volumePermissions:
enabled: true

livenessProbe:
enabled: false
readinessProbe:
enabled: false

最佳答案

刚刚重新创建了您的设置。一切正常

$ k create ns mongo-test
namespace/mongo-test created

$ k -n mongo-test create -f mongo-svc-sts.yaml
statefulset.apps/mongo created
service/mongo-headless created

结果

$ k -n mongo-test get all
NAME READY STATUS RESTARTS AGE
pod/mongo-0 1/1 Running 0 44s
pod/mongo-1 1/1 Running 0 40s
pod/mongo-2 1/1 Running 0 27s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/mongo-headless ClusterIP None <none> 27017/TCP 13m

NAME READY AGE
statefulset.apps/mongo 3/3 45s

端口转发

$ k -n mongo-test port-forward svc/mongo-headless 27018:27017
Forwarding from 127.0.0.1:27018 -> 27017
Forwarding from [::1]:27018 -> 27017
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018

指南针 enter image description here

mongo-svc-sts.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongo
spec:
serviceName: "mongo"
replicas: 3
selector:
matchLabels:
app.kubernetes.io/name: mongo
app.kubernetes.io/component: backend
template:
metadata:
labels:
app.kubernetes.io/name: mongo
app.kubernetes.io/component: backend
spec:
tolerations:
- operator: Exists
containers:
- name: mongo
image: mongo:latest
args:
- --bind_ip
- 0.0.0.0
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 27017
---
apiVersion: v1
kind: Service
metadata:
labels:
app: mongo
service: logging
name: mongo-headless
spec:
type: ClusterIP
clusterIP: None
ports:
- port: 27017
selector:
app.kubernetes.io/name: mongo
app.kubernetes.io/component: backend

为了能够帮助您,请使用该 YAML 并发布输出。如果它不起作用,您可能应该调试您的 k8s 安装

关于mongodb - 如何使用指南针连接到 mongodb replicaset (k8s),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74429226/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com