0% found this document useful (0 votes)
27 views43 pages

03 Repeatable Dev Environments

Uploaded by

yw113job
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views43 pages

03 Repeatable Dev Environments

Uploaded by

yw113job
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Infrastructure as Code:

Repeatable Development
Environments Visual Studio Code

CSCI-GA 2820, Graduate Division, Computer Science

Instructor:
John J Rofrano
Senior Technical Staff Member | DevOps Champion
IBM T.J. Watson Research Center
rofrano@cs.nyu.edu (@JohnRofrano)
1

@JohnRofrano
DevOps Principle of the Day:
“AUTOMATE EVERYTHING!”
Even the Development Team !!!

@JohnRofrano 2
Infrastructure as Code

Objectives
The objectives of this class are to:
- How to create customized
development environments with
Docker and Visual Studio Code
- Discuss other IaC tools for developers
- How to leverage Docker containers for
middleware deployment
- How to make your DevOps team
more productive

@JohnRofrano 3
Infrastructure as Code

Clone the Project from GitHub

• For each lab you will need to close the GitHub repo and change directory (cd) into it and
then start Visual Studio Code telling it to open the current directory (code .)

$ git clone https://github.com/nyu-devops/lab-starter.git


$ cd lab-starter
$ code .

@JohnRofrano 4
Infrastructure as Code

The Developer On-boarding Problem


https://johnrofrano.medium.com/creating-reproducible-development-environments-fac8d6471f35

• Manually creating local environments for


developers to work in is:
- Time consuming
- Error prone
- Inconsistent at best
- Unreproducible at worst!

@JohnRofrano 5
Infrastructure as Code

The Goal is
to Avoid
This

@JohnRofrano 6
Infrastructure as Code

The Wrong Solution

• Carefully and painstakingly create


documentation in the form of “runbooks” that
give step-by-step instructions on how to
recreate the development environment via cut-
n-paste
• NOT VERY AGILE !!!
- We value: Working software over
comprehensive documentation

@JohnRofrano 7 Image: ID 125314060 © Olga Shcherba | dreamstime.com


Infrastructure as Code

Use Vagrant to quickly provision


The Right complex con gurations consistently
Solution: with repeatability
Infrastructure as
Code
Use VirtualBox to create
Virtual Machines for
Automate the creation of development
local development
environments right on
your laptop or desktop
Use Docker as a development
environment and to handle middleware
without any installation

Use Visual Studio Code to create


portable containerized development
environments automatically
Visual Studio Code
@JohnRofrano 8
fi
Infrastructure as Code

What is Infrastructure?

• Transportation infrastructure is the underlying


technology that makes transportation work:
- Highways
- Bridges
- Tunnels
- Roads
- Tool Booths
- etc...

@JohnRofrano 9
Infrastructure as Code

What is Infrastructure?

• Computer infrastructure is also the underlying technology


that makes computer systems work:
- Physical Computers
- Virtual Machines
- Storage Systems
- Network Switches
- Software Systems
- Satellites
- etc...

@JohnRofrano 10
Infrastructure as Code

What is Infrastructure as Code?

• Describing the state of your infrastructure in an


executable textual format (i.e., code)
• Provisioning and con guring the infrastructure
using that textual description
• Con guration Management Systems make this
possible
• Never perform con gurations manually
• Always use version control

@JohnRofrano 11
fi
fi
fi
Infrastructure as Code

What are These are all independent virtual computers

Virtual
Machines?

@JohnRofrano 12
Infrastructure as Code 6 Core CPU
Keyboard & Mouse

Physical
Machines
32 GB Memory (RAM)
Before you can
understand Virtual
Machines you must Video Card (GPU)
understand Physical
Machines

1TB Hard Drive (Storage)

Computer Motherboard

@JohnRofrano 13
Infrastructure as Code

Virtual Machines (a.k.a. Virtual Hardware)

• Virtual Machines (VM) are created by VM VM VM


emulating or virtualizing computer hardware in APP 1 APP 2 APP 3
software
BINS/LIBS BINS/LIBS BINS/LIBS
• The virtualization is provided by software
called a Hypervisor GUEST OS GUEST OS GUEST OS
(Ubuntu) (Windows) (RedHat)
• Each Guest OS thinks it’s talking to dedicated
computer hardware but it is really talking to the HYPERVISOR (Virtual Hardware)

