AGENDA:
-------
- Dockerfile instructions
- CMD vs ENTRYPOINT
- Registry in dockerhub
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ex1: create an image named ubuntujava with tag as v1
take ubuntu as the base image, update apt package & install java (or default-jre)
Whenever i start my container, echo comamnd to get executed
FROM ubuntu
RUN apt update -y
RUN apt install default-jre -y
CMD ["echo","i have created image / container with java installed"]
#observation: build the image & run container, once container gets started observe the CMD instruction gets executed (echo command)
Note:
-----
as best practice in CMD & ENTRYPOINT instructions use them inside square brackets with comma seperation,
i.e exec form ==> ["<commands>"]
ex: ["echo","hello world"]
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Instructions in Dockerfile (continued....)
-------------------------------------------
8) COPY --> Copying the files from the dockerhost machine to the container.
syntax: COPY <files/Directory_name_in_dockerhost> <path_inside_container>
9) ADD --> Used for copying files from host to container, it can also be used for downloading files from remote servers.
synatx: ADD <files/Directory_name_in_dockerhost> <path_inside_container>
ADD https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.82/bin/apache-tomcat-9.0.82.tar.gz /opt
What is difference between COPY & ADD instruction in Dockerfile?
-----------------------------------------------------------------
Both COPY & ADD instructions are used to copy files/Directories from DockerHost to Container.
ADD can also be used to download file (like wget command in linux) & also ADD can untar/unzip file inside containers,
10) ENV --> used for specifying the environment variables that should be passed to the container.
ENV <Variable_name> <variable_value>
ENV sportsman viratkohli
inside container ==> echo $sportsman
11) EXPOSE -- Used to specify the port on which we want to run our container
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
What is difference between CMD & ENTRYPOINT instruction in Dockerfile:
------------------------------------------------------------------------
CMD ==> we will define the commands that needs to be executed when a container starts.
ENTRYPOINT ==> we will define the commands that needs to be executed when a container starts.
*****important_interview_question*************
Difference between CMD & ENTRYPOINT ?
-------------------------------------
CMD sets the default commands that needs to be executed when a container starts.
CMD command/instruction can be easily overridden while creating a container with different command.
whereas ENTRYPOINT command/instruction can't be overridden while creating a container.
Most of Docker containers by default have ENTRYPOINT ==> which is ==> /bin/sh -c
ENTRYPOINT generally used if we want to container as an executable ( like only purpose of running container is to run a script only)
Can we have both CMD & ENTRYPOINT in docker file?
-------------------------------------------------
Yes we can have both in a Dockerfile. but CMD instructions will be passed as an arguments for ENTRYPOINT.
ex: usecase1 of using both CMD & ENTRYPOINT?
FROM ubuntu
CMD ls
ENTRYPOINT ["echo", "Helloworld"]
build above Dockerfile, run container ==> container Will be executed as below ===> Helloworld ls
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Scenario 1:
-----------
Create a dockerfile by taking
- ubuntu as the base image
- create a user as bharath & make him as default user after logging in container
- default working directory as opt
Construct an image from the dockerfile.
vi Dockerfile
FROM ubuntu
RUN useradd Bharath
CMD ["/bin/bash"]
ENTRYPOINT ["echo", "Helloworld"]
USER bharath
WORKDIR opt
RUN apt update -y && apt install default-jre -y && apt install maven -y && apt install git -y
COPY ./samplefile /opt
ADD https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.0-M26/bin/apache-tomcat-11.0.0-M26.zip /opt
ENV king kohli
or
FROM ubuntu
RUN useradd Bharath
CMD ["/bin/bash"]
ENTRYPOINT ["echo", "Helloworld"]
USER bharath
WORKDIR opt
COPY ./samplefile /opt
ADD https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.0-M26/bin/apache-tomcat-11.0.0-M26.zip /opt
ENV king kohli
docker build -t <image_name> .
create conatiner & verify
Scenario 2:
------------
Create a dockerfile by taking
- alpine as the base image
- copy samplefile to docker container to /opt directory
Construct an image from the dockerfile.
--> create a file called samplefile in dockerHost
vi Dockerfile
FROM alpine
COPY ./samplefile /opt
docker build -t <image_name> .
create container & verify
Scenario 3:
------------
Create a dockerfile by taking
- busybox as the base image
- download maven installation file to docker conatiner /opt directory
Construct an image from the dockerfile.
FROM centos
ADD https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz /opt
Note:
-----
Alpine & busybox are ligh weight (smaller sized) docker images which will have all linux utilities
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
How long will a docker container run?
--------------------------------------
Every docker image come with default process.
As long as default process is running, the container will be running condition. The moment, the default process execution is completed, the container will get itself moved to exited / stopped state.
(default process ==> whatever mentioned in CMD instruction in Dockerfile)
in already running containers, we can check default process using docker ps command & observe under COMMAND sections
Practicals & observations on understanding default process in containers:
-------------------------------------------------------------------------
scenario 1:
-----------
Create Dockerfile with below mentioned instructions
FROM ubuntu
CMD ["date"]
build this Dockerfile & create a conatiner from it & observe that conatiner has moved in to exited state.
Reason? ==> container has exited, because it has completed running its default process (whatever mentioned in CMD instruction i.e date command )
For all linux based containers( ubuntu,centos,amazonlinux.....) , the default process is shell process for that Dockerfile will look like
FROM ubuntu
CMD ["/bin/bash"]
/bin/bash or bash -- is nothing but the terminal.
Hence we are able to enter -it mode in ubuntu/centos or any OS based containers.
alltogether
FROM ubuntu
RUN useradd bharath
USER bharath
WORKDIR opt
COPY ./samplefile /opt
ADD https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.0-M26/bin/apache-tomcat-11.0.0-M26.zip /opt
ENV king kohli
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Kubernates
Docker containers have several drawbacks, and container orchestration helps overcome these drawbacks in the following simple terms:
Drawbacks of Docker Containers:
-------------------------------
1. Complexity:
If we have multiple containers, Managing many containers can be complex and time-consuming.
2. Scaling:
It can be challenging to scale containers up and down to match application demand.
3. High Availability:
Ensuring applications are always available can be difficult.
4. Load Balancing:
Distributing incoming traffic evenly among containers can be a manual task.
5. Resource Efficiency:
Containers can sometimes use hardware resources inefficiently, leading to wastage.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Container orchestration:
------------------------
Container orchestration is a process of running docker containers in a distributed environment, on multiple docker host machines.
container orchestration tools:
------------------------------
Docker swarm / kubernetes is the tool used for performing container orchestration.
kubernetes is most widely used container orchestration tool in market.
If we are only running a single container or two containers together then we may not need an orchestrator.
How Container Orchestration Helps in overcoming docker container limitations?
-----------------------------------------------------------------------------
Container orchestration tools like Kubernetes solve these problems:
1. Simplies the container handling:
Orchestration tools takes care running containers in different dockerhosts, which will help in managing containers.
2. Auto-Scaling of containers:
scaling means increasing / decreasing the number of containers.
Orchestration tools automatically scale containers based on demand, so your application can handle traffic spikes.
3. High Availability:
if a container fails (or) exits, orchestration tools automatically restarts the failed container (or) it will replace failed containers with new containers thus ensuring availablity.
4. Networking:
Orchestration tools can create & handle container networking, making it easier for containers to communicate with each other.
5. Resource Efficiency:
Orchestration tools helps in controlling hardware resources used by containers.
6. Load Balancing:
rather than running all container in 1 docker host, we can distrubute load (num of containers) to multiple docker host
In simple terms, container orchestration makes it easier to deploy, manage, and scale containers, ensuring that your applications run smoothly, reliably, and efficiently in a containerized environment.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Note on Keywords:
---------------------------
High availability (HA):
-----------------------
means any application is expected to be up & running all time.
we should build an application environment, which should be highly available.
Downtime :
----------
if an application goes down due to any issue, we will call that as downtime.
we should build a system which will have zero downtime.
what all containerizations tools avaialble in market?
==> Docker, container D , Podman & Rocket etc...
docker is most popular & widely used containerization tool
what all container ORCHESTRATION tools avaialble in market?
==> kubernetes, docker swarm , mesos
kubernetes is most popular & widely used container ORCHESTRATION
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
KUBERNETES:
-----------
- It is a container orchestration tool.
- Kubernetes is an open-source tool developed using GO programming language.
- Kubernetes is als called as k8s
- k8s was developed by Google in 2014 it was made open source & donated to CNCF.
k8s designed in master-slave type architecture.
Kubernetes creates cluster, where we will deploy our application in docker containers form.
cluster is hosted by one node acting as the ‘master’ of the cluster, and other nodes(or dockerhosts) as 'worker nodes (or slave nodes)' which do the actual 'containerization' using docker.
what is cluster?
==> cluster is a combination of k8s master node (control plane) + worker Nodes (docker hosts,dataplane)
what is Master Node?
- The master node is responsible for the management of Kubernetes cluster.
it has 4 main components kube-api server, kube scheduler, controller manager & etcd.
what is Worker Node?
- Worker nodes are the nodes where the application actually running in kubernetes cluster.
it has 3 main components kubelet (agent) , kube-proxy & container engine (docker)
Important Note on Naming conventions in kubernetes:
----------------------------------------------------------------------------
1. servers==> vm == machine == nodes == slaves(workers) == instances ==> same
2. Kubernetes is also called as k8s
3. kubernetes master is also called as control plane
4. kubernetes nodes are also called as slave node or worker nodes
5. yaml files used in k8s are also called as definition files (or) manifest file.
What is Kubernetes (K8s)?
---------------------------
Kubernetes is a powerful tool used to manage and orchestrate containers, it helps us to manage deploy, scale our containers easily and efficiently.
Why do we need Kubernetes?
-------------------------
After Docker came into the Picture, the deployment of the applications was very easy on the containers because containers are lightweight.
But after some time, there were a lot of issues arose such as managing the huge amount of containers in the Production environment where Containers getting failed leading to huge Business losses.
After Kubernetes came, it automates many tasks such as:
- Autoscaling of Containers according to the peak or normal hours.
- Load balancing of multiple containers.
- Automatically deployment of containers to the available nodes in the cluster.
- Self-healing if containers fail.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
In Kubernetes we create cluster, in this cluster we will deploy our containerized application in docker containers form.
This cluster is hosted by one node acting as the ‘master’ of the cluster, and other nodes as ‘worker nodes’ which do the actual ‘containerization‘ using docker.
what is cluster?
==> cluster is a combination of k8s master node (control plane) + worker Nodes
what is Master Node?
- The master node is responsible for the management of Kubernetes cluster.
it has 4 main components kube-api server, kube scheduler, controller manager & etcd.
what is Worker Node?
- Worker nodes are the nodes where the application actually running in kubernetes cluster.
it has 3 main components kubelet (agent) , kube-proxy & container engine (docker)
explain kubernetes architecture?
===========================
kubernetes cluster conatins master & worker nodes.
Master Node
------------------
- The master node is responsible for the management of Kubernetes cluster.
Master contains 4 components.
1) kube-api server
2) kube scheduler
3) controller manager ( acutal state = desired state )
4) etcd
1) kube-api server:
-- kube api server is a Front end to k8s cluster ( like receptionist).
-- It receives the yaml file as input and pass the request to kube scheduler.
2) kube scheduler
-- kube scheduler will take input from kube-api server.
-- So kube scheduler will decide where (in which worker node) to create pods (containers).
3) controller manager:
-- it is the brain behind orchestration process
-- it will constantly monitor the cluster, it will keeps on check if any pods(container) goes down it will try to bring that up.
-- it will always try to make sure that the actual state is same as desired state.
4) etcd:
-- ETCD is like a database to k8s cluster.
-- all information about cluster will be stored here. information like master & worker nodes information, Pods IP's & network details will be stored in etcd.
-- Data is stored in key-value pair, fomat.
All above four components together called as k8s master node.
Worker Node
--------------------
- Worker nodes are the nodes where the application actually running in kubernetes cluster.
Node contains 3 components
1) kubelet
2) container engine
3) kube-proxy
1) kubelet:
--------------
-- is also called as agent, kubelet communicates to kube-scheduler & take actions.
-- kubelet further communicates to container engine ( i.e docker ) so that containers are created.
2) container engine ( docker)
-- container engine recieves the request from kubelet & creates containers
Note: Containers are created inside in pods
3) kube-proxy
kube-proxy is used for networking puprposes, like assigning ip address to pods.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
what is pod?
------------
- Smallest deployable Object that kubernetes can create is pod. Within the pod, we have our containers running.
Note on pods:
-------------
- pod is just like a wrapper(box) around container
- k8s cannot create containers directly so it will create pod (containers will be running inside the pod)
- in 1 pod we can create 1-2 containers, but its best parctice to run 1 container in a pod.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Kubernetes commands are always starts with kubectl.
eg:
to list all pods ==> kubectl get pods
to list all nodes( master + worker nodes) ==> kubectl get nodes
Note on ways of installing k8s:
-------------------------------
kubernetes can be installed using 2 or 3 ways
1. Self managed k8s clusters:
kubeADM way ==> installing k8s software directly on servers by ourselves
we have to manage cluster
2. Using cloud provided k8s services:
-- To practice Kubernetes on AWS , we have a service EKS ( Elastic Kubernetes Service )
entire k8s cluster will be created by AWS cloud provider, AWS will take care of maintaining k8s master server, we only need to add worker nodes to it.
easiest way but it is billed.
3. Minikube:
Minikube is a tool that allows you to run a single-node Kubernetes cluster locally on your machine.
It is designed to enable developers to develop and test applications locally before deploying them to a full-scale Kubernetes cluster.
generally used by developers
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Create Pod
-------------------------
All the commands in kubernetes sill start from kubectl
what is command to create containers in docker?
docker run --name <containername> <image_name>
kubectl run --image <image_name> <pod_name>
kubectl run --image tomee mypod1
How to create pods in kubernetes ? :
------------------------------------
Pods can be creted in two ways
1. YAML files:
- yaml files used in k8s are also called as definition files (or) manifest files.
- yaml file based creation of k8s objects are preffered as we can keep files used for objects creation.
we will write definition files (or) manifest files & execute these file to create pods.
2. commands:
- Through command line (CLI) we can create k8s objects as well
- creating k8s from cli is not best practice
- preffred only, if we want to create a k8s object quickly.
syntax: to create pod from commands (CLI)
kubectl run --image <image_name> <pod_name>
kubectl run --image tomee tomcat-pod
kubectl run --image <docker_imageName> <pod_name>
kubectl run --image tomee tomcatpod
kubectl run --image jenkins/jenkins myjenkinspod
kubectl run --image alpine myalpinepod
To see list of all pods:
------------------------
kubectl get pods
To see list of nodes in a cluster:
----------------------------------
kubectl get nodes
To delete any pod:
------------------
kubectl delete pods <pod_name>
To know on which worker node, pod is running:
----------------------------------------------
kubectl get pods -o wide
( -o wide ==> stands for deatiled output )
-----------------------------------------------------------------------------------
Note:
-----
in kubernetes we will call yaml files as definition files or manifest files ==> both are same
if i write yaml file to create pod ==>
How to write manifest files or yaml files in k8s ?
==================================================
Kubernetes performs container orchestration by using definition files.
Definition file, will have 4 top level elements.
to create any object in k8s we need to write yaml files
all yaml / manifest files in k8s will contain 4 top level elements:
1. apiVersion: apps/v1
2. kind:
3. metadata:
4. spec:
1. apiVersion:
-------------------
Depending on type of kubernetes object we want to create, there is corresponding code library we want to use.
Kind apiVersion
==================================================
Pod v1
Replication COntroller v1
Service v1
NameSpace v1
Secrets v1
ReplicaSet apps/v1
Deployment apps/v1
2. kind: Pod
----------
Refers to kubernetes object which we want to create.
Ex: Pod, Replicaset, service etc
3. metadata:
-----------------
Additional information about the kubernetes object
like name, labels etc
note: labels are used for filtering
4. spec (speicifications):
----------
Contains docker container related information like image name, environment variables, port mapping etc.
spec block
spec:
containers:
- name: <name_of_container>
image: <name_of_image>
Note:
-------
- command to get detailed information about a containers?
docker inspect containername/container id
- command to get detailed information about a pod?
kubectl describe pods <pod_name>
1. it will give detail about
- name of pod,
- on which node pod is running
- labels assigned for pod
- IP of pod
2. in containers block give detail about
- Containers name & container ID
- Image used to create container.
- Details about port / volume mapping & environmentvariables etc
--------------------------------------------
Creating PODS from manifest files:
===================================
Note:
----
- in k8s we will call YAML files as manifest files or definition files.
- 1st element of manifest file apiVersion is written in camel case letters ==> apiVersion: v1
- 2nd element kind will have first letter in uppercase
eg: kind: Pod , kind: Replicasets
------------------------------------------------------------------------------------
Ex1: Create a pod definition file to start apache tomcat in a pod.
Name the pod as tomcat-pod, name the container as tomcat-container.
vi pod-definition1.yml
# what do we need to create?
# pod ==> podname ==> tomcat-pod, container-name ==> tomcat-container , image ==> tomee , label app: tomcat_label & author: bharath
---
apiVersion: v1
kind: Pod
metadata:
name: tomcat-pod
labels:
app: tomcat_label
author: bharath
spec:
containers:
- name: tomcat-container
image: tomee
...
:wq!
kubectl create -f pod-definition1.yml
-----------------------------------------------------
============================================
Command to run the definition file:
-----------------------------------
kubectl create -f <filename>.yml
(or)
kubectl apply -f <filename>.yml
To delete the pod created from the any file:
---------------------------------------------
kubectl delete -f <filename>.yml
==================================================
kubectl get nodes ==> show all nodes of cluster & thier status
kubectl get pods ==> shows all the pods
kubectl run --image <imagename> <podname> ==> to create pod from command directly
kubectl delete pod <podname> ==> to delete pod
------------------------------------
Note:
-----
how to login into container in docker?
docker ==> docker attach <container-name>
docker exec -it <containername> /bin/bash
note: /bin/bash command will open linux terminal inside the container
How to login into pod ?
-----------------------
kubectl exec -it <podname> -- <linux_command>
kubectl exec -it mytomcatpod1 -- /bin/bash
note: /bin/bash command will open linux terminal inside the pod/container
to come out of pod use ==> exit (this will not stop container)
How to run any command in pod from k8s master ?
------------------------------------------------
kubectl exec -it <pod_name> -- <linux_command>
eg: to get container os information
kubectl exec -it <pod_name> -- cat /etc/os-release
Recap of Important pointers about containers:
---------------------------------------------
1. containers will run only till default process is running
that is whatever mentoined CMD / ENTRYPOINT instruction in any dockerfile
2. Containes are ephemeral(shortlived) by default.
that is once containers is deleted, all data inside containers will get deleted permanantely
3. Containers will work on PROCESS ISOLATION principle.
that is process running inside a container will be completely isolated from any other process in docker host