79
Infrastructure Deployment with

Infrastructure Deployment with Docker & Ansible

Embed Size (px)

Citation preview

Page 1: Infrastructure Deployment with Docker & Ansible

Infrastructure Deployment with

Page 2: Infrastructure Deployment with Docker & Ansible

Who I am?

• Robert Reiz

• Software Developer

• I started VersionEye

• Software Dev since 1998

Page 3: Infrastructure Deployment with Docker & Ansible

Agenda

❖ Intro to Docker

❖ Demo

❖ Intro to Ansible

❖ Demo

Page 4: Infrastructure Deployment with Docker & Ansible

Shipment without

Containers

Page 5: Infrastructure Deployment with Docker & Ansible

1956 Malcom McLean introduced the 40’Container - ISO 668.

> 15 Million inst.

2/3 of global trade run over 40’Containers!

Page 6: Infrastructure Deployment with Docker & Ansible

The Logistic Problem

Page 7: Infrastructure Deployment with Docker & Ansible
Page 8: Infrastructure Deployment with Docker & Ansible
Page 9: Infrastructure Deployment with Docker & Ansible

Same Problem in Software Dev.

Page 10: Infrastructure Deployment with Docker & Ansible

Java ? ? ?

Ruby ? ? ?

Node.JS ? ? ?

MySQL ? ? ?

Dev-Env. Test-Env. Prod-Env.

Page 11: Infrastructure Deployment with Docker & Ansible

Java JKD 1.8.14 - Win32 JKD 1.8.1 - Lnx-64 JDK 1.7-patch UNX

Ruby 2.2.2 rvm 2.2.1 nat MRI 2.1.0 rubinius

Node.JS 4.0 win 4.0 Linux 4.0 Linux

MySQL 5.5 win 5.0 Linux 5.0 Linux

Dev-Env. Test-Env. Prod-Env.

Page 12: Infrastructure Deployment with Docker & Ansible

Java

Ruby

Node.JS

MySQL

Dev-Env. Test-Env. Prod-Env.

Page 13: Infrastructure Deployment with Docker & Ansible

What is Docker?

Page 14: Infrastructure Deployment with Docker & Ansible

What is Docker?

❖ Open Source Project started in March 2013

❖ From the makers of dotCloud (PaaS).

❖ Received $162 Million Funding.

❖ Community grows rapidly!

Page 15: Infrastructure Deployment with Docker & Ansible

What is Docker?

❖ Tiny VM (25 MB)

❖ Linux based - LXC Interface / libcontainer

❖ Own Namespaces and Cgroups!

❖ Shared resources with host system.

❖ Changes changed in Layers. Similar to Git!

❖ Originally not for Windows & Mac ! But …-> https://docs.docker.com/installation/windows/ -> https://blog.docker.com/2016/03/docker-for-mac-windows-beta/

Page 16: Infrastructure Deployment with Docker & Ansible

Build - Ship - Run

Docker-Hub

Build RUN

RUN

RUN

docker pushdocker pull

Server Farm Production

Page 17: Infrastructure Deployment with Docker & Ansible

Build

Page 18: Infrastructure Deployment with Docker & Ansible

DockerfileFROM ubuntu:14.10 MAINTAINER Robert Reiz <[email protected]>

ENV LANG en_US.UTF-8

RUN apt-get update RUN apt-get install -y --force-yes -q nginx

ADD nginx.conf /etc/nginx/nginx.conf

CMD nginx

EXPOSE 80

Page 19: Infrastructure Deployment with Docker & Ansible

Build - Dockerfile

> docker build -t reiz/nginx:1.0.0 .

docker image => reiz/nginx:1.0.0

Page 20: Infrastructure Deployment with Docker & Ansible

Ship

Page 21: Infrastructure Deployment with Docker & Ansible

Ship Docker Image

> docker push reiz/nginx:1.0.0

Page 22: Infrastructure Deployment with Docker & Ansible

Run

Page 23: Infrastructure Deployment with Docker & Ansible

Fetch a Docker Image

> docker pull reiz/nginx:1.0.0

Download docker image reiz/nginx:1.0.0 from Docker Hub to local Docker repository.

Page 24: Infrastructure Deployment with Docker & Ansible

Run a Docker Container

> docker run reiz/nginx:1.0.0

Creates a Docker container out of the Docker image reiz/nginx:1.0.0. It runs the nginx process.

Page 25: Infrastructure Deployment with Docker & Ansible

More Commands

> docker stop <container_id>> docker start <container_id>> docker top <container_id>> docker logs <container_id>> docker rm <container_id>

Page 26: Infrastructure Deployment with Docker & Ansible

Important

❖ A Docker Container doesn’t store state!

❖ You can not ssh into a Docker Container!

❖ A container is supposed to run 1 process!

Page 27: Infrastructure Deployment with Docker & Ansible

Shell

Page 28: Infrastructure Deployment with Docker & Ansible

