导语

上一篇文章中我们介绍了Envoy的简单配置,Envoy有三种部署方式,这篇文章中我们来介绍其中的一种部署方式为前端代理。

部署前的准备

安装Docker环境

老版本的Docker被称为docker或docker-engine。如果安装了这些,请卸载它们以及相关的依赖项。

yum remove -y docker docker-common docker-selinux docker-engine

部署docker ce

# 安装yum-utils、device-mapper-persistent-data、lvm2
yum install -y yum-utils device-mapper-persistent-data lvm2
# 添加yum源配置
yum-config-manager --add-repo  https://download.docker.com/linux/centos/docker-ce.repo

可选)启用边缘和测试存储库

# 启动边缘存储库
yum-config-manager --enable docker-ce-edge
# 禁用边缘存储库
yum-config-manager --disable docker-ce-edge
# 启动测试存储库
yum-config-manager --enable docker-ce-test
# 禁用测试存储库
yum-config-manager --disable docker-ce-test

安装docker

yum install docker-ce -y
systemctl start docker
systemctl enable docker

或者安装指定版本的docker

# 查看所有的版本
yum list docker-ce --showduplicates |sort -r
# 指定安装
yum install -y docker-ce-<版本号>

使用镜像加速器

mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://*************.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker

安装Docker-Machine

Docker Machine 是 Docker 官方提供的一个工具,它可以帮助我们在远程的机器上安装 Docker,或者在虚拟机 host 上直接安装虚拟机并在虚拟机中安装 Docker。我们还可以通过 docker-machine 命令来管理这些虚拟机和 Docker。

# https://github.com/docker/machine/releases
$ sudo curl -L https://github.com/docker/machine/releases/download/v0.13.0/docker-machine-`uname -s`-`uname -m` > /usr/local/bin/docker-machine
$ sudo chmod +x /usr/local/bin/docker-machine

安装成功后查看我们的版本输出如下结果

[root@localhost front-proxy]# docker-machine -v
docker-machine version 0.16.1, build cce350d7

安装docker-compose

# 下载文件
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# 添加可执行权限
chmod +x /usr/local/bin/docker-compose
# 测试安装结果
docker-compose -v

部署前端代理

设置Docker Machine

创建一个新的机器来容纳容器:

$ docker-machine create --driver virtualbox default
$ eval $(docker-machine env default)

这是可能会会报如下错误

Error with pre-create check: "This computer doesn't have VT-X/AMD-v enabled. Enabling it in the BIOS is mandatory"

这是因为你的系统没有开启虚拟化引擎,我们使用下面的方法设置
3100692-3963225f0397a8d3.png
按照上面的方法设置后,我们仍然执行上述命令

或许你的系统仍然会出现如下的错误

error with pre-create check: "We support Virtualbox starting with version 5. Your VirtualBox install is \"WARNING: The vboxdrv kernel module is not loaded. Either there is no module\\n         available for the current kernel (3.10.0-514.el7.x86_64) or it failed to\\n         load. Please recompile the kernel module and install it by\\n\\n           sudo /sbin/vboxconfig\\n\\n         You will not be able to start VMs until this problem is fixed.\\n5.2.0r118431\". Please upgrade at 

我们按照提示运行

sudo /sbin/vboxconfig
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
This system is currently not set up to build kernel modules.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
    kernel-devel 
 
This system is currently not set up to build kernel modules.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
    kernel-devel kernel-devel-3.10.0-514.el7.x86_64
 
There were problems setting up VirtualBox.  To re-start the set-up process, run
  /sbin/vboxconfig
as root

发现我们的系统需要这个版本的kernel-devel-3.10.0-514.el7.x86_64
接着我们安装

yum -y install kernel-devel-3.10.0-514.el7.x86_64

这里需要注意的是如果你在操作中遇到这样的问题,你应该安装你的系统需要的kernel-devel版本

安装成功后再次运行下面的命令

docker-machine create --driver virtualbox default

这次终于不再进行报错了,但是我们的命令却卡在了一行命令中,该命令中使用https://github.com/boot2docker/boot2docker/releases/download/v19.03.2-rc1/boot2docker.iso下载镜像,如果你没有翻墙,或许你永远无法完成下载,所以可以在网站上下载好文件
将我们文件按照下载的提示移动到相关的目录,在这里我的目录为/root/.docker/machine/cache/
然后执行以下命令。

docker-machine create --driver virtualbox  --virtualbox-boot2docker-url=/root/.docker/machine/cache/boot2docker.iso  default

这是我们便成功的运行了我们的一个主机

建立本地Envoy克隆仓库,并启动所有的容器

我们使用官方提供的模板进行构建,首先使用git获取源码

$ git clone https://github.com/envoyproxy/envoy.git
$ cd envoy/examples/front-proxy
$ docker-compose up --build -d

经过漫长的等待我们最终运行成功,输入docker-compose ps查看

[root@localhost front-proxy]# docker-compose ps
          Name                         Command               State                            Ports                         
----------------------------------------------------------------------------------------------------------------------------
front-proxy_front-envoy_1   /docker-entrypoint.sh /bin ...   Up      10000/tcp, 0.0.0.0:8000->80/tcp, 0.0.0.0:8001->8001/tcp
front-proxy_service1_1      /bin/sh -c /usr/local/bin/ ...   Up      10000/tcp, 80/tcp                                      
front-proxy_service2_1      /bin/sh -c /usr/local/bin/ ...   Up      10000/tcp, 80/tcp
Last modification:September 6th, 2019 at 02:27 pm
如果觉得我的文章对你有用,请随意赞赏