Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ecosistema de desarrollo en PHP con Docker y Ansible

Ecosistema de desarrollo en PHP con Docker y Ansible
Charla PHP Day 2015

  • Login to see the comments

Ecosistema de desarrollo en PHP con Docker y Ansible

  1. 1. ECOSISTEMA DE DESARROLLO EN PHP CON DOCKER Y ANSIBLE P Vicent Soria phpDaySpain 2015 31 Enero 2015
  3. 3. SÜBRE ¡Vil o Vicent Soria o Valenciano o Automation Engineer en Social Point A o Hago basuritas varias en Tangotree
  4. 4. tangotreejo tango ree Soalabie Development A sociolpoint socialpuintes
  5. 5. NQPH-PSÜNH ÍNDICE ¿Qué es Docker? Docker VS VMs Docker en detalle Ansíble Desarrollo Desplegando a Prod DEMO
  6. 6. ¿ÍIIIÉ ES DÜCKER? *docker "Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. ” httpsJ/ wwwdockeuccom/ whatísdocker What the Fuck
  7. 7. ¿aut ES IJIIBKER? (PARA MI) Herramienta que permite ejecutar procesos aislados unos de otros.
  8. 8. ¿llllÉ ES IJÜCIÍER? TECNÜLHGÍA Para aislar los procesos y los recursos usa: o Namespaces: Procesos o Cgroups: Gestión de los recursos Docker sólo está disponible bajo el kernel de Linux *
  9. 9. ¿llllÉ ES 008m? CARACTERÍSTICAS Portable Autosuficiente (Docker es la única dependencia) Permite desplegar (casi? ) cualquier aplicación Liviano
  10. 10. ¿llllÉ ES DÜCKER? ARlllIITECTIJRA
  11. 11. IJÜBKER VS VMS Virtual Machines mn virluallzed nwllcaliou lnclmes ml only me agzplmannn y Much may n. anIy1Ds al ma, and me necessary mnanes 2M lnhianes. bm also an emite guest nuemllng sysiem- wm may weigh ms ul Ga Docker 17m Dockev Engine ¿mmm compnses msnm avui-canon ami ns dependemles n mus as an isolated puxess m usevsuace on me mi opevalmg system‘ shanng the kernel wm einer cummins Thus ¡lenlays me ¡asoma Isolanon and allocanm mmm; oi VMs mn Is much move amable and eiilclenl http: //stackoverfIow. com/ quest¡ons/1óó470ó9/shouId—i-use— vagrant-or—docker-io-for—creating—an—iso| ated-environment
  12. 12. IJÜBKER EN DETALLE IMAGES Dockerfile FROM ubuntu:14.B4 MAINTAINER Vicent soria RUN apt-get install -y git software-properties-common RUN apt»add-repository ppa: ansible/ ansible RUN apt-get -y update RUN apt»get install -y ansible ADD . /code WORKDIR / code EXPOSE 80 it Provisioning RUN echo “web ansib1e_ssh_host= ansib1e_ssh_port=40022" > hosts RUN ansible-playbook ansible/ dockenyml -c local »i hosts CMD ["/ code/ run. sh“]
  13. 13. IJDÍJKER EN DETALLE IMAGES ansible/ docker. ym| - hosts: web sudo: yes vars: project_path: /code changeinameserver: false roles: - role: common - role: apache - role: php - role: Composer - role: act
  14. 14. DDDllER El! DETALLE ¡Minis Building web. . . ---> 9bd07E480C5b Step 1 : MAINTAINER Vicent soria ——-> Running in 20bbdf23aeB1 . ..> b07af9e0acf8 Removing intermediate container .70bbdf23ae81 Step Z : RUN aptget install -y git software-properties-common ———> Running in <Jf394292414a Reading package lists. .. Building dependcncy tree. .. Reading state inforiiation. .. The following extra packages will be irstalled: Ca-(E"Í1fl(aÍ€S q1r1.2-qlih-2.0 qit-‘nar lSO>COCl€S KFD5>LOCHLGS fig build -—no—cache 2,365 user 0,765 system 0% cpu 8:18,03 total
  15. 15. DDDKER EN DETALLE ¡Minis Con caché Building web. .. ---> 9bdO7e4B0c5b Step 1 : MAINTAINER Vicent soria ———> Using cache ---> b07al9eOacl8 Step 2 : RUN apt-get install —y git software-properties-Common ---> Using cache ———> 4ae2c51fa3a4 Step 3 : RUN apt-add-repository ppa: ansible/ ansible ———> Using cache ---> aO17Z7a60730 Step 4 : RUN apt-get -y update ---> Using cache ——> 8Cdbf8b45348 fig build 2,265 user 1,135 system 14% cpu 24,067 total
  16. 16. DDDllER EN DETALLE ¡Minis Similara GIT vicentsoria/ phpday latest 82f5d561be38 23 hours ago 495 MB < none> < none> < none> < none> < none> ubuntu <none> 90c57050aaa9 23 hours ago 495 MB <none> 4e6c0f1d998b 23 hours ago 359.5 MB <none> 807db845e53b 23 hours ago 359.5 MB <none> edf1d9bf8374 23 hours ago 359.5 MB <none> 611c9e961891 23 hours ago 359.5 MB 14.04 5ba9dab47459 2 days ago 188.3 MB
  17. 17. DDDKER EN DETALLE REPDSITDRY Contienen un conjunto de imágenes (típicamente de un mismo SO, aplicación, etc) Por ejemplo: Ubuntu (httpszl/ registryhubdockencom/ jubuntu/ l o 12.04.5, 12.04, precise o 1404.1, 14.04, trusty, latest o 14.10,utopic o 15.04,Vivid
  18. 18. IJLIBKER EN DETALLE REGISTRY Es el lugar donde se guardan las imágenes (agrupadas en repositorios) Tipos: o Público: Docker Hub (hub. docker. com) o Privado: Docker Hub (de pago), Quay (de pago), el tuyo propio,
  19. 19. DDBNER EN DETALLE IIDNTAINER o Una imagen o Un set de operaciones: start, stop, restart, o Entorno de ejecución CONTAINER ID IMAGE COIVMAND d54rd4d44750 phpdaydockersymfonyiweb: Latest ”/ code/ run. sh” CREATED STATUS PORTS About an hour ago Up About an hour U R_W f: ñWUH->k0/ÍCP NAMES phpdaytlotkersyml any web 1
  20. 20. DDDNER EN DETALLE CDMANDDS (IMAGES) build: Build an image from a Dockerfile commit: Create a new image from a container's changes images: List images pull: PuII an image or a repository from a Docker registry server push: Push an image or a repository to a Docker registry server rmi: Remove one or more images search: Search for an image on the Docker Hub tag: Tag an image into a repository
  21. 21. DDDKER EN DETALLE BDMANDDS (CDNTAINERS) attach: Attach to a running container exec: Run a command in an existing container info: Display system-wide information inspect: Return low-level information on a container kill: Kill a running container logs: Fetch the logs of a container ps: List containers run: Run a command in a new container rm: Remove one or more containers start: Start a stopped container stop: Stop a running container top: Lookup the running processes of a container
  22. 22. DDDNER EN DETALLE GESTIÓN n: RED Container 2 (redis) 6379 A Container 1 (web) ' BO Raí- 172.1142.1
  23. 23. DDDNER EN DETALLE ALDIJNDS EJEMPLDS docker run —t —i ubuntu:14.04 bash docker run -t -i -p 8080:80 --link phpdaydockersymfony_redis_1:redis -v S(pwd): /code ubuntu:14.04 bash
  24. 24. DDCllER EN DETALLE FIlLSH "Fast, isolated development environments using Docker. " httpsJ/ vwvwfigsh
  25. 25. DDBKER EN DETALLE FIILSH fig. ym| mysqldata: image: busybox volumes: - / var/ Lib/ mysql command: echo Data-only container for Mysql mysql: image: mysqL: b:b voLumes_from: - mysqldata environment: MYSOL RDDT PASSWORD: phpday MYSOL_USER: phpday MYSOL_DATABASE: phpday MYSOL_PASSw0RD: phpday
  26. 26. DDCllER EN DETALLE FIILSH fig. ymI redis: image: redis:2 L web: build: . expose: - EL links: - redis — mysql ports: — "3eee: sa" volumes: — . :/code
  27. 27. DDDKER EN DETALLE CDMANDDS FIlLSH build: Build or rebuild services help: Get help on a command kill: Kill containers logs: View output from containers port: Print the public port for a port binding ps: List containers pull: Pulls service images rm: Remove stopped containers run: Run a one-off command scale: Set number of containers for a service start: Start services stop: Stop services restart: Restart services up: Create and start containers
  28. 28. DESARRDLLD ARRANCAR EL ENTDRND o fig run web composer install o fig up -d phpdaydockersymfony webzlatest .1 (Jlv4o: kÁ‘L7L’7 1->u. J/tcp phpdaydockersymfony v mysql : ‘É ¿’noo/ tcp phpdaydocke rsymfonyfn redis: _‘ Iu/ Él/ ÍCD phpdaydockersymfonyfr >
  29. 29. DESARRDLLD LANZAR LDS TESTS fig run -T web bin/ phpunit -c app fig run »T web bin/ phpunit —c app PHPUnit 4 4.4 by Sebastian Bergmann. Configuration read from / code/ app/ phpunit. xml. dist Time: H12 ms, Memory: _H.50Mb OK (3 tests, 7 assertions)
  30. 30. 0 ANSIBLE Ansíble is an IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates. Similar to Chef and Puppet.
  31. 31. ANSIBLE ¿PARA nui? Provisionar imagen Crear infraestructura Provisionar servidor/ es de producción Deploy de contenedor/ es
  32. 32. DESPLEGANDD A PRDDIIIJBIDN PASDS A SEGUIR o Subir la imagen al repositorio o [? ] Crear infraestructura (instala docker) o [? ] Provisionamiento de servidores (otros servicios) o Deploy
  34. 34. DESPLEGANDD A PRDDIIDCIDN INFRAESTRIIIITIJRA S ansible-playbook platform. yml --connection= local -i hosts pLAy [localhost] **x***xx*xxx*xt: *******xxxxx*xx*****x***xxxx**x*****x***xxx* [create infrastructure] >l<*****>r****#**x>l<>kt>r********>CKX<X<**********#***XX* changed: l127.0.0.1] TA5K¡ [print output] txtt****x**x***************x*************************#** ok: [127.e.0.1l = >l “output. stack outputs": ( "DNSName' ' amazonaws. com" "Publiclp "“, "Publicsubnet": "subnet-3bd15b5e", "VPCId": "vpc-74ab7611" i pLAy REcAp xx***x*********xxx**xx**x****x*xxx**x*********xxx***x******x**xxx* 127.e. o.1 : ok=2 changed=1 unreachable=0 failed=0
  35. 35. DESPLEGANDD A PRDDIJCCIDA! DEPLDY ansible-playbook --private<key= S(KEYPATH) -i ansible/ prodjosts ansible/ dep| oy. yml ansible/ dep| oy. ymI - name: pull image shell: docker pull {{ appfimage )) - name: stop container docker: image: "({ appfimage )}" name: phpday state: absent — name: start container docker: image: "ll app_image ))" name: phpday state: running ports: "BB:80" command: /code/ run. sh prod
  36. 36. DESPLEGANDD A PRDDIIIIIIIIÍN DEPLDY run. sh #! /bin/ bash if [ "s1" = = "prod" 1 then echo "Running as prod" cp / code/ app/ config/ parameters_prod. yml. dist / code/ app/ config/ parameters. APP7ENVIR0NMENT= “prod" MYSOL_HOST= ”" else APP ENVIRONMENT= “dev" MYSOL_HOST= "mysql“ fi rm app/ cache/ * app/ cache/ * -Rf php BPP/ Console cache: clear --env=3{APP7ENVIR0NMENT) chmod 777 app/ cache app/ logs -R 4 >
  37. 37. DESPLEGANDD A PRDDIICIIIIÍN DEPLDY run. sh # Load mysql tables USERNAME= phpday PASSWORD= phpday DATABASE= phpday exit_code=1 while [ sexit_code -ne 0 ]; do mysql »h S{MYSOL_HOST) -u S{USERNAME} -»password= S{PASSWORD} SLDATABASE} exit_code= s? l Sexit_code -ne G ] && (echo "Trying to connect“; sleep 1) done mysql -h s{MYSOL_HOST} -u s{USERNAME) --password=5(PASSWORD} s{DATABASE} < / c echo "Tables were created“ # Run apache / usr/ sbin/ apache2ctl -D FOREGROUND 4l >
  38. 38. ¿PREGUNTAS HASTA Alllll?
  39. 39. lï lHHI l;
  40. 40. Gllllill SEGUIR o https: //docs. docker. com/ o Docker Book (http: //wvvw. dockerbook. com/ ) o https: //bitbucketorg/ vicentgodella/ phpday-symfony— docker/
  41. 41. ¡MUCHAS GRAGIAS! vicent {at} ta ngotree. io @vicentgode| |a