文章目录
环境
win7/10
vagrant 2.2.6
virtualBox-6.0.14
centos7
ModaXterm/XShell6
VirtualBox安装
01 访问VirtualBox[官网](https://www.virtualbox.org)
02 选择对应的操作系统版,我这里是选择[6.0.14版本](https://download.virtualbox.org/virtualbox/6.0.14/VirtualBox-6.0.14-133895-Win.exe)安装
03 傻瓜式安装
04 win10中若出现 安装 virtualbox快完成时立即回滚,并提示安装出现严重错误
(1) 打开服务
(2) 找到Device Install Service和Device Setup Manager,然后启动
(3) 再次尝试安装
Vagrant安装
01 访问Vagrant官网
02 下载对应操作系统版本
03 傻瓜式安装
04 win7 安装需要升级powershell版本
win7的powershell默认版为2.0
查看powershell版本命令: Get-host| Select-Object Version
05 命令行输入vagrant,测试是否安装成功
安装centos7
01 创建centos7文件夹,并进入其中[目录全路径不要有中文字符]
mkdir D:\ws\centos/7
02 在此目录下打开cmd,运行vagrant init centos/7
此时会在当前目录下生成Vagrantfile,同时指定使用的镜像为centos/7,关键是这个镜像在哪里,我已经提前准备好了,名称是virtualbox.box文件
03 将virtualbox.box文件添加到vagrant管理的镜像中
(1)下载网盘中的virtualbox.box文件
(2)保存到磁盘的某个目录,比如D:\ws\virtualbox.box
(3)添加镜像并起名叫centos/7:vagrant box add centos/7 D:\ws\virtualbox.box
(4)vagrant box list 查看本地的box[这时候可以看到centos/7]
04 centos/7镜像有了,根据Vagrantfile文件启动创建虚拟机
来到centos7文件夹,在此目录打开cmd窗口,执行vagrant up[打开virtual box观察,可以发现centos7创建成功]
05 以后大家操作虚拟机,还是要在centos文件夹打开cmd窗口操作
vagrant halt 优雅关闭
vagrant up 正常启动
06 vagrant常用命令
(1)vagrant ssh
进入刚才创建的centos7中
(2)vagrant status
查看centos7的状态
(3)vagrant halt
停止/关闭centos7
(4)vagrant destroy
删除centos7
(5)vagrant status
查看当前vagrant创建的虚拟机
(6)Vagrantfile中也可以写脚本命令,使得centos7更加丰富
但是要注意,修改了Vagrantfile,要想使正常运行的centos7生效,必须使用vagrant reload
到这里恭喜你,virutalBox+vagrant搭建的cetons7环境成功
使用ModaXterm/XShell链接centos7
01 使用centos7的默认账号连接
在centos文件夹下执行vagrant ssh-config
关注:Hostname Port IdentityFile
IP:127.0.0.1
port:2222
用户名:vagrant
密码:vagrant
文件:Identityfile指向的文件private-key
02 使用root账户登录
vagrant ssh 进入到虚拟机中
sudo -i
vi /etc/ssh/sshd_config
修改PasswordAuthentication yes
passwd修改密码,比如abc123
systemctl restart sshd
使用账号root,密码abc123进行登录
Vagrantfile通用写法
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "centos/7"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network. use_dhcp_assigned_default_route: true bridge: "en1: Wi-Fi (AirPort)"
config.vm.network "public_network",ip: "192.168.0.110"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
config.vm.provider "virtualbox" do |vb|
vb.memory = "3000"
vb.name= "docker-node1"
vb.cpus= 2
end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
网络小知识:
- config.vm.network “public_network”,ip: “192.168.0.110” 使用固定IP的桥接网
a ip需要和宿主机在同一个网段
b vagrant up 启动是安装 centos7 网络报错时,进入win7的网络的控制版面/更改适配器设置,将Virtualbox Host-Only Network 和 win7的网络链接 进行 桥接
c centos7 可以ping 通宿主机 和ping 通 外网, 宿主机 可以 ping通 centos- config.vm.network “private_network” 私有网络
- config.vm.network “forwarded_port”, guest: 80, host: 8080, host_ip: “127.0.0.1” centos和宿主机通过端口映射,端口转发
vagrant box的打包分发
vagrant box 可以统一开发环境,将你配置好的环境导出给其他人使用可以得到你一样的环境
01 退出虚拟机
vagrant halt
02 打包
vagrant package --output first-docker-centos7.box
03 得到first-docker-centos7.box
04 将first-docker-centos7.box添加到其他的vagrant环境中
vagrant box add first-docker-centos7 first-docker-centos7.box
05 得到Vagrantfile
vagrant init first-docker-centos7
06 根据Vagrantfile启动虚拟机
vagrant up [此时可以得到和之前一模一样的环境,但是网络要重新配置]
docker安装
# 01 进入centos7
vagrant ssh
# 02 卸载之前的docker
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 03 安装必要的依赖
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
# 04 设置docker仓库 [设置阿里云镜像仓库可以先自行百度,后面课程也会有自己的docker hub讲解]
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# 05 配置阿里云加速器 [访问这个地址https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors]
使用自己的阿里云账号登录,查看菜单栏左下角,发现有一个镜像加速器:
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://uasve2p6.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
# 06 安装docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
# 07 启动docker
sudo systemctl start docker
# 08 测试docker安装是否成功
sudo docker run hello-world
docker基本体验
01 创建tomcat容器
docker pull tomcat
docker run -d --name my-tomcat -p 9090:8080 tomcat
02 创建mysql容器
docker run -d --name my-sql -p 3301:3306 -e \
MYSQL_ROOT_PASSWORD=abc123 --privileged mysql
03 进入容器里面
docker exec -it containerid /bin/bash
docker pull # 拉取镜像到本地
docker run # 根据某个镜像创建容器
-d # 让容器在后台运行,其实就是一个进程
--name # 给容器指定一个名字
-p # 将容器的端口映射到宿主机的端口
-v # 将容器的文件映射到宿主机上 数据卷
docker exec -it # 进入到某个容器中并交互式运行