michael yuen

Docker Containers and Kubernetes Fundamentals

2024-03-30

freeCodeCamp YouTube course

Written by: my

https://youtu.be/kTp5xUtcalw?si=LvtISWZTXKtWGVVt

I asked ChatGPT to recommend me a course and this was the first recommendation. Let’s give it a shot. I’ll be doing this on a Mac and a PC in tandem.

Setup

  1. Clone: https://github.com/K8sAcademy/Fundamentals-HandsOn
  2. Install Docker Desktop: https://www.docker.com/products/docker-desktop/

Install was quick and easy on Mac. Took longer on PC and required a complete restart. Then, it complained about my Windows Subsystem for Linus being outdated, but upon following the prompted instructions it said it was already up to date.🤷‍♂️

Anyway, Docker Desktop up and running on Mac and PC ✅

MicroServices Concepts

Strangler Pattern

https://martinfowler.com/bliki/StranglerFigApplication.html

A way to migrate from monolith to microservices.

Microservices Anti Patterns

Microservices are not magic pixie dust (lol)

Microservices Benefits & Drawbacks

Cloud Native

https://www.cncf.io/about/who-we-are/

https://landscape.cncf.io/

Cloud Native Concepts

Mentality: Pets vs Cattle

Note: super non-vegan simile 😭

Cloud Native Trail Map

https://www.cncf.io/blog/2018/03/08/introducing-the-cloud-native-landscape-2-0-interactive-edition/

  1. Containerization
  1. CI/CD
  1. Orchestration & Application Definition
  1. Observability & Analysis
  1. Service Proxy, Discovery, and Mesh
  1. Networking & Policy

Containers Concepts

What is a Container?

Why Containers?

What is virtualized?

Compared with Container:

container registry

orchestrator

What is Docker?

ok, but really…

Docker CLI Management Commands

Cheatsheet

Attach Shell

Cleaning up

Hands-on portion

Open VSCode terminal and run different docker cli commands.

Running docker container exec -it webserver bash to attach to the running container is SO COOL!

Docker CLI Building Containers

Dockerfile

text file listing steps to build an image

Most simple version:

FROM nginx:alpine
COPY . /usr/share/nginx/html

Node site:

FROM alpine
RUN apk add -update nodejs nodejs-npm
COPY . /src
WORKDIR /src
RUN npm install
EXPOSE 8080
ENTRYPOINT ["node", "./app.js"]

Hands-on

Following tutorial, ran different docker (container) commands from the command palette:

… checking out info panels Docker extension provides …

Persisting Data

Containers are ephemerous and stateless.

You don’t usually store data in containers. You could write data in them, but it will be lost when the container is destroyed.

To persist data, use a Volume.

App sees a Volume just like any other folder.

⚠️Still a chance to lose the data if the VM crashes.

Volumes

Hands-on

Demonstration of creating a volume and adding some data to it, then removing the volume and creating a new volume to show the data has persisted.

YAML

Linting

https://www.yamllint.com

Docker Compose Concepts

Multi container apps

Docker compose is a way to define and run multiple containers using a single YAML file. There’s a compose plugin that will extend the CLI.

Docker compose example:

# now optional
version: '3.9'
# 3 containers
services:
  # defines the hostname -- how they can communicate with each other
  webapi1:
    image: .../webapi1
    ports:
      - '8081:80'
    restart: always
  webapi2:
    image: .../webapi2
    restart: always
  apigateway:
    image: .../apigateway
    restart: always

Should I use it or not?

Docker Compose Commands

Docker Compose file is located in a folder.

Docker Compose v2 New Commands

Hands-on

Using docker compose CLI commands to build, start, stop, and remove containers.

Docker Compose Features

Resource Limits

services:
  redis:
    image: redis:alpine
    deploy:
      resources:
        # max limit
        limits:
          cpus: '0.50'
          memory: 150M\
        # start by allocating 1/4 of CPU and 20mb ram
        reservations:
          cpus: '0.25'
          memory: 20M

Environment Variables

  services:
    web:
      image: nginx:alpine
      environment:
        - DEBUG=1
        - FOO=BAR

or override via command line: docker compose up -d -e DEBUG=-

reference by ${}:

services:
  db:
    image: "postgres:${POSTGRES_VERSION}

or an .env file

Networking

by default, all containers specified in a compose file will see each other using service names

services:
  web:
    image: nginx:alpine
    ports:
      # can be accessed from outside docker network at 8080
      # listening inside docker network 80 -- so db can talk to it at 80
      - "8080:80"
  db:
    image: postgres
    ports:
      # internal. web can talk to db at 5432, but db is not accessible outside docker network
      - "5432"

Can also define restrictions with networks:

# proxy can talk to app because they share a network, but not db
# app can talk to either proxy or db
# db can only talk to app
service:
  proxy:
    image: nginx
    networks:
      - frontend
  app:
    image: myapp
    networks:
      - frontend
      - backend
  db:
    image: postgres
    networks:
      - backend
networks:
  frontend:
  backend:

Dependence

# wait for db until starting app
services:
  app:
    image: myapp
    depends_on:
      - db
  db:
    image: postgres
    networks:
      - back-tier

Restart Policy

services:
  app:
    image: myapp
    restart: always # default is no

Container Registries

Kubernetes

What is it?

What can it do?

What can’t it do?

Architecture

Running k8s locally

K8s CLI & Context

API server runs on master node. Exposes REST API. Define desired state in YAML files.

kubectl is the cli. Communicates with apiserver. Configuration stored locally:

K8s Context

kubectl Cheatsheet

Hands-on

Using kubectl commands.

Declarative way vs. Imperative way

Namespaces

You define a namespace:

apiVersion: v1
kind: Namespace
metadata:
  name: prod

Use namespace in other resources:

kind: Pod
metadata:
  name: myapp-pod
  namespace: prod

Namespace Cheatsheet

Master Node

Also called the control plane. Where kubernetes services and controller are located, which are also called master components. You don’t usually run your containers on the master node.

etcd is key value datastore where state of cluster is stored. apiserver communicates with it.

kube-apiserver

REST interface. Communicates with etcd.

etcd

single source of truth

kube-control-manager

controller of controllers

cloud-control-manager

Interact with cloud providers controllers

kube-scheduler

Add ons

Worker Nodes

Nodes running containers.

kubelet

kube-proxy

Container runtime

Nodes pool

Pods


Tags