Thursday, March 9, 2023

Deploy Application on Swarm Cluster

root@ip-172-31-15-174 ~]# cat myservice.yml

version: '3'

services:

  my-app:

    image: nginx:latest

    deploy:

      replicas: 3

      restart_policy:

        condition: on-failure

    ports:

      - "8081:80" 



[root@ip-172-31-15-174 ~]# docker stack deploy --compose-file myservice.yml test-app

Creating network test-app_default

Creating service test-app_my-app


[root@ip-172-31-15-174 ~]# docker ps -a

CONTAINER ID   IMAGE                      COMMAND                  CREATED         STATUS                          PORTS

   NAMES

1050478c859a   nginx:latest               "/docker-entrypoint.…"   2 minutes ago   Exited (0) About a minute ago

   test-app_my-app.1.ifnzpj756lq6f27g3h7fdvrxz

0f94419281dc   nginx:latest               "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes                    80/tcp

   test-app_my-app.2.vjo1lcb3tismhc4psq21xe555

880fe2587512   nginx:latest               "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes                    80/tcp

   test-app_my-app.3.j2wonx6yr7v3vejt65xv7rfqa

11d0593d925d   dockersamples/visualizer   "/sbin/tini -- node …"   8 minutes ago   Up 8 minutes (healthy)          0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   strange_ritchie

[root@ip-172-31-15-174 ~]#

[root@ip-172-31-15-174 ~]#

[root@ip-172-31-15-174 ~]#

[root@ip-172-31-15-174 ~]# docker service ls

ID             NAME              MODE         REPLICAS   IMAGE          PORTS

1daq9y4d70yr   test-app_my-app   replicated   2/3        nginx:latest   *:8081->80/tcp

Wednesday, March 8, 2023

Docker Swarm V/s Kubernetes

 



  1. Architecture: Docker Swarm is built into the Docker ecosystem, while Kubernetes is a standalone tool that can be run on any infrastructure.

  2. Scalability: While both tools are designed to be scalable, Kubernetes is better suited for managing large, complex containerized applications.

  3. Flexibility: Kubernetes is more flexible than Docker Swarm, as it can be configured and extended to meet the needs of any organization.

  4. Complexity: Kubernetes is a more complex tool than Docker Swarm, which means it can take longer to learn and set up.


Docker Compose V/s Docker Swarm

 Compose and Swarm are both tools provided by Docker for managing and deploying containerized applications, but they have different use cases.

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define your application’s services, networks, and volumes in a single file, and then use that file to create and start all the services that make up your application. Docker Compose is useful for development and testing environments where you need to spin up multiple containers quickly.

Docker Swarm, on the other hand, is a native clustering and orchestration tool for Docker. It allows you to manage a cluster of Docker nodes and deploy containerized applications across that cluster. Docker Swarm provides features like service discovery, load balancing, and rolling updates, making it a good choice for production environments where high availability and scalability are critical.

In summary, Docker Compose is ideal for single host environments, while Docker Swarm is designed for more complex multi-host environments where scaling and high availability are important.

Tuesday, March 7, 2023

Docker Swarm Installation

 Install docker on Master and worker node and master node execute docker swarm init command.

[root@ip-172-31-34-29 ~]# docker swarm init

Swarm initialized: current node (xk88ov6mds5qdmzaj2obkfwyp) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join --token SWMTKN-1-58apbdwqjurmocvp1mlbgu85w8340g8beh65ztuvjw43x30cqp-bqthz5iqs32vtu0tiuc6efqhr 172.31.34.29:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.


Add worker node and manager in swarm cluster.

docker swarm join-token worker

docker swarm join-token manager

Remove manager and worker node:

docker swarm leave

#docker node ls ==> to check swarm cluster node details.


===============


v2.16.0


sudo curl -L "https://github.com/docker/compose/releases/download/{v2.16.0}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose



step-1) Create 2 node


step 2) One master and one worker


step 3) make sure docker installed on both the host.


step 4) #docker --version



step 5) execute below commands on master node


   #docker swarm init


step 6) execute below command on worker node:

docker swarm join --token SWMTKN-1-58apbdwqjurmocvp1mlbgu85w8340g8beh65ztuvjw43x30cqp-bqthz5iqs32vtu0tiuc6efqhr 172.31.34.29:2377  ==> PLease check docker swarm init command output.



step 7) execute below commands on master node:

   #docker node ls

   #docker run -it -d -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock dockersamples/visualizer

   (https://github.com/dockersamples/docker-swarm-visualizer)


step 8)  create yml file and deplyo it in swarm cluster.


[root@master ~]# cat sample.yml

version: '3'


