Kubernetes

30/05/2019

Kubernetes Is

“Kubernetes is an open-source system for managing containerized applications across multiple hosts in a cluster. Kubernetes provides mechanisms for application deployment, scheduling, updating, maintenance, and scaling.”

Kubernetes Is


$ kubectl
kubectl controls the Kubernetes cluster manager.

Find more information at https://github.com/kubernetes/kubernetes.

Usage:
  kubectl [flags]
  kubectl [command]

Available Commands:
  get            Display one or many resources
  describe       Show details of a specific resource or group of resources
  create         Create a resource by filename or stdin
...
					

K8S Architecture

Master Node

Worker Node

K8S Core Concepts

  • Containers
  • Nodes
  • Pods
  • Controllers
  • Services
  • Persistent Volumes

Containers

  • Unix processes, *NOT* VMs
  • Application + dependencies = image
  • Runtime environment (cgroups, namespaces, env vars)

FROM alpine:3.3

# install common packages
RUN apk add --update-cache curl bash openssl sudo && rm -rf /var/cache/apk/*

# install etcdctl and confd
RUN apk add --update-cache curl tar \
    && curl -sSL https://github.com/coreos/etcd/releases/download/v2.2.1/etcd-v2.2.1-linux-amd64.tar.gz \
    | tar -vxz -C /usr/local/bin --strip=1 etcd-v2.2.1-linux-amd64/etcdctl \
    && chown root:root /usr/local/bin/etcdctl \
    && curl -sSL -o /usr/local/bin/confd https://github.com/kelseyhightower/confd/releases/download/v0.10.0/confd-0.10.0-linux-amd64 \
    && chmod +x /usr/local/bin/confd

# define execution environment
CMD ["/app/bin/boot"]
EXPOSE 8000
						

Node

Runs containers and proxies service requests.
Supporting processes:

  • docker
  • kubelet
  • proxy

Pod

Represents a logical application.

  • One or more containers
  • Shared namespace

Controllers

Define states of pods

  • Deployment: Used for stateless apps
  • StatefulSet: Used for statefull apps
  • DaemonSet: Ensures that pods are running on all cluster nodes

Service

Service discovery for pods.

  • Proxy runs on each node
  • Virtual IP per service (no port collisions)
  • Basic round-robin algorithm
  • Dynamic backends based on label queries
  • Types: ClusterIP, NodePort, LoadBalancer

Persistent Volumes

  • PersistentVolume (PV)
  • PersistentVolumeClaim (PVC)

Deployment


controllers/nginx-deployment.yaml 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

						

K8S Runs Everywhere

Demo: Create a Vagrant Cluster


export NUM_MINIONS=2
export KUBERNETES_PROVIDER=vagrant
curl -sS https://get.k8s.io | bash
cd kubernetes/
ls
cd cluster/
ln -s kubectl.sh kubectl
export PATH=`pwd`:$PATH

kubectl get nodes
kubectl get pods
kubectl get services
						

Demo: Install and Scale an RC


cat manifests/k8s-basics-rc.yaml
kubectl create -f manifests/k8s-basics-rc.yaml
kubectl describe rc k8s-basics
kubectl get pods
kubectl describe pod 
kubectl logs -f 
kubectl exec  -- ls -alh
kubectl scale rc k8s-basics --replicas=4
kubectl get pods --output=wide
curl http://10.245.1.3:8888
cat manifests/k8s-basics-svc.yaml
kubectl create -f manifests/k8s-basics-svc.yaml
kubectl get service
kubectl get service k8s-basics -o yaml
curl -sS http://10.245.1.3:/tree
# open in browser
# cleanup
kubectl delete svc k8s-basics
kubectl delete rc k8s-basics

Demo: Deploy SOG Product Selector microservice