GVKun编程网logo

[转载] Centos7 的安装、Docker1.12.3 的安装,以及 Docker Swarm 集群的简单实例

15

本篇文章给大家谈谈[转载]Centos7的安装、Docker1.12.3的安装,以及DockerSwarm集群的简单实例,同时本文还将给你拓展1.docker学习笔记之入门,docker在centos

本篇文章给大家谈谈[转载] Centos7 的安装、Docker1.12.3 的安装,以及 Docker Swarm 集群的简单实例,同时本文还将给你拓展1.docker 学习笔记之入门,docker 在 centos7 下的安装以及基础命令的使用、CentOS 下 Docker 与.netcore (五)之 三剑客之一 Docker-swarm 集群、centos7 64bit 安装 docker 最新版教程(Centos7 下安装 Docker Engine)、CentOS7 下 Docker 安装、启动等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

[转载] Centos7 的安装、Docker1.12.3 的安装,以及 Docker Swarm 集群的简单实例

[转载] Centos7 的安装、Docker1.12.3 的安装,以及 Docker Swarm 集群的简单实例

 

1、环境准备
​ 本文中的案例会有四台机器,他们的 Host 和 IP 地址如下
c1 -> 10.0.0.31 c2 -> 10.0.0.32 c3 -> 10.0.0.33 c4 -> 10.0.0.34
​ 四台机器的 host 以 c1 为例:
[root@c1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 #::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.0.0.31 c1 10.0.0.32 c2 10.0.0.33 c3 10.0.0.34 c4
1.1、Centos 7 64 位安装
​ 以 c1 安装为示例,安装过程中使用英文版本,然后点击 continue
​ 点击 LOCALIZATION 下面的 Data & Time, 然后选择 Asia/shanghai 时区,点击 Done.
​ 点击 SYSTEM 下面的 INSTALLATION DESTINATION,选择你的硬盘后,在下面的单选框中,选择 I will configure partitioning 点击 Done, 我们来自定义硬盘和分区
​ 点击 Click here to create them automatically,系统会自动帮我们创建出推荐的分区格式。
​ 我们将 /home 的挂载点删除掉,统一加到点 /,文件类型是 xfs,使用全部的硬盘空间,点击 Update Settings,确保后面软件有足够的安装空间。 最后点击左上角的 Done 按钮
xfs 是在 Centos7.0 开始提供的,原来的 ext4 虽然稳定,但最多只能有大概 40 多亿文件,单个文件大小最大只能支持到 16T (4K block size) 的话。而 XFS 使用 64 位管理空间,文件系统规模可以达到 EB 级别。
用于正式生产的服务器,切记必须把数据盘单独分区,防止系统出问题时,保证数据的完整性。比如可以再划分一个,/data 专门用来存放数据。
​ 在弹出的窗口中点击 Accept Changes
​ 点击下图中的位置,设置机器的 Host Name,这里我们安装机器的 Host Name 为 c1
​ 最后点击右下角的 Begin Installation,过程中可以设置 root 的密码,也可以创建其他用户
1.2、网络配置
​ 以下以 c1 为例
[root@c1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet BOOTPROTO=static #启用静态 IP 地址 DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no NAME=eth0 UUID=e57c6a58-1951-4cfa-b3d1-cf25c4cdebdd DEVICE=eth0 ONBOOT=yes #开启自动启用网络连接 IPADDR0=192.168.0.31 #设置 IP 地址 PREFIXO0=24 #设置子网掩码 GATEWAY0=192.168.0.1 #设置网关 DNS1=192.168.0.1 #设置 DNS DNS2=8.8.8.8
​ 重启网络:
[root@c1 ~]# service network restart
​ 更改源为阿里云
[root@c1 ~]# yum install -y wget [root@c1 ~]# cd /etc/yum.repos.d/ [root@c1 yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak [root@c1 yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo [root@c1 yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo [root@c1 yum.repos.d]# yum clean all [root@c1 yum.repos.d]# yum makecache
​ 安装网络工具包和基础工具包
[root@c1 ~]# yum install net-tools checkpolicy gcc dkms foomatic openssh-server bash-completion -y
1.3、更改 hostname
​ 在四台机器上依次设置 hostname,以下以 c1 为例
[root@localhost ~]# hostnamectl --static set-hostname c1 [root@localhost ~]# hostnamectl status Static hostname: c1 Icon name: computer-vm Chassis: vm Machine ID: e4ac9d1a9e9b4af1bb67264b83da59e4 Boot ID: a128517ed6cb41d083da61de5951a109 Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-327.36.3.el7.x86_64 Architecture: x86-64
1.4、配置 ssh 免密码登录登录
​ 先后在四台机器分别执行,以 c1 为例
[root@c1 ~]# ssh-keygen #一路按回车到最后
​ 在免登录端修改配置文件
[root@c1 ~]# vi /etc/ssh/sshd_config
#找到以下内容,并去掉注释符#   RSAAuthentication yes   PubkeyAuthentication yes   AuthorizedKeysFile .ssh/authorized_keys
​ 将 ssh-keygen 生成的密钥,分别复制到其他三台机器,以下以 c1 为例
[root@c1 ~]# ssh-copy-id c1 The authenticity of host ''c1 (10.0.0.31)'' can''t be established. ECDSA key fingerprint is 22:84:fe:22:c2:e1:81:a6:77:d2:dc:be:7b:b7:bf:b8. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@c1''s password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh ''c1''" and check to make sure that only the key(s) you wanted were added. [root@c1 ~]# ssh-copy-id c2 The authenticity of host ''c2 (10.0.0.32)'' can''t be established. ECDSA key fingerprint is 22:84:fe:22:c2:e1:81:a6:77:d2:dc:be:7b:b7:bf:b8. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@c2''s password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh ''c2''" and check to make sure that only the key(s) you wanted were added. [root@c1 ~]# ssh-copy-id c3 The authenticity of host ''c3 (10.0.0.33)'' can''t be established. ECDSA key fingerprint is 22:84:fe:22:c2:e1:81:a6:77:d2:dc:be:7b:b7:bf:b8. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@c3''s password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh ''c3''" and check to make sure that only the key(s) you wanted were added. [root@c1 ~]# ssh-copy-id c4 The authenticity of host ''c4 (10.0.0.34)'' can''t be established. ECDSA key fingerprint is 22:84:fe:22:c2:e1:81:a6:77:d2:dc:be:7b:b7:bf:b8. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@c4''s password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh ''c4''" and check to make sure that only the key(s) you wanted were added.
​ 测试密钥是否配置成功
[root@c1 ~]# for N in $(seq 1 4); do ssh c$N hostname; done; c1 c2 c3 c4
​ 安装 ntp 时间同步工具和 git
[root@c1 ~]# for N in $(seq 1 4); do ssh c$N yum install ntp git -y; done;
2、安装 Docker1.12.3 和初步配置
​ 可以直接在 github 上获取 Docker 各个版本包: https://github.com/docker/docker/releases
​ 链接中提供了所有的 Docker 核心包: http://yum.dockerproject.org/repo/main/centos/7/Packages/
2.1、安装 Docker1.12.3
​ 不建议直接使用 Docker 官方的 docker yum 源进行安装,因为会依据系统版本去选择 Docker 版本,不能指定相应的版本进行选择安装。在四台机器上依次执行下面的命令,可以将下面的命令,直接复制粘贴到命令行中
mkdir -p ~/_src \ && cd ~/_src \ && wget http://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-selinux-1.12.3-1.el7.centos.noarch.rpm \ && wget http://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-1.12.3-1.el7.centos.x86_64.rpm \ && wget http://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-debuginfo-1.12.3-1.el7.centos.x86_64.rpm \ && yum localinstall -y docker-engine-selinux-1.12.3-1.el7.centos.noarch.rpm docker-engine-1.12.3-1.el7.centos.x86_64.rpm docker-engine-debuginfo-1.12.3-1.el7.centos.x86_64.rpm
2.2、 验证 Docker 是否安装成功
​ Centos7 中 Docker1.12 中默认使用 Docker 作为客户端程序,使用 dockerd 作为服务端程序。
[root@c1 _src]# docker version Client: Version: 1.12.3 API version: 1.24 Go version: go1.6.3 Git commit: 6b644ec Built: OS/Arch: linux/amd64 Cannot connect to the Docker daemon. Is the docker daemon running on this host?
2.3、启动 Docker daemon 程序
​ 在 Docker1.12 中,默认的 daemon 程序是 dockerd,可以执行 dockerd 或者使用系统自带 systemd 去管理服务。但是需要注意的是,默认用的都是默认的参数,比如私有网段默认使用 172.17.0.0/16 ,网桥使用 docker0 等等
[root@c1 _src]# dockerd INFO[0000] libcontainerd: new containerd process, pid: 6469 WARN[0000] containerd: low RLIMIT_NOFILE changing to max current=1024 max=4096 WARN[0001] devmapper: Usage of loopback devices is strongly discouraged for production use. Please use `--storage-opt dm.thinpooldev` or use `man docker` to refer to dm.thinpooldev section. WARN[0001] devmapper: Base device already exists and has filesystem xfs on it. User specified filesystem will be ignored. INFO[0001] [graphdriver] using prior storage driver "devicemapper" INFO[0001] Graph migration to content-addressability took 0.00 seconds WARN[0001] mountpoint for pids not found INFO[0001] Loading containers: start. INFO[0001] Firewalld running: true INFO[0001] Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address INFO[0001] Loading containers: done. INFO[0001] Daemon has completed initialization INFO[0001] Docker daemon commit=6b644ec graphdriver=devicemapper version=1.12.3 INFO[0001] API listen on /var/run/docker.sock
2.3、通过系统自带的 systemctl 启动 docker,并启动 docker 服务
[root@c1 _src]# systemctl enable docker && systemctl start docker Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
使用 dockerd --help 查看启动参数
[root@c1 _src]# dockerd --help Usage: dockerd [OPTIONS] A self-sufficient runtime for containers. Options: --add-runtime=[] Register an additional OCI compatible runtime --api-cors-header Set CORS headers in the remote API --authorization-plugin=[] Authorization plugins to load -b, --bridge #指定容器使用的网络接口,默认为 docker0,也可以指定其他网络接口 --bip #指定桥接地址,即定义一个容器的私有网络 --cgroup-parent #为所有的容器指定父 cgroup --cluster-advertise #为集群设定一个地址或者名字 --cluster-store #后端分布式存储的 URL --cluster-store-opt=map [] #设置集群存储参数 --config-file=/etc/docker/daemon.json #指定配置文件 -D #启动 debug 模式 --default-gateway #为容器设定默认的 ipv4 网关 (--default-gateway-v6) --dns=[] #设置 dns --dns-opt=[] #设置 dns 参数 --dns-search=[] #设置 dns 域 --exec-opt=[] #运行时附加参数 --exec-root=/var/run/docker #设置运行状态文件存储目录 --fixed-cidr #为 ipv4 子网绑定 ip -G, --group=docker #设置 docker 运行时的属组 -g, --graph=/var/lib/docker #设置 docker 运行时的家目录 -H, --host=[] #设置 docker 程序启动后套接字连接地址 --icc=true #是内部容器可以互相通信,环境中需要禁止内部容器访问 --insecure-registry=[] #设置内部私有注册中心地址 --ip=0.0.0.0 #当映射容器端口的时候默认的 ip (这个应该是在多主机网络的时候会比较有用) --ip-forward=true #使 net.ipv4.ip_forward 生效,其实就是内核里面 forward --ip-masq=true #启用 ip 伪装技术 (容器访问外部程序默认不会暴露自己的 ip) --iptables=true #启用容器使用 iptables 规则 -l, --log-level=info #设置日志级别 --live-restore #启用热启动 (重启 docker,保证容器一直运行 1.12 新特性) --log-driver=json-file #容器日志默认的驱动 --max-concurrent-downloads=3 #为每个 pull 设置最大并发下载 --max-concurrent-uploads=5 #为每个 push 设置最大并发上传 --mtu #设置容器网络的 MTU --oom-score-adjust=-500 #设置内存 oom 的平分策略 (-1000/1000) -p, --pidfile=/var/run/docker.pid #指定 pid 所在位置 -s, --storage-driver #设置 docker 存储驱动 --selinux-enabled #启用 selinux 的支持 --storage-opt=[] #存储参数驱动 --swarm-default-advertise-addr #设置 swarm 默认的 node 节点 --tls #使用 tls 加密 --tlscacert=~/.docker/ca.pem #配置 tls CA 认证 --tlscert=~/.docker/cert.pem #指定认证文件 --tlskey=~/.docker/key.pem #指定认证 keys --userland-proxy=true #为回环接口使用用户代理 --userns-remap #为用户态的 namespaces 设定用户或组
2.4、修改 docker 的配置文件
​ 以下以 c1 为例,在 ExecStart 后面加上我们自定义的参数,其中三台机器也要做同步修改
[root@c1 ~]# vi /lib/systemd/system/docker.service
[Unit] Description=Docker Application Container Engine Documentation=https://docs.docker.com After=network.target [Service] Type=notify # the default is not to use systemd for cgroups because the delegate issues still # exists and systemd currently does not support the cgroup feature set required # for containers run by docker # Overlayfs 跟 AUFS 很像,但是性能比 AUFS 好,有更好的内存利用。 # 加上阿里云的 docker 加速 ExecStart=/usr/bin/dockerd -s=overlay --registry-mirror=https://7rgqloza.mirror.aliyuncs.com --insecure-registry=localhost:5000 -H unix:///var/run/docker.sock --pidfile=/var/run/docker.pid ExecReload=/bin/kill -s HUP $MAINPID # Having non-zero Limit*s causes performance problems due to accounting overhead # in the kernel. We recommend using cgroups to do container-local accounting. LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity # Uncomment TasksMax if your systemd version supports it. # Only systemd 226 and above support this version. #TasksMax=infinity TimeoutStartSec=0 # set delegate yes so that systemd does not reset the cgroups of docker containers Delegate=yes # kill only the docker process, not all processes in the cgroup KillMode=process [Install] WantedBy=multi-user.target
重启 docker 服务,以保证新的配置生效
[root@c1 ~]# systemctl daemon-reload && systemctl restart docker.service
3、创建 swarm 集群
10.0.0.31 (hostname:c1) 作为 swarm manager1
10.0.0.32 (hostname:c2) 做为 swarm manager2
10.0.0.33 (hostname:c3) 做为 swarm agent1
10.0.0.34 (hostname:c4) 做为 swarm agent2
3.1、开放 firewall 防火墙端口
​ 在配置集群前要先开放防火墙的端口,将下面的命令,复制、粘贴到 4 台机器的命令行中执行。
firewall-cmd --zone=public --add-port=2377/tcp --permanent && \ firewall-cmd --zone=public --add-port=7946/tcp --permanent && \ firewall-cmd --zone=public --add-port=7946/udp --permanent && \ firewall-cmd --zone=public --add-port=4789/tcp --permanent && \ firewall-cmd --zone=public --add-port=4789/udp --permanent && \ firewall-cmd --reload
​ 以 c1 为例,查看端口开放情况
[root@c1 ~]# firewall-cmd --list-ports 4789/tcp 4789/udp 7946/tcp 2377/tcp 7946/udp
3.2、设置 swarm 集群并将其他 3 台机器添加到集群
​ 在 c1 上初始化 swarm 集群,用 --listen-addr 指定监听的 ip 与端口
[root@c1 ~]# docker swarm init --listen-addr 0.0.0.0 Swarm initialized: current node (73ju72f6nlyl9kiib7z5r0bsk) is now a manager. To add a worker to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-47dxwelbdopq8915rjfr0hxe6t9cebsm0q30miro4u4qcwbh1c-4f1xl8ici0o32qfyru9y6wepv \ 10.0.0.31:2377 To add a manager to this swarm, run ''docker swarm join-token manager'' and follow the instructions.
使用 docker swarm join-token manager 可以查看加入为 swarm manager 的 token
​ 查看结果,可以看到我们现在只有一个节点
[root@c1 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 73ju72f6nlyl9kiib7z5r0bsk * c1 Ready Active Leader
​ 通过以下命令,我们将另外 3 台机器,加入到集群中,将下面的命令,复制、粘贴到 c1 的命令行中
for N in $(seq 2 4); \ do ssh c$N \ docker swarm join \ --token SWMTKN-1-47dxwelbdopq8915rjfr0hxe6t9cebsm0q30miro4u4qcwbh1c-4f1xl8ici0o32qfyru9y6wepv \ 10.0.0.31:2377 \ ;done
​ 再次查看集群节点情况,可以看到其他机器已经添加到集群中,并且 c1 是 leader 状态
[root@c1 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 2qn7aw9ihbjphtnm1toaoevq8 c4 Ready Active 4cxm0w5j3x4mqrj8f1kdrgln5 * c1 Ready Active Leader 4wqpz2v3b71q0ohzdifi94ma9 c2 Ready Active 9t9ceme3w14o4gfnljtfrkpgp c3 Ready Active
​ 将 c2 也设置为集群的主节点,先在 c1 上查看加入到主节点的 token
[root@c1 ~]# docker swarm join-token manager To add a manager to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-47dxwelbdopq8915rjfr0hxe6t9cebsm0q30miro4u4qcwbh1c-b7k3agnzez1bjj3nfz2h93xh0 \ 10.0.0.31:2377
​ 根据 c1 的 token 信息,我们先在 c2 上脱离集群,再将 c2 加入到管理者
[root@c2 ~]# docker swarm leave Node left the swarm. [root@c2 ~]# docker swarm join \ > --token SWMTKN-1-47dxwelbdopq8915rjfr0hxe6t9cebsm0q30miro4u4qcwbh1c-b7k3agnzez1bjj3nfz2h93xh0 \ > 10.0.0.31:2377 This node joined a swarm as a manager.
​ 这时我们在 c1 和 c2 任意一台机器,输入 docker node ls 都能够看到最新的集群节点状态,这时 c2 的 MANAGER STATUS 已经变为了 Reachable
[root@c1 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 2qn7aw9ihbjphtnm1toaoevq8 c4 Ready Active 4cxm0w5j3x4mqrj8f1kdrgln5 * c1 Ready Active Leader 4wqpz2v3b71q0ohzdifi94ma9 c2 Down Active 9t9ceme3w14o4gfnljtfrkpgp c3 Ready Active ai6peof1e9wyovp8uxn5b2ufe c2 Ready Active Reachable
因为之前我们是使用 docker swarm leave,所以早期的 c2 的状态是 Down,可以通过 docker node rm <ID> 命令删除掉
3.3、创建一个 overlay 网络
​ 单台服务器的时候我们应用所有的容器都跑在一台主机上,所以容器之间的网络是能够互通的。现在我们的集群有 4 台主机,如何保证不同主机之前的 docker 是互通的呢?
swarm 集群已经帮我们解决了这个问题了,就是只用 overlay network.
​ 在 docker 1.12 以前,swarm 集群需要一个额外的 key-value 存储 (consul, etcd etc). 来同步网络配置,保证所有容器在同一个网段中。在 docker 1.12 已经内置了这个存储,集成了 overlay networks 的支持.
​ 查看原有网络
[root@c1 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 23ee2bb5a2a1 bridge bridge local fd17ed8db4d8 docker_gwbridge bridge local 6878c36aa311 host host local 08tt2s4pqf96 ingress overlay swarm 7c18e57e24f2 none null local
可以看到在 swarm 上默认已有一个名为 ingress 的 overlay 网络,默认在 swarm 里使用,本文会创建一个新的
​ 创建一个名为 idoall-org 的 overlay 网络
[root@c1 ~]# docker network create --subnet=10.0.9.0/24 --driver overlay idoall-org e63ca0d7zcbxqpp4svlv5x04v [root@c1 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 5e47ba02a985 bridge bridge local fd17ed8db4d8 docker_gwbridge bridge local 6878c36aa311 host host local e63ca0d7zcbx idoall-org overlay swarm 08tt2s4pqf96 ingress overlay swarm 7c18e57e24f2 none null local
新的网络 (idoall-org) 已创建
--subnet 用于指定创建 overlay 网络的网段,也可以省略此参数
​ 可以使用 docker network inspect idoall-org 查看我们添加的网络信息
[root@c1 ~]# docker network inspect idoall-org [ { "Name": "idoall-org", "Id": "e63ca0d7zcbxqpp4svlv5x04v", "Scope": "swarm", "Driver": "overlay", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "10.0.9.0/24", "Gateway": "10.0.9.1" } ] }, "Internal": false, "Containers": null, "Options": { "com.docker.network.driver.overlay.vxlanid_list": "257" }, "Labels": null } ]
3.4、在网络上运行容器
alpine 镜像在 idoall-org 网络上启动 3 个实例
[root@c1 ~]# docker service create --name idoall-org-test-ping --replicas 3 --network=idoall-org alpine ping baidu.com avcrdsntx8b8ei091lq5cl76y [root@c1 ~]# docker service ps idoall-org-test-ping ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR 42vigh5lxkvgge9zo27hfah88 idoall-org-test-ping.1 alpine c4 Running Starting 1 seconds ago aovr8r7r7lykzmxqs30e8s4ee idoall-org-test-ping.2 alpine c3 Running Starting 1 seconds ago c7pv2o597qycsqzqzgjwwtw8b idoall-org-test-ping.3 alpine c1 Running Running 3 seconds ago
可以看到 3 个实例,分别部署在 c1、c3、c4 三台机器上
也可以使用 --mode golbal 指定 service 运行在每个 swarm 节点上,稍后会有介绍
3.5、扩展 (Scaling) 应用
​ 假设在程序运行的时候,发现资源不够用,我们可以使用 scale 进行扩展,现在有 3 个实例,我们更改为 4 个实例
[root@c1 ~]# docker service scale idoall-org-test-ping=4 idoall-org-test-ping scaled to 4 [root@c1 ~]# docker service ps idoall-org-test-ping ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR 42vigh5lxkvgge9zo27hfah88 idoall-org-test-ping.1 alpine c4 Running Running 4 minutes ago aovr8r7r7lykzmxqs30e8s4ee idoall-org-test-ping.2 alpine c3 Running Running 4 minutes ago c7pv2o597qycsqzqzgjwwtw8b idoall-org-test-ping.3 alpine c1 Running Running 4 minutes ago 72of5dfm67duccxsdyt1e25qd idoall-org-test-ping.4 alpine c2 Running Running 1 seconds ago
3.6、对 service 服务进行指定运行
​ 在上面的案例中,不管你的实例是几个,是由 swarm 自动调度定义执行在某个节点上。我们可以通过在创建 service 的时候可以使用 --constraints 参数,来对 service 进行限制,例如我们指定一个服务在 c4 上运行:
[root@c1 ~]# docker service create \ --network idoall-org \ --name idoall-org \ --constraint ''node.hostname==c4'' \ -p 9000:9000 \ idoall/golang-revel
服务启动以后,通过浏览 http://10.0.0.31:9000/,或者 31-34 的任意 IP, 都可以看到效果,Docker Swarm 会自动做负载均衡,稍后会介绍关于 Docker Swarm 的负载均衡
​ 由于各地的网络不同,下载镜像可能有些慢,可以使用下面的命令,对命名为 idoall-org 的镜像进行监控
[root@c1 ~]# watch docker service ps idoall-org
​ 除了 hostname 也可以使用其他节点属性来创建约束表达式写法参见下表:
节点属性
匹配
示例
node.id
节点 ID
node.id == 2ivku8v2gvtg4
node.hostname
节点 hostname
node.hostname != c2
node.role
节点 role: manager
node.role == manager
node.labels
用户自定义 node labels
node.labels.security == high
engine.labels
Docker Engine labels
engine.labels.operatingsystem == ubuntu 14.04
​ 我们也可以通过 docker node update 命令,来为机器添加 label,例如:
[root@c1 ~]# docker node update --label-add site=idoall-org c1 [root@c2 ~]# docker node inspect c1 [ { "ID": "4cxm0w5j3x4mqrj8f1kdrgln5", "Version": { "Index": 108 }, "CreatedAt": "2016-12-11T11:13:32.495274292Z", "UpdatedAt": "2016-12-11T12:00:05.956367412Z", "Spec": { "Labels": { "site": "idoall-org" ... ]
​ 对于已有 service, 可以通过 docker service update, 添加 constraint 配置,例如:
[root@c1 ~]# docker service update registry --constraint-add ''node.labels.site==idoall-org''
3.7、测试 docker swarm 网络是否能互通
​ 在 c1 上执行
[root@c1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c683692b0153 alpine:latest "ping baidu.com" 25 minutes ago Up 25 minutes idoall-org-test-ping.4.c7pv2o597qycsqzqzgjwwtw8b [root@c1 ~]# docker exec -it 47e5 sh / # ping idoall-org.1.9ne6hxjhvneuhsrhllykrg7zm PING idoall-org.1.9ne6hxjhvneuhsrhllykrg7zm (10.0.9.8): 56 data bytes 64 bytes from 10.0.9.8: seq=0 ttl=64 time=1.080 ms 64 bytes from 10.0.9.8: seq=1 ttl=64 time=1.349 ms 64 bytes from 10.0.9.8: seq=2 ttl=64 time=1.026 ms
idoall-org.1.9ne6hxjhvneuhsrhllykrg7zm 是容器在 c4 上运行的名称
在使用 exec 进入容器的时候,可以只输入容器 id 的前 4 位
​ 在 c4 上执行
[root@c4 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1ead9bb757a0 idoall/docker-golang1.7.4-revel0.13:latest "/usr/bin/supervisord" About a minute ago Up 58 seconds idoall-org.1.9ne6hxjhvneuhsrhllykrg7zm 033531b30b79 alpine:latest "ping baidu.com" About a minute ago Up About a minute idoall-org-test-ping.1.6st5xvehh7c3bwaxsen3r4gpn [root@c2 ~]# docker exec -it f49c435c94ea sh bash-4.3# ping idoall-org-test-ping.4.cirnop0kxbuxiyjh87ii6hh4x PING idoall-org-test-ping.4.cirnop0kxbuxiyjh87ii6hh4x (10.0.9.6): 56 data bytes 64 bytes from 10.0.9.6: seq=0 ttl=64 time=0.531 ms 64 bytes from 10.0.9.6: seq=1 ttl=64 time=0.700 ms 64 bytes from 10.0.9.6: seq=2 ttl=64 time=0.756 ms
3.8、测试 dokcer swarm 自带的负载均衡
​ 使用 --mode global 参数,在每个节点上创建一个 web 服务
[root@c1 ~]# docker service create --name whoami --mode global -p 8000:8000 jwilder/whoami 1u87lrzlktgskt4g6ae30xzb8 [root@c1 ~]# docker service ps whoami ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR cjf5w0pv5bbrph2gcvj508rvj whoami jwilder/whoami c2 Running Running 16 minutes ago dokh8j4z0iuslye0qa662axqv \_ whoami jwilder/whoami c3 Running Running 16 minutes ago dumjwz4oqc5xobvjv9rosom0w \_ whoami jwilder/whoami c1 Running Running 16 minutes ago bbzgdau14p5b4puvojf06gn5s \_ whoami jwilder/whoami c4 Running Running 16 minutes ago
​ 在任意一台机器上执行以下命令,可以发现,每次获取到的都是不同的值,超过 4 次以后,会继续轮询到第 1 台机器
[root@c1 ~]# curl $(hostname --all-ip-addresses | awk ''{print $1}''):8000 I''m 8c2eeb5d420f [root@c1 ~]# curl $(hostname --all-ip-addresses | awk ''{print $1}''):8000 I''m 0b56c2a5b2a4 [root@c1 ~]# curl $(hostname --all-ip-addresses | awk ''{print $1}''):8000 I''m 000982389fa0 [root@c1 ~]# curl $(hostname --all-ip-addresses | awk ''{print $1}''):8000 I''m db8d3e839de5 [root@c1 ~]# curl $(hostname --all-ip-addresses | awk ''{print $1}''):8000 I''m 8c2eeb5d420f
 
 
博文作者:迦壹
博客地址:Centos7 的安装、Docker1.12.3 的安装,以及 Docker Swarm 集群的简单实例
转载声明:可以转载,但必须以超链接形式标明文章原始出处和作者信息及版权声明,谢谢合作!

 

1.docker 学习笔记之入门,docker 在 centos7 下的安装以及基础命令的使用

1.docker 学习笔记之入门,docker 在 centos7 下的安装以及基础命令的使用

1、更新update到最新的版本  
    yum  update
2、卸载老版本docker
    yum  remove docker  docker-common docker-selinux  docker-engine
3、安装需要的软件包
    yum install -y yum-utils  device-mapper-persistent-data lvm2
4、设置yum源
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
5、查看docker版本
    yum list docker-ce --showduplicates|sort -r  

6、安装docker
    yum  install  docker-ce-18.03.1.ce -y  #yum  install docker-ce-3:20.10.9-3.el8
7、启动docker
    systemctl start docker
8、加入开机自启
    systemctl enable docker

9、配置国内镜像

    vi /etc/docker/daemon.json 
    {
        "registry-mirrors": ["http://hub-mirror.c.163.com"]
    }
Docker 包括三个基本概念:
镜像(Image)
容器(Container)
仓库(Repository)

如若以上操作 中出现错误

则强制安装此版本

yum install https://download.docker.com/linux/centos/8/x86_64/stable/Packages/containerd.io-1.4.3-3.1.el8.x86_64.rpm

1.启动
 docker start 容器名
2.删除
   docker rm 容器名
3.停止所容器
  docker stop  $(docker ps -a -q)
  docker rm  $(docker ps -a -q) 

4.docker build -t 名称 .
  Usage:docker build [OPTIONS] PATH | URL | -

  OPTIONS:
  -t ,--tag list  #构建后的镜像名称
  -f, --file string #指定Dockerfiile文件位置
    示例:
     1,docker build . 
     2,docker build -t redis:v1 .
     3,docker build -t redis:v2 -f /path/Dockerfile /path

Dockerfile 快速入门指令

  1.FROM 指定基础镜像

    所谓定制镜像,那一定是以一个镜像为基础,在其上进行定制。就像我们之前运行了一个redis镜像的容器,再进行修改一样,基础镜像是必须指定的。而 FROM 就是指定基础镜像,因此一个  Dockerfile  中  FROM  是必备的指令,并且必须是第一条指令。
     Docker Store 上有非常多的高质量的官方镜像,有可以直接拿来使用的服务类的镜像,如nginx  、 redis  、 mongo  、 mysql  、 httpd  、 php  、 tomcat  等;如果没有找到对应服务的镜像,官方镜像中还提供了一些更为基础的操作系统镜像,
    如ubuntu  、 debian  、 centos、 alpine 等这些操作系统的软件库

  2、RUN 执行命令

    RUN  指令是用来执行命令行命令的。
    格式:
      RUN \<command\> (类似/bin/sh -cshell格式)

      RUN ["executable", "param1", "param2"] (exec格式)

    类似shell脚本风格的:
        FROM alpine
        RUN sed -i ''s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g'' /etc/apk/repositories
        RUN apk add  gcc g++ libc-dev  wget vim  openssl-dev make  linux-headers 
        RUN mkdir -p /usr/src/redis
        RUN cd /usr/src/redis
        RUN wget -O redis-4.0.11.tar.gz  "http://download.redis.io/releases/redis-4.0.11.tar.gz"
        RUN tar -xzf redis-4.0.11.tar.gz -C /usr/src/redis
        RUN cd /usr/src/redis/redis-4.0.11 &&  make && make PREFIX=/usr/local/redis install
    之前说过,Dockerfile 中每一个指令都会建立一层, RUN  也不例外。每一个  RUN  的行为,就和刚才我们手工建立镜像的过程一样:新建立一层,在其上执行这些命令,执行结束后, commit这一层的修改,构成新的镜像。使用 Dockerfile 定制镜像
    而上面的这种写法,创建了很多层镜像。这是完全没有意义的,而且很多运行时不需要的东西,都被装进了镜像里,比如编译环境、更新的软件包等等。结果就是产生非常臃肿、非常多层的镜像,不仅仅增加了构建部署的时间,也很容易出错。 这是很多初学
    Docker 的人常犯的一个错误。Union FS是有最大层数限制的,比如 AUFS,曾经是最大不得超过 42 层,现在是不得超过 127 层。

 

导出容器

 导出容器可以导出一个已经创建的容器到一个文件,不管容器处于什么状态,可以使用docker export 命令。 命令格式为:docker export [-o|--output[=""]] CONTATINER 其中可以通过-o来指定导出的tar文件名,也可以直接通过重定向来实现。

另一种格式为:docker export 容器 id/name > 文件名.tar

导入容器

  导入的文件可以使用docker import 命令导入变成镜像,该命令的格式为:

   docker import [-c|--change[=[]]] [-m|--message[=MESSAGE]] file|URL|-[REPOSITORY[:YAG]]

   其中-c 选项可以在导入的同时执行对容器进行修改的Dockerfile指令(网络等修改)

另外 docker save&load 命令也可以导入一个镜像存储文件,但是跟 docker import 命令是有区别的

 

用 export&import 还是 save & load ?

export&import 和 save & load 是 docker 中的两组命令,我们先主要看看他们的区别:

类型 导出的对象 导出文件大小 是否可回滚到历史层
export & import 将容器导出
save & load 用来将一个或者多个 image 打包
  相对于文件大小来说,save方式导出的文件比export方式导出的文件大
  正是因为save方式保存了镜像的历史和层(layer),使其可以层回滚,即回滚到之前的历史层,所以save方式导出的文件稍微大一些
  反观export方式,在导出过程中丢失所有的历史,导致其不可以层回滚,导出的文件会小一些
  可以通过 docker history 镜像名 看到层关系

3.2、容器网络

Docker 安装后,默认会创建下面三种网络类型

docker network ls 查看默认的网络

在启动容器时使用 --network bridge 指定网络类型

bridge:桥接网络

默认情况下启动的 Docker 容器,都是使用 bridge,Docker 安装时创建的桥接网络,每次 Docker 容器重启时,会按照顺序获取对应的 IP 地址,这个就导致重启下,Docker 的 IP 地址就变了

none:无指定网络 使用 --network=none ,docker 容器就不会分配局域网的 IP

host: 主机网络 使用 --network=host,此时,Docker 容器的网络会附属在主机上,两者是互通的。 例如,在容器中运行一个 Web 服务,监听 8080 端口,则主机的 8080 端口就会自动映射到容器中

3.3、 指定自定义网络

因为默认的网络不能制定固定的地址,所以我们将创建自定义网络,并指定网段(IP 可以理解为 4 组 8 个二进制组成的一段代码,然后右侧网段的 24 是指锁定前面 24 位,也就是说生成的 IP 会是 192.168.1.X,1-255):192.168.1.0/24 并命名为 mynetwork,指令如下:
docker network create --subnet=192.168.1.0/24 mynetwork

使用此 docker 镜像 创建容器

代码如下:

docker run -itd --name redis-master --net mynetwork -p 6380:6379 --ip 192.168.1.2 redis 

参数说明: 具体可以参看一些文档
http://www.runoob.com/docker/docker-run-command.html 

http://www.dockerinfo.net/docker%E5%AE%B9%E5%99%A8-2

-d: 后台运行容器,并返回容器ID; -i: 以交互模式运行容器,通常与 -t 同时使用; -p: 端口映射,格式为:主机(宿主)端口:容器端口 -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用; -V: 为容器挂载目录,比如 /usr/docker/data:/data 前者为数宿主机目录后者为容器内目录 --ip: 为容器制定一个固定的ip --net: 指定网络模式 这样,我们创建了2个容器,来一起查看下结果 docker ps -a 查看容器ip地址 docker network inspect mynetwork

 

docker build -t redis-test . #在文件夹中用默认的 Dockerfile 创建镜像

docker images #查看当前 docker 镜像

docker history 625dcdca8ecb #查看某个镜像的历史

docker ps -a #查看当前运行的容器

docker run -itd -p 8001:80 -v xxx1:xxx2 --name redis-master redis-test #使用镜像(redis-test)运行容器(redis-master) 宿主机 8001 端口映射到容器 80 端口 宿主机 xxx1 地址与容器 xxx2 地址共享(容器写入 xxx2 的数据同样会出现在宿主机的 xxx1 里)

docker rm 538265c49277 #移除容器

docker exec -it redis-master bash #进入容器 (如果出现:OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: \"bash\": executable file not found in $PATH": unknown ,那么就把 bash 改为 sh :docker exec -it redis-master sh)

docker stop $(docker ps -a -q) #停止全部容器(-q 返回容器 id)

docker rm $(docker ps -a -q) #移除所有容器

docker rmi $(docker images -q) #删除所有镜像

docker export f4092a93c312 > nginx-jzy.tar #docker export 容器 id > 文件名.tar   导出容器(镜像快照)

docker import nginx-jzy.tar nginx-jzy:v2 #使用镜像快照创建镜像,创建的镜像没有 history

docker run -itd --name nginx-jzy-v2 e70d288bc4ef bash #使用上面创建的镜像创建容器,这里必须要有 bash 或者其他可以被挂载的进程

docker save a68a3617e7dd > nginx-jzy-save.tar #保存镜像

docker rmi a68a3617e7dd #删除镜像,因为上面导出容器使用的是这个镜像,所以生成了一个依赖这个镜像的镜像 删除失败了(Error response from daemon: conflict: unable to delete a68a3617e7dd (cannot be forced) - image has dependent child images)

docker image inspect --format=''{{.RepoTags}} {{.Id}} {{.Parent}}'' $(docker image ls -q --filter since=a68a3617e7dd) #找出 from 这个镜像的子镜像([nginx-jzy:v2] sha256:e70d288bc4ef3f20015ca839653c1c1555ee7b2524e4085845348c7d990eb998)

docker rmi -f e70d288bc4ef #删除子镜像失败(Error response from daemon: conflict: unable to delete e70d288bc4ef (cannot be forced) - image is being used by running container be7eea6f8a9b)

docker stop be7eea6f8a9b #停止容器

docker rm be7eea6f8a9b #删除容器

docker rmi -f e70d288bc4ef #删除子镜像成功

docker rmi a68a3617e7dd #删除镜像成功

docker load < nginx-jzy-save.tar #读取保存的镜像文件(生成的镜像文件是有 history 的)

docker inspect imageid #查看镜像详情

 docker network ls #查看网络列表

docker network inspect id1 #查看网络 id 为 id1 的信息

docker network create --subnet=192.168.1.0/24 redis-network #新增网络连接方式(增加网段)

docker run -itd --name redis-test2 --network=redis-network --ip 192.168.1.3 -p 6381:6379 -v /usr/docker/redis/conf/:/usr/src/redis/conf/redis #根据之前的 redis 镜像生成 redis-test2 容器 使用自定义的 redis-network 网络,指定 ip(必须在网段内),指定端口映射,指定共享文件夹

iptables -t nat -L -n #查看桥接网络

1. 为什么系统镜像那么小呢? 因为 docker 镜像在运行的时候直接使用 docker 宿主机器的 kernel,Linux 操作系统由内核空间和用户空间组成。

内核空间是 kernel,用户空间是 rootfs, 不同 Linux 发行版的区别主要是 rootfs. 比如 Ubuntu 14.04 使用 upstart 管理服务,apt 管理软件包; 而 CentOS 7 使用 systemd 和 yum。这些都是用户空间上的区别,Linux kernel 差别不大。所以 Docker 可以同时支持多种 Linux 镜像, 模拟出多种操作系统环境。容器共同使用镜像的内容(如果发生更改先复制镜像层到容器层然后再修改)

2. 镜像的文件如何存储的?

镜像文件是分层存储的,我们可以通过 docker history 命令查看到镜像的历史,也就是层

启动镜像的时候,一个新的可写层会加载到镜像的顶部。这一层通常称为 “容器层”, 之下是 “镜像层”。
容器层可以读写,容器所有发生文件变更写都发生在这一层。镜像层 read-only, 只允许读取。

Container 最上面是一个可写的容器层,以及若干只读的镜像层组成,Container 的数据就存放在这些层中,这样的分层结构最大的特性是 Copy-On-Write (写时复制):

    新数据会直接存放在最上面的 Container 层。

    修改现有的数据会先从 Image 层将数据复制到容器层,修改后的数据直接保存在 Container 层,Image 层保持不变。

3. 启动的容器会占用大量空间吗?

    创建镜像的时候,分层可以让 docker 只保存我们添加和修改的部分内容。其他内容基于 base 镜像,不需要存储,读取 base 镜像即可。如此,当我们创建多个镜像的时候,所有的镜像共享 base 部分。节省了磁盘空间

    docker 通过一个叫做 copy-on-write (CoW) 的策略来保证 base 镜像的安全性,以及更高的性能和空间利用率

    简单的说,启动容器的时候,最上层容器层是可写层,之下的都是镜像层,只读层。

    当容器需要读取文件的时候:从最上层镜像开始查找,往下找,找到文件后读取并放入内存,若已经在内存中了,直接使用。(即,同一台机器上运行的 docker 容器共享运行时相同的文件)。

    当容器需要添加文件的时候:直接在最上面的容器层可写层添加文件,不会影响镜像层。

    当容器需要修改文件的时候:从上往下层寻找文件,找到后,复制到容器可写层,然后,对容器来说,可以看到的是容器层的这个文件,看不到镜像层里的文件。容器在容器层修改这个文件。

    当容器需要删除文件的时候:从上往下层寻找文件,找到后在容器中记录删除。即,并不会真正的删除文件,而是软删除。

    综上,Docker 镜像通过分层实现了资源共享,通过 copy-on-write 实现了文件隔离。

 

    

CentOS 下 Docker 与.netcore (五)之 三剑客之一 Docker-swarm 集群

CentOS 下 Docker 与.netcore (五)之 三剑客之一 Docker-swarm 集群

 CentOS 下 Docker 与.netcore (一) 之 安装

CentOS 下 Docker 与.netcore (二) 之 Dockerfile

CentOS 下 Docker 与.netcore (三)之 三剑客之一 Docker-Compose

CentOS 下 Docker 与.netcore (四)之 三剑客之一 Docker-machine+jenkins 简单自动化部署

CentOS 下 Docker 与.netcore (五)之 三剑客之一 Docker-swarm 集群

1. 什么是 docker-swarm

在公司网站流量爆发式增长的时候,一台服务器往往不能满足需求,之前我的解决办法是通过 nginx 来实现负载均衡,有了 docker-machine 之后,解决方案会更加简单。当流量增加时,我们按照需要往 docker-swarm 集群中添加主机就可以了。

2. 原理

我们把一组 docker-swarm 集群中的所有 docker 容器统称作一个 service,其中的每一个 docker 容器称做一个 task,如下图

3. 开始搭建环境

上一章我们讲了 docker-machine,在这里我们需要用到。我在我本地 Hyper-v 创建 4 个虚拟机

其中:

CentOS 设置静态 IP:  192.168.8.200,并在这个虚机上搭建好 docker-machine

CentOS2 设置静态 IP:192.168.8.201

CentOS3 设置静态 IP:192.168.8.202

CentOS3 设置静态 IP:192.168.8.203

然后通过 200 节点,分别在 201,202,203 上搭建好 docker

4. 创建集群(swarm 命令)

 1)docker-mashine  ssh 到 host1 上创建 leader

[root@localhost ~]# docker-machine ssh host1
[root@host1 ~]# docker swarm init --advertise-addr 192.168.8.201

2)docker-mashine  ssh 到 host2 加入集群

[root@localhost ~]# docker-machine ssh host2

3)docker-mashine  ssh 到 host1 上查看集群列表

到此,集群构建完毕,是一个最简单的集群

5. 新建.netcore 项目

 1)修改 ValueController.cs
// GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            var ip = HttpContext.Connection.LocalIpAddress;
            var port = HttpContext.Connection.LocalPort;
            return new string[] { ip.ToString(), port.ToString() }; }
2)修改 Program.cs
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args).UseUrls("http://*:5000")
                .UseStartup<Startup>();
3)新建 dockerfile,并经属性设置为:始终复制到输出目录