services:

  bb-app:

    image: nginx

    ports:

      - "8000:3000"

  


 docker stack deploy -c sample.yml demo

 =========================================


docker swarm init add what you get to worker node --open the port

docker node ls

docker service ls

docker run -it -d -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock dockersamples/visualizer

docker ps -a

docker service create --name nginxweb -p 8081:80 nginx

docker service create --name nginxweb1 -p 8082:80 --replicas 5 nginx

docker service ps nginxweb1

docker service scale nginxweb1=7

docker service scale nginxweb1=1

docker node update --availability drain docker

docker node update --availability active docker

docker node ls

 

Docker Compose Installation

 Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.


Download docker-compose:

1.Run this command to download the current stable release of Docker Compose:

$  sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

2.Apply executable permissions to the binary:

chmod +x /usr/local/bin/docker-compose

3. #ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

[root@ip-172-31-34-29 DockerDemo]# docker-compose --version

docker-compose version 1.29.2, build 5becea4c


To run a nginx container the manual is

root@ip-172-31-34-29:~$ docker container run -itd nginx 

but if the same container if we want to create using docker-compose file then we need to create a YAML file using vi docker-compose.yaml and snippet is shown below

version: '3'

services:

  nginxwebapp:

    image: nginx

    ports: 

      - "8000:80"

Version :- Property is defined by the specification for backward compatibility but is only informative.

Services:- It defines the number of applications or containers which need to be created. As shown in the above example under nginxwebapp it defines the first container which will get created using image nginx  and expose ports “8000:80”. By giving the image name as nginx, we are specifying the container should get built on nginx and we can give any image name as per the requirement. Ports property defines the port mapping of the host to the container, port 8000 is the host port that will map to port 80 of the container.


[root@ip-172-31-34-29 DockerDemo]# docker-compose up -d

Starting dockerdemo_webapp2_1 ... done

Starting dockerdemo_webapp1_1 ... done

[root@ip-172-31-34-29 DockerDemo]# docker ps

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS                                   NAMES

80ea921350ed   nginx     "/docker-entrypoint.…"   50 seconds ago   Up 2 seconds   0.0.0.0:8001->80/tcp, :::8001->80/tcp   dockerdemo_webapp2_1

d679f04c516f   nginx     "/docker-entrypoint.…"   50 seconds ago   Up 2 seconds   0.0.0.0:8000->80/tcp, :::8000->80/tcp   dockerdemo_webapp1_1


[root@ip-172-31-34-29 DockerDemo]# docker-compose down

Stopping dockerdemo_webapp2_1 ... done

Stopping dockerdemo_webapp1_1 ... done

Removing dockerdemo_webapp2_1 ... done

Removing dockerdemo_webapp1_1 ... done

Removing network dockerdemo_default


Docker Swarm V/s Docker Compose:

The difference between Docker Swarm and Docker Compose is that Compose is used for configuring multiple containers in the same host. Docker Swarm is different in that it is a container orchestration tool. This means that Docker Swarm lets you connect containers to multiple hosts similar to Kubernetes





Monday, March 6, 2023

Shell and Executable form

 Exec Form:

Dockerfile:

FROM ubuntu

RUN apt-get update && apt-get install -y nginx

EXPOSE 80

VOLUME My_Vol

ENTRYPOINT ["nginx", "-g", "daemon off;"]  ==> executable form


Login to Container and check nginx process:

[root@ip-172-31-34-29 DockerDemo]# docker exec -it 6633147f038d sh

# top

top - 17:59:43 up 5 days, 16:01,  0 users,  load average: 0.07, 0.04, 0.01

Tasks:   4 total,   1 running,   3 sleeping,   0 stopped,   0 zombie

%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

MiB Mem :    964.8 total,    175.9 free,    176.1 used,    612.7 buff/cache

MiB Swap:      0.0 total,      0.0 free,      0.0 used.    641.1 avail Mem


  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND

    1 root      20   0   55200  12036  10400 S   0.0   1.2   0:00.20 nginx

    7 www-data  20   0   55524   3260   1324 S   0.0   0.3   0:00.00 nginx

   13 root      20   0    2888    968    880 S   0.0   0.1   0:00.19 sh

   18 root      20   0    7300   3476   2932 R   0.0   0.4   0:00.00 top



Shell Form:


Dockerfile:

[root@ip-172-31-34-29 DockerDemo]# cat Dockerfile

FROM ubuntu

RUN apt-get update && apt-get install -y nginx

EXPOSE 80

VOLUME My_Vol

#ENTRYPOINT ["nginx", "-g", "daemon off;"]

ENTRYPOINT nginx -g "daemon off;"



[root@ip-172-31-34-29 DockerDemo]# docker exec -it 13af7484a21d  sh

