07-Docker集群准备

nobility 发布于 2022-08-08 2334 次阅读


Docker集群准备

在VMware中的Linux虚拟机中使用虚拟机,首先开启该CPU的虚拟化,右击当前虚拟机选择设置,点击处理器勾选虚拟化引擎下的虚拟化 Intel VT -x/EPT 或 AMD-V/RVI(V)选项,并且虚拟机需要足够的内存

虚拟环境准备

在Linux使用VirtualBox进行虚拟机的创建, VirtualBox是一款开源虚拟机软件,支持各个系统

  1. 为了使用国yum源安装VirtualBox,新建/etc/yum.repos.d/virtualbox.repo文件,将下面内容复制进去,注释最好删除
[virtualbox]
name=Virtualbox Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/virtualbox/rpm/el$releasever/  #清华大学的的
#baseurl=https://mirrors.cloud.tencent.com/virtualbox/rpm/el$releasever/  #腾讯云的
gpgcheck=0
enabled=1
  1. 刷新yum源并进行安装
yum makecache  #刷新yum源
yum search VirtualBox  #搜索VirtualBox的版本
yum install VirtualBox-6.1  #安装时必须要指定版本号

虚拟机准备

Vagrant是用于构建和管理虚拟机环境的工具,可管理VirtualBox,VMware和一些云端虚拟机,从Vagrant官网下载该工具,也可从该源中获取

wget https://releases.hashicorp.com/vagrant/2.2.14/vagrant_2.2.14_linux_amd64.zip  #下载
unzip vagrant_2.2.14_linux_amd64.zip -d /usr/local/bin/   #安装
vagrant -v  #查看版本

镜像准备

最后先将镜像下载到本地,再添加到镜像仓库中,不要使用URL远程添加,否则可能出错后就白白下载了

进行虚拟机镜像的下载,国内镜像不太全,centos使用中科大的源进行查找(可改变URL的版本部分),ubuntu使用清华的源进行查找

  • vagrant box add 镜像名 VagrantBox镜像路径:将镜像添加到镜像仓库中,并自定义命名
  • vagrant box list:查看镜像仓库中有的镜像
  • vagrant box remove 镜像名:删除镜像仓库中的镜像

虚拟机创建

需要在当前目录中执行下面命令才行

  • vagrant init 镜像名:在当前目录初始化一个Vagrantfile文件,该文件用于描述要启动的虚拟机
  • vagrant up:根据当前目录下的Vagrantfile创建一个虚拟机
  • vagrant status:查看虚拟机状态
  • vagrant ssh:进入创建的虚拟机
  • vagrant halt:停止虚拟机
  • vagrant destroy:删除虚拟机

实验环境虚拟机构建

端口转发

由于是虚拟机中的虚拟机,所以真实机想要访问虚拟机中的虚拟机就需要做端口转发才能访问到

需要保证firewall服务已经启动,并且允许伪装IP

firewall-cmd --query-masquerade  #检查是否允许伪装IP
firewall-cmd --permanent --add-masquerade  #允许防火墙伪装IP
firewall-cmd --permanent --remove-masquerade  #禁止防火墙伪装IP

使用下面命令即可做端口转发,根据实际情况进行修改

firewall-cmd  --zone=public --add-forward-port=port=80:proto=tcp:toport=80:toaddr=192.168.99.102
#添加端口转发规则,前面是虚拟机中的端口,后面是虚拟机中的虚拟机的端口和ip

firewall-cmd  --zone=public --remove-forward-port=port=80:proto=tcp:toport=80:toaddr=192.168.99.102
#删除端口转发规则,将add该为remove即可

Docker虚拟机快速搭建

docker-machine是docker官方用快速创建安装docker的虚拟机工具,需要建立在有虚拟机的基础上使用,首先需要安装,执行下面命令即可安装

base=https://github.com/docker/machine/releases/download/v0.16.0 &&
curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
sudo mv /tmp/docker-machine /usr/local/bin/docker-machine &&
chmod +x /usr/local/bin/docker-machine
  • docker-machine create 虚拟机名:创建安装好docker的虚拟机
  • docker-machine ssh 虚拟机名:进入某个虚拟机
  • docker-machine config 虚拟机名:配置某个虚拟机
    • -H:指定ip和端口,比如tcp://192.168.99.103:2376
  • docker-machine stop 虚拟机名:停止某个虚拟机
  • docker-machine start 虚拟机名:启动某个虚拟机
  • docker-machine restart 虚拟机名:重启某个虚拟机
  • docker-machine rm 虚拟机名:删除某个虚拟机,删除全部命令类似dockerdocker-machine rm -f $(docker-machine ls -q)
  • docker-machine ls:列出当前所有使用该工具安装的虚拟机

镜像加速

进入创建的虚拟机后先切换为root用户,在执行下面命令并退出,具体如下

sudo -i                                                                                                     
echo '{"registry-mirrors": ["https://reg-mirror.qiniu.com"]}' > /etc/docker/daemon.json
exit

由于docker-machine系统过于简单,没有服务管理命令,需要将所有虚拟机重启,执行docker-machine restart $(docker-machine ls -q)即可

文件上传

镜像下载还是很慢,可将下载好的文件上传到docker-machin虚拟机中,需要执行下面命令,注意只能上传到tmp目录下,其他目录没有权限

docker-machine scp  源主机路径 目标主机路径
#源主机路径和目标主机路径形式如下
#root@localhost:path docker@node2:/tmp

错误与解决

缺失vagrant命令依赖库

运行vagrant命令时报如下错误:根据提示进入网站解决

dlopen(): error loading libfuse.so.2

AppImages require FUSE to run. 
You might still be able to extract the contents of this AppImage 
if you run it with the --appimage-extract option. 
See https://github.com/AppImage/AppImageKit/wiki/FUSE 
for more information

根据官方提示安装,具体就是下面步骤

yum install epel-release  #安装epel源
yum --enablerepo=epel install fuse-sshfs  #安装fuse-sshfs库
user="$(whoami)"
groupadd fuse
usermod -a -G fuse "$user" 

添加镜像时的错误

在添加镜像时,出现如下错误:说bsdtar命令找不到

The executable 'bsdtar' Vagrant is trying to run was not
found in the PATH variable. This is an error. Please verify
this software is installed and on the path.

使用yum进行安装bsdtar工具即可

yum install bsdtar

启动虚拟机时的错误

错误1

说虚拟机受到严重错误,无法访问,要么清理要么修复,我选择清理后重新启一个虚拟机

Your VM has become "inaccessible." Unfortunately, this is a critical error
with VirtualBox that Vagrant can not cleanly recover from. Please open VirtualBox
and clear out your inaccessible virtual machines or find a way to fix
them.

删除当前目录下的.vagrant隐藏文件重新启动

错误2

未找到虚拟机软件

No usable default provider could be found for your system.

Vagrant relies on interactions with 3rd party systems, known as
"providers", to provide Vagrant with resources to run development
environments. Examples are VirtualBox, VMware, Hyper-V.

The easiest solution to this message is to install VirtualBox, which
is available for free on all major platforms.

If you believe you already have a provider available, make sure it
is properly installed and configured. You can see more details about
why a particular provider isn't working by forcing usage with
`vagrant up --provider=PROVIDER`, which should give you a more specific
error message for that particular provider.

根据提示执行vagrant up --provider=virtualbox指定虚拟机软件,又出现如下错误:说virtualbox在当前系统不可用

The provider 'virtualbox' that was requested to back the machine
'default' is reporting that it isn't usable on this system. The
reason is shown below:

VirtualBox is complaining that the kernel module is not loaded. Please
run `VBoxManage --version` or open the VirtualBox GUI to see the error
message which should contain instructions on how to fix this error.

根据提示执行VBoxManage --version:说未加载vboxdrv内核模块

WARNING: The vboxdrv kernel module is not loaded. Either there is no module
         available for the current kernel (3.10.0-1127.el7.x86_64) or it failed to
         load. Please recompile the kernel module and install it by

           sudo /sbin/vboxconfig

         You will not be able to start VMs until this problem is fixed.
6.1.16r140961

根据提示执行/sbin/vboxconfig

vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Starting VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
This system is currently not set up to build kernel modules.
Please install the gcc make perl packages from your distribution.
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-1127.el7.x86_64
This system is currently not set up to build kernel modules.
Please install the gcc make perl packages from your distribution.
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-1127.el7.x86_64

There were problems setting up VirtualBox.  To re-start the set-up process, run
  /sbin/vboxconfig
as root.  If your system is using EFI Secure Boot you may need to sign the
kernel modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) before you can load
them. Please see your Linux system's documentation for more information.

根据提示安装 kernel-develkernel-devel-3.10.0-1127以及gcc make perl编译工具,当前yum源中发现并没有该版本的内核,经过在网上的搜索在该网站找到了该版本的内核,进行下载后安装

yum install gcc make perl
yum install kernel-devel kernel-devel-3.10.0-1127  #镜像中发现并没有该版本的内核
wget https://linuxsoft.cern.ch/cern/centos/7/updates/x86_64/Packages/Packages/kernel-devel-3.10.0-1127.el7.x86_64.rpm
yum install kernel-devel-3.10.0-1127.el7.x86_64.rpm  #本地安装
/sbin/vboxconfig
错误3

找不到rsync路径

"rsync" could not be found on your PATH. Make sure that rsync
is properly installed on your system and available on the PATH.

Vagrantfile中添加下面配置

Vagrant.configure("2") do |config|

  #...上面配置省略
  config.vm.synced_folder ".", "/vagrant", disabled: true  #禁用同步文件夹

  #...下面配置省略
end
错误4

Vagrant无法挂载VirtualBox共享目录

Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrant

The error output from the command was:

mount: unknown filesystem type 'vboxsf'

安装vagrant-vbguest插件即可,从国内源进行安装

vagrant plugin install vagrant-vbguest --plugin-clean-sources --plugin-source https://gems.ruby-china.com/
错误5

错误的命令:umount: /mnt: not mounted

The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

umount /mnt

Stdout from the command:

Stderr from the command:

umount: /mnt: not mounted

卸载并且重新安装vagrant-vbguest插件

vagrant plugin uninstall vagrant-vbguest
vagrant plugin install vagrant-vbguest --plugin-version 0.21 --plugin-clean-sources --plugin-source https://gems.ruby-china.com/
此作者没有提供个人介绍
最后更新于 2022-08-08