FROM microsoft/dotnet:2.1-aspnetcore-runtime
MAINTAINER yishi.chen

LABEL description="DockerSwarmTest"
LABEL version="1.0"


WORKDIR /app
COPY . .
EXPOSE 5000
ENTRYPOINT ["dotnet","DockerSwarmTest.dll"]

4)构建 image 并上传到 docker-hub

5)创建 service

我们先只创建一个容器

测试接口

6)添加一个容器
[root@host1 ~]# docker service update --replicas 2 cys_web

查看运行状态

两个都已经启动

6. 模拟并发请求接口

新建一个控制台程序

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace Client { class Program { static HttpClient httpClient = new HttpClient(); static void Main(string[] args) { Parallel.For(1, 10000, i => { HttpResponseMessage response = httpClient.SendAsync(new HttpRequestMessage { Method = new HttpMethod("GET"), RequestUri = new Uri("http://192.168.8.202:5000/api/values") }).Result; var result = response.Content.ReadAsStringAsync().Result; Console.WriteLine(result); }); } } }

运行测试,结果如下:

我们发现,请求 http://192.168.8.202:5000/api/values 时,是对 service 中的两个容器实现轮询调用的,因此,docker swarm 很简单的实现了负载,我们可以再把 host3 加进集群里,再测试一下有三个容器的情况的结果。

