from docker to production - sunshinephp 2017

Post on 09-Feb-2017

52 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

SunshinePHP 2017 1

From Docker to ProductionChris Tankersley@dragonmantankSunshinePHP 2017

SunshinePHP 2017 2

SunshinePHP 2017 3

Subliminal Advertisement

SunshinePHP 2017 4

SunshinePHP 2017 5

SunshinePHP 2017 6

SunshinePHP 2017 7

SunshinePHP 2017 8

SunshinePHP 2017 9

SunshinePHP 2017 10

Considerations

SunshinePHP 2017 11

How does your app handle storage?

SunshinePHP 2017 12

What does your build process look like?

SunshinePHP 2017 13

How well does your app handle scaling?

SunshinePHP 2017 14

Does it make sense?

SunshinePHP 2017 15

The Build Process

SunshinePHP 2017 16

Start Small• Build your application• Run composer• Run npm/bower• Build JS/CSS

• Use the compiled output to build an image with docker build• Push full image to private registry

SunshinePHP 2017 17

docker build• Additional options to look at• -f, --file – Specify a different filename for the Dockerfile• --no-cache – Don’t use a cached layer• --pull – Always pull a new version of the image

SunshinePHP 2017 18

Sample usagedocker build \ --no-cache \ –f docker/php/phpserver.dockerfile \ –t prod_php /opt/builds/20161010

SunshinePHP 2017 19

phpserver.dockerfileFROM php:fpmRUN docker-php-ext-install pdo pdo_mysqlCOPY ./ /var/www

SunshinePHP 2017 20

Docker Compose

SunshinePHP 2017 21

Very Good for Small Deployments• Can be used to augment your dev environment• Works well with Docker Machine

SunshinePHP 2017 22

Create a machinedocker-machine create --driver digitalocean \ --digital-ocean-access-token [token] \ sunshinephp2017

SunshinePHP 2017 23

SunshinePHP 2017 24

Switch to the remote node• Run docker-machine env sunshinephp2017

& "C:\Program Files\Docker\Docker\Resources\bin\docker-machine.exe" env sunshinephp2017 | Invoke-Expression

SunshinePHP 2017 25

Set up docker-compose• Docker Compose allows multiple config files with -f• Have a base docker-compose.yml for Production• Add a secondary one for Development

SunshinePHP 2017 26

version: '2'volumes: mysqldata: driver: localservices: nginx: build: context: ./ dockerfile: ./nginx.dockerfile ports: - 80:80 - 443:443 phpserver: build: context: ./ dockerfile: ./phpserver.dockerfile working_dir: /var/www/public mysqlserver: image: mysql environment: [redacted] volumes: - mysqldata:/var/lib/mysql

docker-compose.yml

SunshinePHP 2017 27

version: '2'volumes: mysqldata: driver: localservices: nginx: image: nginx volumes: - ./output_dev:/var/www/public:ro - ./app/nginx/default.conf:/etc/nginx/conf.d/default.conf - ./ssl:/etc/nginx/ssl/ phpserver: build: context: ./ dockerfile: ./phpserver.dockerfile working_dir: /var/www/public volumes: - ./app:/var/www/ - ./vendor:/var/www/vendor mysqlserver: image: mysql environment: [redacted] volumes: - mysqldata:/var/lib/mysql

docker-compose.dev.yml

SunshinePHP 2017 28

When doing developmentdocker-compose \ –f docker-compose.yml \ –f docker-compose.dev.yml \ up -d

SunshinePHP 2017 29

When doing a deploymentdocker-compose up -d

SunshinePHP 2017 30

Other Alternative – Variable Substitution• Docker Compose allows variable substitution inside the file• Wrap variables in ${}• image: ${DEPLOY_VERSION}_php

SunshinePHP 2017 31

When doing a deploymentdocker build –f […] –t 20161010_php /opt/builds/20161010DEPLOY_VERSION=20161010 docker-compose up -d

SunshinePHP 2017 32

Docker Swarm

SunshinePHP 2017 33

What is Swarm?• Docker-supplied clustering• Define a series of services that can be deployed

SunshinePHP 2017 34

Setup

Manager

Node 2Node 1

SunshinePHP 2017 35

Register a node as a Managerdocker swarm init -- advertise- addr 172.16.0.245

SunshinePHP 2017 36

Add nodesdocker swarm join --token [token] 172.16.0.245:2377

SunshinePHP 2017 37

