Introduction to Kubernetes notes

What are containers?

What are containers?

As part of Docker documentation “a container is a standard unit of software that packages up code and all its depedencies so the application runs quickly and reliably from one computing environment to another”.

The advantages of containers

  • Portability: Containers let you run software easily in a variety of environments.
  • Consistency: Containers help software run in the same way no matter which environment you running in.
  • Low overhead: Containers use fewer resources than some other technologies, such as virtual machines.

Container Runtimes

A container runtime is the software used to run containers on the machine

What does kubernetes do?

As part of Kubernetes documentation “Kubernetes also known as K8s is an open-source system for automating deployment, scaling, and management of containerized applications”.

  • Kuberbetes helps you deploy containers across a pool of compute resources, such as servers.
  • Kubernetes allows you easily manage multiple replicas of your application across multiple servers.
  • Kubernetes makes it easy to scale up (run more replicas of the application) and scale down (run fewere replicas of the application)
  • Kubernetes provides a framework for managing and controlling your containers network communications.
  • Kubernetes offers a variety of features that give you the ability to build more secure applications.
  • Kubernetes can help you manage application configuration and pass configuration data to your containers.

What is a kubernetes cluster?

A Kubernetes cluster is a collection of worker machines that run containers.

What is kubernetes control plane?

  • A collection of services that control the cluster.
  • User interacts with the cluster using the control plane.
  • Monitors the state of the cluster.

Those services can run on any node of the cluster but usually those are dedicated nodes.

What are kubernetes worker nodes?

  • A node that runs containers within the cluster
  • Runs and manages containers on the node
  • Monitors the state of containes on the node and reports the status back on the control plane.

Kubernetes control plane components

  • Consists of multiple individual components
  • They can run on any node
  • Can run multiple instances of each component for high availability

Build a Kubernetes cluster

On all nodes run the following, those commands load some kernel modules and modify system settings that are required.

cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf 
overlay 
br_netfilter 
EOF 
 
sudo modprobe overlay 
 
sudo modprobe br_netfilter 
 
cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf 
net.bridge.bridge-nf-call-iptables  = 1 
net.ipv4.ip_forward                 = 1 
net.bridge.bridge-nf-call-ip6tables = 1 
EOF 
 
sudo sysctl --system

Install and configure containerd

sudo apt-get update && sudo apt-get install -y containerd 
 
sudo mkdir -p /etc/containerd 
 
sudo containerd config default | sudo tee /etc/containerd/config.toml 
 
sudo systemctl restart containerd

On all nodes install kubeadm, kubelet and kubectl

sudo apt-get update && sudo apt-get install -y apt-transport-https curl 
 
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - 
 
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list 
deb https://apt.kubernetes.io/ kubernetes-xenial main 
EOF 
 
sudo apt-get update 
 
sudo apt-get install -y kubelet=1.20.1-00 kubeadm=1.20.1-00 kubectl=1.20.1-00 
 
sudo apt-mark hold kubelet kubeadm kubectl

On control plane node initialize the cluster and set-up kubectl access.

sudo kubeadm init --pod-network-cidr 192.168.0.0/16 
 
mkdir -p $HOME/.kube 
 
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config 
 
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Verify that cluster is working

kubectl version

Install the Calico network add-on

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

After some minutes verify that the calico related kube-system pods working

kubectl get pods -n kube-system

Get the join command, execute this on the control node

kubeadm token create --print-join-command

Copy the join command output from the control plane node and run it on each node as root

sudo kubeadm join ...

On control plane node verify that all nodes in your cluster are ready

kubectl get nodes

What is the Kubernetes API?

  • Is the core of kubernetes control plane.
  • Is a basic HTTP API.
  • The API lets users query and manipulate objects, thereby controlling the cluster.

kubectl is a tool that allows us to interact with the kubernetes cluster using the kubernetes API.kubectl get pods -n kube-system

Pods running under the kube-system namespace are pods responsible for kubernetes functionality.

if -n parameter is ommited then kubectl will query the API for pods running on the “default” namespace.

We can do an HTTP query directly using the raw parameter and the answer will be JSON formated.kubectl get --raw /api/v1/namespaces/kube-system/pods

If we want to query a specific podkubectl get --raw /api/v1/namespaces/kube-system/pods/etcd-k8s-control

Kubernetes objects

  • Kubernetes objects are persistent data entries stored by kubernetes, They represent the state of the cluster.
  • You can deploy and configure applications, run containers and configure cluster behavior by creating, modifying and deleting objects.
  • Kubernetes objects are usually represented in the form of YAML data, the following object is an nginx pod running an nginx container.apiVersion: v1
    kind: Pod
    metadata:
     name: nginx-pod
    spec:
     containers:
     - name: nginx
  • The apiVersion specifies which version of the kubernetes API the YAML data is compatible with.

kubectl command line tool

Is the official command line interface for kubernetes

Get list of serviceskubectl get service

Create a YAML definition for a service

apiVersion: v1 
kind: Service 
metadata: 
  name: my-service 
spec: 
  selector: 
    app: my-app 
  ports: 
    - protocol: TCP 
      port: 80 
      targetPort: 80

Create the service

kubectl create -f my-service.yml --save-config

The save-config option says kubernetes to save a copy of this file. (where?)

Get the list of services again

kubectl get service my-service

Get detailed info about the service

kubectl describe service my-service

Edit the YAML file

targetPort: 8080

Apply the service

kubectl apply -f my-service.yml

Delete the service

kubectl delete service my-service

Managing containers with pods

A pod is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containersapiVersion: v1
kind: Pod
metadata:
 name: nginx
spec:
 containers:
   name: nginx
   image: nginx
   ports:
   - containerPort: 80

  • kind — Specifies the object type as Pod.
  • metadata.name — The name of the Pod object.
  • spec.containers — Provides a list of one or more containers included in the Pod.
  • spec.containers[].image — Specifies the name of the container image containing the software you want to run.

To create the pod

kubectl create -f nginx-pod.yml

To check pod status

kubectl get pods -o wide

Check the nginx container logs

kubectl logs nginx
Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…