运行 mongodb inside kubernetes - azure aks 失败
run mongodb inside kubernetes - azure aks fails
我尝试 运行 在托管在 azure aks 上的 kubernetes 集群中 mongodb
我没能得到它 运行ning,按照这个教程:https://kubernetes.io/blog/2017/01/running-mongodb-on-kubernetes-with-statefulsets/
这是我使用的 yaml:
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: default-view
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: view
subjects:
- kind: ServiceAccount
name: default
namespace: default
---
apiVersion: v1
kind: Service
metadata:
name: mongo
labels:
name: mongo
spec:
ports:
- port: 27017
targetPort: 27017
clusterIP: None
selector:
role: mongo
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: mongo
spec:
serviceName: "mongo"
replicas: 2
template:
metadata:
labels:
role: mongo
environment: test
spec:
terminationGracePeriodSeconds: 10
containers:
- name: mongo
image: mongo
command:
- mongod
- "--replSet"
- rs0
- "--bind_ip"
- 0.0.0.0
- "--smallfiles"
- "--noprealloc"
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=mongo,environment=test"
volumeClaimTemplates:
- metadata:
name: mongo-persistent-storage
annotations:
volume.beta.kubernetes.io/storage-class: "managed-premium"
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 32Gi
我使用的连接字符串是:
"mongodb://mongo-0.mongo,mongo-1.mongo:27017/databasename\_?"
从我的 JS 应用程序中我得到:
database names cannot contain the character '\'
如何从 JS 应用程序连接到 mongodb?
我能够使用此连接字符串解决问题:
mongodb://mongo-0.mongo,mongo-1.mongo:27017/chronas-api_?
但是有人可以向我解释这个连接字符串魔术以及我如何使用 "Robo 3T" 连接到这个集群吗?
mongodb://mongo-0.mongo,mongo-1.mongo:27017
您表示您有一个包含 2 个成员的副本集,并且都使用端口 27017。您的 mongodb 库将处理 url 以连接到集群。
为了在本地连接,您必须进行端口转发:
kubectl port-forward mongo-0 27017 # or mongo-1
然后您可以使用本地主机 (127.0.0.1) 和端口 27017 与 Robo 3T 连接到选定的 mongodb。
我尝试 运行 在托管在 azure aks 上的 kubernetes 集群中 mongodb
我没能得到它 运行ning,按照这个教程:https://kubernetes.io/blog/2017/01/running-mongodb-on-kubernetes-with-statefulsets/
这是我使用的 yaml:
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: default-view
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: view
subjects:
- kind: ServiceAccount
name: default
namespace: default
---
apiVersion: v1
kind: Service
metadata:
name: mongo
labels:
name: mongo
spec:
ports:
- port: 27017
targetPort: 27017
clusterIP: None
selector:
role: mongo
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: mongo
spec:
serviceName: "mongo"
replicas: 2
template:
metadata:
labels:
role: mongo
environment: test
spec:
terminationGracePeriodSeconds: 10
containers:
- name: mongo
image: mongo
command:
- mongod
- "--replSet"
- rs0
- "--bind_ip"
- 0.0.0.0
- "--smallfiles"
- "--noprealloc"
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=mongo,environment=test"
volumeClaimTemplates:
- metadata:
name: mongo-persistent-storage
annotations:
volume.beta.kubernetes.io/storage-class: "managed-premium"
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 32Gi
我使用的连接字符串是:
"mongodb://mongo-0.mongo,mongo-1.mongo:27017/databasename\_?"
从我的 JS 应用程序中我得到:
database names cannot contain the character '\'
如何从 JS 应用程序连接到 mongodb?
我能够使用此连接字符串解决问题:
mongodb://mongo-0.mongo,mongo-1.mongo:27017/chronas-api_?
但是有人可以向我解释这个连接字符串魔术以及我如何使用 "Robo 3T" 连接到这个集群吗?
mongodb://mongo-0.mongo,mongo-1.mongo:27017
您表示您有一个包含 2 个成员的副本集,并且都使用端口 27017。您的 mongodb 库将处理 url 以连接到集群。
为了在本地连接,您必须进行端口转发:
kubectl port-forward mongo-0 27017 # or mongo-1
然后您可以使用本地主机 (127.0.0.1) 和端口 27017 与 Robo 3T 连接到选定的 mongodb。