Core-Concepts

Cluster Architecture

  • Kubelet listens for commanda (on each node)

  • Kube proxy manages communication between workers (on each node)

Containers

CRI - lets different solutions for running containers work (containerd etc)

Imagespec - how container images are setup Runtimespec - how containers run

ContainerD

For debugging ctr official tool

Alt tool: nerdctl - more user friendly, similar to docker cli

crictl works across all CRI runtimes, good for debugging

Very similar to docker

etcd

  • KV store

  • 2 main APIs (v2, and v3), significant API change

  • All k8s changes modify etcd

Components

  • kube-apiserver

    • Who you talk to with kubectl

    • Only think that talks to etcd

    • either

      • process with settings in systemd service

      • or pod with settings in /etc/kubernetes/manifests/kube-apiserver.yaml (kubeadm)

  • kube-scheduler

    • Schedules pods on workers, updates etcd

    • decides which pod goes where based on requirements

  • kubelet

    • Makes changes on worker

    • does EVERYTHING on node, communicates with api-server

    • Need to run on worker as service

  • Controller-Manager (brain of k8s)

    • Manages controllers (processes that monitor status of components, nodes etc)

    • Controllers are inside Controller-Manager process

  • kube-proxy

    • Deals with communications

    • Internal IPs can change on nodes, we use services instead of pod IPs

    • kube-proxy runs on each node and creates rules based on services so pod is accessible

Pods

  • We can create pods with yaml

  • Several keys required in yaml

Required:

Typical pod values:

For viewing state:

Checking where pod is located:

ReplicaSets

  • A controller

  • Lets is run multiple pods for HA

  • Enforces number of pods

  • Also used for load scaling

  • Controller with balance pods across multiple nodes

ReplicaSet replaces depreciated Replication Controller

Depreciated Replication Controller:

Create:

So spec.template is children

ReplicaSet:

selector is main difference, its required and takes children labels

Create:

ReplicaSet monitors and keeps pods up based on labels and selectors.

Scaling

Several options for scaling.

Deployments

Used for rolling updates and scaling.

Deployments are a superset of other objects like ReplicaSet

Compared to ReplicaSet only kind: Deployment needs changing:

Creating YAML in CKA

Using the kubectl run command can help in generating a YAML template. And sometimes, you can even get away with just the kubectl run command without having to create a YAML file at all. For example, if you were asked to create a pod or deployment with a specific name and image, you can simply run the kubectl run command.

Create an NGINX Pod

Generate POD Manifest YAML file (-o yaml). Don’t create it(–dry-run)

Create a deployment

Generate Deployment YAML file (-o yaml). Don’t create it(--dry-run)

Generate Deployment YAML file (-o yaml). Don’t create it (--dry-run) and save it to a file.

Make necessary changes to the file (for example, adding more replicas) and then create the deployment.

OR

In k8s version 1.19+, we can specify the –replicas option to create a deployment with 4 replicas.

Services

Help with establishing connections.

Pods are on private net, we need to expose services within them

Service is an object that:

  • NodePort: forwards ports from node to pod

  • ClusterIP: Creates virtual IP for internal communication

  • LoadBalance: Distributes traffic

NodePort

  • TargetPort: pod port

  • Port: port for Service to Pod

  • NodePort: port on Node

NodePort

For multiple Pods the service matches all matching labels and load balances

When Pods are on different nodes the service spans them all, and you can use any node IP.

ClusterIP

When many clusters of Pods need to talk between various services we use ClusterIP:

ClusterIP

LoadBalancer

Lets us use ONE ip for app.

Uses native cloud provider LB. If unsupported reverts to NodePort. Same config as NodePort.

Namespaces

Allows grouping resources.

Default namespace is default. kubernetes shas a few for the system:

  • kube-system

  • kube-public

Can set quotas per namespace.

If you are connecting to external namespaces outside of your own you need to append the namespace:

EG: For db-service in dev namespace:

  • db-service.dev.svc.cluster.local

This DNS entry is added by default.

Namespaces-DNS

Can put namespace in metadata

To create a Namespace:

or

We can switch namespace:

All namespaces:

For quotas:

Imperative vs Declerative

Imperative:

Declerative:

Use kubectl and describe state:

apply modifies state to match file.

Declerative is best practice.

For apply:

  • Edit original yaml

  • kubectl apply -f $TARGET

  • Can kubectl apply -f $DIRECTORY

Apply

3 States:

  • Local file

  • Last applied

  • Live object

  • If object doesnt exist, apply creates

  • The initial state is stored

  • On next change we compare differences with "last applied"

  • Intelligently updates live configuration

Dont mix apply and imperative.

Last updated