# top

top - 18:02:06 up 5 days, 16:03,  0 users,  load average: 0.00, 0.02, 0.00

Tasks:   5 total,   1 running,   4 sleeping,   0 stopped,   0 zombie

%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

MiB Mem :    964.8 total,    176.3 free,    175.3 used,    613.2 buff/cache

MiB Swap:      0.0 total,      0.0 free,      0.0 used.    641.9 avail Mem


  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND

    1 root      20   0    2888    956    864 S   0.0   0.1   0:00.19 sh

    7 root      20   0   55200  12004  10364 S   0.0   1.2   0:00.00 nginx

    8 www-data  20   0   55524   3376   1440 S   0.0   0.3   0:00.00 nginx

    9 root      20   0    2888    988    896 S   0.0   0.1   0:00.19 sh

   14 root      20   0    7304   3304   2760 R   0.0   0.3   0:00.00 top

   

   

[root@ip-172-31-34-29 DockerDemo]# docker ps

CONTAINER ID   IMAGE         COMMAND                  CREATED              STATUS          PORTS     NAMES

13af7484a21d   shellform     "/bin/sh -c 'nginx -…"   About a minute ago   Up 59 seconds   80/tcp    relaxed_borg

6633147f038d   executeform   "nginx -g 'daemon of…"   3 minutes ago        Up 3 minutes    80/tcp    gallant_neumann


Exec form

This is the preferred form for CMD and ENTRYPOINT instructions.

The SHELL form runs the command as a child process (on a shell). The EXEC form runs the executable on the main proces

RUN V/S CMD V/S ENTRYPOINT

Docker images and layers

When Docker runs a container, it runs an image inside it. This image is usually built by executing Docker instructions, which add layers on top of existing image or OS distribution.

OS distribution is the initial image and every added layer creates a new image.

Final Docker image reminds an onion with OS distribution inside and a number of layers on top of it. 

For example, your image can be built by installing a number of deb packages and your application on top of Ubuntu 14.04 distribution.


RUN

RUN instruction allows you to install your application and packages requited for it. It executes any commands on top of the current image and creates a new layer by committing the results. Often you will find multiple RUN instructions in a Dockerfile.

RUN has two forms:

RUN <command> (shell form)

RUN ["executable", "param1", "param2"] (exec form)



CMD

CMD instruction allows you to set a default command, which will be executed only when you run container without specifying a command. If Docker container runs with a command, the default command will be ignored. If Dockerfile has more than one CMD instruction, all but last CMD instructions are ignored.


CMD has three forms:


CMD ["executable","param1","param2"] (exec form, preferred)

CMD ["param1","param2"] (sets additional default parameters for ENTRYPOINT in exec form)

CMD command param1 param2 (shell form)


Let’s have a look how CMD instruction works. The following snippet in Dockerfile

CMD echo "Hello world" 

when container runs as docker run -it <image> will produce output

Hello world

but when container runs with a command, e.g., docker run -it <image> /bin/bash, CMD is ignored and bash interpreter runs instead:

root@7de4bed89922:/#


--------------

ENTRYPOINT

ENTRYPOINT instruction allows you to configure a container that will run as an executable. It looks similar to CMD, because it also allows you to specify a command with parameters. The difference is ENTRYPOINT command and parameters are not ignored when Docker container runs with command line parameters. (There is a way to ignore ENTTRYPOINT, but it is unlikely that you will do it.)

ENTRYPOINT has two forms:

ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred)

ENTRYPOINT command param1 param2 (shell form)

Be very careful when choosing ENTRYPOINT form, because forms behaviour differs significantly.

Exec form

Exec form of ENTRYPOINT allows you to set commands and parameters and then use either form of CMD to set additional parameters that are more likely to be changed. ENTRYPOINT arguments are always used, while CMD ones can be overwritten by command line arguments provided when Docker container runs. For example, the following snippet in Dockerfile

ENTRYPOINT ["/bin/echo", "Hello"]

CMD ["world"]

when container runs as docker run -it <image> will produce output

Hello world

but when container runs as docker run -it <image> John will result in

Hello John

----


Use RUN instructions to build your image by adding layers on top of initial image.

Prefer ENTRYPOINT to CMD when building executable Docker image and you need a command always to be executed. Additionally use CMD if you need to provide extra default arguments that could be overwritten from command line when docker container runs.

Choose CMD if you need to provide a default command and/or arguments that can be overwritten from command line when docker container runs


Sample Game App Deployment on EKS cluster

 https://padmakshi.medium.com/setting-up-an-eks-cluster-and-deploying-a-game-application-a-step-by-step-guide-08790e0be117