Top Banner
Scaling MongoDB with Docker and cgroups Marco Bonezzi Technical Services Engineer, MongoDB [email protected] @marcobonezzi
34

MongoDB World 2016: Scaling MongoDB with Docker and cGroups

Jan 26, 2017

Download

Technology

MongoDB
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

Scaling MongoDB with Docker and cgroups

Marco Bonezzi

Technical Services Engineer, MongoDB

[email protected]

@marcobonezzi

Page 2: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

About the speaker I am Marco Bonezzi, TSE at MongoDB

TSE = helping customers with a variety of issues

Based in Dublin, Ireland Experience in distributed systems, high availability:

Page 3: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

How many of you have ever... 1.  … manually deployed a MongoDB replica sets or sharded clusters?

2.  ... had issues with resource allocation?

3.  ... used Docker?

4.  … used MongoDB running on Docker?

Page 4: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

We know how it feels…

Different architectures in

Development and Production

Co-located MongoDB processes

Production != docker run mongodb

Page 5: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Whatarethepainpoints?Deployment

Complex architectures and deployment patterns Resource Contention

Multiple instances competing for resources MongoDB & Docker

Combining both: network configuration, container location, other

Page 6: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Howtosolvethis?Deployment

Using predefined cluster patterns Replicating environments

Resource Control Setting limits to key resources

MongoDB & Docker Create once, deploy everywhere Deploy patterns, not processes

Orchestration

Resource Management

Automate for scaling

Page 7: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

AboutthistalkPatterns for successful deployments

Difference between success and failure Orchestrating MongoDB with Docker

MongoDB cluster on AWS with containers Patterns with Swarm and Compose

Managing container resources with cgroups Benefits of cgroups in a MongoDB cluster

PS

S

Page 8: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Redundancy and fault tolerance Deploy an odd number of voting members Members ó Majority required ó Fault tolerance

High availability and resource colocation Single member of a replica set / server

Shards as Replica Set

Ideally: primary / secondary / secondary

Deployment patterns: Replica Set and Sharded Clusters

Server 3

Server 2

Server 1

mongos

Primary

Primary RS1

Secondary Secondary

Secondary RS2

Secondary RS3

Secondary RS1

Primary RS2

Secondary RS3

Secondary RS1

Secondary RS2

Primary RS3

mongos mongos

cfgsvr1 cfgsvr2 cfgsvr3

Page 9: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Docker •  Noun: a person employed in a port to load and unload ships (from “what

is docker” on Google)

Containers: Isolated process in userspace Application + dependencies Shared kernel and libraries Can run on any infrastructure (or cloud)

www.docker.com

Page 10: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

44% of orgs adopting

microservices

Why use Docker?

41% want application

portability

13x improvement in

release frequency

62% MTTR on software

issues

60% Using Docker to migrate to cloud

Reason to run containers:

SPEED Microservices architectures Efficiency Cloud

(TheDockerSurvey,2016)

Page 11: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Goodnews:Dockercanhelpwiththis!

Orchestration Coordinate MongoDB containers to deploy a recommended deployment pattern

Resource Control Sizing each instance (and each cluster) to avoid resource contention issues

Page 12: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Orchestrating MongoDB with Docker How can we use Docker for MongoDB deployments?

How can we deploy these patterns using Docker containers? Why should we use Docker?

Our recipe:

Page 13: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Dockerecosystem

Provisioning and managing your Dockerized hosts Native clustering: turns a pool of Docker hosts into a single, virtual Docker host.

Define a multi-container application with all of its dependencies in a single file

S

Page 14: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

WhyDockerSwarm?

5x faster than Kubernetes to spin up

a new container

7x faster than Kubernetes to list all

running containers

Evaluating Container Platforms at Scale 1000 EC2 instances in a cluster What is their performance at scale? Can they operate at scale? What does it take to support them at scale?

https://medium.com/on-docker/evaluating-container-platforms-at-scale-5e7b44d93f2c#.k2fxds8c2

hGps://www.docker.com/survey-2016

Page 15: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

swarm-node-2

swarm-node-3

swarm-node-1

Swarm multi-host networking How each mongod process connects to other processes outside of the host? •  Swarm overlay container-to-container network •  Using the hostname defined in the Compose file

Page 16: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Swarm filters to build our patterns Constraint filters

Mark each mongod container with a label:

“role=mongod” “replset=rs1”

Page 17: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Affinity filters Prevent multiple RS members on the same host:

"affinity:replset!=rs1”

swarm-node-1

swarm-node-3

swarm-node-2

Affinity filters for container distribution

Page 18: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Road to container success Deploying containers to the right node is not enough…

