概述

kubernetes搭建mongodb副本集群后,进入pod能够连接mongodb集群;配置对应的service后,外网用service能单个的连接mongod,但是外网不能连接整个集群。

报错

{
	"message" : "getaddrinfo ENOTFOUND tap-mongodb-0.tap-mongodb-none-service.tm.svc.cluster.local",
	"reason" : {
		"type" : "ReplicaSetNoPrimary",
		"servers" : {
			
		},
		"stale" : false,
		"compatible" : true,
		"heartbeatFrequencyMS" : 10000,
		"localThresholdMS" : 15,
		"setName" : "rs0",
		"maxSetVersion" : 3,
		"maxElectionId" : ObjectId("7fffffff000000000000000e"),
		"commonWireVersion" : 7
	}
}

原因

        进入pod进入mongodb,执行命令rs.conf()得

rs0:PRIMARY> rs.conf()
{
        "_id" : "rs0",
        "version" : 3,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "tap-mongodb-0.tap-mongodb-none-service.tm.svc.cluster.local:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "tap-mongodb-1.tap-mongodb-none-service.tm.svc.cluster.local:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "tap-mongodb-2.tap-mongodb-none-service.tm.svc.cluster.local:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("620cb881ea67b07abe622854")
        }
}

由错误信息和conf信息得知,副本集的members.host配置的为k8s的dns方式例如{tap-mongodb-0.tap-mongodb-none-service.tm.svc.cluster.local:27017}

解决

方式一:在k8s内部使用mongodb副本集

方式二:修改mongodb的conf,修改其中的members.host

Logo

开源、云原生的融合云平台

更多推荐