创建一个支持学习和实验新软件的环境。
- 来源:https://linux.cn/article-12970-1.html
- 做者:Mike Calizo
- 译者:Xingyu.Wang
(本文字数:7596,阅读时长大约:9 分钟)html
可以构建和拆解公有云环境是很是有用的,但咱们大多数人都不能轻松访问公有云。退而求其次的最好办法就是在本地机器上创建一个实验室,但即便在本地机器上运行也会带来性能、灵活性和其余挑战。大多数时候,本地机器上额外的工做负载会干扰咱们平常的工做,它们固然也会影响你提供一个现成的环境来玩耍和实验新软件。node
几年前,当我和个人团队开始学习 Ansible 时,咱们就遇到了这个挑战。咱们找不到一个能够单独使用的环境,咱们对这种状况的失望致使咱们中的一些人中止了实验。咱们知道须要找到一个解决方案。python
咱们花了不少时间研究各类方案,得出了一套工具,使咱们的好奇心可以在咱们彻底控制的环境中学习。咱们能够在本地机器上轮换和拆解实验室环境,而不须要访问内部实验室或公共云。linux
本文将解释如何在 20 分钟内以彻底自动化的方式在本地机器上部署本身的实验室环境。git
你能够在个人 GitHub 仓库中找到这个练习的全部代码。github
工具和软件
本方案使用如下工具和软件:web
- Ansible 是咱们选择的自动化工具,由于它易于使用,并且足够灵活,能够知足实验室的要求。
- Vagrant 易于使用,用于构建和维护虚拟机。
- VirtualBox 是一个托管管理程序,能够在 Windows 和 Linux 环境中使用。
- Fedora v30+ 是我本地机器上的操做系统。
你必须进行如下设置才能创建环境:shell
- 一个互联网链接
- 在 BIOS 中启用虚拟化技术支持(如下是在个人联想笔记本上的过程)
- Vagrant v2.2.9
- 最新版本的 Ansible
- 最新版本的 VirtualBox
- Fedora v30+ 宿主机操做系统
这个实验室环境有什么?
这个项目旨在部署一个带有 Ansible 引擎和多个 Linux 节点的 Ansible 主机,以及一些预加载和预配置的应用程序(httpd 和 MySQL)。它还启用了 Cockpit,这样你就能够在测试过程当中监控虚拟机(VM)的状态。使用预部署的应用程序的缘由是为了提升效率(因此你没必要花时间安装这些组件)。这样你就能够专一于建立角色和剧本,并针对上述工具部署的环境进行测试。bootstrap
咱们肯定,对于咱们的用例来讲,最好的方案是多机 Vagrant 环境。Vagrant 文件建立了三个 CentOS 虚拟机,以模拟两个目标主机和一个 Ansible 控制机。centos
- Host1: 没有图形用户界面(GUI),安装 httpd 和 MySQL
- Host2: 没有 GUI,安装了 httpd 和 MySQL
- Ansible-host:没有 GUI,安装了 Ansible 引擎
启用多个管理程序
若是使用了多个管理程序,一些管理程序可能不容许你拉起虚拟机。要解决这个问题,请遵循如下步骤(基于 Vagrant 的安装说明)。
首先,找出管理程序的名称:
$ lsmod | grep kvm
kvm_intel 204800 6
kvm 593920 1 kvm_intel
irqbypass 16384 1 kvm
我感兴趣的是 kvm_intel
,但你可能须要另外一个(好比 kvm_amd
)。
以 root 身份运行如下内容,将该管理程序列入黑名单:
$ echo 'blacklist kvm-intel' >> /etc/modprobe.d/blacklist.conf
从新启动你的机器并尝试再次运行 Vagrant。
Vagrant 文件
cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
# Define VMs with static private IP addresses, vcpu, memory and vagrant-box.
boxes = [
{
:name => "web1.demo.com", ⇒ Host1 this is one of the target nodes
:box => "centos/8", ⇒ OS version
:ram => 1024, ⇒ Allocated memory
:vcpu => 1, ⇒ Allocated CPU
:ip => "192.168.29.2" ⇒ Allocated IP address of the node
},
{
:name => "web2.demo.com", ⇒ Host2 this is one of the target nodes
:box => "centos/8",
:ram => 1024,
:vcpu => 1,
:ip => "192.168.29.3"
},
{
:name => "ansible-host", ⇒ Ansible Host with Ansible Engine
:box => "centos/8",
:ram => 8048,
:vcpu => 1,
:ip => "192.168.29.4"
}
]
# Provision each of the VMs.
boxes.each do |opts|
config.vm.define opts[:name] do |config|
# Only Enable this if you are connecting to Proxy server
# config.proxy.http = "http://usernam:password@x.y:80"⇒ Needed if you have a proxy
# config.proxy.https = "http://usernam:password@x.y:80"
# config.proxy.no_proxy = "localhost,127.0.0.1"
config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true
config.ssh.insert_key = false
config.vm.box = opts[:box]
config.vm.hostname = opts[:name]
config.vm.provider :virtualbox do |v| ⇒ Defines the vagrant provider
v.memory = opts[:ram]
v.cpus = opts[:vcpu]
end
config.vm.network :private_network, ip: opts[:ip]
config.vm.provision :file do |file|
file.source = './keys/vagrant' ⇒ vagrant keys to allow access to the nodes
file.destination = '/tmp/vagrant' ⇒ the location to copy the vagrant key
end
config.vm.provision :shell, path: "bootstrap-node.sh" ⇒ script that copy hosts entry
config.vm.provision :ansible do |ansible| ⇒ declaration to run ansible playbook
ansible.verbose = "v"
ansible.playbook = "playbook.yml" ⇒ the playbook used to configure the hosts
end
end
end
end
这些是你须要注意的重要文件。
inventory-test.yaml
:链接到节点的清单文件playbook.yaml
:Vagrant 供应者调用的用于配置节点的剧本文件- `Vagrantfile':Vagrant 用来部署环境的文件
- Vagrant 密钥文件:链接实验室环境中各节点的 Vagrant 密钥
你能够根据你的须要调整这些文件。Ansible 的灵活性使你有能力根据你的须要声明性地改变你的环境。
部署你的实验室环境
首先,克隆这个 GitHub 仓库 中的代码:
$ git clone https://github.com/mikecali/ansible-labs-101.git
Cloning into 'ansible-labs-101'...
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 15 (delta 2), reused 10 (delta 0), pack-reused 0
Unpacking objects: 100% (15/15), 6.82 KiB | 634.00 KiB/s, done.
接下来,将你的目录改成 vagrant-session-2
,并查看其内容:
$ ls
Bootstrap-node.sh inventory keys playbook.yml README.md Vagrantfile
如今你已经拥有了实验室环境所需的全部工件和配置文件。要部署环境,请运行:
$ vagrant up
只要有一个像样的网络链接,只须要 20 分钟左右就能够获得一个运行环境:
$ vagrant up
Bringing machine 'web1.demo.com' up with 'virtualbox' provider...
Bringing machine 'web2.demo.com' up with 'virtualbox' provider...
Bringing machine 'ansible-host' up with 'virtualbox' provider...
==> web1.demo.com: Importing base box 'centos/8'...
==> web1.demo.com: Matching MAC address for NAT networking...
==> web1.demo.com: Checking if box 'centos/8' version '1905.1' is up to date...
==> web1.demo.com: Setting the name of the VM: ansible-labs_web1democom_1606434176593_70913
==> web1.demo.com: Clearing any previously set network interfaces...
==> web1.demo.com: Preparing network interfaces based on configuration...
web1.demo.com: Adapter 1: nat
web1.demo.com: Adapter 2: hostonly
==> web1.demo.com: Forwarding ports...
web1.demo.com: 22 (guest) => 2222 (host) (adapter 1)
==> web1.demo.com: Running 'pre-boot' VM customizations...
==> web1.demo.com: Booting VM...
==> web1.demo.com: Waiting for machine to boot. This may take a few minutes...
web1.demo.com: SSH address: 127.0.0.1:2222
web1.demo.com: SSH username: vagrant
web1.demo.com: SSH auth method: private key
[...]
一旦该剧本执行完成,你会看到这样的输出:
PLAY RECAP *********************************
Ansible-host : ok=20 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=3
Real 18m14.288s
User 2m26.978s
Sys 0m26.849s
确认全部虚拟机都在运行:
$ vagrant status
Current machine states:
Web1.demo.com running (virtualbox)
Web2.demo.com running (virtualbox)
ansible-host running (virtualbox)
[...]
你能够经过登陆其中一个虚拟机进一步调查。访问 ansible-host
:
> vagrant ssh ansible-host
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Thu Nov 26 12:21:23 2020 from 10.0.2.2
[vagrant@ansible-host ~] uptime
16:46:42 up 1:24, 1 user, load average: 0.00, 0.01, 0.04
最后,你可使用 Ansible 模块来 ping 你建立的其余节点:
[vagrant@ansible-host]$ ansible -i inventory-test.yaml \
webservers -m ping -u vagrant
192.168.29.2 | SUCCESS => {
"Ansible-facts": {
"Discovered_interpreter_python": "/usr/libexec/platform-python"
},
"Changed": false;
"Ping": "pong"
}
[...]
清理
运行以下命令来清理环境:
$ vagrant destroy [vagrant machine name]
你的输出会像这样:


有创意的学习
在本身的实验室里利用本身的时间学习 Ansible 这样的软件是一个好习惯,但因为受到没法控制的限制,可能会很困难。
有时候,你须要发挥创意,找到另外一种方法。在开源社区中,你能够选择不少方案;咱们选择这些工具的主要缘由之一是,它们是许多人经常使用和熟悉的。
另外,请注意,这些剧本并无按照个人要求进行优化。请随时改进它们,并在评论中分享你的工做。
via: https://opensource.com/article/20/12/ansible-lab
做者:Mike Calizo 选题:lujun9972 译者:wxy 校对:wxy