[root@host1 ~]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
b6qt1dnbj4hnf2jks0rnxjm8y *   host1               Ready               Active              Leader              18.09.0
p9vfq9qt6dtv929an5hzmirn9     host2               Ready               Active                                  18.09.0
uy37l58v2ooux2tju89ex1o7q     host3               Ready               Active                                  18.09.0

运行控制台测试

三台机器完成负载!!

 

至此三剑客分享完毕

centos7 64bit 安装 docker 最新版教程(Centos7 下安装 Docker Engine)

centos7 64bit 安装 docker 最新版教程(Centos7 下安装 Docker Engine)

一。先决条件

首选需要一个 64 位操作系统和 3.10 或者更版本的内核。

查看当前内核版本:

?

1 2

$ uname -r 3.10.0-229.el7.x86_64

二.yum 安装 Docker Engine

安装 Docker Engine 有两种方式,我们先选择 yum 安装,并且 Docker 官方提供了 yum 源。

1. 使用普通用户 sudo 或者 root 登录到你的服务器,更新 yum,确保你的软件都是最新的

?

1

$ yum update

2. 添加 Docker 的 yum 源

?

1 2 3 4 5 67 8

$ sudo tee /etc/yum.repos.d/docker.repo <<-''EOF'' [dockerrepo] name=Docker Repository baseurl=https://yum.dockerproject.org/repo/main/centos/7/ enabled=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg EOF