docker-compose.ymlversion: '3'

services: phpserver: image: php:apache ports: - 80:80

SunshinePHP 2017 38

Deploy the stack$ docker stack deploy -c docker-compose.yml myappCreating network myapp_defaultCreating service myapp_phpserver

SunshinePHP 2017 39

docker-compose.ymlversion: '3'

services: phpserver: image: nginx ports: - 80:80

SunshinePHP 2017 40

Deploy the stack$ docker stack deploy -c docker-compose.yml myappUpdating service myapp_phpserver (id: sy8qtegme06g8wqgl0w9707fr)

SunshinePHP 2017 41

docker-compose.ymlversion: '3'

services: phpserver: image: nginx ports: - 80:80 deploy: replicas: 5

SunshinePHP 2017 42

docker-compose.ymlversion: '3'

services: phpserver: image: nginx ports: - 80:80 deploy: replicas: 5

SunshinePHP 2017 43

docker stack ps myappID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS

72mud5othsjf myapp_phpserver.1 nginx:latest node2 Running Running 8 minutes ago

wsf3m32u9vcr \_ myapp_phpserver.1 php:apache manager1 Shutdown Shutdown 11 minutes ago

xf3wyh289bec myapp_phpserver.2 nginx:latest node1 Running Preparing 20 seconds ago

fehf1vdx4m0r myapp_phpserver.3 nginx:latest manager1 Running Preparing 20 seconds ago

pwnq65e6w7ew myapp_phpserver.4 nginx:latest manager1 Running Preparing 20 seconds ago

roxtanjughq8 myapp_phpserver.5 nginx:latest node2 Running Running 20 seconds ago

SunshinePHP 2017 44

Rancher

SunshinePHP 2017 45

Rancher and RancherOS

SunshinePHP 2017 46

Manages your Containers

SunshinePHP 2017 47

Manages your Hosts

SunshinePHP 2017 48

Allows you to monitor containers

SunshinePHP 2017 49

Allows you to manage your applications

SunshinePHP 2017 50

Allows you to deploy your applications

SunshinePHP 2017 51

Allows you to deploy your applications

SunshinePHP 2017 52

Supports Docker Compose

SunshinePHP 2017 53

Blue-Green Deployments

Router

App v1 App v2

SunshinePHP 2017 54

Blue-Green Deployments

Router

App v3 App v2

SunshinePHP 2017 55

Blue-Green Deployments

Router

App v3 App v2

SunshinePHP 2017 56

Add rancher/server to the master

docker run -d \ --restart=always \ -p 8080:8080 \ –name=rancher \ rancher/server

SunshinePHP 2017 57

Add rancher/agent to nodes

docker run -d \ --privileged \ -v /var/run/docker.sock:/var/run/docker.sock \ rancher/agent:v0.7.9 \ http://192.168.99.100:8080/v1/scripts/[hash]

SunshinePHP 2017 58

All Done!

SunshinePHP 2017 59

Deploying with Rancher CLI

SunshinePHP 2017 60

What is it?• Rancher has an API!• Small executable that interacts with Rancher API• Kind of like a custom docker-compose

SunshinePHP 2017 61

Get an API Key

SunshinePHP 2017 62

Get an API Key

SunshinePHP 2017 63

Download Rancher CLI

SunshinePHP 2017 64

Export Stack Config

SunshinePHP 2017 65

Two Config Files

SunshinePHP 2017 66

docker-compose.yml

SunshinePHP 2017 67

Deploy ScriptACCESS_KEY="C4F407CE1D8C59EB53BE"SECRET="daNENHR241Jzm5Z9iw6VsujD9hWfjHWrDzkKmKiA"RANCHER_URL="http://192.168.99.100:8080"

./rancher-compose --secret-key=${SECRET} --access-key=${ACCESS_KEY} --url=${RANCHER_URL} --file=docker-compose.yml --rancher-file=rancher-compose.yml -p phptest up --upgrade -d

SunshinePHP 2017 68

Edit and Deploy

SunshinePHP 2017 69

Edit and Deploy

SunshinePHP 2017 70

Finish Upgrade

SunshinePHP 2017 71

Thank You!• Software Engineer for InQuest• Author of “Docker for Developers”• https://leanpub.com/dockerfordevs

• Co-Host of “Jerks Talk Games”• http://jerkstalkgames

• http://ctankersley.com• chris@ctankersley.com• @dragonmantank

top related