docker资源配额及私有镜像仓库

1、docker资源配额
1.1 docker容器控制cpu

Docker通过cgroup来控制容器使用的资源限制,可以对docker限制的资源包括CPU、内存、磁盘

1.1.1 指定docker容器可以使用的cpu份额

#查看配置份额的帮助命令:

[root@node1 ~]# docker run --help|grep cpu-shares

cpu配额参数:-c, --cpu-shares int

CPU shares (relative weight) 在创建容器时指定容器所使用的CPU份额值。cpu-shares的值不能保证可以获得1个vcpu或者多少GHz的CPU资源,仅仅只是一个弹性的加权值。

默认每个docker容器的cpu份额值都是1024。在同一个CPU核心上,同时运行多个容器时,容器的cpu加权的效果才能体现出来。

例: 两个容器A、B的cpu份额分别为1000和500,结果会怎么样?
情况1:A和B正常运行,占用同一个CPU,在cpu进行时间片分配的时候,容器A比容器B多一倍的机会获得CPU的时间片。

情况2:分配的结果取决于当时其他容器的运行状态。比如容器A的进程一直是空闲的,那么容器B是可以获取比容器A更多的CPU时间片的; 比如主机上只运行了一个容器,即使它的cpu份额只有50,它也可以独占整个主机的cpu资源。

cgroups只在多个容器同时争抢同一个cpu资源时,cpu配额才会生效。因此,无法单纯根据某个容器的cpu份额来确定有多少cpu资源分配给它,资源分配结果取决于同时运行的其他容器的cpu分配和容器中进程运行情况。

例1:给容器实例分配512权重的cpu使用份额
参数: --cpu-shares 512

[root@node1 ~]# docker run -it --cpu-shares 512 centos /bin/bash
[root@2548d840433f /]# cat /sys/fs/cgroup/cpu/cpu.shares
512

注:稍后,我们启动多个容器,测试一下是不是只能使用512份额的cpu资源。单独一个容器,看不出来使用的cpu的比例。 因没有docker实例同此docker实例竞争。

总结:
通过-c设置的 cpu share 并不是 CPU 资源的绝对数量,而是一个相对的权重值。某个容器最终能分配到的 CPU 资源取决于它的 cpu share 占所有容器 cpu share 总和的比例。通过 cpu share 可以设置容器使用 CPU 的优先级。

比如在 host 中启动了两个容器:

docker run --name "container_A" -c 1024 ubuntu
docker run --name "container_B" -c 512 ubuntu

container_A 的 cpu share 1024,是 container_B 的两倍。当两个容器都需要 CPU 资源时,container_A 可以得到的 CPU 是 container_B 的两倍。

需要注意的是,这种按权重分配 CPU只会发生在 CPU资源紧张的情况下。如果 container_A 处于空闲状态,为了充分利用 CPU资源,container_B 也可以分配到全部可用的 CPU。

1.1.2 CPU core核心控制

参数:–cpuset可以绑定CPU
对多核CPU的服务器,docker还可以控制容器运行限定使用哪些cpu内核和内存节点,即使用–cpuset-cpus和–cpuset-mems参数。对具有NUMA拓扑(具有多CPU、多内存节点)的服务器尤其有用,可以对需要高性能计算的容器进行性能最优的配置。如果服务器只有一个内存节点,则–cpuset-mems的配置基本上不会有明显效果。

扩展:
服务器架构一般分: SMP、NUMA、MPP体系结构介绍
从系统架构来看,目前的商用服务器大体可以分为三类:

  1. 即对称多处理器结构(SMP : Symmetric Multi-Processor) 例: x86 服务器,双路服务器。主板上有两个物理cpu
  2. 非一致存储访问结构 (NUMA : Non-Uniform Memory Access) 例: IBM 小型机 pSeries 690
  3. 海量并行处理结构 (MPP : Massive ParallelProcessing) 。 例: 大型机 Z14
1.1.3 CPU配额控制参数的混合使用

在上面这些参数中,cpu-shares控制只发生在容器竞争同一个cpu的时间片时有效。
如果通过cpuset-cpus指定容器A使用cpu 0,容器B只是用cpu1,在主机上只有这两个容器使用对应内核的情况,它们各自占用全部的内核资源,cpu-shares没有明显效果。

如何才能有效果?
容器A和容器B配置上cpuset-cpus值并都绑定到同一个cpu上,然后同时抢占cpu资源,就可以看出效果了。

例1:测试cpu-shares和cpuset-cpus混合使用运行效果,就需要一个压缩力测试工具stress来让容器实例把cpu跑满。

如何把cpu跑满?
如何把4核心的cpu中第一和第三核心跑满?可以运行stress,然后使用taskset绑定一下cpu。

先扩展:stress命令
概述:linux系统压力测试软件Stress 。

[root@node1 ~]# yum install -y epel-release
[root@node1 ~]# yum install stress -y

stress参数解释

-?        显示帮助信息
-v        显示版本号
-q       不显示运行信息
-n       显示已完成的指令情况
-t        --timeout  N  指定运行N秒后停止        
           --backoff   N   等待N微妙后开始运行
-c       产生n个进程 :每个进程都反复不停的计算随机数的平方根,测试cpu
-i        产生n个进程 :每个进程反复调用sync(),sync()用于将内存上的内容写到硬盘上,测试磁盘io
-m     --vm n 产生n个进程,每个进程不断调用内存分配malloc()和内存释放free()函数 ,测试内存
          --vm-bytes B  指定malloc时内存的字节数 (默认256MB)
         --vm-hang N   指定在free栈的秒数   
-d    --hadd n  产生n个执行write和unlink函数的进程
         -hadd-bytes B  指定写的字节数
         --hadd-noclean  不unlink        
注:时间单位可以为秒s,分m,小时h,天d,年y,文件大小单位可以为K,M,G

例1:产生2个cpu进程,2个io进程,20秒后停止运行

[root@node1 ~]# stress -c 2 -i 2 --verbose --timeout 20s  

#如果执行时间为分钟,改20s 为1m
在这里插入图片描述
例1:测试cpuset-cpus和cpu-shares混合使用运行效果,就需要一个压缩力测试工具stress来让容器实例把cpu跑满。 当跑满后,会不会去其他cpu上运行。 如果没有在其他cpu上运行,说明cgroup资源限制成功。
实例3:创建两个容器实例:docker10 和docker20。 让docker10和docker20只运行在cpu0和cpu1上,最终测试一下docker10和docker20使用cpu的百分比。实验拓扑图如下:
在这里插入图片描述
运行两个容器实例
[root@xianchaomaster1 ~]# docker run -itd --name docker10 --cpuset-cpus 0,1 --cpu-shares 512 centos /bin/bash
#指定docker10只能在cpu0和cpu1上运行,而且docker10的使用cpu的份额512
#参数-itd就是又能打开一个伪终端,又可以在后台运行着docker实例
[root@xianchaomaster1 ~]# docker run -itd --name docker20 --cpuset-cpus 0,1 --cpu-shares 1024 centos /bin/bash
#指定docker20只能在cpu0和cpu1上运行,而且docker20的使用cpu的份额1024,比dcker10多一倍
测试1: 进入docker10,使用stress测试进程是不是只在cpu0,1上运行:

[root@node1 ~]# docker exec -it  docker10  /bin/bash
[root@d1a431815090 /]#  yum install -y epel-release  #安装epel扩展源
[root@d1a431815090 /]# yum install stress -y    #安装stress命令
[root@d1a431815090 /]#  stress -c 2 -v   -t  10m  #运行2个进程,把两个cpu占满

在物理机另外一个虚拟终端上运行top命令,按1快捷键,查看每个cpu使用情况:
在这里插入图片描述
可看到正常。只在cpu0,1上运行
测试2: 然后进入docker20,使用stress测试进程是不是只在cpu0,1上运行,且docker20上运行的stress使用cpu百分比是docker10的2倍

[root@node1 ~]# docker exec -it  docker20  /bin/bash
[root@d1a431815090 /]#  yum install -y epel-release  #安装epel扩展源
[root@d1a431815090 /]# yum install stress -y  
[root@f24e75bca5c0 /]# stress  -c 2 -v -t 10m

在另外一个虚拟终端上运行top命令,按1快捷键,查看每个cpu使用情况:
在这里插入图片描述
注:两个容器只在cpu0,1上运行,说明cpu绑定限制成功。而docker20是docker10使用cpu的2倍。说明–cpu-shares限制资源成功。

1.2 docker容器控制内存

Docker提供参数-m, --memory=""限制容器的内存使用量。
例1:允许容器使用的内存上限为128M:

[root@node1 ~]# docker run -it  -m 128m centos

查看:

[root@40bf29765691 /]# cat /sys/fs/cgroup/memory/memory.limit_in_bytes 
134217728

注:也可以使用tress进行测试,到现在,我可以限制docker实例使用cpu的核心数和权重,可以限制内存大小。
例2:创建一个docker,只使用2个cpu核心,只能使用128M内存

[root@node1 ~]# docker run -it --cpuset-cpus 0,1 -m 128m centos 
1.3 docker容器控制IO
[root@node1 ~]# docker run --help | grep write-b
      --device-write-bps value      Limit write rate (bytes per second) to a device (default [])  

#限制此设备上的写速度(bytes per second),单位可以是kb、mb或者gb。
–device-read-bps value #限制此设备上的读速度(bytes per second),单位可以是kb、mb或者gb。

情景:防止某个 Docker 容器吃光你的磁盘 I / O 资源

例1:限制容器实例对硬盘的最高写入速度设定为 2MB/s。
–device参数:将主机设备添加到容器

[root@node1 ~]# mkdir -p /var/www/html/
[root@node1 ~]#  docker run -it  -v /var/www/html/:/var/www/html --device /dev/sda:/dev/sda --device-write-bps /dev/sda:2mb centos  /bin/bash

[root@bd79042dbdc9 /]# time dd if=/dev/sda of=/var/www/html/test.out bs=2M count=50 oflag=direct,nonblock

注:dd 参数:
direct:读写数据采用直接IO方式,不走缓存。直接从内存写硬盘上。
nonblock:读写数据采用非阻塞IO方式,优先写dd命令的数据
50+0 records in
50+0 records out
52428800 bytes (52 MB) copied, 50.1831 s, 2.0 MB/s

real 0m50.201s
user 0m0.001s
sys 0m0.303s
注: 发现1秒写2M。 限制成功。

2、docker容器运行结束自动释放资源
[root@node1 ~]# docker run --help | grep rm
      --rm 参数:                 Automatically remove the container when it exits

作用:当容器命令运行结束后,自动删除容器,自动释放资源
例:

[root@node1 ~]# docker run -it --rm --name xianchao centos  sleep 6

物理上查看:

[root@node1 ~]# docker ps -a | grep xianchao
6c75a9317a6b        centos              "sleep 6"           6 seconds ago       Up 4 seconds                            mk

等5s后,再查看:

[root@node1 ~]# docker ps | grep xianchao #自动删除了
3、docker私有镜像仓库harbor

Harbor介绍
Docker容器应用的开发和运行离不开可靠的镜像管理,虽然Docker官方也提供了公共的镜像仓库,但是从安全和效率等方面考虑,部署我们私有环境内的Registry也是非常必要的。Harbor是由VMware公司开源的企业级的Docker Registry管理项目,它包括权限管理(RBAC)、LDAP、日志审核、管理界面、自我注册、镜像复制和中文支持等功能。
官网地址:https://github.com/goharbor/harbor

3.1 为Harbor自签发证书
[root@192 ~]# hostnamectl set-hostname harbor && bash
[root@harbor ~]# mkdir /data/ssl -p
[root@harbor ~]# cd /data/ssl/

生成ca证书:

openssl genrsa -out ca.key 3072

#生成一个3072位的key,也就是私钥

openssl req -new -x509 -days 3650 -key ca.key -out ca.pem

#生成一个数字证书ca.pem,3650表示证书的有效时间是3年,按箭头提示填写即可,没有箭头标注的为空:
在这里插入图片描述
生成域名的证书:

openssl genrsa -out harbor.key  3072

#生成一个3072位的key,也就是私钥

openssl req -new -key harbor.key -out harbor.csr

#生成一个证书请求,一会签发证书时需要的,标箭头的按提示填写,没有箭头标注的为空:
在这里插入图片描述
签发证书:

openssl x509 -req -in harbor.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out harbor.pem -days 3650

显示如下,说明证书签发好了:
在这里插入图片描述

3.2 安装Harbor
3.2.1 安装Docker

关闭防火墙

[root@ harbor~]# systemctl stop firewalld && systemctl disable firewalld

关闭iptables防火墙

[root@ harbor~]# yum install iptables-services -y  #安装iptables

禁用iptables

root@ harbor~]# service iptables stop   && systemctl disable iptables

清空防火墙规则

[root@ harbor~]# iptables -F 

关闭selinux

[root@ harbor~]# setenforce 0
[root@harbor~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

注意:修改selinux配置文件之后,重启机器,selinux才能永久生效

配置时间同步

[root@harbor~]# yum install -y ntp ntpdate
[root@xianchaomaster1 ~]# ntpdate cn.pool.ntp.org 

#编写计划任务

[root@harbor~]# crontab -e 
* */1 * * * /usr/sbin/ntpdate   cn.pool.ntp.org

重启crond服务使配置生效:

[root@xianchaomaster1 ~]# systemctl restart crond

配置hosts文件

[root@harbor ssl]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.75.140 master
192.168.75.141 harbor
192.168.75.142 node2

安装基础软件包

[root@ harbor~]# yum install -y  wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel  python-devel epel-release openssh-server socat  ipvsadm conntrack 

安装docker-ce,配置docker-ce国内yum源(阿里云)

[root@ harbor~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

安装docker依赖包

[root@ harbor~]# yum install -y yum-utils device-mapper-persistent-data lvm2

安装docker-ce

[root@ harbor~]# yum install docker-ce -y

启动docker服务

[root@ harbor~]# systemctl start docker && systemctl enable docker
3.2.2 开启包转发功能和修改内核参数

内核参数修改:br_netfilter模块用于将桥接流量转发至iptables链,br_netfilter内核参数需要开启转发。

[root@ harbor~]# modprobe br_netfilter
[root@ harbor~]# cat > /etc/sysctl.d/docker.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
[root@harbor ~]# sysctl -p /etc/sysctl.d/docker.conf

:Docker 安装后出现:WARNING: bridge-nf-call-iptables is disabled 的解决办法:
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1

net.ipv4.ip_forward = 1:
将Linux系统作为路由或者VPN服务就必须要开启IP转发功能。当linux主机有多个网卡时一个网卡收到的信息是否能够传递给其他的网卡 如果设置成1 的话 可以进行数据包转发,可以实现VxLAN 等功能。不开启会导致docker部署应用无法访问。

重启docker

[root@node1~]# systemctl restart docker  
[root@xianchaomaster1 ~]# scp /etc/docker/daemon.json 192.168.40.181:/etc/docker/
[root@harbor ~]# systemctl daemon-reload
[root@harbor ~]# systemctl restart docker
3.2.4 安装harbor

创建安装目录

[root@harbor ssl]# mkdir /data/install -p
[root@harbor ssl]# cd /data/install/

安装harbor

/data/ssl目录下有如下文件:
ca.key  ca.pem  ca.srl  harbor.csr  harbor.key  harbor.pem

[root@harbor install]# cd /data/install/
#把harbor的离线包harbor-offline-installer-v2.3.0-rc3.tgz上传到这个目录,离线包在课件里提供了
下载harbor离线包的地址:
https://github.com/goharbor/harbor/releases/tag/
解压:

[root@harbor install]# tar zxvf harbor-offline-installer-v2.3.0-rc3.tgz
[root@harbor install]# cd harbor
[root@harbor harbor]# cp harbor.yml.tmpl harbor.yml 
[root@harbor harbor]# vim harbor.yml

修改配置文件:

hostname:  harbor  

#修改hostname,跟上面签发的证书域名保持一致
#协议用https

certificate: /data/ssl/harbor.pem
private_key: /data/ssl/harbor.key

邮件和ldap不需要配置,在harbor的web界面可以配置
其他配置采用默认即可
修改之后保存退出
:harbor默认的账号密码:admin/Harbor12345
安装docker-compose
上传课件里的docker-compose-Linux-x86_64文件到harbor机器

[root@harbor harbor]# mv docker-compose-Linux-x86_64.64 /usr/bin/docker-compose
[root@harbor harbor]# chmod +x /usr/bin/docker-compose

注: docker-compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排。Docker-Compose的工程配置文件默认为docker-compose.yml,Docker-Compose运行目录下的必要有一个docker-compose.yml。docker-compose可以管理多个docker实例。

安装harbor需要的离线镜像包docker-harbor-2-3-0.tar.gz在课件,可上传到harbor机器,通过docker load -i解压

[root@harbor install]#  docker load -i docker-harbor-2-3-0.tar.gz 
[root@harbor install]# cd /data/install/harbor
[root@harbor harbor]# ./install.sh

看到下面内容,说明安装成功:
[Step 5]: starting Harbor …
Creating network “harbor_harbor” with the default driver
Creating harbor-log … done
Creating registryctl … done
Creating harbor-db … done
Creating redis … done
Creating registry … done
Creating harbor-portal … done
Creating harbor-core … done
Creating harbor-jobservice … done
Creating nginx … done
✔ ----Harbor has been installed and started successfully.----

在自己电脑修改hosts文件
在这里插入图片描述在hosts文件添加如下一行,然后保存即可
机器ip harbor

扩展
如何停掉harbor:

[root@harbor harbor]# cd /data/install/harbor
[root@harbor harbor]# docker-compose stop 

如何启动harbor:

[root@harbor harbor]# cd /data/install/harbor
[root@harbor harbor]# docker-compose start

如果docker-compose start启动harbor之后,还是访问不了,那就需要重启虚拟机

3.3 Harbor 图像化界面使用说明

在浏览器输入:https://harbor
接收风险并继续,出现如下界面,说明访问正常
在这里插入图片描述
账号:admin
密码:Harbor12345
输入账号密码出现如下:

所有基础镜像都会放在library里面,这是一个公开的镜像仓库

新建项目->起个项目名字test(把访问级别公开那个选中,让项目才可以被公开使用)
在这里插入图片描述
在这里插入图片描述

3.4 测试使用harbor私有镜像仓库

#修改docker配置

[root@node2 ~]# vim /etc/docker/daemon.json

{  "registry-mirrors": ["https://rsbud4vc.mirror.aliyuncs.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub-mirror.c.163.com"],
"insecure-registries": ["192.168.75.141","harbor"]
}

修改配置之后使配置生效:

[root@node2 ~]# systemctl daemon-reload && systemctl restart docker

注意:
配置新增加了一行内容如下:

"insecure-registries": ["192.168.75.141","harbor"]

上面增加的内容表示我们内网访问harbor的时候走的是http,192.168.75.141是安装harbor机器的ip

登录harbor:
[root@node2 ~]# docker login 192.168.75.141

Username:admin
Password: Harbor12345

输入账号密码之后看到如下,说明登录成功了:

Username: admin
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

#导入tomcat镜像,tomcat.tar.gz在课件里

[root@node2 ~]#docker load -i tomcat.tar.gz
#把tomcat镜像打标签

[root@node2 ~]# docker tag tomcat:latest  192.168.75.141/test/tomcat:v1

执行上面命令就会把192.168.75.141/test/tomcat:v1上传到harbor里的test项目下

[root@node2 ~]# docker push 192.168.75.141/test/tomcat:v1

执行上面命令就会把192.168.75.141/test/tomcat:v1上传到harbor里的test项目下
在这里插入图片描述

3.5 从harbor仓库下载镜像

在node2机器上删除镜像

[root@node2 ~]# docker rmi -f 192.168.75.141/test/tomcat:v1

拉取镜像

[root@node2 ~]# docker pull 192.168.75.141/test/tomcat:v1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值