3. 安装 Docker 软件包

?

1

$ yum install docker-engine

4. 设置 Docker 服务开机自启

?

1

$ systemctl enable docker.service

5. 启动 Docker 服务

?

1

$ systemctl start docker

6. 验证 Docker 是否安装成功

?

1

$ docker run --rm hello-world

至此:docker 最新版已经安装完成,下面我们进行生产环境镜像的创建,使用 dockerfile

dockerfile (白话其实就是一个脚本,把一个纯镜像按照所需应用或者环境每个操作集成到一个文件执行安装的过程,类似于 linux 的 shell.)

走世界自由行环境需求(tomcat+jdk)

#以下是我们采用纯净 centos,之后安装所需环境依赖的应用(tomcat+jdk)

FROM centos

#以下是我们声明的维护人或者叫做作者

 

MAINTAINER locutus "zoushijie"

 

# OS 环境配置

RUN yum install -y wget

 

# 安装 JDK

RUN mkdir /opt/jdk

RUN wget -P /opt/jdk/ http://zsj-linux.oss-cn-hangzhou.aliyuncs.com/yunwei/jdk-8u162-linux-x64.tar.gz

RUN tar -zxvf /opt/jdk/jdk-8u162-linux-x64.tar.gz -C /opt/jdk && rm -rf /opt/jdk/jdk-8u162-linux-x64.tar.gz

 

