[Containerized application design and development] 2.1 Basic knowledge of containerization and Docker containers

Past review:

Chapter 1: [Cloud Native Concepts and Technologies]

Containerized application design and development is an application design and development methodology based on container technology . It splits the application into multiple small services and encapsulates them into portable, scalable and lightweight containers.

In containerized application design and development, containerization engines such as Docker are used to create, manage, and deploy container images. These containers are then orchestrated using a container orchestration system such as Kubernetes and provide features such as automated scaling and load balancing.

Additionally, the application is refactored using a microservices architecture so that each service can be developed, tested, and deployed independently. You can also use best practices like declarative configuration and infrastructure as code to simplify the deployment and maintenance process.

Containerized application design and development enables greater flexibility, portability, and reliability, and enables developers to build and deploy applications more efficiently.

2.1 Containerization basics and Docker containers

Containerization is a technology that virtualizes applications at the operating system level, packaging the application and its dependencies into a portable container. Containerization technology helps simplify application development, deployment, and management, and increases application portability and scalability.

2.1.1 Container creation

The creation of cloud-native containers refers to packaging applications and all their dependencies into an independent operating environment, and deploying them on cloud computing platforms using container orchestration tools (such as Kubernetes). Below is an example of creating a cloud-native container using CentOS and Java. Usually includes the following steps:

1. Install the Docker engine

We need to install the Docker engine on the Centos image so that we can create, run and manage containers. Enter the following command in the Linux terminal:

insert image description here

2. Write a Java application

After installing the Docker engine, we need to write a Java application and build an executable jar package. Here is a simple Java code example:

insert image description here

It is estimated that everyone is familiar with how to package a Java application into a jar package. We can use Maven to build a jar package that the application can execute:

insert image description here

After executing the above command, an executable jar package named "hello-world.jar" will be generated in the target directory.

3. Write the Dockerfile

Next, we need to write a Dockerfile that defines how to build the Docker image. Enter the project root directory in the terminal, create a new Dockerfilefile named " " (note the capitalization), and enter the following content:

insert image description here

  1. FROM : specifies the base image;
  2. COPY : copy all files in the current directory to the container directory;
  3. ENTRYPOINT: defines the command to be executed when running the container;

4. Build the Docker image

Finally, we can build and run the Docker image with the following commands:

insert image description here
The first command builds a Docker image called "hello-world-image" using the Dockerfile in the current directory.

The second command will use this image to create a container called "hello-world-container" and map it to port 8080 on the host.

From this, you can visit in your browser http://localhost:8080to verify that the application is working correctly.

2.1.2 Container Management

The management of cloud-native containers refers to the containers used to deploy and manage applications in cloud computing environments. Container technology can easily package an application and all its dependencies into an independent operating environment to achieve a more efficient, reliable and flexible application deployment and management method.

In cloud-native applications, Kubernetessuch to manage containers. At its core is a control plane (Control Plane), which consists of multiple components and is responsible for managing the state of the entire cluster, including operations such as scheduling, scaling, monitoring, and fault recovery. Users interact with the control plane through the Kubernetes API, and define and deploy applications through resource types such as deployment, service, and pod.

The following is a simple Java code example that demonstrates how to create a Deployment to deploy a Java application:

insert image description here
The code uses a Kubernetes Deployment resource configuration file to define the deployment of a Java application named "my-java-app".

It defines that there should be three replicas running the application, and specifies other configuration information such as container images and ports.

The Kubernetes control plane will use this information to create and manage Pod objects to ensure that the application is always in a stable state across the cluster.

Guess you like

Origin blog.csdn.net/weixin_44427181/article/details/130289241