(未完待续)
如果已经安装有Docker,根据自身情况选择是否重新安装,首先卸载旧版的Docker
yum update & yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
添加下列依赖包
[root@hadoop1 sword]# yum install -y yum-utils device-mapper-persistent-data lvm2
yum-utils是repository的扩展管理工具,device-mapper-persistent-data、lvm2用于Docker的储存设备映射(devicemapper)
配置阿里云yum源
[root@hadoop1 sword]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
查看添加了yum源之后能够安装的Docker的版本号
[root@hadoop1 sword]# yum list docker-ce --showduplicates
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.huaweicloud.com
* extras: mirrors.163.com
* updates: mirrors.163.com
Available Packages
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable
docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.6-3.el7 docker-ce-stable
安装新版containerd
dnf install https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/edge/Packages/containerd.io-1.2.10-3.2.el7.x86_64.rpm
安装最新版本
[root@hadoop1 sword]# yum install docker-ce
或者指定版本安装
[root@hadoop1 sword]# yum install docker-ce-18.03.0.ce
启动并查看docker服务情况
[root@hadoop1 sword]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@hadoop1 sword]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: inactive (dead)
Docs: https://docs.docker.com
在/etc/docker/daemon.json中添加镜像加速器
{
"registry-mirrors":[
"https://[YOUR-OWN-MIRROR]"
]
}
重启docker
[root@hadoop1 docker]# systemctl daemon-reload
[root@hadoop1 docker]# systemctl restart docker
测试docker的运行情况
[root@hadoop1 eureka]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2020-02-21 11:48:56 CST; 37min ago
Docs: https://docs.docker.com
Main PID: 50409 (dockerd)
Tasks: 15
Memory: 399.6M
CGroup: /system.slice/docker.service
└─50409 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Feb 21 11:48:56 hadoop1 dockerd[50409]: time="2020-02-21T11:48:56.154603535+08:00" level=info msg="Loading containers: start."
Feb 21 11:48:56 hadoop1 dockerd[50409]: time="2020-02-21T11:48:56.508708882+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Da... IP address"
Feb 21 11:48:56 hadoop1 dockerd[50409]: time="2020-02-21T11:48:56.661662179+08:00" level=info msg="Loading containers: done."
Feb 21 11:48:56 hadoop1 dockerd[50409]: time="2020-02-21T11:48:56.679894996+08:00" level=info msg="Docker daemon" commit=369ce74a3c graphdriver(s)=overlay2 version=19.03.6
Feb 21 11:48:56 hadoop1 dockerd[50409]: time="2020-02-21T11:48:56.680056192+08:00" level=info msg="Daemon has completed initialization"
Feb 21 11:48:56 hadoop1 dockerd[50409]: time="2020-02-21T11:48:56.695352732+08:00" level=info msg="API listen on /var/run/docker.sock"
Feb 21 11:48:56 hadoop1 systemd[1]: Started Docker Application Container Engine.
Feb 21 12:20:25 hadoop1 dockerd[50409]: time="2020-02-21T12:20:25.213504614+08:00" level=info msg="ignoring event" module=libcontainerd namespace=moby topic=/tasks/delete t....TaskDelete"
Feb 21 12:20:26 hadoop1 dockerd[50409]: time="2020-02-21T12:20:26.738593695+08:00" level=info msg="Layer sha256:bd1beb0076589cecb5e1c6037832bc0fb62c64e31729b73061378810ece9... cleaned up"
Feb 21 12:20:26 hadoop1 dockerd[50409]: time="2020-02-21T12:20:26.829088101+08:00" level=info msg="Layer sha256:bd1beb0076589cecb5e1c6037832bc0fb62c64e31729b73061378810ece9... cleaned up"
Hint: Some lines were ellipsized, use -l to show in full.
安装docker-compose
在 https://github.com/docker/compose/releases 下载适合版本的docker-compose到/usr/local/bin中
此处为 docker-compose-linux-x86-64,并修改名字为docker-compose,(或者直接使用curl,但是很慢)
修改它的的权限
[root@hadoop1 sword]# chmod +x /usr/local/bin/docker-compose
以Eureka为例,Eureka是服务注册与发现中心。在整个微服务云架构中,除了Eureka之外,还有平台监控服务,API网关服务,服务提供者,服务提供者等其他微服务,为了起到隔离的效果。原则上每个服务需要独立构建一个镜像,并且有一些镜像出于负载均衡的考虑,往往需要开启复数以上的容器,像Eureka镜像一般情况下则需要开启至少三个容器。
Eureka它自身的项目配置文件application.yml如下
spring:
application:
name: server-netflix-eureka
server:
port: 8080
eureka:
instance:
hostname: 192.168.36.128
client:
# 是否需要将自己注册到Eureka
registerWithEureka: true
# 表示是否从Eureka获取注册信息,如果是单一节点,则不需要从其他Eureka节点获取信息,则可以设置为false,如果是集群,则为true
fetchRegistry: true
serviceUrl:
defaultZone: http://192.168.36.128:8080/eureka/,http://192.168.36.128:8180/eureka/,http://192.168.36.128:8280/eureka/
其中192.168.36.128是虚拟机的地址。server.port是容器内部的端口,这个端口在创建容器阶段会与虚拟机的端口做映射,实际上访问的时候,也是访问的虚拟机端口,所以deaultZone中的端口实际上是虚拟机的端口。
将该项目打包成jar包,netflix-eureka-0.0.1-SNAPSHOT.jar,并将它拷贝到/home/sword/docker/eureka下,接着就可以在该文件夹下创建Dockerfile文件
From openjdk:8-jre
RUN mkdir /app
COPY netflix-eureka-0.0.1-SNAPSHOT.jar /app/
CMD java -jar /app/netflix-eureka-0.0.1-SNAPSHOT.jar
EXPOSE 8080
Dockerfile编写完成后,使用docker build构建镜像
[root@hadoop1 eureka]# docker build -t server-eureka .
Sending build context to Docker daemon 71.66MB
Step 1/5 : From openjdk:8-jre
8-jre: Pulling from library/openjdk
dc65f448a2e2: Pull complete
346ffb2b67d7: Pull complete
dea4ecac934f: Pull complete
f003f5c25f68: Pull complete
c92ee316420d: Pull complete
38a95be757d0: Pull complete
Digest: sha256:f8c6c258547b62639685ba92f278188c359401ea6f165c0048477a6680849385
Status: Downloaded newer image for openjdk:8-jre
---> 2df355aab4eb
Step 2/5 : RUN mkdir /app
---> Running in f723aac0059b
Removing intermediate container f723aac0059b
---> ccffc6ead6c2
Step 3/5 : COPY netflix-eureka-0.0.1-SNAPSHOT.jar /app/
---> 6210e4b236d3
Step 4/5 : CMD java -jar /app/netflix-eureka-0.0.1-SNAPSHOT.jar
---> Running in bbbd3f9b0198
Removing intermediate container bbbd3f9b0198
---> 88f9bee2e31c
Step 5/5 : EXPOSE 8080
---> Running in 9a25c52bdaa0
Removing intermediate container 9a25c52bdaa0
---> e7b77d8625ae
Successfully built e7b77d8625ae
Successfully tagged server-eureka:latest
查看创建的镜像
[root@hadoop1 eureka]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
server-eureka latest c0ca3815e24c 37 seconds ago 336MB
openjdk 8-jre 2df355aab4eb 2 weeks ago 264MB
创建docker-compose.yml文件,以集群的方式启动Eureka
version: "3.1"
services:
server-eureka-1:
restart: always
image: server-eureka
container_name: server-eureka-1
ports:
- 8080:8080
server-eureka-2:
restart: always
image: server-eureka
container_name: server-eureka-2
ports:
- 8180:8080
server-eureka-3:
restart: always
image: server-eureka
container_name: server-eureka-3
ports:
- 8280:8080
使用以下命令创建并启动容器
[root@hadoop1 eureka]# docker-compose up -d
Creating network "eureka_default" with the default driver
Creating server-eureka-1 ... done
Creating server-eureka-3 ... done
Creating server-eureka-2 ... done
查看启动的容器
[root@hadoop1 eureka]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b73d29d137c2 server-eureka "/bin/sh -c 'java -j…" 8 minutes ago Up 8 minutes 0.0.0.0:8180->8080/tcp server-eureka-2
560a34069551 server-eureka "/bin/sh -c 'java -j…" 8 minutes ago Up 8 minutes 0.0.0.0:8280->8080/tcp server-eureka-3
c636006ff2b1 server-eureka "/bin/sh -c 'java -j…" 8 minutes ago Up 8 minutes 0.0.0.0:8080->8080/tcp server-eureka-1