# 安装 tomcat

RUN mkdir /opt/tomcat

RUN wget -P /opt/tomcat http://45.77.240.64/apache-tomcat-8.0.47.tar.gz

RUN tar -zxvf /opt/tomcat/apache-tomcat-8.0.47.tar.gz -C /opt/tomcat && rm -rf /opt/tomcat/apache-tomcat-8.0.47.tar.gz

 

#设置环境变量

ENV JAVA_HOME /opt/jdk/jdk1.8.0_162

ENV CATALINA_HOME /opt/tomcat/apache-tomcat-8.0.47

ENV PATH $PATH:$JAVA_HOME/bin

ENV CLASSPATH .:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

#打包项目并拷贝到 tomcat webapps 目录

RUN mkdir /opt/webapp

ADD ./ /opt/webapp

RUN cd /opt/webapp && cp /opt/webapp/war/eureka-server.war /opt/tomcat/apache-tomcat-8.0.47/webapps/

 

#开启内部服务端口

EXPOSE 8080

 

#启动 tomcat 服务器

CMD ["./opt/tomcat/apache-tomcat-8.0.47/bin/catalina.sh","run"] && tail -f /opt/tomcat/apache-tomcat-8.0.47/logs/catalina.out

此为没有修改默认目录容器,下面我们进行修改默认访问目录操作

