Print
Category: Technology
Hits: 18664

What is docker?
Our application needs required environment and start it in the expected way
Docker does these on behalf of us by setup the required environment and lauch the app provided the details in a configuration file

What is kubernetes?
It is a tool for running a multiple different containers

How kubernetes works ?
We give instruction through a configuration file which tells how do we want the containers to run and interact (network communication) with other containers

What are all the major components in kubernetes?

It consist of 2 major components

1) Cluster (Its a collection of Node. Node is nothing but a virtual machine)
2) Master (It manages the cluster and get our instruction and run the container accordingly across the cluster of virtual machines (nodes) )

What is the configuration file used to tell docker how to setup environment and run the app?

The config file name called "Dockerfile" (note there is no extension for this file) which contains the list of instruction to setup environment and idea about how to start the application

Example "DockerFile"

FROM    node:alpine
WORKDIR /app
COPY package.json ./
RUN npm install
COPY ./ ./
CMD ["npm","start"]

Where,

FROM        => Refers to an image of the framework we want to setup (.net, node JS). You can compare it with ISO image of OS
WORKDIR  => Create and set a folder inside the docker where we are going to setup our application in this folder
COPY         => Copy file named "package.json" from local folder into the folder "app" inside docker
RUN           => Executes given parameter inside docker.

                      Example, "npm install" installs all libraries listed in the copied "package.json" file (above step)


COPY         => Copy local files into the docker image(mapped folder ie., app).

                       Example, it copies all files of Node JS into "app" folder in docker


CMD           => Used to run any command with parameter inside docker.

                       Example, run the command "npm" by supplying parameter "start" which launches node JS app

 

Can you list few important docker commands to build and run?

- Build a docker image based on docker file (example above) in the current directory and tag it as tekkieshaper/customer

$ docker build -t tekkieshaper/customer .

- Start a container based on the provided image

$ docker run tekkieshaper/customer

- Start a container based on image id/tag and land on container's command prompt (interactive mode)

$ docker run -it tekkieshaper/customer

- how to find the container id ? print the running container and get its id

$ docker ps

- run a command in the running container ie., inside the container


$ docker exec -it [containerID] [command]

$ docker exec -it 58ccab3c1e71 sh

Does container will permanently removed it once its stopped (docker stop <<container_ID/Name>>)?

- No, it persists with "Exited (255)" state and you can't assign this container name to any other new container until it get deleted

$ docker ps (displays only running container)

How to delete all stopped container, unused networks and dangling images & cache?

$ docker system prune

WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all dangling build cache

Are you sure you want to continue? [y/N] y

Deleted Containers:
c89c7faa9afd74fc8ac0bcdee7b77a3d12be0625fbaf5e2d010ec8097e2dd3dc
df6e7c7a8457090f68ab8a91035737bc68f6afdca2103af2feb5151ec89688ba
5f7bac50988126cf6f09f5f0ecda631fc6f3fe1a615382765a823958c42680f6
3e33a42f0eeedb6c21470c86a65c4a431d306e55d314335783fb58183c1247c1
690c67dfeeb8e85f0d963c14ff28608a12985158941d8f73c403ab1770aada9f
5f2ac36dbd45122e975a4b18d6f1808077b5b48a37ec31d538e2774dc1040ed4

Total reclaimed space: 1.234GB

 

Can we extract only only container's ID of exited container?

- Yes, possible using below command

$docker ps --filter status=exited -q

How to remove volumes, networks & image
$docker system prune --volumes

(or)

prune networks, images and volumes
$docker network prune
$docker image prune
$docker volumes prune

 

 

 

How to remove all stopped containers without asking confirmation (force) which are created more than 5 minutes ago (until=5m)

$docker container prune --force --filter "until=5m"

 

How to install docker framework on windows platform?

You can follow the instruction under https://docs.docker.com/docker-for-windows/install/

Note: You need to register a program called "windows insider program" in Microsoft which highlights that this framework is still at testing phase and not responsible for any crash https://insider.windows.com/en-us/register

What are all the important terminologies in Kubernetes?

Cluster  => collect of nodes + a master to manage them
Node     => virtual machine which run our container
Pod       => similar to container but a Pod run multiple container
Service  => provide an ease way to identify running URL and access a running container
deployment => monitors a set of pods and make sure they are running and restarts them if they crash

What is the use of Kubernetes config file?

It tells Kubernetes about creation of different objects of it. Kubernetes object includes deployments, pods and services

What is the format used in Kubernetes config file?

Its YAML syntax

Where the Kubernetes file stored?

It always store in project source code (ex: projectfolder\infra\k8s\configFileName.yaml)

What are all ways to create Kubernetes objects (deployment, pods,service)?

It can be created,

Option 1: Directly run the command (kubectl) as per kubernetes document but only do this for testing purpose (NOT RECOMMENDED)

Option 2:  Through config file which provides clear definition of what our cluster is running (RECOMMENDED WAY as it act as documentation)

What is the use of provide version number in docker image?

Upon see the version number by Kubernetes it checks the image on local otherwise it first look up is in dockerhub.io

List important Kubernetes commands to build and run?

kubectl apply -f helloworld.yaml (tells Kubernetes to process the config file)

kubectl get pods (list all the running pods)