Get a Shell

> docker run -it reiz/mongodb:3.2.0 /bin/bash

Starts a new Docker container with an active shell.

Page 29: Infrastructure Deployment with Docker & Ansible

Volumes

Page 30: Infrastructure Deployment with Docker & Ansible

Mount a Volume

> docker run -v /mnt/mongodb:/data -d reiz/mongodb:3.2.0

Mounts “/mnt/mongodb” directory into the Docker container as “/data”. Keep the data on the host. That’s how you keep data persisted.

Page 31: Infrastructure Deployment with Docker & Ansible

Environment Variables

Page 32: Infrastructure Deployment with Docker & Ansible

Set environment variables

> docker run --env LANG=en_US.UTF-8 -d reiz/mongodb:3.2.0

You can overwrite ENV variables from the Dockerfile here and also define completely new ones.

Page 33: Infrastructure Deployment with Docker & Ansible

Links

Page 34: Infrastructure Deployment with Docker & Ansible

Link Docker Containers

> docker run —name mongodb -d versioneye/mongodb:1.0.2 > docker run —link mongodb:mongo versioneye/api:1.0.0

MONGO_PORT=tcp://172.1.10.1:27017 MONGO_PORT_27017_TCP=tcp://172.1.10.1:27017 MONGO_PORT_27017_TCP_ADDR=172.1.10.1 MONGO_PORT_27017_TCP_PORT=27017 MONGO_PORT_27017_TCP_PROTO=tcp

Environment variables are injected in 2nd container:

Page 35: Infrastructure Deployment with Docker & Ansible

Link Docker Containers

Linking only works on same hosts!

Page 36: Infrastructure Deployment with Docker & Ansible

Docker Compose

api: image: versioneye/rails_api:2.5.7 ports: - "9090:9090" container_name: "api" links: - mongodb:db - elasticsearch:es

mongodb: image: reiz/mongodb:2.6.6_2 container_name: “mongodb"

elasticsearch: image: reiz/elasticsearch:0.9.1 container_name: "elasticsearch"

docker-compose.yml describes a whole

set of Docker containers

Page 37: Infrastructure Deployment with Docker & Ansible

Docker Compose

> docker-compose up -d

> docker-compose ps

> docker-compose stop

Page 38: Infrastructure Deployment with Docker & Ansible

Docker Compose

> docker-compose build api > docker-compose up --no-deps -d api

Updating a single container, not ALL of them.

Page 39: Infrastructure Deployment with Docker & Ansible

Docker Compose

> docker-compose scale worker=3

Scaling up containers

Page 40: Infrastructure Deployment with Docker & Ansible

DEMO

Page 41: Infrastructure Deployment with Docker & Ansible

Service Discovery

Page 42: Infrastructure Deployment with Docker & Ansible

Service Discovery

❖ Environment Variables

❖ Mount configuration via volumes

❖ Linking

❖ Use a service like: etcd, zookeeper etc…

Page 43: Infrastructure Deployment with Docker & Ansible

Orchestration

Page 44: Infrastructure Deployment with Docker & Ansible

Docker Orchestration

❖ CM-Tools (Chef, Puppet, Ansible, Salt)

❖ http://kubernetes.io/

❖ https://coreos.com/

❖ https://docs.docker.com/swarm/

❖ https://www.openshift.com/

Page 45: Infrastructure Deployment with Docker & Ansible
Page 46: Infrastructure Deployment with Docker & Ansible

2012 VersionEye is running on Heroku.

Page 47: Infrastructure Deployment with Docker & Ansible

2013 VersionEye moves to AWS because of the Amazon Activate Program!

Page 48: Infrastructure Deployment with Docker & Ansible

WWW

API

APP x 3

RabbitMQ

Tasks

MongoDB MongoDB MongoDB

Elastic Search

MemcachedCrawlers

x N

VersionEye Infrastructure

Page 49: Infrastructure Deployment with Docker & Ansible

Handcrafted Servers are

❖ hard to maintain ❖ very time/cost intensive ❖ setup is not easily reproducible ❖ many times very buggy

Page 50: Infrastructure Deployment with Docker & Ansible

Reasons for Ansible

❖ No Master ❖ No Agents ❖ Configuration in Yaml ❖ Very easy to learn

Page 51: Infrastructure Deployment with Docker & Ansible

Server

Server

Server

Server

You SSH

Ansible works via SSH. No Master Server! No Agent on the Server is required.

Page 52: Infrastructure Deployment with Docker & Ansible

Installation

Page 53: Infrastructure Deployment with Docker & Ansible

sudo pip install ansible

Page 54: Infrastructure Deployment with Docker & Ansible

brew update brew install ansible

Page 55: Infrastructure Deployment with Docker & Ansible

Ansible Concepts

❖ Inventory ❖ Playbooks ❖ Roles ❖ Tasks / Handlers / Vars ❖ Modules

