Containers are a form of operating system Virtualization Technology. Before Containers there is the use of virtual machines. But they are heavy weight and each virtual machine has its own Guest OS and their own kernel whereas the Containers use the host OS kernel and is much lighter in size when compared to Virtual Machines. Containers can be build and run in seconds. Building containers for the products that are extensively used by companies can help them in building their applications more scalable. The main aim of the project is to build containers for the following products MySQL and Servlet Project.
- In future this project is extended for deploying IBM Product Applications
- At present we are dealing the deployment of Tomcat Servlet and MySQL on cloud
Clone this project and open it in any of your favourite IDE. Please see the Prerequisites that are necessary. Missing any od them may cause error or stop the process flow.
- Knowledge of OS
- Knowledge of Namespace
- Docker
- Azure or AWS or GCP cloud account, You can use any cloud service provider for my use I am using IBM Cloud Account
- Kubernetes service on cloud provider. I am using IBM Cloud Kubernetes service
- Installing Docker :- Please refer to the following document for Docker installation.
- Signing up to cloud. IBM Cloud , Azure, AWS and GCP.
- Creating Kubernetes service IBM Cloud Kubernetes Service.
- Refer to IBM Cloud CLI for installing the IBM Cloud CLI on your machine.
----- For Linux -----
curl -sL https://ibm.biz/idt-installer | bash
----- For windows -----
[Net.ServicePointManager]::SecurityProtocol = "Tls12, Tls11, Tls, Ssl3"; iex(New-Object Net.WebClient).DownloadString('https://ibm.biz/idt-win-installer')
ibmcloud plugin install container-registry -r Bluemix
-
Docker setup
docker login #create a network in docker docker network create --subnet=172.18.0.0/16 <dock-servlet_default>
-
MySQL Container
-
Open
dock-servlet/sql/Dockerfile
-
Here I used my own SQL file (
dump.sql
). Please use your own SQL file -
After that open Terminal and go to the Dockerfile location
-
Building MySQL Image
docker build -t <docker_username>/<repository_name>:<tag> . docker build -t sarathchanda24/mysql:latest .
-
Running the Image (Containers)
docker run --net <Network Name> --ip <Ip address for the MySQL Container> -p <External Port for testing>:<Containers MySQL Port(Default is 3306)> --name <container name> <Registry name>/<Image name>:<image tag> . docker run --net dock-servlet_default --ip 172.18.0.6 -p 9306:3306 --name sarath-sql sarathchandra24/mysql:latest .
-
Please note the IP address and this is the same IP for the Mysql in Docker network and Paste it in src/dbs/CredentialsOfDataBase.java
-
WAR file generation
- Open the project in your IDE
- Right click on the project and
export
it asWAR
- Note the
WAR FILE LOCATION
-
Tomcat Container
-
open
dock-servlet/Dockerfile
-
Specify the location of the
WAR File
atCOPY <WAR lOCATION> <DESTINATION WAR NAME>
-
After that open Terminal and go to the Dockerfile location
-
Building Tomcat Image
docker build -t <docker_username>/<repository_name>:<tag> . docker build -t sarathchanda24/tomcat:latest .
-
Running the Image (Containers)
docker run --net <Network Name> --ip <Ip address for the Tomcat Container> -p <External Port for Container (testing)>:<Containers Tomcat Port(Default is 8080)> --name <container name> <Registry name>/<Image name>:<image tag> . docker run --net dock-servlet_default --ip 172.18.0.5 -p 9080:8080 --name sarath-tomcat sarathchandra24/tomcat:latest .
-
docker ps
or docker container ls
- If they are running then directly go to MySQL Database Testing.
- If they are not running then try to solve with the Exited code mentioned after typing the command
docker container ls -a
ordocker ps -a
- Open MySQL WorkBench
- Do the following as showed in Image
- Check whether the Database and tables exist
- Pull up your favourite Web Browser
- Type
https://<IP Address>:<port as mentioned above>/<war-filename>
. Example localhost:9080/eidiko
-
login to IBM Cloud CLI
ibmcloud login # Signin with your IBM Cloud credentials
-
Setup the container registry
ibmcloud cr login # This make you signin to your IBM Cloud Registry and note the CR address like us.icr.io
-
Add NameSpace to your Registry
ibmcloud cr namespace-add eid-pro # This will create a name space in your registry
-
Verify the NameSpace is present
ibmcloud cr namespace-list # This will display the namespaces in your cloud
-
See the storage clusters
ibmcloud cs clusters # This will display all your clusters that are in IBM Cloud # Note down the cluster name
-
Public credentials of the Cluster ibmcloud cs workers # This will display the public IP of the cluster note it down
-
setup the kubernetes connection from your local machine to cluster
ibmcloud cs cluster config <cluster name> # Now this asks to add kubectl to path, copy and paste it in your terminal. Now you can access kubectl
-
login to your docker
docker login # log-in with your docker credentials
go to dock-servlet/sql/
-
Building image
docker build --tag <CR address>/<namespace>/<image_name> . # Example: docker build --tag us.icr.io/eid-pro/mysql . # This lets you build the mysql image which is same as the previously built sarathchandra/mysql image
-
Pushing image to the cloud
docker push <CR address>/<name_space>/<image_name> # Example: docker push us.icr.io/eid-pro/mysql # This will push the image into your Cloud Registry
-
Running it on Kubernetes
kubectl run <container-name> --image=<CR address>/<namespace>/<image_name> # Example: kubectl run mysql --image=us.icr.io/eid-pro/mysql
-
Verify the container is running or not
kubect get pods # This will display all the the running containers. # Check whether the container is running
-
Expose the Container
kubectl expose deployment/<container-name> --type="NodePort" --port=<port Number> # Example: kubectl expose deployment/mysql --type="NodePort" --port=3306
-
Container Description
kubectl describe service <container_name> # Example: kubectl describe service mysql # Note the internal IP for tomcat communication # Note the NodePort
-
For testing the MySQL database use
Public IP
&Node Port
, Do Mysql Database Testing
-
go to
dock-servlet/
-
update CredentialsOfDataBase.java with the credentials of container
-
Building image
docker build --tag <CR address>/<namespace>/<image_name> . # Example: docker build --tag us.icr.io/eid-pro/tomcat . # This lets you build the Tomcat image which is same as the previously built sarathchandra/tomcat image
-
Pushing image to the cloud
docker push <CR address>/<name_space>/<image_name> # Example: docker push us.icr.io/eid-pro/tomcat # This will push the image into your Cloud Registry
-
Running it on Kubernetes
kubectl run <container-name> --image=<CR address>/<namespace>/<image_name> # Example: kubectl run tomcat --image=us.icr.io/eid-pro/tomcat
-
Verify the container is running or not
kubect get pods # This will display all the the running containers. # Check whether the container is running
-
Expose the Container
kubectl expose deployment/<container-name> --type="NodePort" --port=<port Number> # Example: kubectl expose deployment/tomcat --type="NodePort" --port=8080
-
Container Description
kubectl describe service <container_name> # Example: kubectl describe service tomcat # Note the NodePort
-
For testing the Tomcat Service use
Public IP
&Node Port
, Do Tomcat Testing
- Java 13 - Programming Language used
- Docker - containers creation
- Kubernetes - container-orchestration system
- IBM Cloud Kubernetes Service - Cloud service provider and Kubernetes Service used
- Sarath Chandra Oruganti - SarathChandra24