創建一個支持學習和實驗新軟件的環境。node
可以構建和拆解公有云環境是很是有用的,但咱們大多數人都不能輕鬆訪問公有云。退而求其次的最好辦法就是在本地機器上創建一個實驗室,但即便在本地機器上運行也會帶來性能、靈活性和其餘挑戰。大多數時候,本地機器上額外的工做負載會干擾咱們平常的工做,它們固然也會影響你提供一個現成的環境來玩耍和實驗新軟件。python
幾年前,當我和個人團隊開始學習 Ansible 時,咱們就遇到了這個挑戰。咱們找不到一個能夠單獨使用的環境,咱們對這種狀況的失望致使咱們中的一些人中止了實驗。咱們知道須要找到一個解決方案。linux
咱們花了不少時間研究各類方案,得出了一套工具,使咱們的好奇心可以在咱們徹底控制的環境中學習。咱們能夠在本地機器上輪換和拆解實驗室環境,而不須要訪問內部實驗室或公共雲。git
本文將解釋如何在 20 分鐘內以徹底自動化的方式在本地機器上部署本身的實驗室環境。github
你能夠在個人 GitHub 倉庫中找到這個練習的全部代碼。web
工具和軟件
本方案使用如下工具和軟件:shell
- Ansible 是咱們選擇的自動化工具,由於它易於使用,並且足夠靈活,能夠知足實驗室的要求。
- Vagrant 易於使用,用於構建和維護虛擬機。
- VirtualBox 是一個託管管理程序,能夠在 Windows 和 Linux 環境中使用。
- Fedora v30+ 是我本地機器上的操做系統。
你必須進行如下設置才能創建環境:bootstrap
- 一個互聯網鏈接
- 在 BIOS 中啓用虛擬化技術支持(如下是在個人聯想筆記本上的過程)
- Vagrant v2.2.9
- 最新版本的 Ansible
- 最新版本的 VirtualBox
- Fedora v30+ 宿主機操做系統
這個實驗室環境有什麼?
這個項目旨在部署一個帶有 Ansible 引擎和多個 Linux 節點的 Ansible 主機,以及一些預加載和預配置的應用程序(httpd 和 MySQL)。它還啓用了 Cockpit,這樣你就能夠在測試過程當中監控虛擬機(VM)的狀態。使用預部署的應用程序的緣由是爲了提升效率(因此你沒必要花時間安裝這些組件)。這樣你就能夠專一於建立角色和劇本,並針對上述工具部署的環境進行測試。centos
咱們肯定,對於咱們的用例來講,最好的方案是多機 Vagrant 環境。Vagrant 文件建立了三個 CentOS 虛擬機,以模擬兩個目標主機和一個 Ansible 控制機。ruby
- 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
