Unit #2
Deployments


use ←↑↓→ or <space>

Apps in k8s

 
 
 
 
 
 
 

"App" = container image

Combines operating system, application

Docker images de facto standard

Where to get images?

Use, modify public images

Build your own!

Container registry

Hosts container images

Can be shared, or private self-hosted

Docker hub

Largest, shared container registry

hub.docker.org

Downloader, beware!

Always security review public images!

Be aware of supply-chain attacks

Flight deck

TEN7 own, Drupal and k8s friendly containers

github.com/ten7/flight-deck

Deploying applications

 
 

Pods

Running instance of an application

Containers vs. Pods

Pod can be one or more containers

Each container shares the same localhost

IP address, DNS name, Volumes, etc.

 
 
 
 
 
 

Co-hosting containers

Ensure tight coupling where needed

Apache+Varnish, NGINX+PHP-FPM

You can't "just run it"

Static pods rarely created directly

Created indirectly through a managing object

Deployments

Describes ideal state of a pod

Can run multiple replicas of the pod

Defining Deployments

Two methods

kubectl create objectType

kubectl apply -f /path/to/file.yml

Files are better

Same format as when edited

Easily added to version control (git)

YAML

Indent-based markup format

All k8s objects can be created in YAML

Header

apiVersion: apps/v1
kind: Deployment

Metadata

Name

Labels

Annotations

metadata:
  labels:
    app: web
  name: web

Specification

Main body of the document

Defines the object, or template of objects to create

Deployment spec

  • Number of replicas
  • Selector (we'll use this later)
  • Template of pod to create

Pod template

Like a YAML document itself

Has it's own metadata, spec

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
  name: web
spec:             # Deployment's spec start here.
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:       # Pod template starts here.
    metadata:
      labels:
        app: web
    spec:
      containers:
        - image: ten7/flight-deck-drupal:latest
          name: web
          ports:
            - containerPort: 80
(Scroll down.)

Working with deployments

Applying

Deploys definitions to cluster

$ kubectl --kubeconfig=/path/to/kubeconfig.yml apply -f /path/to/file.yml

Validation

Done by kubectl before creation

Transaction-like, doesn't create partial objects

Updating from file

kubectl apply -f /path/to/file.yml

Existing object will be patched

Listing deployments

$ kubectl --kubeconfig="/path/to/kubeconfig.yml" get deployments
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web    1/1     1            1           8m52s

Getting details

kubectl describe deployment name

Includes operational information

"Hot" editing

Edits definition in-place on cluster

Useful for critical fixes, troubleshooting

kubectl --kubeconfig="/path/to/kubeconfig.yml" edit deployment web

Listing pods

kubectl get pods

$ kubectl --kubeconfig="/path/to/kubeconfig.yml" get pods

NAME                   READY   STATUS    RESTARTS   AGE
web-56c74df886-46wcf   1/1     Running   0          37m
web-56c74df886-gpf9c   1/1     Running   0          3m13s
web-56c74df886-kz95s   1/1     Running   0          3m13s

Pod names

deploymentName-uniqueID

Names are non-deterministic!

Getting a shell

kubectl exec -it command

Runs command in pod, like /bin/bash

kubectl --kubeconfig="/path/to/kubeconfig.yml" exec -it web-56c74df886-gpf9c /bin/bash
(Scroll right.)

Exposing deployments

 
 
 
 
 
 
 
 

Services

Consistent application endpoint

Creates internal DNS name

apiVersion: v1       # The version is different!
kind: Service
metadata:
  name: web
spec:
  ports:             # Service can have one or more ports
    - name: http
      port: 80
      protocol: TCP
  selector:          # This looks familiar...
    app: web

Selector

Associates Deployments to Pods...

...and Pods to Services

apiVersion: apps/v1   # Deployment def starts here.
kind: Deployment
metadata:
  labels:
    app: web
  name: web
spec:
  replicas: 1
  selector:           # Manages pods with label
      app: web        # app=web
  template:
    metadata:
      labels:         # Create pods with label
        app: web      # app=web
    spec:
      containers:
        - image: ten7/flight-deck-drupal:latest
          name: web
          ports:
            - containerPort: 80
---
apiVersion: v1        # Service def starts here.
kind: Service
metadata:
  name: web
spec:
  ports:
    - name: http
      port: 80
      protocol: TCP
  selector:           # Route to pods with label
    app: web          # app=web

YAML multidoc

Allows multiple roots in same file

Separated by ---

Optional, useful for highly-related definitions

Managing Services

kubectl get services

kubectl describe service name

kubectl edit services name

Service types

ClusterIP: default, internal only

NodePort: Exposes port on each node

LoadBalancer: External access

---
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  ports:
    - name: http
      port: 80
      protocol: TCP
  selector:
    app: web
  type: LoadBalancer   # Create external load balancer

Lab #2

  1. Create Deployment YAML
  2. Apply YAML to the cluster
  3. Observe, work with pods
  4. Edit deployment