mongodb - Kubernetes 与 MongoDb

标签 mongodb kubernetes

我关注了https://www.mongodb.com/blog/post/running-mongodb-as-a-microservice-with-docker-and-kubernetes并尝试在 Google Cloud 帐户上使用 LoadBalancer Service 和 Replication Controller 运行单个 mongodb 实例。

以下是yaml文件:

---
apiVersion: v1
kind: Service
metadata:
  name: mongo-svc-a
  labels:
    name: mongo-svc-a
spec:
  type: LoadBalancer
  ports:
  - port: 27017
    targetPort: 27017
    protocol: TCP
    name: mongo-svc-a
  selector:
    name: mongo-node1
    instance: rod
---
apiVersion: v1
kind: ReplicationController
metadata:
  name: mongo-rc1
  labels:
    name: mongo-rc
spec:
  replicas: 1
  selector:
    name: mongo-node1
  template:
    metadata:
      labels:
        name: mongo-node1
        instance: rod
    spec:
      containers:
      - name: mongo-node1
        image: mongo
        ports:
        - containerPort: 27017
        volumeMounts:
        - name: mongo-persistent-storage1
          mountPath: /data/db
      volumes:
      - name: mongo-persistent-storage1
        gcePersistentDisk:
          pdName: mongodb-disk1-in-cluster1
          fsType: ext4

以下是创建的服务、复制 Controller 和 Pod 的详细信息。一切似乎都很好。
$ kubectl describe service mongo-svc-a 
Name:                   mongo-svc-a
Namespace:              default
Labels:                 name=mongo-svc-a
Selector:               instance=rod,name=mongo-node1
Type:                   LoadBalancer
IP:                     10.3.241.11
LoadBalancer Ingress:   104.198.236.2
Port:                   mongo-svc-a     27017/TCP
NodePort:               mongo-svc-a     31808/TCP
Endpoints:              10.0.0.3:27017
Session Affinity:       None
No events.


$ kubectl describe rc mongo-rc1
Name:           mongo-rc1
Namespace:      default
Image(s):       mongo
Selector:       name=mongo-node1
Labels:         name=mongo-rc
Replicas:       1 current / 1 desired
Pods Status:    1 Running / 0 Waiting / 0 Succeeded / 0 Failed
Volumes:
  mongo-persistent-storage1:
    Type:       GCEPersistentDisk (a Persistent Disk resource in Google Compute Engine)
    PDName:     mongodb-disk1-in-cluster1
    FSType:     ext4
    Partition:  0
    ReadOnly:   false
No events.


$ kubectl describe pod mongo-rc1-h0j8r
Name:           mongo-rc1-h0j8r
Namespace:      default
Node:           gke-cluster1-default-pool-d58b6c05-74fq/10.128.0.6
Start Time:     Sat, 11 Feb 2017 18:43:00 +0530
Labels:         instance=rod
                name=mongo-node1
Status:         Running
IP:             10.0.0.3
Controllers:    ReplicationController/mongo-rc1
Containers:
  mongo-node1:
    Container ID:       docker://9f28e482d3806b74f7f595c47e6c7940c2313e95860db13d137ad6eaa88bb341
    Image:              mongo
    Image ID:           docker://sha256:ad974e767ec4f06945b1e7ffdfc57bd10e06baf66cdaf5a003e0e6a36924e30b
    Port:               27017/TCP
    Requests:
      cpu:              100m
    State:              Running
      Started:          Sat, 11 Feb 2017 18:45:37 +0530
    Ready:              True
    Restart Count:      0
    Volume Mounts:
      /data/db from mongo-persistent-storage1 (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-3wfv3 (ro)
    Environment Variables:      <none>
Conditions:
  Type          Status
  Initialized   True 
  Ready         True 
  PodScheduled  True 
Volumes:
  mongo-persistent-storage1:
    Type:       GCEPersistentDisk (a Persistent Disk resource in Google Compute Engine)
    PDName:     mongodb-disk1-in-cluster1
    FSType:     ext4
    Partition:  0
    ReadOnly:   false
  default-token-3wfv3:
    Type:       Secret (a volume populated by a Secret)
    SecretName: default-token-3wfv3
QoS Class:      Burstable
Tolerations:    <none>
No events.

但是当我尝试远程连接时,mongo --hostname <Extenal_IP_of_the_service>到新创建的 monogdb 实例,我无法连接。我相信配置似乎没问题。任何帮助,将不胜感激。谢谢,
  • 帕万
  • 最佳答案

    尝试了不同的方法并成功了。

    遵循以下教程:https://kubernetes.io/docs/tutorials/stateful-application/run-stateful-application/

    暴露了 headless 服务:

    kubectl expose service your_service_name --port=27017 --target-port=27017 --name=mongo-lb-service --type=LoadBalancer
    

    关于mongodb - Kubernetes 与 MongoDb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42176516/

    相关文章:

    kubernetes - 将 GCloud 中 GKE 中的运行状况检查从 HTTP 更改为 TCP 后重置

    node.js - Mongoose findOne 当数据不存在时卡住

    php - 选择字段的 Symfony2 DataTransformer

    kubernetes - kubectl运行--command vs —参数

    docker - kubectl delete/create secret 禁止(谷歌云平台)

    Kubernetes 工作永无止境

    mongodb - 使用 FOSUserBundle 防止重复登录

    node.js - Mongoose 中 document.save() 后的人口数量错误

    php - 从使用 ISODate 保存的 mongodb 数据库获取时间戳

    kubernetes - 如何使用 Google Compute Engine Instances 设置 Tensorflow 集群来训练模型?