0% found this document useful (0 votes)
13 views

Devops Lab programs

The document outlines the steps to develop a simple containerized application using Docker, including creating a Docker file, building the image, and running the container. It also discusses integrating Kubernetes with Docker for managing containers, automating the process of running applications, and using Selenium for automated testing of both JavaScript and containerized applications. Additionally, it provides example code for writing test cases using Selenium to verify application functionality.

Uploaded by

cdurgabai26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Devops Lab programs

The document outlines the steps to develop a simple containerized application using Docker, including creating a Docker file, building the image, and running the container. It also discusses integrating Kubernetes with Docker for managing containers, automating the process of running applications, and using Selenium for automated testing of both JavaScript and containerized applications. Additionally, it provides example code for writing test cases using Selenium to verify application functionality.

Uploaded by

cdurgabai26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

7.

Develop a simple containerized application using Docker


Aim: Develop a simple containerized application using Docker
Description: Here an example of how you can develop a simple containerized application using Docker:
Choose an application:
 Choose a simple application that you want to containerize.
For example, a python script that Prints “Hello World”.
Write a Docker file: Create a file named “Docker file” in the same directory as the application.
In the docker file, specify the base image, copy the application in to the container, and specify the
command to run the application. Here’s an example for a Python script:

#Use the official Python image as the base


image FROM python:3.9

#Copy the python script into the


container COPY hello.py/app/

#Set the working directory to/app/


WORKDIR/app/

#Run the Python script when the container starts


CMD [“python”, “hello.py”]
 Build the Docker image:
Run the following command to build the docker image:
$docker build –t myimage.
The command builds a new Docker image using the Docker file and tag the image with
the name “myimage”

 Run the Docker container:


Run the following command to start a new container based on the image:
$docker run –name mycontainer myimage.

This command starts a new container by named “mycontainer” based on the “myimage” image and
run the Python script inside the container.
 Verify the output:
Run the following command to verify the output of the container:
$docker logs mycontainer
This command displays the logs of the container and should show the “Hello World” output.
This is the simple example of how you can use Docker to containerize an application. In the real-world
scenario, you would likely have more complex requirements such as running multiple containers,
managing network connections, and persisting data. However, this example should give you a good
starting point for using Docker to containerize your applications.
Experiment no: 8. Integrate Kubernetes and Docker
Aim: Integrate Kubernetes and Docker
Description: Kubernetes and Docker are both popular technologies for managing containers, but they
are used for different purposes. Kubernetes is an orchestration platform that provides a higher-level
abstraction for managing containers, while Docker is a containerization technology that provides a
lower-level runtime for containers.
To integrate Kubernetes and Docker, you need to use Docker to build and package the application as a
container image and then use Kubernetes to manage and orchestrate the containers.
Here’s a high-level overview of the steps to integrate Kubernetes and Docker:
 Build a docker image:
Use Docker to build a Docker image of your application. You can use a Docker file to specify
the base image, copy the application into the container, and specify the command to run the
application.

 Push the Docker image to a registry:


Push the Docker image to a container registry. Such as Docker Hub or Google Container
Registry, so that it can be easily accessed by Kubernetes. Deploy the Docker image to a
Kubernetes cluster.
Use Kubernetes to deploy the Docker image to a cluster. This involves creating a deployment that
specifies the number of replicas and the image to be used, and creating a service that expose the
deployment to the network.

 Monitor and manage the containers: Use Kubernetes to monitor and manage the containers. This
includes the scaling the number of replicas, update the image, and rolling out updates to the
containers.

 Continuously integrate and deploy changes: Use a continuous integration and deployment
(CI/CD) pipeline to automatically build, push and deploy changes to the docker image and
Kubernetes cluster.

This makes it easier to make updates to the application and ensure that the latest
version is always running in the cluster. By integrating Kubernetes and docker, you can leverage the
strength of both technologies to manage container in a scalable, reliable and efficient manner
Experiment no: 9. Automate the process of running containerized application developed in
exercise 7 using Kubernetes.
Aim: Automate the process of running containerized application developed in exercise 7
using Kubernetes
Description: To automate the process of running the containerized application developed in exercise7
using Kubernetes, you can follow these steps:

 Create a Kubernetes cluster: Create a Kubernetes cluster that cloud provided such as google cloud
or amazon web services, or using local installation of Minikube.

 Push the Docker image to registry: Push the Docker image of your application to a
container registry, such as Docker Hub or Google Container Registry.

 Create a deployment: Create a deployment in Kubernetes that specifies the number of replicas
and the Docker image to use. Here’s an example of a deployment YAML file:

apiVersion:apps/v1
kind: Deployment
metadata:
name:myapp
spec:
replicas:3
selector:
matchLabels:
app:myapp
template:
metadata:
labels:
app:myapp
spec:
container:
-name: myapp
image:myimage
ports:
-containerPort:80

 Create a service: Create a service in Kubernetes that exposes the deployment to the network.