hypervisor that is sharing a much larger system


HOST OPERATING SYSTEM

HARDWARE

Physical Machine

@JohnRofrano 14
Infrastructure as Code

VirtualBox

• VirtualBox is a free Hypervisor that runs on


Intel macOS, Windows, and Linux
• Virtualizes CPU, Memory, Hard Disks, CD,
Network Adapters, etc.
• Similar to VMware Workstation on a PC, or
VMware Fusions and Parallels Desktop on a
Mac
• Allows you to run applications in a Virtual
Machine
• No emulation, does not work on Apple M1
Silicon
@JohnRofrano 15
Infrastructure as Code

Other Popular Desktop Hypervisor Solutions

• Parallels Desktop supports CPU virtualization


• VMware Fusion for Mac and Workstation for
Windows supports virtualization
• QEMU provides hardware emulation of other
systems Parallels Desktop Fusion / Workstation

• UTM front-end for QEMU


• Multipass provides Ubuntu VM's on Mac (Intel
and Apple Silicon), Windows, and Linux

QEMU UTM Multipass

@JohnRofrano 16
Infrastructure as Code

The How do you know if you have the


correct infrastructure to run the
Software application?
Stack
Software Applications

Do have the correct version Libraries and Frameworks (J2EE, Flask, Rails, .Net Framework)
of .Net Framework?