查看正在运行的容器:docker ps -l

之后进入容器:docker exec -it dd46f73e366e /bin/bash

进入 tomcat 修改 server.xml 文件,添加对应微服务名称:<Context path=""docBase=" 微服务名称 "debug="0"reloadable="true" />

之后要保证容器运行的状态下退出交互界面:Ctrl+p+q,我们可以看到容器仍在运行,下面我们保存一下刚才修改的容器,并以微服务命名

docker commit dd4 eureka-server

查看新生成的镜像:出现了名为 eureka-server 的镜像

删除本地不用的镜像。docker rmi -f <镜像 id>,首先先停止运行中的容器,docker stop < 镜像 id>,停止运行中的容器,要从 docker ps 里查看 id. 不是在 docker images 里查看。

CentOS7 下 Docker 安装、启动

CentOS7 下 Docker 安装、启动

↑点击上方 “程序猿技术大咖” 免费订阅
       最近在搞微服务,打算采用 docker 进行部署,便于环境迁移。本文就在 CentOS 7 下安装 docker 的过程,进行汇总整理,与大家进行分享,成功安装过程见 2.2 章节。

1、检查系统内核

      Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的 CentOS 版本是否支持 Docker 。

通过 uname -r 命令查看你当前的内核版本。

  1. [xcbeyond@docker ~]$ uname -r

  2. 3.10.0-327.10.1.el7.x86_64

2、安装 Docker

       安装过程及其坎坷,出现了各种问题,并进行记录整个过程,与大家进行分享。2.1 节记录安装过程中出现的各种问题,2.2 节整理的是安装成功过程,作为正常安装步骤。

2.1 安装失败

(1)Docker 软件包和依赖包已经包含在默认的 CentOS-Extras 软件源里。执行命令 yum install docker

安装过程如下:

  1. [xcbeyond@docker etc]$ sudo yum install docker

  2. Loaded plugins: fastestmirror, langpacks

  3. base                                                             | 3.6 kB  00:00:00

  4. docker-ce-stable                                                 | 2.9 kB  00:00:00

  5. extras                                                           | 3.4 kB  00:00:00

  6. updates                                                          | 3.4 kB  00:00:00

  7. docker-ce-stable/x86_64/primar FAILED

  8. https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/repodata/251ca0bf128072f68c0ccae90067d231841488501814e0b0b6f492c17c069daa-primary.sqlite.bz2: [Errno 14] curl#60 - "Peer''s Certificate issuer is not recognized."

  9. Trying other mirror.

  10. It was impossible to connect to the CentOS servers.

  11. This could mean a connectivity issue in your environment, such as the requirement to configure a proxy,

  12. or a transparent proxy that tampers with TLS security, or an incorrect system clock.

  13. You can try to solve this issue by using the instructions on https://wiki.centos.org/yum-errors

  14. If above article doesn''t help to resolve this issue please use https://bugs.centos.org/.


  15. Loading mirror speeds from cached hostfile

  16. * base: mirrors.nwsuaf.edu.cn

  17. * extras: mirror.lzu.edu.cn

  18. * updates: mirrors.163.com

  19. docker-ce-stable/x86_64/primar FAILED

  20. https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/repodata/251ca0bf128072f68c0ccae90067d231841488501814e0b0b6f492c17c069daa-primary.sqlite.bz2: [Errno 14] curl#60 - "Peer''s Certificate issuer is not recognized."

  21. Trying other mirror.

  22. docker-ce-stable/x86_64/primary_db                               |  15 kB  00:00:00

  23. Resolving Dependencies

  24.                   ………………


  25. Transaction Summary

  26. ========================================================================================

  27. Install  1 Package (+12 Dependent packages)

  28. Upgrade  1 Package (+15 Dependent packages)


  29. Total download size: 38 M

  30. Is this ok [y/d/N]: y

  31. Downloading packages:

  32. No Presto metadata available for base

  33. updates/7/x86_64/prestodelta                                     | 538 kB  00:00:00

  34. (1/29): atomic-registries-1.22.1-22.git5a342e3.el7.x86_64.rpm    |  35 kB  00:00:00

  35. (2/29): container-selinux-2.68-1.el7.noarch.rpm                  |  36 kB  00:00:00

  36. (3/29): docker-common-1.13.1-74.git6e3bb8e.el7.centos.x86_64.rpm |  92 kB  00:00:00

  37. (4/29): container-storage-setup-0.11.0-2.git5eaf76c.el7.noarch.r |  35 kB  00:00:00

  38. (5/29): libselinux-2.5-12.el7.x86_64.rpm                         | 162 kB  00:00:00

  39. (6/29): libselinux-utils-2.5-12.el7.x86_64.rpm                   | 151 kB  00:00:00

  40. (7/29): libselinux-python-2.5-12.el7.x86_64.rpm                  | 235 kB  00:00:00

  41. (8/29): libsemanage-2.5-11.el7.x86_64.rpm                        | 150 kB  00:00:00

  42.                     ………………

  43. (29/29): docker-1.13.1-74.git6e3bb8e.el7.centos.x86_64.rpm       |  16 MB  00:00:09

  44. ----------------------------------------------------------------------------------------

  45. Total                                                      3.9 MB/s |  38 MB  00:09

  46. Running transaction check

  47. Running transaction test

  48. Transaction test succeeded

  49. Running transaction

  50. Updating   : libsepol-2.5-8.1.el7.x86_64                                         1/45

  51. Updating   : libselinux-2.5-12.el7.x86_64                                        2/45

  52. Updating   : libsemanage-2.5-11.el7.x86_64                                       3/45

  53. Installing : lz4-1.7.5-2.el7.x86_64                                              4/45

  54. Updating   : systemd-libs-219-57.el7_5.1.x86_64                                  5/45

  55. Updating   : systemd-219-57.el7_5.1.x86_64                                       6/45

  56. Updating   : libselinux-utils-2.5-12.el7.x86_64                                  7/45

  57. Updating   : policycoreutils-2.5-22.el7.x86_64                                   8/45

  58. Updating   : selinux-policy-3.13.1-192.el7_5.6.noarch                            9/45

  59. Updating   : selinux-policy-targeted-3.13.1-192.el7_5.6.noarch                  10/45

  60. warning: /etc/selinux/targeted/seusers created as /etc/selinux/targeted/seusers.rpmnew

  61. ‘/etc/selinux/targeted/modules/active/seusers’ -> ‘/etc/selinux/targeted/active/seusers.local’

  62. Installing : 1:oci-register-machine-0-6.git2b44233.el7.x86_64                   11/45

  63. Updating   : libsemanage-python-2.5-11.el7.x86_64                               12/45

  64. Updating   : libselinux-python-2.5-12.el7.x86_64                                13/45

  65. Installing : 1:oci-systemd-hook-0.1.17-2.git83283a0.el7.x86_64                  14/45

  66. Installing : 2:oci-umount-2.3.3-3.gite3c9055.el7.x86_64                         15/45

  67. Updating   : setools-libs-3.3.8-2.el7.x86_64                                    16/45

  68. Updating   : policycoreutils-python-2.5-22.el7.x86_64                           17/45

  69. Installing : 2:container-selinux-2.68-1.el7.noarch                              18/45

  70. Installing : container-storage-setup-0.11.0-2.git5eaf76c.el7.noarch             19/45

  71. Installing : python-pytoml-0.1.14-1.git7dea353.el7.noarch                       20/45

  72. Installing : 1:atomic-registries-1.22.1-22.git5a342e3.el7.x86_64                21/45

  73. Installing : 1:skopeo-containers-0.1.31-1.dev.gitae64ff7.el7.centos.x86_64      22/45

  74. Installing : 2:docker-common-1.13.1-74.git6e3bb8e.el7.centos.x86_64             23/45

  75. Installing : 2:docker-client-1.13.1-74.git6e3bb8e.el7.centos.x86_64             24/45

  76. Installing : subscription-manager-rhsm-certificates-1.20.11-1.el7.centos.x86_   25/45

  77. Installing : 2:docker-1.13.1-74.git6e3bb8e.el7.centos.x86_64                    26/45

  78. Updating   : systemd-python-219-57.el7_5.1.x86_64                               27/45

  79. Updating   : systemd-sysv-219-57.el7_5.1.x86_64                                 28/45

  80. Updating   : libgudev1-219-57.el7_5.1.x86_64                                    29/45

  81. Cleanup    : policycoreutils-python-2.2.5-20.el7.x86_64                         30/45

  82. Cleanup    : selinux-policy-targeted-3.13.1-60.el7_2.3.noarch                   31/45

  83. warning: /etc/selinux/targeted/modules/active/seusers.final saved as /etc/selinux/targeted/modules/active/seusers.final.rpmsave

  84. Cleanup    : systemd-python-219-19.el7_2.4.x86_64                               32/45

  85. Cleanup    : selinux-policy-3.13.1-60.el7_2.3.noarch                            33/45

  86. Cleanup    : systemd-sysv-219-19.el7_2.4.x86_64                                 34/45

  87. Cleanup    : policycoreutils-2.2.5-20.el7.x86_64                                35/45

  88. Cleanup    : systemd-219-19.el7_2.4.x86_64                                      36/45

  89. Cleanup    : setools-libs-3.3.7-46.el7.x86_64                                   37/45

  90. Cleanup    : libselinux-utils-2.2.2-6.el7.x86_64                                38/45

  91. Cleanup    : libselinux-python-2.2.2-6.el7.x86_64                               39/45

  92. Cleanup    : libsemanage-python-2.1.10-18.el7.x86_64                            40/45

  93. Cleanup    : libsemanage-2.1.10-18.el7.x86_64                                   41/45

  94. Cleanup    : libgudev1-219-19.el7_2.4.x86_64                                    42/45

  95. Cleanup    : systemd-libs-219-19.el7_2.4.x86_64                                 43/45

  96. Cleanup    : libselinux-2.2.2-6.el7.x86_64                                      44/45

  97. Cleanup    : libsepol-2.1.9-3.el7.x86_64                                        45/45

  98. Verifying  : libsemanage-python-2.5-11.el7.x86_64                                1/45

  99. Verifying  : libsemanage-2.5-11.el7.x86_64                                       2/45

  100. Verifying  : libselinux-python-2.5-12.el7.x86_64                                 3/45

  101. Verifying  : subscription-manager-rhsm-certificates-1.20.11-1.el7.centos.x86_    4/45

  102. Verifying  : 2:container-selinux-2.68-1.el7.noarch                               5/45

  103. Verifying  : libselinux-utils-2.5-12.el7.x86_64                                  6/45

  104. Verifying  : policycoreutils-2.5-22.el7.x86_64                                   7/45

  105. Verifying  : libsepol-2.5-8.1.el7.x86_64                                         8/45

  106. Verifying  : 1:skopeo-containers-0.1.31-1.dev.gitae64ff7.el7.centos.x86_64       9/45

  107. Verifying  : policycoreutils-python-2.5-22.el7.x86_64                           10/45

  108. Verifying  : 1:oci-systemd-hook-0.1.17-2.git83283a0.el7.x86_64                  11/45

  109. Verifying  : selinux-policy-targeted-3.13.1-192.el7_5.6.noarch                  12/45

  110. Verifying  : python-pytoml-0.1.14-1.git7dea353.el7.noarch                       13/45

  111. Verifying  : systemd-python-219-57.el7_5.1.x86_64                               14/45

  112. Verifying  : systemd-219-57.el7_5.1.x86_64                                      15/45

  113. Verifying  : libgudev1-219-57.el7_5.1.x86_64                                    16/45

  114. Verifying  : 2:docker-common-1.13.1-74.git6e3bb8e.el7.centos.x86_64             17/45

  115. Verifying  : systemd-libs-219-57.el7_5.1.x86_64                                 18/45

  116. Verifying  : lz4-1.7.5-2.el7.x86_64                                             19/45

  117. Verifying  : 1:oci-register-machine-0-6.git2b44233.el7.x86_64                   20/45

  118. Verifying  : 2:oci-umount-2.3.3-3.gite3c9055.el7.x86_64                         21/45

  119. Verifying  : 2:docker-client-1.13.1-74.git6e3bb8e.el7.centos.x86_64             22/45

  120. Verifying  : 2:docker-1.13.1-74.git6e3bb8e.el7.centos.x86_64                    23/45

  121. Verifying  : systemd-sysv-219-57.el7_5.1.x86_64                                 24/45

  122. Verifying  : setools-libs-3.3.8-2.el7.x86_64                                    25/45

  123. Verifying  : libselinux-2.5-12.el7.x86_64                                       26/45

  124. Verifying  : 1:atomic-registries-1.22.1-22.git5a342e3.el7.x86_64                27/45

  125. Verifying  : container-storage-setup-0.11.0-2.git5eaf76c.el7.noarch             28/45

  126. Verifying  : selinux-policy-3.13.1-192.el7_5.6.noarch                           29/45

  127. Verifying  : systemd-python-219-19.el7_2.4.x86_64                               30/45

  128. Verifying  : selinux-policy-targeted-3.13.1-60.el7_2.3.noarch                   31/45

  129. Verifying  : libsemanage-python-2.1.10-18.el7.x86_64                            32/45

  130. Verifying  : setools-libs-3.3.7-46.el7.x86_64                                   33/45

  131. Verifying  : libsemanage-2.1.10-18.el7.x86_64                                   34/45

  132. Verifying  : systemd-219-19.el7_2.4.x86_64                                      35/45

  133. Verifying  : systemd-sysv-219-19.el7_2.4.x86_64                                 36/45

  134. Verifying  : libselinux-utils-2.2.2-6.el7.x86_64                                37/45

  135. Verifying  : systemd-libs-219-19.el7_2.4.x86_64                                 38/45

  136. Verifying  : selinux-policy-3.13.1-60.el7_2.3.noarch                            39/45

  137. Verifying  : libsepol-2.1.9-3.el7.x86_64                                        40/45

  138. Verifying  : libgudev1-219-19.el7_2.4.x86_64                                    41/45

  139. Verifying  : libselinux-python-2.2.2-6.el7.x86_64                               42/45

  140. Verifying  : policycoreutils-2.2.5-20.el7.x86_64                                43/45

  141. Verifying  : libselinux-2.2.2-6.el7.x86_64                                      44/45

  142. Verifying  : policycoreutils-python-2.2.5-20.el7.x86_64                         45/45


  143. Installed:

  144. docker.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos


  145. Dependency Installed:

  146. atomic-registries.x86_64 1:1.22.1-22.git5a342e3.el7

  147. container-selinux.noarch 2:2.68-1.el7

  148. container-storage-setup.noarch 0:0.11.0-2.git5eaf76c.el7

  149. docker-client.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos

  150. docker-common.x86_64 2:1.13.1-74.git6e3bb8e.el7.centos

  151. lz4.x86_64 0:1.7.5-2.el7

  152. oci-register-machine.x86_64 1:0-6.git2b44233.el7

  153. oci-systemd-hook.x86_64 1:0.1.17-2.git83283a0.el7

  154. oci-umount.x86_64 2:2.3.3-3.gite3c9055.el7

  155. python-pytoml.noarch 0:0.1.14-1.git7dea353.el7

  156. skopeo-containers.x86_64 1:0.1.31-1.dev.gitae64ff7.el7.centos

  157. subscription-manager-rhsm-certificates.x86_64 0:1.20.11-1.el7.centos


  158. Updated:

  159. systemd.x86_64 0:219-57.el7_5.1


  160. Dependency Updated:

  161. libgudev1.x86_64 0:219-57.el7_5.1

  162. libselinux.x86_64 0:2.5-12.el7

  163. libselinux-python.x86_64 0:2.5-12.el7

  164. libselinux-utils.x86_64 0:2.5-12.el7

  165. libsemanage.x86_64 0:2.5-11.el7

  166. libsemanage-python.x86_64 0:2.5-11.el7

  167. libsepol.x86_64 0:2.5-8.1.el7

  168. policycoreutils.x86_64 0:2.5-22.el7

  169. policycoreutils-python.x86_64 0:2.5-22.el7

  170. selinux-policy.noarch 0:3.13.1-192.el7_5.6

  171. selinux-policy-targeted.noarch 0:3.13.1-192.el7_5.6

  172. setools-libs.x86_64 0:3.3.8-2.el7

  173. systemd-libs.x86_64 0:219-57.el7_5.1

  174. systemd-python.x86_64 0:219-57.el7_5.1

  175. systemd-sysv.x86_64 0:219-57.el7_5.1


  176. Complete!

(2)查看 Docker 版本。执行命令 docker version

  1. [xcbeyond@docker etc]$ docker version

  2. Client:

  3. Version:         1.13.1

  4. API version:     1.26

  5. Package version:

  6. Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

(3) 启动 Docker。执行命令 service docker start.

  1. [xcbeyond@docker ~]$ sudo service docker start

  2. Redirecting to /bin/systemctl start  docker.service

  3. Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.

哎,启动失败了。查看下 docker 状态,如下所示:

  1. [xcbeyond@docker ~]$ systemctl status docker

  2. docker.service - Docker Application Container Engine

  3. Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)

  4. Active: failed (Result: exit-code) since Wed 2018-08-29 23:07:19 CST; 1min 13s ago

  5. Docs: http://docs.docker.com

  6. Process: 18527 ExecStart=/usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --init-path=/usr/libexec/docker/docker-init-current --seccomp-profile=/etc/docker/seccomp.json $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY $REGISTRIES (code=exited, status=1/FAILURE)

  7. Main PID: 18527 (code=exited, status=1/FAILURE)


  8. Aug 29 23:07:17 docker systemd[1]: Starting Docker Application Container Engine...

  9. Aug 29 23:07:18 docker dockerd-current[18527]: time="2018-08-29T23:07:18.066916004+...d"

  10. Aug 29 23:07:18 docker dockerd-current[18527]: time="2018-08-29T23:07:18.071982389+...1"

  11. Aug 29 23:07:19 docker dockerd-current[18527]: time="2018-08-29T23:07:19.085154395+08...

  12. Aug 29 23:07:19 docker dockerd-current[18527]: Error starting daemon: SELinux is no...e)

  13. Aug 29 23:07:19 docker systemd[1]: docker.service: main process exited, code=exite...URE

  14. Aug 29 23:07:19 docker systemd[1]: Failed to start Docker Application Container Engine.

  15. Aug 29 23:07:19 docker systemd[1]: Unit docker.service entered failed state.

  16. Aug 29 23:07:19 docker systemd[1]: docker.service failed.

  17. Hint: Some lines were ellipsized, use -l to show in full.

