Docker data management, communications networks, resource control, docker swarm cluster configuration and use of

Doker Data Management

Data management operations of the container can see easily the data generated within the container or containers of the plurality of data sharing. Data Management Docker container mainly in two ways: data volume (Data Volumes) and data volume container (Data Volumes Containers).


First, (creating their own textbook on learning my-vol, practical application here, you can specifically found online tutorial) data volumes

The data volume is a special directory for the use of containers, the container, the directory of the host may be mounted to the data volume, to achieve migration of data between the host and the container.

1. Download Mirror centos

docker pull centos

2. Create a data volume

# Create a container named web1 and create two data volumes are mounted on / data1 and / data2 directory using centos mirror, where the self-created directory file

docker run -d -v /data1 -v /data2 --name web1 -it centos /bin/bash

Docker data management, network communications and resource control, the following is my thought experiment, I do not know the parameters, but also more common

        -i : 让容器对的输入保持打开
        -t : 让Docker分配一个伪终端
        -d : 让docker以守护形式在后台运行

3. Go to the container to view / data1 and / data2 directory

docker exec -it web1 /bin/bash

ls -l

 


4. Mount the host as a data volume directory

# Create a container named using centos web2 image, the host computer / var / www directory is mounted to the container / data1 directory

docker run -d -v /var/www:/data1 --name web2 -it centos /bin/bash

Note: The path to the local host directory must use an absolute path, if path does not exist, Docker automatically creates the appropriate path.

5. Create a file in the file / var / www directory of the host
 

cd /var/www

touch file

ls


6. running into the container, to view the appropriate mount directory / data1 directory

docker exec -it web2 /bin/bash

cd /data1

ls


It realized the data migration from the host to the container.


Second, the data volume of the container

Data volume container is an ordinary container, specifically providing data volumes to mount other container used. Some data sharing between the containers.
1. Create a data volume of the container as the container

Using the previous data of the created volume container web1
2. Use --volumes-from the data volume to mount the container web1 new container, a new container named db1

docker run -it --volumes-from web1 --name db1 centos /bin/bash

ls


3. Create a file in the file data volume / data2 directory db1 container

cd /data2

touch file

ls


4. In the web1 container / data2 directory to view files created file

docker exec -it web1 /bin/bash

cd /data2

ls


这样就可以通过数据卷容器实现容器之间的数据共享。


Docker网络通信

Docker提供了映射端口到宿主机和容器互联机制来为容器提供网络服务。


一、端口映射

Docker提供端口映射机制来将容器内的服务提供给外部网络访问,实质上就是提供将宿主机的端口映射到容器中,使得外部网络访问宿主机的端口便可访问容器内的服务。

根据书本所写以及我看的网上的教程材料可以知道,实现端口映射可以用两种方式

#方法一:
命令格式: docker run -d -P 镜像名称
其中-P(大写)选项实现随机映射

#方法二:
命令格式: docker run -d -p 宿主机的端口号:容器内的端口号 镜像名称
其中-p(小写)选项指定要映射的端口


1.端口随机映射

 docker run -d -P httpd:centos   #随机映射

使用docker ps -a命令查看端口的映射

2.指定端口映射

 

docker run -d -p 49280:80 httpd:centos #指定端口映射

使用docker ps -a命令查看端口的映射

二、容器互联

容器互联是通过容器的名称在容器键建立一条专门的网络通信隧道从而实现容器的互联。简单点说,就是在源容器和接收容器间建立一条隧道,接收容器可以看到源容器指定的信息。

格式为--link name:alias
其中name是要连接的容器名称,alias是这个连接的别名

1.创建源容器

使用docker命令建立容器A,使用--name指定容器名称为test1.

docker run -itd -P --name test1  centos  /bin/bash


2.创建接收容器

使用docker run命令建立容器B,使用--name指定容器名称为test2,使用--link指定连接容器以实现容器互联。

docker run -itd  -P --name test2 --link test1:test1 centos  /bin/bash


3.测试容器互联

进入接收容器,使用ping命令查看是否能连通。

docker exec -it test2 /bin/bash
ping test1

注意:

不同容器之间想要互相通信必须在同一个网络环境;使用默认bridge网络管理的容器可以使用容器IP进行通信,但无法使用容器名称进行通信;而使用自定义网络管理的容器则可以同时使用容器名称进行通信


部署docker Swarm

实验环境:

这里选择三台主机运行Swarm,依次为:

node1   192.168.100.5

node2  192.168.100.6

node3  192.168.100.7

基本环境配置

3台主机确保时间一致 ntp

3台主机均关闭selinux,开启路由转发。 /ect/sysctl.conf

[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1

关闭selinux

[root@localhost ~]# setenforce 0

3台主机修改/etc/hosts文件

wKioL1mrwk6Tw4RBAAAJvJ4uGWE420.png-wh_50

Ping  连通性

wKiom1mrwrryIT9wAAAV0-_-RLE154.png-wh_50

开启宿主机之间的端口 

TCP端口2377集群管理端口

TCP与UDP端口7946节点之间通讯端口

TCP与UDP端口4789 overlay网络通讯端口

wKioL1mrwrDik1x-AAAycdfHzk4610.png-wh_50


配置所有节点密钥登录

配置所下节点密钥互信, 在node1可以免密码登录各节点,只在node1上执行:

生成sshkey

[root@localhost ~]# ssh-keygen

发布sshkey到各个节点  ssh-copy-id node2  3 1

wKiom1mrwujBTAq7AAArrFBozmQ320.png-wh_50

测试密钥登录

wKioL1mrwtvAwrKtAAAjRjRvsaM654.png-wh_50

安装docker 1.12  如yum报错 rm -rf  /var/rum/yum.pid 删除pid  3台都需要安装

[root@localhost /]# yum -y install docker
[root@localhost /]# systemctl start docker

docker Swarm 模式简介

主要使用三个新的命令行工具创建一个swarm集群:

docker swarm 开启swarm模式; 加入Swarm集群; 配置集群参数

docker node 查询集群节点信息; 提升/移除一个管理节点; 管理swarm节点主机

docker service 创建管理 service

可以查看docker  --help

注意 你我们需要在一个node1上初始化swarm集群, 其他node加入这个集群就行了, 所以以下命令只需要在node1上运行.

[root@localhost /]# docker swarm init --advertise-addr 192.168.100.5

wKiom1mrwyiyzxZtAAAjufiqens222.png-wh_50

1) 查看swarm集群node列表 

[root@localhost /]# docker node ls

wKioL1mrwzHAj5bhAAAQFQ193l4292.png-wh_50

我们的其他节点服务器,以worker角色加入swarm集群需要登录到服务器运行如下命令:

[root@localhost /]# docker swarm  join-token  worker

1) 其他节点以worker加入集群使用上面查看到的命令加入集群(node2、node3上)

wKiom1mrw4nwYwpzAAAYnhZ4tFI360.png-wh_50

1) 查看集群节点情况,验证加入与否

wKioL1mrw3_wziR8AAAXmNDSaYE922.png-wh_50

1) 提升node2为管理 

[root@node1 /]# docker node promote node2

wKiom1mrw7vRpdP3AAAYJieaYyQ176.png-wh_50

docker  swarm:集群管理,子命令主要有下面几个。

docker swarm init命令用于初始化一个集群

dockerswarm join命令用于加入一个现有集群

dockerswarm leave命令由于离开集群


有了Docker Swarm集群我们如何把我们的应用跑在Swarm集群上呢?

很简单, 基本上原来我们使用docker run的命令创建容器, 把前面替换成docker service create就行了.

建议搭建一个registry,为所的docker主机提供镜像下载,否则你需要在每个docker主机本地存在容器镜像。

所以搭建一个私有仓库,由私有仓库提供所需要的镜像,我的实验环境中用node1同时作为registry。

拉取本地私有仓库registry,查看registry镜像

wKioL1mrw9nBDK62AAAX33zcGpI317.png-wh_50

上传registry2.tar

wKiom1mrw9bSFZcWAAAgjeklmR4979.png-wh_50

运行容器(端口映射、随docker启动时容器亦启动、路径映射、名字)

[root@node1 src]# mkdir -p /opt/data/registry
[root@node1 src]# docker run -d -p 5000:5000 --restart=always -v /opt/data/registry/:/var/lib/registry --name registry2 registry:2

wKioL1mrw-nQ3Xx0AAAX33zcGpI085.png-wh_50

查看私有仓库(可以看到仓库为空)

[root@node1 src]# curl 192.168.100.5:5000/v2/_catalog

所有节点指向registry服务器:

停止docker服务

[root@node1 src]# vim /usr/lib/systemd/system/docker.service

wKioL1mrxNPg_HtPAAAW-TKPj40708.png-wh_50

[root@node1 src]# systemctl daemon-reload 
[root@node1 src]# systemctl start docker

1. 推送镜像并验证

node2上传镜像

[root@node2 src]# docker load < centos7.tar

node2配置Dockerfile

[root@node2 apache]# vim dockerfile 
FROM docker.io/centos:latest
RUN yum -y install httpd net-tools
RUN sed 's/#ServerName /ServerName /g' -i /etc/httpd/conf/httpd.conf
EXPOSE 80
CMD  ["/usr/sbin/httpd","-DFOREGROUND"]

node2构建

[root@node2 apache]# docker build -t 192.168.100.5:5000/centos:httpd .

node2上传镜像到registry

查看registry中镜像

[root@node2 apache]# curl 192.168.100.5:5000/v2/_catalog

node3、node1测试从registry下载镜像  在nond3 测试

wKiom1mrxVehKZ3VAAAivWU--P4490.png-wh_50

wKioL1mrxTyQ6TKMAAARo9ikvdA304.png-wh_50


overlay网络

解决了镜像构建问题, 为了让应用跑在swram集群上,我们还需要解决容器间的网络访问问题.

单台服务器的时候我们应用所有的容器都跑在一台主机上, 所以容器之间的网络是互通的. 现在我们的集群有3台主机, 所以docker应用的服务会分布在这3台主机上.

如何保证不同主机上的容器网络互通,而swarm集群已经帮我们解决了这个问题了,就是只用overlay network.

下面我们演示下如何创建一个 overlay network:

注:swarm上默认已有一个名为ingress的overlay 网络, 可以直接使用, 但本文会创建一个新的

为我们的docker应用创建一个名为dockercoins的overlay network

1. node2上创建overlay network

[root@node2 apache]# docker network create --driver overlay dockercoins
[root@node2 apache]# docker network ls

wKiom1mrxbKjKKl9AAAnWiyX3Fk581.png-wh_50

1. node1上查看(worker节点看不到)

wKioL1mrxanDPn7yAAAdxNGAfr0100.png-wh_50

注:一旦新的任务被指定给这个节点,Overlay网络就会被按需创建。

在swarm集群上运行docker应用

Docker swarm引入了服务的概念,一个服务由多个任务组成,一个任务即一个运行的容器。

服务包括两种类型:

复制服务(replicated services):类似 k8s 中复制集的概念,保持一定数量的相同任务在集群中运行;

全局服务(global services):类似 k8s 中 daemon 的概念,每个工作节点上运行一个。

发布服务:

在manager上执行如下命令:

下面我们可以使用之前push到本地镜像仓库的镜像启动服务, 以centos:http为例:

以复制服务类型运行服务

在manager上执行如下命令:

[root@node1 src]# docker service create --replicas 1 --network dockercoins --name web1 -p 8000:80 192.168.100.5:5000/centos:httpd
efd1jxmt3pthvwscgkt1n1hj6

wKiom1mrxfOSCUsUAAAjK7W-NGQ339.png-wh_50

浏览器验证:(三个节点ip:8000都可以访问)

wKioL1mrxeejb03wAABZmNNgzqA183.png-wh_50

如运行web2,俩个容器运行服务:

[root@node1 src]# docker service create --replicas 2 --network dockercoins --name web2 -p 8080:80 192.168.100.5:5000/centos:httpd
f2wh08sgy2q2wp5s63oczxk1t

wKioL1mrxgLQhfa5AAAxkH7KDtM294.png-wh_50

从上图可以看到web2名称的service有2个副本分别运行在node2和node3节点上

1. 以全局服务类型运行服务:

wKiom1mrxiySvC1aAAAROb4ALmY752.png-wh_50

从下图可以看到服务web4在每个节点上都运行一个

wKioL1mrxiGQ1RNGAABIaTdq2ZI848.png-wh_50

下面我们扩展旧的服务,从下图可以看到web1  service目前只有一个副本

[root@node1 /]# docker service scale web1=3

wKiom1mrxlez5I7TAAAcYTQsvY8071.png-wh_50

wKiom1mrxmTjtZF8AAAWhv15hD4392.png-wh_50

可以看到web1服务扩展到3个副本数。

3 缩减已有的服务副本数:

[root@node1 /]# docker service scale web1=2

wKioL1mrxmTyrx5fAAAVFv3byWY972.png-wh_50

可以看到web1服务缩减到2个副本数。

Swarm节点是自组织(self-organizing)和自修复(self-healing)的,什么意思?只要有节点或容器宕掉,swarm engine就会尝试修复,下面我们来具体看一下


自修复(self-healing)

wKiom1mrxpOyHLVUAAApqHy2_Gw850.png-wh_50

可以看到:3个节点,运行8个任务(容器)。包含一个registry容器。

wKioL1mrxovxo3ZOAAAssES16NE652.png-wh_50

我们模拟让node3上的容器都宕掉或部分宕掉

[root@node3 /]# docker stop $(docker ps -aq)

wKioL1mrxrPgkspcAAAJ5CWAQv8348.png-wh_50

结果:

wKiom1mrxtjw44a1AAAg4LV8DSM415.png-wh_50

可以看到docker会在相同节点上启动1个不同ID的容器。

Self-Organizing

现在我们让node3整个宕掉,node3上的容器会自动在其它节点上启动。

在manager节点上执行docker  server ps服务名

[root@node3 /]# systemctl stop docker

结果: node1 上

wKioL1mrxtnigVi3AAAu_Iyv4F4502.png-wh_50

node2

wKiom1mrxwKQB2ZXAAAmXpIu4oI806.png-wh_50

可以看到node3上的web2.2在node2上启动了;至于node3上的web4.0则因为是全局服务类型,故而node3停止服务后,会停掉。


Docker资源控制(没有全部尝试,但是根据我有的资料整理了出来)

Docker是使用Cgroup机制进行管理的,Cgroup是Control group的简写,是Linux内核提供的一种限制所使用物理资源机制,这些资源主要包括CPU、内存、blkio.

一、对CPU的控制


1.限制CPU的使用速率

--cpu-quota选项限制CPU的使用率,CPU的百分比是以1000为单位的。

docker run --cpu-quota 20000 centos(容器名)    #cpu的使用率限定为20%

2.多任务按比例分享CPU

--cpu-shares设置CPU按比例共享CPU资源

docker run --cpu-shares 1024 容器A
docker run --cpu-shares 1024 容器B
docker run --cpu-shares 1024 容器C

3.限制CPU内核使用

--cpuset-cpus使某些程序独享CPU内核,以便提高其处理速度

docker run --cpuset-cpus 0,1 容器名       #容器独享 第1和第2个内核

二、对内存使用的限制

docker run -m命令限制容器内存使用量。

docker run -m 512m 容器名      #限制容器内存512M

三、对blkio的限制

在一台服务器上进行容器的混合部署,会出现同时有几个程序写磁盘数据的情况,可以通过--device-write-bps选项限制写入的ipos,相应的还有--device-read-bps选项限制读取的ipos。但是这个方法只能针对blkio限制的是设备,而不是分区。

例如限制容器的/dev/sda1的写入ipos为1MB

docker run --device-write-bps /dev/sda1:1mb 容器名

 

Guess you like

Origin blog.csdn.net/qq_23090489/article/details/92568647