Do have the correct version Language Runtimes (Java, Python, Ruby, C#)
of C#?

Do have the correct version


Operating System (Linux, macOS, Windows, etc.)
of Windows?

Do have enough CPU,


Hardware Computer (CPU, Memory, Storage, Network, etc.)
Memory, and Disk space?
Infrastructure

@JohnRofrano 17
Infrastructure as Code

Software
Stack for
This Class
Our Microservice Application

Flask 2.1.2 (Framework)


Visual Studio Code

Vagrant or Visual Studio Python 3.9 (Runtime)


Code will provision all
of this for you
Debian Linux 11 (Guest OS)
automatically

Docker Daemon (Virtual Hardware)

@JohnRofrano 18
Infrastructure as Code

What is Vagrant?

• Vagrant is a developers tool for automating the


creation of lightweight, reproducible and
portable virtual environments via command-
line
• It supports VirtualBox, VMware, SoftLayer,
Amazon AWS and Digital Ocean
• It supports con guration management utilities
like Ansible, Puppet, Chef, etc.
• It supports Docker natively which makes is
easy to use Docker containers in VMs

@JohnRofrano 19
fi
Infrastructure as Code

Vagrant is the Puppet Master

• Vagrant controls VirtualBox so you don't


have to
• Vagrant also installs software inside of the
Virtual Machine
• All of this is controlled by a single le
called: Vagrantfile
• You start it with one command:
vagrant up

• This is known as "Infrastructure as Code"

@JohnRofrano 20
fi
Infrastructure as Code

Example
Vagrantfile
This will create:
Ubuntu 20.04 VM
2 CPUs
2 GB memory
Network IP address
Forward port 8080
Install git, python, and
pip

@JohnRofrano 21
Infrastructure as Code

Where did
ubuntu/ https://app.vagrantup.com/boxes/search

focal64
come
from?

@JohnRofrano 22
Infrastructure as Code

What is
Docker?

@JohnRofrano 23
Infrastructure as Code

What is Docker?

• Docker packages applications into Containers


that include all of their dependencies, but share
the kernel with other containers
• Containers run as an isolated process in user
space on the host operating system
• Containers are not tied to any speci c
infrastructure
- Docker containers run on any computer, on
any infrastructure and in any cloud.
• This guarantees that it will always run the same,
regardless of the environment it is running in.
@JohnRofrano 24
fi
Infrastructure as Code

Docker Provides Portable Isolated Environments

• Docker gives you portable isolated


environments
• You could have an application that requires
Python 3.6 running in one container
• With an application that requires Python 3.9
running in another
• Completely isolated from each other will no
chance of library collisions

@JohnRofrano 25
Infrastructure as Code

Our Docker Image

• Docker can take your entire Software Stack and package it into an Image from which you
can create Containers (like Tupperware for your code)

Our Microservice Application

Flask 2.1 (Framework)

Python 3.9 (Runtime)

image
Debian Linux 11 (Operating System)

@JohnRofrano
26
Introduction to Docker

Images from Dockerfiles


• Whatever is needed to build the image is specified in
the Dockerfile and checked into GitHub
("infrastructure as code")
• Anyone who uses this image is guaranteed to get the
same service instance
• You can start from an operating system image and
install all of the prerequisites just like on a VM
• Then copy your application code and resolve and
library dependencies
• Finally add the command to run your application

@JohnRofrano 27
Infrastructure as Code

Containers inside a Virtual Machine


You can run Docker containers inside of a Virtual Machine

Redis Container

Docker Daemon

Guest OS (Ubuntu Linux)

Hypervisor (VirtualBox)

Host OS (Windows or macOS)

@JohnRofrano 28
Infrastructure as Code

Docker for

+
Development

Visual Studio Code

@JohnRofrano 29
Infrastructure as Code

What is Visual Studio Code Remote Containers?

• Visual Studio Code Remote Containers


leverages Docker to provide a development
environment in a container
• Creates a .devcontainers/ folder
• Controls how the environment is built using a
devcontainer.json le.

• Integrates with git inside the container


• Allows for multiple containers with databases
and other middleware that is needed for
development using docker-compose

@JohnRofrano 30
fi
Infrastructure as Code

Path to creating a dev container


https://code.visualstudio.com/docs/remote/create-dev-container

• Create a devcontainer.json, which describes how VS Code should start the container and
what to do after it connects
• Optionally create Dockerfile to make and persist changes to the dev container, such as
installation of new software
• Optionally con gure multiple containers through Docker Compose.
• As you make changes, build your dev container to ensure changes take effect

@JohnRofrano 31
fi
Infrastructure as Code

Remote Containers Basic Files

• Remote Containers uses the following le structure:

.devcontainer/
!"" devcontainer.json

• Simplest devcontainer.json le
{
"image": "mcr.microsoft.com/vscode/devcontainers/typescript-node:0-12"
}

https://code.visualstudio.com/docs/remote/devcontainerjson-reference

@JohnRofrano 32
fi
fi
Infrastructure as Code

Remote Containers using Dockerfile

• Remote Containers uses the following le structure:


.devcontainer/
#"" Dockerfile
!"" devcontainer.json

• devcontainer.json using Docker le


{ Dockerfile
"dockerFile": "Dockerfile",
"context": ".." FROM python:3.9-slim
} RUN apt update && \
apt install -y git wget zip

EXPOSE 8080

@JohnRofrano 33
fi
fi
Infrastructure as Code

Remote Containers Example with Dockerfile


devcontainer.json

@JohnRofrano 34
Infrastructure as Code
Start with Python image

Remote
Containers
Add required packages

Example
Dockerfile
Establish a non-root user with
password-less sudo privileges

Establish a working directory

Update Python tools

Expose a port and become user

@JohnRofrano 35
Infrastructure as Code

Remote Containers with Docker-compose

• You can also use a docker-compose le:


.devcontainer/
#"" Dockerfile
#"" devcontainer.json
!"" docker-compose.yml

• Docker-compose allows you to bring up multiple containers


• This allows you to provision a database or message broker or any other service you need to
develop with

@JohnRofrano 36
fi
Infrastructure as Code

VSCode Example devcontainer.json with docker-compose


These are now in the docker-compose.yml le
devcontainer.json

@JohnRofrano 37
fi
Infrastructure as Code

VSCode
Example
Docker Development container

Compose
file
docker-compose.yml

Database container

@JohnRofrano 38
Infrastructure as Code

Differences in devcontainer.json with docker-compose


docker-compose.yml devcontainer.json

@JohnRofrano 39
Infrastructure as Code

Baked vs. Fried

• Do you Bake software into the image?


- VM's and containers can be created quicker
because most software is pre-installed
• Or do you Fry it up fresh when you need it?
- More versatile and less maintenance when
software needs updating
• Determine which to use based on your needs
(we will use frying for the labs)

@JohnRofrano 40
Summit of Innovation
Sea of Technical Debt

@JohnRofrano 41
Infrastructure as Code

Hands-On
“live session”

@JohnRofrano 42
Infrastructure as Code

Summary

• You just deployed your rst Docker


environment with VSCode
• You learned how to automatically install
additional software
• You added Docker containers to supply
needed middleware
• You can now check your .devcontainer
folder into GitHub so that others can create
the perfect development environment when
working on your project.

@JohnRofrano 43
fi

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy