Micro Services to deploy common tools --docker


Before using docker, first use Ali cloud mirrored accelerator to accelerate, or else things are very slow to download. . . Detailed Configuration: using aliyun docker mirror source to mirror the accelerator acceleration

The role of 1.docker

Docker provide lightweight virtualization, you can get an extra layer of abstraction from Docker, you can run a plurality of micro Docker containers on a single machine,

And each container has a micro-micro-service or a standalone application, for example, you can run Tomcat in a Docker, and MySQL running in another Docker,

Both can run on the same server or multiple servers. Possible future of each application must Docker.

Container and start the relationship is very fast. Docker is now able to have the following eight purposes:

1.1 simplifies the configuration

This is the initial purpose of Docker, the biggest advantage of virtual machine (VM) is based on the configuration of your application can run seamlessly on any platform. Docker provides the same capabilities similar to the VM,

But without any side effects, it allows you to configure the environment and then deploy into the code, the same Docker configuration can be used in a variety of environments, which is the actual application environment and to achieve a decoupling of the underlying environment.

1.2 Pipeline Management Code

Can be managed in the code stream of the pipeline conduit, a developer from machine to machine in this production process can be effectively managed. Because there will be a variety of different environments in this process, each of which may have a slight difference, Docker provides consistency across these heterogeneous environments in a micro-environment, from development to deployment for smooth release.

Production of the 1.3 developer

In a development environment, we want our development environment can be closer to a production environment, we will let each service runs in its own VM, this will simulate the production environment,

For example, sometimes we do not always need to cross-Fi, so we can run multiple Docker loading a range of services to maximize the production of a distributed simulation environments deployed on a single machine.

1.4 application isolation

There are many reasons you need to run multiple applications on a single machine, which requires the original monolithic monolithic applications cut into many micro-services. Decoupling between applications,

The service is deployed in multiple applications in multiple Docker can easily achieve this goal.

1.5 Services merge

Use Docker can combine multiple services to reduce cost, small footprint operating system, shared across multiple instances of free memory, these techniques can Docker resources to more closely

Provide more effective services merge.

More than 1.6 Tenant

Docker containers can be used as a multi-tenant cloud computing, the use of Docker can easily create multiple instances should run for each tenant, which benefit the environment as well as its flexible fast diff command effectively.

1.7 rapid deployment

Docker through the process of creating a container, without having to restart the operating system, can be closed within a few seconds, you can create or destroy resources in the data center, do not worry about the extra consumption.

A typical data center efficiency is 30%, by more aggressive resource allocation to achieve a more cost-effective way of polymerization of a new resource allocation for instance,

We can easily exceed this utilization, greatly improving the utilization efficiency of the data center.

We all know that the operating system is divided into kernel and user space. For the latter Linux, the kernel starts, it will mount the root file system to provide user-space support.

The Docker image (Image), the equivalent of a root file system. For example, the official mirroring ubuntu: 14.04 contains the complete set of Ubuntu 14.04

Minimum system root file system.

Docker image is a special file system, in addition to providing the desired containers run programs, libraries, resources, and other configuration files,

Also it contains a number of configuration parameters to prepare for the runtime (such as anonymous volume, environment variables, users, etc.). Image does not contain any dynamic data,

Its contents are not changed after build.

1.8 tiered storage

Because the operating system image contains the complete root file system, the volume is often large, so Docker design, take full advantage of Union FS technology,

It is designed as a tiered storage architecture. So, strictly speaking, the mirror is not as packed as an ISO file, a virtual image is just concept, which reflects the actual

It is not composed of one file, but by a set of file system components, or by a coalition of multi-layer file system.

When constructing the mirror, will build a layer, the previous layer is the basis of the latter layer. Each layer you've built will not change, any change in the level occurs only in his own this layer.

For example, one file before delete operation, the actual not really delete the previous layer files, but only deleted in the current layer mark for the file. When the final container operation,

Although not see this file, but in fact the file will always follow the mirror. Thus, when building the mirror, extra care needs, as far as possible each layer of the layer containing only

Things need to add any extra things to be cleared away before the end of the layer build.

Further characterized in that the tiered storage mirror multiplexing, the customized becomes easier. Even before using the constructed image as a base layer, and then further adding a new layer,

To customize their content required to build a new image.
Use the principle of Docker

2.docker installation

2.1 Installation Docker

Docker official recommended to install in Ubuntu, because Docker is based on Ubuntu release, but problems arise general Docker Ubuntu is the first update or patch. In many versions of CentOS is not supported by some of the latest patch update package.

Since we are using the learning environment it is CentOS, so here we will Docker installed on CentOS. Note: It is recommended to install at least CentOS7.x version, in CentOS6.x version, you need to install before installing a lot of other environmental and Docker many patches do not support updates.

Please direct mount curriculum supporting Centos7.x mirror

(1) yum update to the latest package

sudo yum update

(2) require installation package, yum-util provide yum-config-manager function, the other two are driven dependent devicemapper

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

(3) Set yum source cloud Ali

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

(4) mounted docker

sudo yum install docker-ce

(5) After installing the version View docker

docker -v

Here Insert Picture Description

2.2 Set mirror ustc

ustc is a veteran of linux mirror service provider, and as far in ubuntu 5.04 version when in use. docker Mirror accelerator speed ustc quickly. One of the advantages ustc docker mirror is no need to register, is a real public service.

https://lug.ustc.edu.cn/wiki/mirrors/help/docker

Edit the file:

vi /etc/docker/daemon.json  

Enter the following in the file:

{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}

2.3 Docker start and stop

systemctl command is the command system services manager

Start docker:

systemctl start docker

Stop docker:

systemctl stop docker

Restart docker:

systemctl restart docker

View docker Status:

systemctl status docker

boot:

systemctl enable docker

View docker summary information

docker info

View docker help documentation

docker --help

3 Common Commands

3.1 mirroring command

3.1.1 View Mirror

docker images

REPOSITORY: image name

TAG: image tag

IMAGE ID: ID mirroring

CREATED: Creation Date mirrored (not the acquisition date of the mirror)

SIZE: image size

These images are stored in the host Docker / var / lib / docker directory

3.1.2 Search Mirror

If you need to find the image you want from the network, you can search by the following command

docker search 镜像名称

NAME: warehouse Name

DESCRIPTION: Mirror Description

STARS: user evaluation, a mirror of the reaction popularity

OFFICIAL: Are official

AUTOMATED: automatically build, indicating that the mirror automatically build process created by the Docker Hub

3.1.3 pull mirroring

Pulling mirror image is downloaded from the central to the local repository

docker pull 镜像名称

For example, I want to download centos7 Mirror

docker pull centos:7

3.1.4 remove the mirror

Mirror Mirror ID delete Press

docker rmi 镜像ID

Delete all mirrors

docker rmi `docker images -q`

3.2 vessel-related commands

3.2.1 View container

View container running

docker ps

Here Insert Picture Description
View all containers

docker ps –a

Here Insert Picture Description
View the last run of the container

docker ps –l

Check stop container

docker ps -f status=exited

3.2.2 Creating and starting container

Create a custom container Parameters:

Create a container command: docker run

-i: indicates the operating container

-t: a rear container starts will enter its command line. After the addition of these two parameters, the vessel will be able to create log into. That is assigned a pseudo-terminal.

-name: named container created.

-v: represents the directory mapping relationship (the former is the host directory, which is mapped to a directory on the host), you can use multiple -v do multiple directories or file mappings. Note: It is the directory for mapping, making changes on the host, and then to share the container.

-d: run behind the -d parameter will create a guardian of the container does not automatically logged container after (this container is created in the background, adding -i -t if only two parameters, it will automatically go after creation container).

-p: mapping the port, the former is a host port, which is mapped in the port of the container. You can use multiple -p port mapping to do more

Create a container (1) interactive mode

docker run -it --name=容器名称 镜像名称:标签 /bin/bash

Such as:

docker run -it --name=mycentos centos:7  /bin/bash

Then we see the ps command, you can see the launch of the vessel was found, state to start state

Exit the current container

exit

(2) create the container type guard mode:

docker run -di --name=容器名称 镜像名称:标签

Such as:

docker run -id --name=tensquare_mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=123456 centos/mysql-57-centos7

Here Insert Picture Description

Login guardian container ways:

docker exec -it 容器名称 (或者容器ID)  /bin/bash

Such as:

docker exec -it mynginx /bin/bash

3.2.3 stop and start the container

Stop the container:

docker stop 容器名称(或者容器ID)

Start container:

docker start 容器名称(或者容器ID)

3.2.4 file copy

You can use the cp command if we need to copy the file into the container

docker cp 需要拷贝的文件或目录 容器名称:容器目录

Files can also be copied from the container

docker cp 容器名称:容器目录 需要拷贝的文件或目录

3.2.5 directory is mounted

When we can create a container, the container directory in the directory are mapped host, so that we can modify the host file to a directory in order to influence the container.
Create a container add -v parameter back to the host directory: container, for example:

docker run -di -v /usr/local/myhtml:/usr/local/myhtml --name=mycentos3 centos:7

If you share a multi-level directory, insufficient permissions prompt may appear.

This is because the security module selinux CentOS7 authority of the ban, and we need to add parameters --privileged = true to solve the problem without permission mounted directory

3.2.6 View IP address container

We can see the vessel running the following command a variety of data

docker inspect 容器名称(容器ID) 

You can also perform the following command to direct output direct IP address

docker inspect --format='{{.NetworkSettings.IPAddress}}' 容器名称(容器ID)

3.2.7 delete container

Removes the specified container:

docker rm 容器名称(容器ID)

4 application deployment

4.1 MySQL deployment

