我已经设置了Percona MongoDB出口商,当我试图为我的MongoDB实例抓取指标时,我得到一个错误:
An error has occurred while connecting to MongoDB:
cannot connect to MongoDB: server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: mongodb-headless-service.labs.svc.cluster.local:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp: lookup mongodb-headless-service.svc.cluster.local on 159.XX.XX.XX:XX: no such host }, ] }
MongoDB和Percona-MongoDB出口商部署在相同的命名空间在Kubernetes上,我创建了一个用户(管理员级别)来提取指标:
db.createUser({ user: "promt", pwd: "abc123", roles: [ { role: "clusterMonitor", db: "admin" },{ role: "read", db: "local" } ], mechanisms:["SCRAM-SHA-1"]})
验证导出器是否能够从我使用的MongoDB实例中抓取任何指标:
kubectl -n labs port-forward service/mongodb-exporter 9216
,我可以通过http://localhost:9216
访问导出器在Percona mongodb - exporters部署中,我指定了如下的蒙古包uri:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-exporter
namespace: labs
spec:
replicas: 1
selector:
matchLabels:
app: mongodb-exporter
template:
metadata:
labels:
app: mongodb-exporter
spec:
containers:
- name: mongodb-exporter
image: percona/mongodb_exporter:0.30
imagePullPolicy: "IfNotPresent"
args:
- "--mongodb.direct-connect=false"
- "--mongodb.uri=mongodb://promt:abc123@mongo-headless-service.svc.cluster.local/admin"
ports:
- name: metrics
containerPort: 9216
resources:
requests:
memory: 128Mi
cpu: 250m
我尝试了很多选项,但它们都导致相同的错误:
添加k8s命名空间到mongo-uri
——mongodb.uri = mongodb://promt: abc123@mongodb-headless-service。实验室.svc.cluster.local/admin
添加SSL选项——mongodb.uri = mongodb://promt: abc123@mongodb-headless-service.svc.cluster.local/管理?ssl = false
从使用k8s无头服务切换到ClusterIP
——mongodb.uri = mongodb://promt: abc123@mongo-single-clusterip.labs.svc.cluster.local/admin
- 启用直连选项
- "——mongodb.direct-connect = true"-"mongodb.uri = mongodb://promt: abc123@mongodb-headless-service.svc.cluster.local/admin"
对于所有这些选项,我也尝试了添加ssl选项和命名空间的变化,但在所有情况下都以相同的错误失败.
我的MongoDB实例是一个独立部署,定义如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
namespace: labs
labels:
app: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: docker.io/bitnami/mongodb:4.4.6-debian-10-r0
imagePullPolicy: "IfNotPresent"
securityContext:
runAsNonRoot: true
runAsUser: 1001
ports:
- containerPort: 27017
env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: mongodb-secret-amended
key: mongo-root-username
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret-amended
key: mongo-root-password
volumeMounts:
- mountPath: /data/db
name: mongodb-vol
volumes:
- name: mongodb-vol
persistentVolumeClaim:
claimName: mongodb-claim
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
namespace: labs
spec:
type: NodePort
selector:
app: mongodb
ports:
- protocol: TCP
port: 27017
targetPort: 27017
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-headless-service
namespace: labs
spec:
selector:
app: mongodb
ports:
- protocol: TCP
port: 27017
targetPort: 27017
---
apiVersion: v1
kind: Service
metadata:
name: mongo-single-clusterip
namespace: labs
spec:
type: ClusterIP
selector:
app: mongodb
ports:
- protocol: TCP
port: 27017
targetPort: 27017
对于上下文,我遵循这里和这里给出的示例。
我错过了什么?
您得到的错误消息中说明了原因:
error occured during connection handshake: dial tcp: lookup mongodb-headless-service.svc.cluster.local on 159.XX.XX.XX:XX: no such host
DNS查找FQDN:mongodb-headless-service.svc.cluster.local
失败。
您可以通过以下步骤确认DNS设置是否正确:https://kubernetes.io/docs/tasks/administer-cluster/dns-debugging-resolution/
您需要在配置中将mongoDB的URI更改为mongodb-headless-service.labs.svc.cluster.local
;它应该开始工作了。