"Kubernetes" three hours to conquer kubernetes! (2): Create a container image for each service

Source: Internet
Author: User
Tags nginx server docker hub docker run docker registry docker machine

What is a container?

Kubernetes is a container management platform. It is conceivable that we need containers to manage them. But what is a container? The best answers to the official Docker documentation are:

A container image is a lightweight, standalone, executable package that contains everything that can be run: Code, runtime, System tools, system libraries, settings. For Linux and Windows-based applications, containerized software can run as usual regardless of the environment.
This means that the container can run on any computer, even on the product server, without any difference.

For a more descriptive picture, let's compare the React application running on a virtual machine and inside a container.

Providing React static files via virtual machines

Disadvantages of using virtual machines include:

    • Resource inefficient, each virtual machine needs a fully mature operating system;
    • have dependencies on the platform. Functions that run well on the local machine may not work properly on the product server;
    • Heavier and slower scale than the container.
Providing React static files through containers

Advantages of using containers include:

    • Resource efficient, using the host operating system with the help of Docker;
    • There is no dependency on the platform. Containers that can be run on the local machine can work on any machine;
    • Provides lightweight services through the image layer.

These are the most prominent features and advantages of using containers. For more information, please refer to the Docker official documentation: Https://www.docker.com/what-container.

Creating container images for React applications (Docker Introduction)

The most basic component of a Docker container is. Dockerfile. The most basic component of the Dockerfile file is container mirroring, and we'll show you how to create a container image that meets your application needs through the following series of instructions.

Before you start defining Dockerfile, let's recall the steps to React a static file using the Nginx service:

    • Create a static file (NPM run build);
    • Start the Nginx server;
    • Copy the contents of the build folder of the front-end project to the nginx/html directory.

In the next section, you'll notice that creating a container is very similar to the process of building a local React.

Defining Dockerfile for the front end

The front-end Dockerfile is built with only two steps. This is because the Nginx team provides us with a basic nginx image that we can use directly. These two steps are as follows:

    • Start the basic Nginx image;
    • Copy the Sa-frontend/build directory to the nginx/html of the container.
      The converted Dockerfile is as follows:
FROM nginxCOPY build /usr/share/nginx/html

Are you surprised? This file is readable and can be summed up as:

Start with the Nginx image (whatever it is). Copy the build directory to the nginx/html directory of the image. Then it's all right!

You might be wondering, where do I copy the build file? For example:/usr/share/nginx/html. Very simple: It is documented in the Nginx image document of the Docker Hub.

Build and push containers

Before we push the image, we need a container to register to host the image. Docker Hub is a free cloud container service that we'll use to do demos. Next, there are 3 tasks to complete:

    • Install Docker CE;
    • Register Docker Hub;
    • Run the following command in the terminal to log in:
docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"

After completing any of the above, go to directory sa-frontend. Then run the following command (replace the $DOCKER user name with your Docker hub username, for example: Rinormaloku/sentiment-analysis-frontend).

docker build -f Dockerfile -t $DOCKER_USER_ID/sentiment-analysis-frontend .

Now we can delete-F Dockerfile, because we are already in the directory containing the Dockerfile.

We can use the Docker push command to push the image:

docker push $DOCKER_USER_ID/sentiment-analysis-frontend

Make sure the image has been successfully pushed to the Docker hub code base.

Running the container

Now anyone can get the image in the $DOCKER _user_id/sentiment-analysis-frontend and run it:

docker pull $DOCKER_USER_ID/sentiment-analysis-frontenddocker run -d -p 80:80 $DOCKER_USER_ID/sentiment-analysis-frontend

The Docker container is already in the running state!
Before we go on to the next step, let's talk about 80:80, a lot of people are puzzled about it:

    • The first 80 is the port number of the host (for example: My Computer);
    • The second 80 is the port number of the container, and the request will be forwarded here.

