Using Docker+kubernetes Practice (1)

Source: Internet
Author: User
Tags node server docker registry

Simple Introduction

Use docker+kubernetes today to build a simple Hello World node. js Application.

Actual operation

1. Follow the tutorial to install docker+kubernetes for MAC


About Tool
About states

2. Verify Success


Docker
Kubernetes version

This docker+kubernetes installation is successful.

2. Writing the node. JS Application

Save this code in a folder named Hellonode with the file name Server.js:

var http = require (' http ');

var handlerequest = function (Request, response) {

Console.log (' Received Request for URL: ' + request.url);

Response.writehead (200);

Response.End (' Hello world! ');

};

var www = http.createserver (handlerequest);

Www.listen (8080);

You can try node server.js to run it, you should be able to see "Hello world!" on HTTP./localhost:8080/ Message Press CTRL-C to stop the node. JS server that is running.

3. Package the application in a Docker container

You need to use the Dockerfile file to create the container and create the Dockerfile file in the Server.js folder as follows:

From node:6.9.2

EXPOSE 8080

COPY Server.js.

CMD node Server.js

This configuration of the Docker image starts with the official node. JS LTS image in the Docker registry, exposes port 8080, copies the server.js file to the mirror, and starts the node. JS server.

4. Create a mirror based on the Dockerfile file using the build command

To build a Docker image using the Docker daemon (note the trailing dots):

Docker build-t Hello-node:v1.

5. Create a deployment

The Kubernetes Pod is a combination of one or more containers for management and networking. The pod in this tutorial has only one container. The Kubernetes deployment checks the health of the pod and restarts the pod container when the pod terminates. Deployment is the recommended way to manage the creation and expansion of pods.

Use the Kubectl Run command to create a deployment that manages the pod. The pod runs container based on the hello-node:v1docker image. Set the--IMAGE-PULL-POLICY flag never to always use the local image instead of extracting it from the Docker registry (because you haven't pushed it there yet):

Kubectl run Hello-node--image=hello-node:v1--port=8080--image-pull-policy=never

To view the deployment:

Kubectl Get deployments

Output:


Output

To view pods:

Kubectl Get Pods

Output:


Output

6. Create a service

By default, the pod can only be accessed through the internal IP address in the Kubernetes cluster. To access container from outside the hello-nodekubernetes virtual network, you must expose the pod as a kubernetes service . You can use the following Kubectl expose command to expose pods to the public Internet:

Kubectl Expose Deployment Hello-node--type=loadbalancer

To view the service you just created:

Kubectl Get Services

Output:


Output

The--type=loadbalancer flag represents a service that is exposed outside the cluster.

7. Automatically open the browser window with the local IP address and display the "Hello World" message.


Hello world!

8. Update the application

This edits the Server.js file to return the new message:

Response.End (' Hello World again! ');

9. Rebuilding the version of the image

Docker build-t Hello-node:v2.

10. Update the image of the deployment:

Kubectl Set Image Deployment/hello-node Hello-node=hello-node:v2


Update complete

11. Run your app again to see the new message:


Update complete

12. Kubernetes Instrument panel


Instrument panel

Reference: Kubernetes

Using Docker+kubernetes Practice (1)

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.