PB138 presents
YAML and Containers
presented by Lukas Grolig
Yaml
YAML (a recursive acronym for "YAML Ain't Markup Language") is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted.
YAML targets many of the same communications applications as Extensible Markup Language (XML) but has a minimal syntax which intentionally differs from SGML.
YAML
IS USED BY MANY MODERN APPS
Basics
- YAML files should end with .yaml
-
YAML is case sensitive
- YAML does not allow the use of tabs. Spaces are used instead as tabs are not universally supported.
Data Types
-
scalars (strings / numbers)
-
sequences (arrays / lists)
-
mappings (hashes / dictionaries)
Scalar types
They store single value
integer: 25
string: "25"
float: 25.0
boolean: true
Scalar types
They store single value
integer: 25
string: "25"
float: 25.0
boolean: true
Note:
Yaml is supporting single ' or double " quotation marks. Double quotation marks allow you to use escapings to represent ASCII and Unicode characters.
Sequences
aka your old friend list
- Cat
- Dog
- Goldfish
-
- Cat
- Dog
- Goldfish
-
- Python
- Lion
- Tiger
One dimensional
Multi dimensional
Sequences
we can do also inline sequences
npcflag: [GOSSIP, VENDOR]
Mappings
most probably you have seen dictionary
animal: pets
Mappings
we can assign sequence to the mapping
units:
- Footman
- Grunt
- Knight
- Ogre
Mappings
or use complex keys
? ["Tower", "Tier 1 Unit"]
: "Gremlin"
Sets
they are special case of mapping
set:
? item1
? item2
? item3
Sequence of Mappings
they are special case of mapping
-
name: Mark McGwire
points: 65
grade: "A"
-
name: Sammy Sosa
points: 63
grade: "B"
Sequence of Sequences
- [name , hr, avg ]
- [Mark McGwire, 65, 0.278]
- [Sammy Sosa , 63, 0.288]
Mapping of Mappings
Mark McGwire: {hr: 65, avg: 0.278}
Sammy Sosa: {
hr: 63,
avg: 0.288
}
Anchors
---
hr:
- Mark McGwire
# Following node labeled SS
- &SS Sammy Sosa
rbi:
- *SS # Subsequent occurrence
- Ken Griffey
When you want avoid duplicities. It is pointer and reference.
One comlex example
---
invoice: 34843
date : 2001-01-23
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
- sku : BL394D
quantity : 4
description : Basketball
price : 450.00
- sku : BL4438H
quantity : 1
description : Super Hoop
price : 2392.00
tax : 251.42
total: 4443.52
comments:
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
Sometimes we have problems with types
Pluto is a planet: false
Do we assign string or boolean here?
Pluto is a planet: !!bool false
Like this it is clear
- true # boolean
- "true" # string, because it's quoted
- !!str true # string, because of !!str
- !!bool "true" # boolean, because of !!bool
That's it for YAML
Now let's talk about containers
Before we start with Docker
HTTP
some infrastructure refreshments
Client
Server
Well, not realy
More realistic
Browser
DNS Server
CDN
Proxy
App Server 1
App Server 2
App Server 3
Database
Cache (eg. Redis)
Your infrasturcture
1.
2.
3.
Server
- Can be bare metal
- Can be Virtual Machine
- Can be Container
- And we have also "serverless" architecture
Comparison between bare metal and VM
Hardware
Hardware
OS
App
App
Hypervisor
OS
OS
App
App
Docker
Automates the deployment of software applications inside containers by providing an additional layer of abstraction and automation of OS-level virtualization.
Containers vs VMs
The industry standard today is to use Virtual Machines (VMs) to run software applications. VMs run applications inside a guest Operating System, which runs on virtual hardware powered by the server’s host OS
VMs are great at providing full process isolation for applications: there are very few ways a problem in the host operating system can affect the software running in the guest operating system, and vice-versa. But this isolation comes at great cost — the computational overhead spent virtualizing hardware for a guest OS to use is substantial.
Containers take a different approach: by leveraging the low-level mechanics of the host operating system, containers provide most of the isolation of virtual machines at a fraction of the computing power.
They have their own user space but share host's kernel
Why use containers?
-
You have the same environment on deployment machines as on dev machines. No more works on my machine
-
You can utilize you machines much more
-
You define your infrastructure in easy readable text format. You can version it!
How to get started?
-
Install Docker for Desktop (Windows and Mac). Windows users need Pro version. It is available to you from our faculty.
Let's take a look on basic commands
docker pull helloworld
You must pull image from Docker Hub
Is that image now available on my machine?
docker images
Let's take a look on basic commands
docker run helloworld
Ok, now I can run it
BTW if it is not pulled, it will be pulled when runned.
More commands
docker ps
docker ps -a
How to check what images are running?
Sometimes we must cleanup
docker kill container_id # kills running container
docker rm container_id # remove cotainer image from your machine
More commands
docker run -p 8888:80 containerName
Often you must map ports
Or you can run deamon
docker run -d --name nodeapp -p 8888:5000 mywebsite/nodeapp
Until now we used existing containers
Dockerfile
When you want to create your own image with running system and some application, your create Dockerfile (recommended name) and build it.
FROM node:13.10.1-alpine3.11
After creating file specify base image
Next time to define what should happen.
Like defining work directory.
WORKDIR /usr/src/website
Dockerfile
COPY . .
Copy files to container
And run some custom commands
In this stage you have application build.
RUN npm install
RUN npm run build
Expose port to outside:
EXPOSE 5000
Dockerfile
CMD [“node”, “./“build/index.js]
And setup run command
Result
FROM node:13.10.1-alpine3.11
WORKDIR /usr/src/website
COPY . .
RUN npm install
RUN npm run build
EXPOSE 5000
CMD [“node”, “./“build/index.js]
And now build it
docker build -t mywebsite/nodeapp .
Now move to multiple containers for 1 app
Docker Compose
Docker Compose helps you setup multiple containers at once. Let’s say you want to have a web server, database, and application running. You would have to write lot of Shell code without Compose.
Compose offers simple way to define such architecture in human friendly way.
Docker Compose
Start by creating docker-compose.yml (or any other yml file)
Yes, it's YAML file!
Docker Compose
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
volumes:
- wordpress:/var/www/html
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
volumes:
wordpress:
db:
And run it
docker stack deploy -c docker-compose.yml wordpress
# or
docker-compose -f docker-compose.yml up
That's it for today.
Any questions?
See you next time :)
PB138 Yaml and Containers
By Lukáš Grolig
PB138 Yaml and Containers
- 582