Skip to content

Instantly share code, notes, and snippets.

@BLannoo
Created July 25, 2019 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BLannoo/24e921e8c04a250e8b5d5832916b90d8 to your computer and use it in GitHub Desktop.
Save BLannoo/24e921e8c04a250e8b5d5832916b90d8 to your computer and use it in GitHub Desktop.

Devops Workshop (Participant Guide)

Purpose of this workshop:

  • Lower barrier to use Devops starter-kit
  • Teach how to provision an application in the cloud

General Topics covered:

  • Automated testing
  • Automated builds
  • Continuous deployment
  • Infrastructure as code
  • Cloud

You will learn to use:

  • Dockerfile
  • AWS console
  • Ansible playbook
  • Jenkinsfile

Local setup (30 minutes)

Loading the Demo application onto your machine, and verify that it works.

Application

  1. Clone the repository
  2. Import the project into IntelliJ
  3. Set your project JDK to JDK 11
  4. Verify that you can run the tests, and that they pass
  5. Run the application, and verify you can access the application in your browser, and see "Hello world Piet"
  6. Create your own git branch
  7. Change "Hello world Piet" to "Hello world ", and update the Unit Test.

Ansible & AWS

  1. Create a virtualenv to install ansible and the aws-cli
    # create a virtual environment for python 3 (such that it does not collide with other python projects)
    python3 -m venv .venv
    
    # turn on the venv (this is why this script should be sourced)
    source ./.venv/bin/activate
    
    # install: ansible, awscli and boto (to allow ansible to talk to aws)
    pip install -r ops/local/requirements.txt
  2. Setup your machine to access AWS
  3. WARNING: For those who already have a project on another AWS account.
    • Don't run playbooks on your DEFAULT AWS profile.
  4. Run the ansible-playbook test.yml from the ops/ansible directory.
    • You also need to specify your AWS profile in the environment variable AWS_PROFILE.
    • Verify that ansible works, and you get no errors in the output.

Docker (1 hour)

Creating a docker image that will run your application.

Have a look at:

  1. Create a directory for building a docker image, containing:
    • A copy of the application JAR file
    • A new Dockerfile
  2. Dockerfile:
    • Based on OpenJDK 11
    • Specify that the JAR should be added during build
    • Make sure your application port is available from outside of the container
    • Let your docker image run Java with the copied JAR file on boot
  3. Build your dockerfile using the docker build command
  4. Run your image with docker run (don't forget port mapping)
  5. See that you can access the application from localhost

Pushing the image to the docker registry.

  1. Build your image with a tag that points to the Docker Repository, add a tag with your name
    • De docker repository voor deze workshop is 0000000000000.dkr.ecr.eu-west-1.amazonaws.com/devops-workshop-demo
    • You can find this by going to AWS ECR in the Ierland region
  2. Login into the Docker Repository on amazon (ECR)
    $(AWS_PROFILE=devops-workshop aws ecr get-login --no-include-email --region eu-west-1)
  3. Push your image to the repository (specify the full url, excluding the tag)

https://eu-west-1.console.aws.amazon.com/ecr/repositories/devops-workshop-demo/?region=eu-west-1

AWS Manual Deploy (1 hour)

  1. Create an SSH key-pair
  2. Add your SSH public key to AWS
    • aws console > EC2 > Key Pairs > Import Key Pair
    • Key pair name: devops-workshop-Piet
  3. Create an EC2 from the console
    • Use our devops workshop image (devops-workshop-application-ami)
    • Use our devops workshop security group (devops-workshop-sg)
    • Use our devops workshop role (devops-workshop-ec2)
    • Preferably with a tag: Name: Piet
  4. SSH to your instance with ssh
    • Find your IP in the AWS console
    • The username is ec2-user
  5. Launch the application
    • login to the docker container registry
    • use docker run (with your image)

Checklist:

  • You can access the application online, and see "Hello world (your name)"

Automatic deploy with ansible (1 hour)

Deploying an EC2

Create an Ansible playbook with a task that deploys an EC2:

  • Region
  • AMI
  • Role
  • Security Group
  • SSH Key pair name
  • instance type
  • tags

Hints:

  • Have a look at the test.yml playbook for an example.
  • Search for "Ansible ec2" on google for documentation.
  • Use AWS Console to find the ID's of the resources above (use your own ec2 as an example)

Checklist:

  • You can SSH to the EC2
  • Terminate your instance after you're done

Starting the Application on boot

Launch your ec2 instance with a user-data script that runs your docker image.

Hints:

  • Check the Ansible EC2 documentation
  • Pass User-Data to the ec2, and indicate that it's a bash script
  • Remember the commands you ran to start the application manually on EC2
  • Check the logs to see if your script executed correctly /var/log/cloud-init-output.log

Checklist:

  • You can view your application, online, in the browser
  • Manually terminate your instance

Jenkins pipeline (1 hour)

Visit Jenkins:

  • url: the ip-address of ec2: devops-workshop-jenkins
  • user: admin
  • password: ask the facilitator to share it in the slack channel

Task:

  • Modify the Jenkinsfile (ops/jenkins/Jenkinsfile)
  • Stages:
    • Building & Testing the application with gradle
    • Building & Pushing the docker image
    • Deploying the application on EC2 with ansible

Hints:

  • Jenkins is preinstalled with docker, java, ansible and aws
  • You do not need to activate the virtual environment
  • Speed up your Jenkinsfile experiments!
    • Edit and rerun your pipeline from within Jenkins
    • Click on a build number, click "Replay", click Run
  • Run gradle without the daemon

Checklist:

  • Change your hello world message
  • Wait for the pipeline to finish
  • See that you see your newly deployed application
  • Cleanup your old instances
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment