Public | Automated Build

Last pushed: 3 months ago
Short Description
the origin of this registry is https://hub.docker.com/r/tarosky/k8s-mongo-ha/ . This is clone .
Full Description

Kubernetes MongoDB with High Availability

Sample of Kubernetes MongoDB and a utility Image implemented using StatefulSet.

Requirements

  • Kubernetes 1.6.* cluster
  • MongoDB 3.4

Quick Start

If you already have a Kubernetes cluster, you can deploy High Availability MongoDB using the following command:

$ kubectl create -f example/
pod "console" created
service "mongo" created
deployment "mongo-supervisor" created
statefulset "mongo" created

Accessing MongoDB

You can access MongoDB using console pod:

$ kubectl exec -ti console -- /bin/bash
root@console:# export TARGET='mongo-0.mongo.default.svc.cluster.local:27017'
root@console:# mongo "${TARGET}" --quiet --eval 'db.test_col.insert({"foo": "bar"})'
WriteResult({ "nInserted" : 1 })
root@console:/# mongo $TARGET --quiet --eval 'db.test_col.find({"foo": "bar"})'  
{ "_id" : ObjectId("59210a0dc566ec973e149251"), "foo" : "bar" }
root@console:/# mongo 'mongo-1.mongo.oshita.svc.cluster.local:27017' --quiet --eval 'rs.slaveOk();db.test_col.find({"foo": "bar"})' # access slave 
{ "_id" : ObjectId("59210a0dc566ec973e149251"), "foo" : "bar" }
root@console:/# mongo $TARGET --quiet --eval 'db.test_col.remove({"foo": "bar"})'
WriteResult({ "nRemoved" : 1 })

Scale Up and Down

With tarosky/k8s-mongo-ha, you can scale up/down MongoDB like the normal Deployment resources:

$ kubectl scale --replicas=5 statefulset/redis-mongo
statefulset "mongo" scaled

Sample Code in Python

$ kubectl exec -ti console -- ipython
In [1]: from redis import StrictRedis

In [2]: from redis.sentinel import Sentinel

In [3]: sentinel = Sentinel([
   ...:         ('redis-sentinel-0.redis-sentinel.default.svc.cluster.local', 26379),
   ...:         ('redis-sentinel-1.redis-sentinel.default.svc.cluster.local', 26379),
   ...:         ('redis-sentinel-2.redis-sentinel.default.svc.cluster.local', 26379)
   ...:     ], socket_timeout=0.1
   ...: )

In [4]: master = sentinel.master_for(
   ...:     'mymaster',
   ...:     password='_redis-server._tcp.redis-server.default.svc.cluster.local',
   ...:     socket_timeout=0.1
   ...: )

In [5]: slave = sentinel.slave_for(
   ...:     'mymaster',
   ...:     password='_redis-server._tcp.redis-server.default.svc.cluster.local',
   ...:     socket_timeout=0.1
   ...: )

In [6]: master.set('foo', 'bar')
Out[6]: True

In [7]: slave.get('foo')
Out[7]: b'bar'

Running the Test Script

$ pyvenv .venv
$ source .venv/bin/activate
$ pip install -r test/requirements.txt

You can run the test command using the following command:

$ KUBE_NAMESPACE='{{Your name space}}' nosetests test/test.py
Docker Pull Command
Owner
ilil
Source Repository

Comments (0)