网上查阅了大量资料,进行配置的修改等,尝试都未能解决,最终决定还是卸载掉,重新换种方式来安装吧。

2.2 安装成功

(1)卸载 docker,并配置 docker.repo,如下所示:

  1. [xcbeyond@docker run]$ sudo yum remove docker

  2. ……

  3. [xcbeyond@docker run]$ sudo yum remove docker-selinux

  4. ……

  5. [xcbeyond@docker run]$ sudo vi /etc/yum.repos.d/docker.repo

  6. [dockerrepo]

  7. name=Docker Repository

  8. baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/

  9. enabled=1

  10. gpgcheck=1

  11. gpgkey=https://yum.dockerproject.org/gpg

(2)安装 docker,执行命令 yum install docker-engine,如下所示:

(如失败,则可多执行几次便可。安装过程,因需下载安装包,请耐心等待)

  1. [xcbeyond@docker run]$ sudo yum install docker-engine

  2. Loaded plugins: fastestmirror, langpacks

  3. base                                                             | 3.6 kB  00:00:00    

  4. dockerrepo                                                       | 2.9 kB  00:00:00    

  5. extras                                                           | 3.4 kB  00:00:00    

  6. updates                                                          | 3.4 kB  00:00:00    

  7. Loading mirror speeds from cached hostfile

  8. * base: mirror.lzu.edu.cn

  9. * extras: mirrors.aliyun.com

  10. * updates: mirrors.aliyun.com

  11. Resolving Dependencies

  12. --> Running transaction check

  13. ---> Package docker-engine.x86_64 0:17.05.0.ce-1.el7.centos will be installed

  14. --> Processing Dependency: docker-engine-selinux >= 17.05.0.ce-1.el7.centos for package: docker-engine-17.05.0.ce-1.el7.centos.x86_64

  15. --> Running transaction check

  16. ---> Package docker-engine-selinux.noarch 0:17.05.0.ce-1.el7.centos will be installed

  17. --> Finished Dependency Resolution


  18. Dependencies Resolved


  19. ========================================================================================

  20. Package                   Arch       Version                      Repository      Size

  21. ========================================================================================

  22. Installing:

  23. docker-engine             x86_64     17.05.0.ce-1.el7.centos      dockerrepo      19 M

  24. Installing for dependencies:

  25. docker-engine-selinux     noarch     17.05.0.ce-1.el7.centos      dockerrepo      28 k


  26. Transaction Summary

  27. ========================================================================================

  28. Install  1 Package (+1 Dependent package)


  29. Total download size: 20 M

  30. Installed size: 70 M

  31. Is this ok [y/d/N]: y

  32. Downloading packages:

  33. warning: /var/cache/yum/x86_64/7/dockerrepo/packages/docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch.rpm: Header V4 RSA/SHA512 Signature, key ID 2c52609d: NOKEY

  34. Public key for docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch.rpm is not installed

  35. (1/2): docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch.rpm  |  28 kB  00:00:01    

  36. (2/2): docker-engine-17.05.0.ce-1.el7.centos.x86_64.rpm                               22% [=================                                                           ] 6.3 kB/s | 4.5 MB  00:40:30 ETA % [=================                                                           ] 8.6 kB/docker-engine-17.05.0.ce-1.el7 FAILED                                          7:57 ETA

  37. https://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-17.05.0.ce-1.el7.centos.x86_64.rpm: [Errno 12] Timeout on https://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-17.05.0.ce-1.el7.centos.x86_64.rpm: (28, ''Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds'')

  38. Trying other mirror.

  39. (2/2): docker-engine-17.05.0.ce-1.el7.centos.x86_64.rpm          |  19 MB  00:08:32    

  40. ----------------------------------------------------------------------------------------

  41. Total                                                      7.8 kB/s |  20 MB  42:45    

  42. Retrieving key from https://yum.dockerproject.org/gpg

  43. Importing GPG key 0x2C52609D:

  44. Userid     : "Docker Release Tool (releasedocker) <docker@docker.com>"

  45. Fingerprint: 5811 8e89 f3a9 1289 7c07 0adb f762 2157 2c52 609d

  46. From       : https://yum.dockerproject.org/gpg

  47. Is this ok [y/N]: y

  48. Running transaction check

  49. Running transaction test

  50. Transaction test succeeded

  51. Running transaction

  52. Installing : docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch                 1/2

  53. Re-declaration of type docker_t

  54. Failed to create node

  55. Bad type declaration at /etc/selinux/targeted/tmp/modules/400/docker/cil:1

  56. /usr/sbin/semodule:  Failed!

  57. Installing : docker-engine-17.05.0.ce-1.el7.centos.x86_64                         2/2

  58. Verifying  : docker-engine-17.05.0.ce-1.el7.centos.x86_64                         1/2

  59. Verifying  : docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch                 2/2


  60. Installed:

  61. docker-engine.x86_64 0:17.05.0.ce-1.el7.centos


  62. Dependency Installed:

  63. docker-engine-selinux.noarch 0:17.05.0.ce-1.el7.centos


  64. Complete!

(3)启动 docker。执行命令 systemctl start docker,如下所示:

[xcbeyond@docker run]$ sudo systemctl start docker

哈哈,大功告成,终于安装成功啦。睡觉喽!


如果在检索镜像时,出现如下类似错误:

  1. [docker@docker xcbeyond]$ docker search redis

  2. Error response from daemon: Get https://index.docker.io/v1/search?q=redis&n=25: x509: certificate signed by unknown authority

 这是由于无法访问国外 docker 镜像仓库的原因,或者访问比较慢,可修改为国内 docker 仓库。docker 在中国已经有了仓库:https://www.docker-cn.com/registry-mirror,访问速度还不错。

修改方法:

(如果没有 daemon.json 文件,则创建)

  1. [docker@docker xcbeyond]$ sudo vi /etc/docker/daemon.json

  2. {

  3. "registry-mirrors": ["https://registry.docker-cn.com"]

  4. }

重启 docker,就可以了。

  1. [root@docker docker]# systemctl restart docker

  2. [docker@docker xcbeyond]$ docker search redis

  3. NAME                                 DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED

  4. redis                                Redis is an open source key-value store th...   5665      [OK]

  5. bitnami/redis                        Bitnami Redis Docker Image                      88                   [OK]

  6. sameersbn/redis                                                                      71                   [OK]

  7. hypriot/rpi-redis                    Raspberry Pi compatible redis image             33                  

  8. tenstartups/redis-commander                                                          32                   [OK]

  9. kubeguide/redis-master               redis-master with "Hello World!"                26                  

  10. kubeguide/guestbook-redis-slave      Guestbook redis slave                           20                  

  11. redislabs/redis                      Clustered in-memory database engine compat...   15                  

  12. webhippie/redis                      Docker images for redis                         9                    [OK]

  13. rediscommander/redis-commander       Alpine image for redis-commander - Redis m...   8                    [OK]

  14. arm32v7/redis                        Redis is an open source key-value store th...   7                    

  15. rtoma/logspout-redis-logstash        Logspout including Redis adapter for sendi...   5                    

  16. oliver006/redis_exporter              Prometheus Exporter for Redis Metrics. Su...   5                    

  17. centos/redis-32-centos7              Redis in-memory data structure store, used...   3                    

  18. tomesar/redis-arm                    Redis for ARM!                                  2                    [OK]

  19. frodenas/redis                       A Docker Image for Redis                        2                    [OK]

  20. dynomitedb/redis                     Redis backend for DynomiteDB.                   2                    [OK]

  21. arm64v8/redis                        Redis is an open source key-value store th...   2                    

  22. kilsoo75/redis-master                This image is for the redis master of SK C...   1                    

  23. tiredofit/redis                      Redis Server w/ Zabbix monitoring and S6 O...   1                    [OK]

  24. circleci/redis                       CircleCI images for Redis                       1                    [OK]

  25. google/guestbook-python-redis        A simple guestbook example written in Pyth...   1                    

  26. anchorfree/redis                     redis cache server for logging                  0                    

  27. iadvize/redis                                                                        0                    

  28. brendangibat/docker-logspout-redis   Docker Logspout container with Logspout-Re...   0                    [OK]


“程序猿技术大咖”,您值得拥有!
公众号 ID:cxyjsdk
长按左侧二维码关注


本文分享自微信公众号 - 程序猿技术大咖(cxyjsdk)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与 “OSC 源创计划”,欢迎正在阅读的你也加入,一起分享。

今天的关于[转载] Centos7 的安装、Docker1.12.3 的安装,以及 Docker Swarm 集群的简单实例的分享已经结束,谢谢您的关注,如果想了解更多关于1.docker 学习笔记之入门,docker 在 centos7 下的安装以及基础命令的使用、CentOS 下 Docker 与.netcore (五)之 三剑客之一 Docker-swarm 集群、centos7 64bit 安装 docker 最新版教程(Centos7 下安装 Docker Engine)、CentOS7 下 Docker 安装、启动的相关知识,请在本站进行查询。

本文标签:

上一篇Docker 持久存储介绍(十三)(docker持久化存储)

下一篇基于 Docker+Prometheus+Grafana 监控 SpringBoot 健康信息(docker 健康检查 监控)