(1) Pull mirror mysql

docker pull centos/mysql-57-centos7

(2) create the container

docker run -di --name=tensquare_mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=123456 centos/mysql-57-centos7

The -p port mapping, port mapping format host: port container runtime

-e Add the environment variable MYSQL_ROOT_PASSWORD on behalf of the root user's login password

(3) remote login mysql

Connecting the host IP, port designated as 33306 (33306 port corresponds to the local computer in docker 3306)
Here Insert Picture Description

4.2 tomcat deployment

(1) Mirror Pull

docker pull tomcat:7-jre7

(2) create the container

Create a container -p indicates the address mapping

docker run -di --name=mytomcat -p 9000:8080 -v /usr/local/webapps:/usr/local/tomcat/webapps tomcat:7-jre7

Screenshot deploy a project:
Here Insert Picture Description

4.3 Nginx deployment

(1) Mirror Pull

docker pull nginx

(2) create Nginx container

docker run -di --name=mynginx -p 80:80 nginx

Visit: http://192.168.110.131screenshot:
Here Insert Picture Description

4.4 Redis deployment

(1) Mirror Pull

docker pull redis

(2) create the container

docker run -di --name=myredis -p 6379:6379 redis

Here Insert Picture Description
Use the desktop version of the test:
Here Insert Picture Description

Backup and Migration 5 (switching between the mirror and the container)

Here Insert Picture Description
Here Insert Picture Description

Image storage container is 5.1

We can save the following command vessel for the mirror

docker commit mynginx mynginx_i

5.2 image backup

We can save the image with the following command as tar files

docker  save -o mynginx.tar mynginx_i

5.3 image recovery and migration

First we removed mynginx_img mirror and then execute the command to be restored

docker load -i mynginx.tar

-I input file

After performing to view mirror again, you can see the image has been restored
Here Insert Picture Description
after recovery:
Here Insert Picture Description

6 Dockerfile

6.1 What is Dockerfile

Dockerfile script is a series of commands and parameters constituted these commands to the base image (such as operating systems centos, etc.) and finally create a new image.

1. For developers: provide a consistent development environment for the development team;
2, for the testers: You can take a direct mirror image constructed when developing or build a new image by Dockerfile file to begin work;
3, for transportation and maintenance personnel: at deployment time, enables seamless migration of applications.

6.2 Common Commands

command effect
FROM image_name:tag Which defines the use of the base image to start the process of building
MAINTAINER user_name Disclaimer mirror creator
ENV key value Set Environment Variables (you can write multiple)
RUN command Dockerfile is a core part (you can write multiple)
ADD source_dir / file dest_dir / file Copy the file to the host of the container, if it is a compressed file, it will automatically extract after replication
COPY source_dir / file dest_dir / file And like ADD, but if there is not a compressed file and unzip
WORKDIR path_dir Set the working directory

6.3 using a script to create a mirror

step:

(1) Create a directory

mkdir –p /usr/local/dockerjdk8

(2) Download jdk-8u171-linux-x64.tar.gz and uploaded to the server (virtual machine) in the / usr / local / dockerjdk8 directory

(3) create a file Dockerfile vi Dockerfile

#依赖镜像名称和ID
FROM centos:7
#指定镜像创建者信息
MAINTAINER ITCAST
#切换工作目录
WORKDIR /usr
RUN mkdir  /usr/local/java
#ADD 是相对路径jar,把java添加到容器中
ADD jdk-8u171-linux-x64.tar.gz /usr/local/java/

#配置java环境变量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_171
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH

(4) Construction of the mirror Run

docker build -t='jdk1.8' .

Note the back of the spaces and points, do not omit

(5) whether to establish a complete view mirror

docker images

7 Docker private warehouse

7.1 private warehouse set up and configuration

(1) Pull private warehouse image (this step is omitted)

docker pull registry

(2) Start private container warehouse

docker run -di --name=registry -p 5000:5000 registry

(3) Open the browser and enter the address http://192.168.184.141:5000/v2/_catalog see {"repositories":[]}representation of private warehouse and build a successful content is empty

(4) modified daemon.json

vi /etc/docker/daemon.json

Add the following content, save and exit.

{"insecure-registries":["192.168.184.141:5000"]} 

This step allows docker trust for private warehouse address

(5) Restart docker Service

systemctl restart docker

Here Insert Picture Description

7.2 Mirroring uploaded to a private warehouse

(1) This marked as private mirror image warehouse

docker tag jdk1.8 192.168.184.141:5000/jdk1.8

(2) container to start again PW

docker start registry

(3) labeled image upload

docker push 192.168.184.141:5000/jdk1.8

Another machine wants to pull this mirror can be used directly:

docker pull 192.168.184.141:5000/jdk1.8

Here Insert Picture Description

Published 122 original articles · won praise 1 · views 7337

Guess you like

Origin blog.csdn.net/qq_36079912/article/details/104281015