39
CI/CD with Jenkins and Docker

CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Embed Size (px)

Citation preview

Page 1: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

CI/CD with Jenkins and Docker

Page 2: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Hello!Teerapat Khunpech

@engineerball

https://github.com/engineerball

https://engineerball.com

2

Page 3: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

DevOps keys

http://web.premsco.com/wp-content/uploads/2014/09/Premsco-industrial-automation.jpg

Page 4: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

What is Continuous integration

▷ A development methodology▷ Verified by builds

○ Unit test○ Functional test○ Integration test

▷ Every commit trigger a build

Page 5: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

What is Continuous delivery

▷ Continuous delivery/deployment▷ Every commit that passed a build

could be deploy to production▷ Automation deploy

Page 6: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

What is Jenkins?

▷ CI/CD application▷ Easy installation▷ Rich plugin▷ Distributed build

Page 7: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Jenkins Workflow

Page 8: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

What is Docker?

Open Source engine for containers

Build, ship, run your application

within containers

Docker enables separation of

concerns

Page 9: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

How can you use Jenkins & Docker together?

Run Jenkins Master & Slave in Docker Build, Test & Deploy Docker Image from Jenkins

Page 10: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Example

CI/CD with Docker

Page 11: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

CI/CD Workflow

Page 12: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Tools

▷ SCM: Github▷ CI/CD: Jenkins 2.0▷ Platform: docker▷ Container Orchestration: docker swarm▷ Service Discovery Tool: Consul

Page 13: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand
Page 14: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Setup

Page 15: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Node0 set up

▷ Start the Consul container$ docker run -d -p 8500:8500 -h consul --name consul progrium/consul -server -bootstrapUnable to find image 'progrium/consul:latest' locallylatest: Pulling from progrium/consul3b4d28ce80e4: Pull completee5ab901dcf2d: Pull complete<snip>

Page 16: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Node1-3 set up

▷ Edit DOCKER_OPTS daemon (/etc/default/docker)

DOCKER_OPTS="-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

--cluster-store=consul://10.128.0.2:8500/network --cluster-advertise=ens4:2375"

Page 17: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Install Docker Swarm Cluster

▷ Node0: Create Swarm Master # Create Swarm Master

$ export TOKEN=$(docker run --rm swarm create)

$ docker run -d -p 3375:2375 swarm manage token://$TOKEN

# Join Swarm Node

$ docker run -d swarm join --addr=10.128.0.3:2375 token://$TOKEN

$ docker run -d swarm join --addr=10.128.0.4:2375 token://$TOKEN

$ docker run -d swarm join --addr=10.128.0.5:2375 token://$TOKEN

Page 18: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Jenkins Setup

▷ Build Docker image inside a docker container

▷ SSH Node with access to docker engine

Page 19: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Jenkins Setup

▷ Node0: Install Jenkins Master$ docker -H unix:///var/run/docker.sock run -d -p 8080:8080 jenkins

Page 20: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Jenkins Setup

▷ Node0: Configure Jenkins Master○ Configure Global Security○ Install plugins

■ Github plugin■ CloudBees Docker Build and Publish

○ Create Jenkins Slave node (node1)

Page 21: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Example Voting App

▷ A Python webapp which lets you vote between two options

▷ A Redis queue which collects new votes

▷ A Java worker which consumes votes and stores them in…

▷ A Postgres database backed by a Docker volume

▷ A Node.js webapp which shows the results of the voting in real time

Page 22: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Voting App

Dockerfile# Using official python runtime base imageFROM python:2.7

# Set the application directoryWORKDIR /app

# Install our requirements.txtADD requirements.txt /app/requirements.txtRUN pip install -r requirements.txt

# Copy our code from the current folder to /app inside the containerADD . /app

# Make port 5000 available for links and/or publishEXPOSE 80

# Define our command to be run when launching the containerCMD ["python", "app.py"]

Page 23: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Result App

DockerfileFROM node:0.10

RUN mkdir /appWORKDIR /app

ADD package.json /app/package.jsonRUN npm install && npm lsRUN mv /app/node_modules /node_modules

ADD . /app

ENV PORT 80EXPOSE 80

CMD ["node", "server.js"]

Page 24: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Worker

DockerfileFROM java:7

RUN apt-get update -qq && apt-get install -y maven && apt-get clean

WORKDIR /code

ADD pom.xml /code/pom.xmlRUN ["mvn", "dependency:resolve"]RUN ["mvn", "verify"]

# Adding source, compile and package into a fat jarADD src /code/srcRUN ["mvn", "package"]

CMD ["/usr/lib/jvm/java-7-openjdk-amd64/bin/java", "-jar", "target/worker-jar-with-dependencies.jar"]

Page 25: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Github Webhook Setup

▷ Automatically build job when pushes are made to Github

Page 26: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

The Build Jobs

▷ Create 3 Jenkins jobs○ build-voting-app○ build-worker○ build-result-app

Page 27: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

The Build Jobs

Page 28: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

The Build Jobs

Page 29: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

The Build Jobs

Page 30: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

The Deploy Job

▷ Create a Jenkins jobs○ Build section, Execute shell

export DOCKER_HOST=10.128.0.4:2375

export DTR="https://registry.hub.docker.com/u"

docker-compose -f vote-apps/docker-compose.yml stop voting-app result-app worker

docker-compose -f vote-apps/docker-compose.yml rm -f

docker-compose -f vote-apps/docker-compose.yml pull voting-app result-app worker

docker-compose -f vote-apps/docker-compose.yml up -d

Page 31: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

The Deploy Job

docker-compose.ymlversion: "2"

services: voting-app: build: ./voting-app/. networks: - front-tier - back-tier

result-app: build: ./result-app/. networks: - front-tier - back-tier

worker: image: manomarks/worker networks: - back-tier

redis: image: redis:alpine container_name: redis networks: - back-tier

db: image: postgres:9.4 container_name: db volumes: - "db-data:/var/lib/postgresql/data" networks: - back-tier

volumes: db-data:

networks: front-tier: back-tier:

Page 32: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Deploying The App

▷ Clone SCM, edit README.md and push to SCM

Page 33: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

Deploying The App

root@node2:~# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES0100cf6b0c14 engineerball/result-app:47d362f35a115f2098c80a11e161eef626e1dbcb "node server.js" 2 hours ago Up 2 hours 0.0.0.0:5001->80/tcp voteapps_result-app_1296eee196e4e engineerball/voting-app:47d362f35a115f2098c80a11e161eef626e1dbcb "python app.py" 2 hours ago Up 2 hours 0.0.0.0:5000->80/tcp voteapps_voting-app_140f8d8aec4ca engineerball/worker:47d362f35a115f2098c80a11e161eef626e1dbcb "/usr/lib/jvm/java-7-" 2 hours ago Up 2 hours voteapps_worker_144da08df3d5c postgres:9.4 "/docker-entrypoint.s" 2 hours ago Up 2 hours 5432/tcp voteapps_db_1fa10362631cb redis "docker-entrypoint.sh" 2 hours ago Up 2 hours 6379/tcp voteapps_redis_1

Page 34: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

The Voting App

Page 35: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

The Voting Result

Page 36: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

DEMO

Page 37: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

In Conclusion

Page 38: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand

In Conclusion

▷ Automate is the key▷ Docker simplifies environment problems▷ Jenkins is ready for Docker and CD

Page 39: CI/CD with Jenkins and Docker - DevOps Meetup Day Thailand