Here’s an example of a service YAML file:
apiVersion:v1
kind: Service
metadata:
name:myapp-service
spec:
selector:
app:myapp
ports:
-name: http
Port:80
targetPort:80
Type:clusterIP

 Apply the deployment and service to the cluster: Applying the deployment and service to the cluster
using the kubect1 command- line tool.
For example:
$kubect1apply-fdeployment.yaml
$kubect1apply-fservice.yaml

 Verify the deployment: Verify the deployment by checking the status of the pods and the service.
For example:
$kubect1getpods
$kubect1getservices

This is the basic example of how to automate the process of running a containerized application using
Kubernetes. In the real-world scenarios, you would likely have more complex requirements. Such as
managing persistent data, scaling and rolling updates, but this example gives you a good starting point
for using Kubernetes to manage your containers.
Experiment no: 10. Install and explore selenium for automated testing
Aim: Install and explore selenium for automated testing
Description: To install and explore selenium for automated testing, you can follow the steps:

Install java development kit (JDK):


 Selenium is written in Java, so you will need to install JDK in order to run it. You can download
and install JDK form the official Oracle website
 Install the Selenium WebDriver:
 You can download the latest version of Selenium Web Driver from the selenium website. You will
also need to download the appropriate driver for your web browser of choice (e.g: Chrome Driver for
Google Chrome)
Install an Integrated Development Environment (IDE):
 To write and run Selenium tests, you will need an IDE. Some popular choices include Eclipse,
IntelliJ IDEA, and Visual Studio Code.
 Write a simple test:
 Once you have an IDE setup, you can write a simple test using the Selenium Web Driver. Here’s an
example in Java:
import org.openqa.selenium,WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main (string [] arg)
{
System.setProperty(“webdriver.chrome.driver”,“path/to/chromedriver”);
WebDriver driver = new ChromeDriver();
driver.get(https://www.google.com);
System.out.println(driver.getTitle());
driver.quit();
 Run the test: Run the test using your IDE or from the command line using the following command:
$javac Main.java
$javacMain

This is a basic example of how to get started with Selenium for automated testing. In a real-world
scenario, you would likely write more complex tests and organize your code into test suites and test
cases but this example should give you a good starting point for exploring Selenium.
Experiment no: 11.Write a simple program in JavaScript and perform testing using
Selenium Aim: Write a simple program in JavaScript and perform testing using Selenium
Program: Simple Java Script program that you can test using Selenium
<!DOCTYPE html>
<html>
<head>
<title> Simple Java Script Program </title>
</head>
<body>
<pid=”output”>0</p>
<button id=”increment-button”>Increment</button>
<script>
const output=document.getElementById(“output”);
const incrementButton=document.getElemetById(“increment-button’);
let count=0;
incrementButton.addEventListener(“click”, function(){
count +=1;
output.innerHTML=count;
});
</script>
</body>
</html>

Write a test case for this program using Selenium


Import org.openqa.selenium.By;
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.chrome.ChromeDriver;
Import org.junit.After;
Import org.junit.Before;
Import org.junit.Test;
Public class Main {
Private WebDriver driver;
@Before
Public void setUp(){
System.setProperty(“webdriver.chrome.driver”,“path/to/chromedriver”);
driver=new ChromeDriver();
}
@Test
Public void testIncrementButton()
{
driver.get(file:///path/to/program.html);
driver.findElement(By.id(“increment-button”)).click();
String result=driver.findElement(By.id(“output”)).getText();
assert result.equals(“1”);
}
@After
Public void tear Down()
{ driver.quit(); }
}

You can run the test case using the following commands:
$javac Main.java
$javac Main

The output of the test case should be:


Time: 0.189
OK (1test)

This output indicates the test case passed, and the increment button was successfully
Clicked, causing the output to be incremented by 1
Experiment: 12. Develop test cases for the above containerized application using Selenium.
Aim: Develop test cases for the above containerized application using
Selenium.
Program: Here is an example of how you could write test cases for the containerized
application using selenium.
Import org.openqa.selenium.By;
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.chrome.ChromeDriver;
Import org.junit.After;
Import org.junit.Before;
Import org.junit.Test;
Public class Main
{
Private WebDriver driver;
@Before
Public void setUp()
{
System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
driver=new ChromeDriver();
}
@Test
Public void testHomePageLoad()
{
driver.get(http://localhost:8080);
String title = driver getTitle();
assert title.equals(“My Containerized Application”);
}
@Test
Public void testSubmitForm()
{
driver.get(http://localhost:8080);
driver.findElement(By.name(“name”)).sendKeys(“JohnDeo”);
driver.findElement(By.name(“email”)).sendkeys(“[email protected]”);
driver.findElement(By.name(“submit”)).click();
String result=driver.findElement(By.id(“result”)).getText();
assert result.equals(“Formsubmitted successfully!”);
}
@After
Public void tearDown()
{ driver.quit(); }
}
You can run the test cases using the following command:
$javac Main.java
$javac Main
The output of test cases should be:
..
Time: 1.135
OK (2test)

This output indicates that both test cases passed, and the containerized application is functioning as
expected.

You might also like