Docker and Kubernetes Concepts, Development from Zero to Hero
Hi everyone my goal in posting this content is have familiarity with docker and kubernetes , and help you how to getstart.
objectives :
- Understand Docker and Linux containers technology
- Understand containers orchestration requirements and available solutions
- Understand Kubernetes design, architecture and its main components
- Setup, configure, and operate production ready Kubernetes
- Design and deploy Cloud Native Applications on Kubernete
Linux Containers
- A container is a technique to isolate a process from other processes running on the same machine.
- Containers existed in Linux/Unix before anyone cared of them.
- Docker just made using Linux containers easier and mass adoption followed.
Docker
The Docker Engine is the Linux daemon responsible for management the containers.
what is docker ?
Docker is a platform for developers and sysadmins to build, share, and run applications with containers. The use of containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.
Images and Containers
A container is nothing but a running process, with some added encapsulation features applied to it in order to keep it isolated from the host and from other containers. One of the most important aspects of container isolation is that each container interacts with its own, private filesystem; this filesystem is provided by a Docker image. An image includes everything needed to run an application — the code or binary, runtimes, dependencies, and any other filesystem objects required.
Containers Vs Virtual Machines
A different way to achieve isolation between processes running on the same physical machine.
Orchestration
The portability and reproducibility of a containerized process mean we have an opportunity to move and scale our containerized applications across clouds and data centers , containers effectively guarantee that those applications will run the same way anywhere, allowing us to quickly and easily take advantage of all these environments. Furthermore, as we scale our applications up, we’ll want some tooling to help automate the maintenance of those applications, able to replace failed containers automatically and manage the rollout of updates and reconfigurations of those containers during their lifecycle. Tools to manage, scale, and maintain containerized applications are called orchestrators, and the most common examples of these are Kubernetes and Docker.
Now If you interesting with Docker and you need try to create and deploy a docker container image >>click here<< To play with Docker
I will help you to create and deploy your first container.
Running a container
Run an application by using the Docker client
docker run busybox:latest echo 'Hello Docker'docker run busybox:latest whoami
When the user specifies an image, Docker looks first for the image on local host. If the image does not exist locally, then the image is pulled from the public image registry >> Docker Hub <<click to going to docker hub
Run the Busybox container interactively
docker run -it busybox
Docker workflow
What happens behind the scenes
Run a container in daemon mode on a random port
docker run -d httpd
Run a container in daemon mode on a defined container port 80
docker run -d -p 80 httpd
Run a container in daemon mode and map the container port 80 to the host port 4000
docker run -d -p 4000:80 httpd
List running containers
docker ps
List all containers
docker ps -a
Remove a stopped container
docker rm webserver
Force remove a running container
docker rm -f webserver
Remove all containers
docker rm $(docker ps -aq)
Start a container and remove it when the container exits
docker run --rm -it busybox
Inspect a container
docker inspect webserver
See logs from a running container
docker logs -f webserver
See the Docker doc website for a complete list of commands. A Docker cheatsheet is available >>Click Here<<
Layered Filesystems
Docker uses a layered storage architecture for the images and containers. A layered filesystem is made of many separate layers to be combined and presented to the user as a single layer, creating the illusion that all files can be changed, including the files belonging to a read only layer.
What is Kubernetes and Why Kubernetes ?
Open Source platform for orchestrating containers in production.
Running containers in production:
- Clustering
- Scheduling
- High Availability
- Load Balancing
- Isolation
Kubernetes Model
Kubernetes Architecture
I will explain step by step to understand architecture, We have two main component first Master and second is Worker .
I will start explain worker, worker this component for end user and it have relationship with Master in the ‘kubelet’ component .
kube-proxy component manages the network rules on each node, Performs connection forwarding and load balancing.
Kubelet component acts as agent responsible for managing the entire lifecycle of workloads, Reports events to the APIs server, Interacts with the container runtime
Docker component this component storage image
Containers component here we have containers are running
Master Component this component for administrator , here we have four component first APIs server
APIs server component Forward facing REST interface into the kubernetes control plane and datastore, all clients and other applications interact with kubernetes strictly through the APIs Server, acts as the gatekeeper to the system by handling authentication and authorisation, request validation, and admission control, behaves as the front-end to the backing etc datastore.
Control manger component Serves as the primary daemon that manages all core component control loops , monitors the cluster state via the APIs server and steers the cluster towards the desired state.
Scheduler component Evaluates workload requirements and attempts to place it on a matching resource.
Etcd component Acts as the system datastore, provides a strong, consistent and highly available key-value store for persisting state, Stores objects and config information
High Avalaibility of Control Plane
Running containers in Kubernetes
A Pod is a set of one or more tightly coupled containers
Containers in a Pod can share data through local volumes and the network interface.
Deploy pods on the worker nodes
Pods are the minimum units of placement in Kubernetes.
ReplicaSet
I need stop here to explain ReplicaSet’s Component
A ReplicaSet’s purpose is to maintain a stable set of replica Pods running at any given time. As such, it is often used to guarantee the availability of a specified number of identical Pods .This component is responsible and for create and kill Pods , which means is responsible for high availability ,
this this component can create pods after declaring yaml and deploy yaml, which mean need put number of ReplicaSet for example is shown below .
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
# modify replicas according to your case
replicas: 3 << see here please
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: php-redis
image: gcr.io/google_samples/gb-frontend:v3
here i put replicas: 3
which mean create 3 pods , this is manual option declaring in ymal file and deploy yaml to scaling pods , we have many ways to high availability with automation , option one using Metric Server I will explain it below .
The three key parts of a ReplicaSet object:
- label selector
- replicas count
- pod template
ReplicaSet Controller loop
Pod High Availability
Metric Server
- An autoscaler obtains metrics from the Metric Server and rescales the target resource.
- Pod’s metrics are collected from cAdvisor and sent to the Metric Server.
Autoscaling based on CPU
Service provisioning
Services are provisioned on the worker nodes by the kube-proxy:
- userspace (deprecated)
- iptables (current default)
- ipvs (recently introduced)
Rolling Update
Rolling update with maxSurge=1 and maxUnavailable=0.
Attaching Volumes to a Pod
Services discovery
Core DNS Service:
- Services registers themself to the Core DNS.
- Pods query the Core DNS for service discovery.
Now we have two way to play with kubernetes tool First download ‘Minikube’
>> click here << to setup local in your laptop “Minikube”
>>click here << to more details how to setup
Command Line for Kubernetes
$ kubectl version
The configuration file
$ cat .kube/config
A pocket cheatsheet for kubectl
can be found >>click here<<
Note please for best practice using Separated yamls each of component and using HELM to run all ymals one time
Deploy complex applications through a lot of yaml files can be a daunting task.
Helm is a tool for application management based on charts.
Please visit my account on GitHub to get my kubernetes files to try deploy
Thank you so much for read my tutorial and i hope you got my point , By
— — - — — — — — — Mohsen Talal — — — — — — — — — -