Create and run container images

Run the following command on the console:

Docker run busybox echo "Hello world"Copy the code

Output:

The principle behind it:

Run a simple Node.js application

Create app.js and Dockerfile files

app.js

const http = require('http');
const os = require('os');

console.log("Kubia server starting...");

var handler = function(request, response) {
  console.log("Received request from " + request.connection.remoteAddress);
  response.writeHead(200);
  response.end("You've hit " + os.hostname() + "\n");
};

var www = http.createServer(handler);
www.listen(8080);
Copy the code

Here an HTTP server is started on port 8080. The server responds to each request with a status code of 200 OK and the words You’ve hit

.

Dockerfile

FROM node:7 # from defines the base image
ADD app.js /app.js Add the app.js file to the image root directory
ENTRYPOINT ["node"."app.js"] # Run the command
Copy the code

Build the container image and run it

docker build -t kubia .
Copy the code

The build process is not done by the Docker client, but by uploading the entire directory of files to the Docker daemon and taking place there. If you use Docker on a non-Linux operating system, the client runs on your host operating system, but the daemon runs in a virtual machine.

Tip: Don’t include any unnecessary files in the build directory, as this will slow down the build — especially if the Docker daemon is running on a remote machine.

When building an image, each individual instruction in the Dockerfile creates a new layer, and each layer can be reused by a different image:

Use the docker image command to list the images stored locally. You will see information about the image you just created

The following commands can be used to run the image:

docker run --name kubia-container -p 8080:8080 -d kubia
Copy the code
The command instructions
–name The name of the container to run
–d The background
-p The native port maps the port within the container

Now try accessing your app at http://localhost:8080

curl localhost:8080
Copy the code

Configure Minikube to run a single-node Kubernetes cluster

Using Minikube is the easiest and fastest way to run a Kubernetes cluster. Minikube is a tool for building single-node clusters, which is useful for testing Kubernetes and developing applications locally.

The installation

You can refer to the official documentation to install on your own platform

Start the

minikube start
Copy the code

Many people may fail to install various components during startup. You are advised to run the following command

minikuebe start --memory=5700 --cpus=4 --registry-mirror=https://qe1j0wqo.mirror.aliyuncs.com - kubernetes - version = v1.19.0Copy the code

Specify the proxy and kubernets version, and use this command to start subsequent restarts.

Viewing cluster information:

kubectl cluster-info
Copy the code

You can run Minikube SSH to log in to the Minikube VM and explore it internally

Run the first application on Kubernetes

Deploy the Node.js application

kubectl run kubia --image=luksa/kubia --port=8080
Copy the code

You can use the commandkubectl get poorminikuebe dashboardCheck that the POD is running.

Introduction to Pod

It uses the idea of multiple co-existing containers. This set of containers is called a POD. A POD is a set of closely related containers that always run together on the same working node and in the same Linux namespace. Each POD is like a separate logical machine, with its own IP, hostname, process, and so on, running a separate application.

Relationships between containers, PODS, and physical working nodes:

Run in Kubernetesluksa/kubiaContainer image:

Accessing Web Applications

Create a service application as the gateway to kubia:

kubectl expose po kubia --type=LoadBalancer --name kubia-http
Copy the code

However, Minikube does not support LoadBalancer services, so the service does not have an external IP and therefore cannot access the application directly. However, the service can be accessed through an external port by executing the following command on the console:

minikube service kubia-http
Copy the code

The browser will open and accesshttp://127.0.0.1:61478Get response content

Pods are ephemeral, and a POD can disappear at any time, either because the node on which it is located fails, because someone deleted a POD, or because a POD was culled from a healthy node. When either of these situations occurs, the new POD has a different IP address than the pod that replaced it. This is where the service is needed. Solve the problem of changing POD IP addresses and exposing multiple pods on a fixed IP and port.