This is from the <主机端口> match to <容器端口> . This means that each request destined for port 80 will be matched to port 80 of the container, as shown in 9.
You can access localhost:80 because the port on the host (your computer) is running on port 80. If Docker is not supported locally, you can open the application on: 80. Run the docker-machine IP command to find the IP of the Docker machine.
Try! You should now be able to access the React application.

Dockerignore file

Just now we see the image of building sa-frontend very slow, sorry, it should be super slow. This is because we have to send the environment files in the build process to the Docker service. More specifically, the environment file in the build process refers to all the data in the Dockerfile directory that will be used when the image is created.

In our case, the Sa-frontend file includes the following folder:

sa-frontend:|   .dockerignore|   Dockerfile|   package.json|   README.md+---build+---node_modules+---public\---src

But we only need the build folder. Uploading other files can be a waste of time. We can save time by removing other directories. This will require the use of. Dockerignore. You may think this is very similar to. Gitignore, for example, you can add all the directories you want to ignore to the. Dockerignore as follows:

node_modulessrcpublic

this. dockerignore file should be in the same folder as Dockerfile. It only takes a few seconds to build the image file.

Let's go ahead and look at the Java application.

Creating container images for Java applications

Do you know? You've learned almost everything about creating container images! That's why this section is so short.

Open Dockerfile in Sa-webapp and you will see only two new keywords:

ENV SA_LOGIC_API_URL http://localhost:5000…EXPOSE 8080

The keyword ENV declares an environment variable within a Docker container. This allows us to provide a URL for the sentiment analysis API when launching the container.

In addition, the keyword EXPOSE provides a port for us to access later. But wait, we did not do this step in the Sa-frontend, said very right! This port is for documentation only, in other words, the port is used to provide information to people reading Dockerfile.

You should have mastered creating and pushing container images. If you encounter any difficulties, you can read the Readme.md file in Sa-webapp.

Creating a container image for a Python application

There are no new keywords in Sa-logic's Dockerfile. Now you're the Docker's Got Talent.

For information on how to create and push container images, please read the readme.md file in the Sa-logic directory.

Testing a containerized application

Can you believe anything that has not been tested? I don't believe it either. So let's test these containers.

1. Run the Sa-logic container and configure the listening port 5050:

docker run -d -p 5050:5000 $DOCKER_USER_ID/sentiment-analysis-logic

2. Run the Sa-webapp container and configure the listening port 8080 (because we have changed the port that the Python app listens to, so we need to rewrite the environment variable Sa_logic_api_url):

$ docker run -d -p 8080:8080 -e SA_LOGIC_API_URL='http://<container_ip or docker machine ip>:5000' $DOCKER_USER_ID/sentiment-analysis-web-app

3. Run the Sa-frontend container:

docker run -d -p 80:80 $DOCKER_USER_ID/sentiment-analysis-frontend

And then it's ready. Open localhost:80 in the browser.

Note: If you change the port of Sa-webapp, or use the IP of the Docker machine, then you need to update the app.js in Sa-frontend and let Analyzesentence get the URL from the new IP or port. Then you need to build and use the updated image.

Quiz questions-why use Kubernetes?

In this section, we learned about Dockerfile, how to use it to create images, and commands to push images into the Docker registry directory. In addition, we explored how to reduce the need to send the environment files in the setup process by ignoring useless files. Finally we ran the application from the container.

Next, let's explain why we want to use Kubernetes. We will introduce Kubernetes in depth below, here I would like to leave you an intellectual question and answer question.

If our sentiment Analysis Network application is done well and suddenly the traffic spikes to millions of requests per minute, then our Sa-webapp and sa-logic will face enormous load pressure. May I ask how we can expand the size of the container?

From: CSDN (No.: Csdnnews), Rinor Maloku, translator: Crescent, Zebian: Guo Rui

"Kubernetes" three hours to conquer kubernetes! (2): Create a container image for each service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.