Next step: Resource control on each swarm cluster node using cgroups

Maritime New Zealand

Page 19: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Resource control with cgroups and Docker Simple parameters to add to docker run or compose:

--cpu-shares

--cpuset-cpus

--memory

--blkio-weight --net

Page 20: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

MongoDB Memory usage in 3.2 with WiredTiger

MongoDB Memory: mongod process: connections, aggregations, mapReduce, etc WiredTiger cache: (0.6 x total memory) – 1 GB

Total = mem(mongod) + mem(WiredTiger cache)

WiredTiger cache

mongod

mongod memory

Page 21: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

cgroup!memory_limit!

Process memory with containers and cgroups

WiredTiger cache

mongod

mongod memory

total memory (seen from mongod process) !

Inside the container •  Can see total memory and not

memory limit WiredTiger cache: •  memory_limit *0.6

Page 22: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

cgroups: Memory and CPU limits

mongod! mongod! mongod! mongod!

MEM = (TOTAL_MEM - OS_MEM) / NUM_MONGOD WiredTiger cache = (MEM * 0.6) CPUSET= 0, 1, …, MAX_CPU_CORES

mongod!

mongod!

mongod!

mongod!

Page 23: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

MongoDB on Docker + cgroups: Memory

WiredTiger cache: 60% of the container memory limit (for each mongod)

Compose: mem_limit!Docker Engine: --memory !

WiredTiger cache !

!!!!!!

mongod memory !

WiredTiger cache !

!!!!!

mongod memory !

WiredTiger cache !

!!!!!!

mongod memory !

WiredTiger cache !

!!!!!!

mongod memory !

OS Memory !

Page 24: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

MongoDB on Docker + cgroups: CPU

mongod (and mongos) mapped to 1+ core Compose: cpuset!

Docker Engine: --cpuset-cpus!

mongod!rs1a !

mongod!rs1a !

mongod!rs2b !

mongod!rs2b !

core0 ! core1 ! core2 ! core3 !

OS ! OS !mongod cfgsrv

-c !mongos !

core4 ! core5 ! core6 ! core7 !

--cpuset-mems (NUMA) !

Page 25: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Understanding resource usage:

•  docker top rs1a !

•  docker stats rs1a !

Container stats available via Docker remote API:

GET /containers/(id)/stats Also available from docker-py:

http://docker-py.readthedocs.org/en/latest/api/#stats

Resource usage with Docker

Page 26: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Resource usage with Docker

Multiple statistics for each container: Memory limit and usage, CPU (per core level), Network, Disk

Useful to combine with MongoDB metrics (like db.serverStatus())

Page 27: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Creating a Swarm cluster on AWS to deploy MongoDB

Page 28: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Creating a Swarm cluster on AWS to deploy MongoDB

DEMO!

Configure docker-machine with ec2 driver (AWS)

Deploy discovery service for Swarm Master

Deploy AWS instances for: •  Swarm master •  Swarm worker nodes

Connect to the Swarm master

Define compose file for deployment

Define Swarm filters and constraints and cgroup

limits

Deploy the environment with a single command using the compose file

Configure our MongoDB sharded cluster using

Cloud Manager API

Page 29: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

What our dockerized sharded cluster looks like…

rs1a !

rs1c !rs1b ! rs2b !

rs2a !

rs2c ! rs3b !

rs3a !

rs3c !

mongos !

cfgsvr-a !

cfgsvr-b !

cfgsvr-c !

dockerC1 ! dockerC2 ! dockerC3 !

PRIMARY !SECONDARY !MONGOS !

Page 30: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

Summary

Page 31: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Advantages of using MongoDB with Docker

Speed: testing and deploying cluster patterns easily Build once, deploy everywhere

Control: Resource control and utilization

Key to success with containers

Agility: Microservices architectures Making change less expensive

Flexibility: Multi vendor cloud opportunities

AWS, Azure, Google, IBM, CloudFoundry

PS

S

Page 32: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

How successful customers use MongoDB with Docker

•  Case Studies @hGps://www.mongodb.com/blog

•  Whitepaper:

“Enabling Microservices – Containers & Orchestration Explained” https://www.mongodb.com/collateral/microservices-containers-and-orchestration-explained

Page 33: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

#MDBW16

Now it’s YOUR turn

Share with us your use case of MongoDB & Docker: http://bit.do/DockerMongoDB

@marcobonezzi

You can actually try this at home: https://github.com/sisteming/mongo-swarm

Page 34: MongoDB World 2016: Scaling MongoDB with Docker and cGroups

Thank You!

Marco Bonezzi [email protected]

@marcobonezzi