我的目标是在StatefulSet
名称空间和production
名称空间中创建staging
。我可以创建生产StatefulSet,但是将其部署到暂存名称空间时,会收到错误消息:
failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
我用于登台设置的YAML如下:
staging-service.yml
apiVersion: v1
kind: Service
metadata:
name: mongodb-staging
namespace: staging
labels:
app: ethereumdb
environment: staging
spec:
ports:
- name: http
protocol: TCP
port: 27017
targetPort: 27017
clusterIP: None
selector:
role: mongodb
environment: staging
staging-statefulset.yml
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: mongodb-staging
namespace: staging
labels:
app: ethereumdb
environment: staging
annotations:
prometheus.io.scrape: "true"
spec:
serviceName: "mongodb-staging"
replicas: 1
template:
metadata:
labels:
role: mongodb
environment: staging
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: role
operator: In
values:
- mongo
- key: environment
operator: In
values:
- staging
topologyKey: "kubernetes.io/hostname"
terminationGracePeriodSeconds: 10
containers:
- name: mongo
image: mongo
command:
- mongod
- "--replSet"
- rs0
- "--smallfiles"
- "--noprealloc"
- "--bind_ip_all"
- "--wiredTigerCacheSizeGB=0.5"
ports:
- containerPort: 27017
volumeMounts:
- name: mongo-persistent-storage
mountPath: /data/db
- name: mongo-sidecar
image: cvallance/mongo-k8s-sidecar
env:
- name: MONGO_SIDECAR_POD_LABELS
value: "role=mongodb,environment=staging"
- name: KUBERNETES_MONGO_SERVICE_NAME
value: "mongodb-staging"
volumeClaimTemplates:
- metadata:
name: mongo-persistent-storage
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: fast-storage
resources:
requests:
storage: 1Gi
production
名称空间部署仅在以下方面有所不同:
--replSet
值(rs0
而不是rs1
)在两个部署中,其他所有内容都相同。
我唯一能想象的是,尽管位于单独的命名空间中,但无法在端口27017
上运行这两个部署。
我对导致上述failed to connect to server
错误的原因感到困惑。
完整的错误日志
Error in workloop { MongoError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (/opt/cvallance/mongo-k8s-sidecar/node_modules/mongodb-core/lib/topologies/server.js:336:35)
at Pool.emit (events.js:182:13)
at Connection.<anonymous> (/opt/cvallance/mongo-k8s-sidecar/node_modules/mongodb-core/lib/connection/pool.js:280:12)
at Object.onceWrapper (events.js:273:13)
at Connection.emit (events.js:182:13)
at Socket.<anonymous> (/opt/cvallance/mongo-k8s-sidecar/node_modules/mongodb-core/lib/connection/connection.js:189:49)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
name: 'MongoError',
message:
'failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]' }
答案 0 :(得分:1)
您似乎收到的错误是来自Pod中的mongo-sidecar容器。至于为什么mongo容器发生故障,您可以获得更多详细信息吗?可能是PVC失效了。
答案 1 :(得分:1)
问题似乎类似于: mongodb-error,但仍然有两个数据库在同一端口上监听。
在两个监听相同端口的mongoDB数据库的上下文中:
答案因所考虑的操作系统而异。一般而言:
但是,由于Linux Kernel 3.9和更高版本,使用SO_REUSEPORT选项添加了对侦听同一端口的多个应用程序的支持。可以在此lwn.net文章上获得更多信息。
但是有解决方法。
在其他端口上运行容器并设置Apache或Nginx。由于Apache / Nginx在端口80上运行,因此不会丢失任何流量,因为80是通用端口。
我推荐Nginx-我发现使用Nginx设置反向代理要容易得多,与Apache相比,它在资源上更轻便。 对于nginx,您需要进行设置并了解有关服务器块的更多信息: 如何在Ubuntu 16.04上安装Nginx。 如何在Ubuntu 16.04上设置Nginx服务器块(虚拟主机)。 在服务器块中,您需要使用proxy_pass,您可以在nginx网站上获取有关其的更多信息。