我使用bitnami图表将mongodb安装为一个副本集,在我的k8s集群上安装了3个副本。
所以我得到了这些吊舱
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指南针进行访问。
我将端口向前设置为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 2d14hvalues.yaml for bitnami helm图表
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发布于 2022-11-17 10:21:15
只是重新建立了你的设置。一切都很好
$ 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罗盘

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安装。
发布于 2022-11-17 10:33:39
它使用端口正向和罗盘在罗盘中的一些配置工作,使用有直接连接的高级选项:

不要添加复制设置选项。
发布于 2022-11-16 16:49:51
据我所知(我使用),不可能使用端口转发连接芒戈副本集与罗盘(您不能使用本地端口两次或多次.)对我来说,因为指南针使用复制集使用的拓扑。因此,您需要遵从来自客户端计算机的拓扑结构。
因此,您可以使用集群IP公开mongo,管理您的主机文件以匹配kubernetes节点。
并像在kubernetes中一样使用标准连接字符串:

更新:您可以将mongo配置为执行群集https://artifacthub.io/packages/helm/bitnami/mongodb。

你还保存了文件:
external_IP1 mongodb-0.mongodb-headless.mongodb.svc.cluster.local
external_IP2 mongodb-1.mongodb-headless.mongodb.svc.cluster.local
https://stackoverflow.com/questions/74429226
复制相似问题