我有以下服务......
apiVersion: v1
kind: Service
metadata:
name: mongo
labels:
app: mongo
spec:
ports:
- name: mongo
port: 27017
clusterIP: None
selector:
app: mongo
以下有状态集......
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongo
spec:
serviceName: mongo
selector:
matchLabels:
app: mongo
replicas: 3
template:
metadata:
labels:
app: mongo
spec:
terminationGracePeriodSeconds: 10
containers:
- name: mongo
image: mongo
command:
- mongod
- "--replSet"
- rs0
- "--smallfiles"
- "--noprealloc"
ports:
- name: mongo
containerPort: 27017
- name: mongo-sidecar
image: cvallance/mongo-k8s-sidecar
env:
- name: "VERSION"
value: "2"
- name: MONGO_SIDECAR_POD_LABELS
value: "role=mongo"
但是,我的代码无法使用网址mongo:27017
建立连接。我已尝试连接到mongo
,mongo-0.mongo:27017
,其他人。如果我执行一个容器并运行$ nslookup mongo
我得到......
Name: mongo
Address 1: 10.1.0.80 mongo-0.mongo.default.svc.cluster.local
Address 2: 10.1.0.81 mongo-1.mongo.default.svc.cluster.local
Address 3: 10.1.0.82 mongo-2.mongo.default.svc.cluster.local
点击$ curl mongo:27017
或$ telnet mongo 27017
会给我一个连接拒绝错误。
答案 0 :(得分:3)
将bind_ip
添加到命令:
command:
- mongod
- "--replSet"
- rs0
- "--smallfiles"
- "--noprealloc"
- "--bind_ip"
- "0.0.0.0"
答案 1 :(得分:1)
好的,所以我解决了这个问题,这是设置bind_ip = 0.0.0.0的情况,但也是我必须将服务中的选择器更改为role: mongo
而不是app: mongo
。然后在我的部署中,我必须在我的元数据中包含role: mongo
,例如......
template:
metadata:
labels:
app: mongo
role: mongo