👨🏻‍💻

kubernetes crash course

Code
Created
Importancevery important
Language
Materialshttps://www.youtube.com/watch?v=X48VuDVv0do
Reviewed
contextDevOpskubernetes
sourceYT

intro

orchestration tool for containers

cons

  • high availablity or no down time
  • scalability
  • disaster recovery

k8s components

pod

service

external service

internal service

ingress

forward requests to services

configmap:

secret:

volumes

we can attach either local or remote (outside k8s cluster)

k8s doesn't manage data persistence you have to manage it explicitly using volumes

replica

  • connected to the same service, another node but same service
  • service is also load balancer

Deployment:

  • you wont create replica for pod but you will define blueprint for the pod which contains how many replica
‼️
stateless

statefulSet:

💡
we need DB replica too but we can't do this using Deployment becuase it has a state (its data)
💡
DB are often hosted outside of K8S cluster

K8S Architecture

worker machine

  • node - or server-
  • each node has multiple pods
  • worker do the actual work

3 processes must be installed on each node

kubelet

  • process of k8s itself
  • interact with node and pods
  • take the config and run the pod inside and assign resources for pods

container runtime

kubeproxy

  • foward the requests
  • has intellegient forward logic to make it good performance

how to interact with cluster

master node

master processes

1 - api server

  • cluster gateway
  • acts as a gateway for authentication
  • only 1 entrypoint to the cluster

2 - scheduler

3 - controller manager

4 - etcd(cluster brain)

application data not stored in etcd

we can multiple master nodes

  • which have its master processes
  • api server is load balanced
  • etcd is distributed storage accross all master nodes

example cluster

MiniKube

  • a one node cluster
  • used to test locally
  • for testing purposes
  • creates a virtual box on your machine
  • node runs in that vbox

kubectl

we need to interact with api Server either using UI, api or CLI ⇒ kubectl and its the most powerful one

minikube installation on ubuntu

link

kubectl basic commands

create

kubectl create deployment NAME --image=image [--dry-run] [options]

configure kubectl with zsh

example

what happens when we run

kubectl create deployment nginx-depl --image=nginx

there's a middle layer between deployment and pod which is replicaset which creates the replicas for the pod

deployment has such as the blueprint for creating pod and its replica

the previous example is minimalistic providing name and image only and the rest is set by default

in real life yopu won't need to deal with replicaset but deployment

> $ k get pods
	> nginx-depl-5c8bf76b5b-mp6hz
> $ k get replicaset
	>nginx-depl-5c8bf76b5b 

layers of abstractions

‼️
everything below deployment is handled by kubernetes, we shouldn't have to worry about

debugging pods

kubectl excec -it [pod name] --bin/bash

delete deployment and apply config file

kubectl delete deployment [name]
‼️
all CRUD happens on the deployment level and all underneath follows

we deal with files for config

kubectl apply -f [filename]
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec: # specs of deployment 
  selector:
    matchLabels:
      app: nginx
  replicas: 1 # tells deployment to run 1 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec: # specs for the pod
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

summary

YAML config files

we have types for secrets

organizing components using namespaces

create namespace

from command line

kubectl create namespace [namespace-name]

add namespace config to file

apiVersion: v1
kind: ConfigMap
metadata:
  name: mongodb-configmap
	namespace: my-namespace
data:
  database_url: mongodb-service

why use namespace ?

  1. structure your components
  1. avoid conflicts between teams
  1. share services between differenet envs
  1. acces and resouce limits on namespace level

characterestics

  1. you can't access most of resouces from another namespaces
    1. each NS should create its own ConfigMap , secret
  1. access service from another namespaces
  1. components which can't live in namespaces
    1. live globally in cluster
    1. can't isolate them
    1. example : volume node

create components in namespace

K8S ingress

minikube config

minikube addons enable ingress 

this command automatically starts k8s Nginx implementation of Ingress controller

‼️
TBD complete ingress docs

HELM

package manager for k8s

templating engine

deploying same app across diff envs

helm chart structure

k8s Volumes

‼️
you need to explicility configure volumes to persist data because k8s doesn't provide that by default
  1. we need a storage that doesn't depend on pod lifecycle
  1. storage must be available on all nodes bec, we don't know on which node it will start
  1. storage needs to survive even if the cluster crash

local vs remote volume

who creates and when ?

storage class

statefulSet for stateful applications

k8s services

what and why ?

clusterIP services

service communication : selector

multiple port services

headless service

NodePort service

‼️
we have range : 30000 - 32767 only
‼️
not secure - not for production

load Balancer service