kubectl describe pod helloworld (print info about the pod named 'helloworld')

kubectl logs helloworld (print logs from the given pod helloworld)

kubectl exec -it helloworld sh (it run command on container inside a pod, if it more than 2 container run then it asks under which one to run the command)

kubectl delete pod helloworld (it delete the pod and container inside)


Why should we use deployment instead directly create pod using Kubernetes config file?

1) If any container crash then deployment automatically recreate it

2) If any new version of the image available then deployment create container for the new image and close previous version container after serve the current request which is being processed

 

Give an example of Kubernetes config file for POD?

helloworld-pod.yaml
 
apiVersion: v1

kind: Pod

metadata:

  name: helloworld

spec:

  containers:

   - name: helloworld

     image: cmr/helloworld

     imagePullPolicy: Never
Run: 

kubectl apply -f  helloworld-pod.yaml
kubectl get pods

 Give an example of Kubernetes config file for DEPLOYMENT?

helloworld-deploy.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: helloworld-deploy

spec:

  replicas: 1

  selector: 

    matchLabels:

      app: helloworld

  template:

    metadata:

      labels:

        app: helloworld

    spec:

      containers:

        - name: helloworld

          image: cmr/helloworld
          imagePullPolicy: Never

              

Run:

kubectl apply -f helloworld-deploy.yaml

kubectl get deployments

kubectl describe deployment helloworld-deploy

kubectl delete deployment helloworld-deploy

How do you update the image used by the deployment?

Option 1:

Make required changes in the project

Rebuild the image by specifying a version in suffix 

In the deployment config file, update the version of the image

kubectl apply -f helloworld-deploy.yaml  (Kubernetes try to find whether this config file already executed or not)

Note: In this case, Kubernetes tells deployment is configured instead created for the first time

Option 2:

Make required changes in the project

Rebuild the image by specifying a version as "latest" or leave empty

Push the docker image to docker hub (docker push cmr/helloworld)

No update in deployment config file which is the advantage in this preferred option 

kubectl rollout restart deployment helloworld-depl

Note: deployment won't recreate only pod get recreated and you can check these by running (kubectl get deployments & pods)

What is service in Kubernetes?

Its an object which makes a channel to communicate between pods and allow pod access from outside world

What are all the type of Kubernetes services available?

1) Cluster IP - help to setup ease URL to access a pod but it exposes pods in the cluster

2) Node Port - helps to access pod from outside the cluster and it is normally used for development purpose

3) Load Balancer - same like "node port" to access pod from outside cluster in production environment

4) External Name - helps to redirects an in-cluster request to CNAME URL

Can you give an example of service config with Node Port file?

helloworld-service.yaml
 
apiVersion: v1

kind: Service

metadata:

  name: helloworld

spec:

  type: NodePort

  selector:

    app: helloworld

  ports:

    - name: helloworld

      protocol: TCP

      port: 3002

      targetPort: 4002
 
Run: 

kubectl apply -f helloworld-service.yaml

kubectl describe service helloworld


What is the difference between EXEC & CMD?

"Docker exec” => executes a command on a running container

“Docker run” => creates a temporary container, executes the command in it and stops the container when it is done

What is the difference between COPY & ADD?

COPY => Its role is to duplicate local into docker image. It doesn’t deal with extracting a compressed file, merely copies it as-is.

ADD => 1) ADD can also copy files from a URL (Example: ADD http://sourcefileserver/url /destination/path)
2) Want to add local files (Example: ADD /source/file/path /destination/path)
3) it copies compressed files, automatically extracting the content in the given destination = (Example: ADD testing.file.tar.gz /app)

What is the best practice in using COPY or ADD?
1) Docker discourage using ADD to download and copy a package from URL (it always unzip it inside docker image even we don't want)
2) wget or curl within a RUN command to get the file
3) In docker official document explicitly advises against using the ADD command

Example:
ADD http://fileserver.com/myzip.file.tar.gz /temp
RUN tar -xjf /temp/myzip.file.tar.gz && make -C /tmp/package.file && rm /tmp/ myzip.file.tar.gz
instead use
RUN curl http://source.file/myzip.file.tar.gz | tar -xjC /tmp/ myzip.file.tar.gz && make -C /tmp/ myzip.file.tar.gz
 
Why getting below docker error and how to fix it?
docker: Error response from daemon: Conflict. The container name "/mydockerapi" is already in use by container "<<Container ID>>". You have to remove (or rename) that container to be able to reuse that name.
 
First understand that,
1) "docker run" will launch an instance (container) of an image for the first time
2) "docker stop" to stop the container
3) "docker start" to resume the created container which was stopped using "docker stop"

Problem
1) If a container exists under same name and try to create again using "docker run" creates above problem
    $ docker run -i -t -p 3001:80 -p 4001:443 --name=mydockerapi dockerapi
 
Solution

1) just "docker start" to resume
    $ docker start dockerapi

2) If you want to create it freshly, then list out all existing container list and then remove required container by its name
    Example,
   
    2.1) $ docker ps -a (it list out all existing container and note down your container name)
    2.2) $ docker rm dockerapi (it removes container "dockerapi")
    2.3) again launch it freshly, $ docker run -i -t -p 3001:80 -p 4001:443 --name=mydockerapi dockerapi