Page 56: Infrastructure Deployment with Docker & Ansible

Inventory

Page 57: Infrastructure Deployment with Docker & Ansible

Inventory

[mongo_master] 168.197.1.14

[mongo_slaves] 168.197.1.15 168.197.1.16 168.197.1.17

[www] 168.197.1.2

Inventory files are simple text files which describe your servers.

IP Addresses or DNS Names grouped by names.

Page 58: Infrastructure Deployment with Docker & Ansible

Inventory

[mongo_master] 168.197.1.14

[mongo_slaves] mongo1.server mongo2.server mongo3.server

[www] 168.197.1.2

List of target hosts. Usually located in

/etc/ansible/hosts

Page 59: Infrastructure Deployment with Docker & Ansible

Inventory

[mongo_master] mongo-[a:c]-server

[mongo_slaves] mongo[1:3].server

[www] {{my_little_webserver}}

Inventory files can take advantage of variables and enumerations

Page 60: Infrastructure Deployment with Docker & Ansible

Playbooks

Page 61: Infrastructure Deployment with Docker & Ansible

Simple Playbook--- - hosts: dev_servers user: ubuntu sudo: true roles: - java - memcached

- hosts: www_servers user: ubuntu sudo: true roles: - java

group name from the inventory file

server auth

Role which should be installed on the server

Page 62: Infrastructure Deployment with Docker & Ansible

Roles / Modules

Page 63: Infrastructure Deployment with Docker & Ansible

simpple role with apt module

--- - name: update debian packages apt: update_cache=true

- name: install Java JDK apt: name=openjdk-7-jdk state=present

Page 64: Infrastructure Deployment with Docker & Ansible

apt module

--- - name: update debian packages apt: update_cache=true

- name: install Java JDK apt: name=openjdk-7-jdk state=present

Documentation of this step!

Module

Parameters of the Module

Page 65: Infrastructure Deployment with Docker & Ansible

--- - name: update debian packages apt: update_cache=true

- name: upgrade packages apt: upgrade=full

- name: ensure that basic packages are installed apt: name={{ item }} state=present with_items: - tree - wget - links2 - gcc - g++ - make - autoconf - automake - libssl-dev - libcurl4-openssl-dev

Page 66: Infrastructure Deployment with Docker & Ansible

Thousands of

Modules

Page 67: Infrastructure Deployment with Docker & Ansible

shell module

--- - name: do what you want shell: /opt/I_can_do_what_I_want.sh

Page 68: Infrastructure Deployment with Docker & Ansible

A role directory

Page 69: Infrastructure Deployment with Docker & Ansible

Variables

--- app_dir: /var/www/versioneye

tomcat/vars/main.yml

--- - name: create versioneye directory command: mkdir -p {{ app_dir }}

tomcat/tasks/main.yml

Page 70: Infrastructure Deployment with Docker & Ansible

Handlers

--- - name: restart mongodb service: name=mongod state=restarted

mongo/handlers/main.yml

- name: copy MongoDB configuration to the server copy: src=mongodb.conf dest=/etc/mongodb.conf notify: restart mongodb

mongo/tasks/main.yml

Page 71: Infrastructure Deployment with Docker & Ansible

Files

mongo/files/mongodb.list

- name: add MongoDB debian server to the list of servers copy: src=mongodb.list dest=/etc/apt/sources.list.d/mongodb.list

mongo/tasks/main.yml

Page 72: Infrastructure Deployment with Docker & Ansible

Real World Use case

Page 73: Infrastructure Deployment with Docker & Ansible

Loadbalancer

APP

APP

APP

Seamless Web App Deployment @ VersionEye

https://gist.github.com/reiz/238e70683bbfbc10bf4c

Page 74: Infrastructure Deployment with Docker & Ansible

Loadbalancer

APP

APP

APP

Docker Hub

1. Build new Docker Image

2. push

3. run

Seamless Web App Deployment @ VersionEye

https://gist.github.com/reiz/238e70683bbfbc10bf4c

Page 75: Infrastructure Deployment with Docker & Ansible

Loadbalancer

APP

APP

APP

Docker Hub

1. pull

2. run

Seamless Web App Deployment @ VersionEye

https://gist.github.com/reiz/238e70683bbfbc10bf4c

Page 76: Infrastructure Deployment with Docker & Ansible

Loadbalancer

APP

APP

APP

Seamless Web App Deployment @ VersionEye

Docker Hub

1. pull

2. run

https://gist.github.com/reiz/238e70683bbfbc10bf4c

Page 77: Infrastructure Deployment with Docker & Ansible

Loadbalancer

APP

APP

APP

Seamless Web App Deployment @ VersionEye

https://gist.github.com/reiz/238e70683bbfbc10bf4c

Page 78: Infrastructure Deployment with Docker & Ansible

Demo

Page 79: Infrastructure Deployment with Docker & Ansible

? ? ? @RobertReiz