Saltstack 简明教程
SaltStack - Salt for Cloud Infrastructure
Salt 提供了一个单独的模块 Salt Cloud ,以便提供 Amazon AWS、Google Compute 等各种云提供商的虚拟机。Salt Cloud 是一个用于配置和管理各种云提供商的 VM 的通用接口。
Salt provides a separate module, Salt Cloud to provide virtual machines of various cloud providers like Amazon AWS, Google Compute, etc. Salt Cloud is a generic interface to configure and manage VMs of various cloud providers.
-
Cloud Config − The main configuration file for Salt Cloud is /etc/salt/cloud and it applies to all the VMs. The main configuration file is called Cloud Config.
-
Cloud Providers − To target specific cloud providers, we can use specific configuration file and they are located in /etc/salt/cloud.providers.d/.conf, these are called as *Cloud Providers.
-
Cloud Profiles − To target a specific VM, we can also use a special configuration file, which applies to that particular VM only and they are located at /etc/salt/cloud.profiles.d/.conf, these are called as *Cloud Profiles.
对于虚拟机,首先应用 Cloud Config 中的设置,然后在 Cloud Providers 中应用,最后由 Cloud Profiles 覆盖。
For a virtual machine, settings in the Cloud Config is applied first and then in the Cloud Providers and finally override is done by the Cloud Profiles.
Installation of Salt Cloud
默认情况下,Salt Cloud 内置于 Salt 中,并且可以随时使用。如果没有,我们可以使用以下命令进行安装。
By default, Salt Cloud is built into the Salt and is readily available. If it is not available, we can install it by using the command below.
pip install salt-cloud
由于 Salt Cloud 是一個單獨的模塊並且在其自己的進程中運行,因此也可以將其安裝在 Salt Minion 系統中,而不是 Salt Master 中。
Since Salt Cloud is a separate module and runs in its own process, it can be installed in the Salt Minion system as well instead of the Salt Master.
Provisioning a Virtual Machine
要置備虛擬機,我們需要定義雲提供商和雲配置文件。一旦創建了這兩者,我們就可以置備一個新的虛擬機來使用它。
To provision a virtual machine, we need to define a cloud provider and a cloud profile. Once both are created, we can provision a new virtual machine for using it.
Cloud Provider
雲主機信息在雲提供商配置文件中配置。通常,需要配置的基本信息是雲驅動程序、用戶名、密碼、私鑰等。讓我們創建一個名為 my-amazon-cloud 的新雲提供商。
Cloud host information are configured in the Cloud provider configuration file. Normally, the basic information, which needs to be configured are cloud driver, username, password, private key, etc. Let us create a new cloud provider named as my-amazon-cloud.
-
Create a file, my-amazon-cloud.conf under /etc/salt/cloud.providers.d/
-
Add a new provider using the ec2 driver.
my-amazon-cloud:
driver: ec2
id: '<AWS_ID>'
key: '<AWS_KEY>'
private_key: /path/to/privatekey.pem
keyname: <name of the key>
securitygroup: default
minion:
master: <master server>
Salt 立即為各種雲主機提供驅動程序,例如 GoGrid、HP Cloud、Google Compute Engine (GCE)、Amazon AWS、Joyent、Linode、OpenNebula、ProfitBricks、Proxmox、Saltify、VexxHost、VMWare 等。
Salt provides drivers for various cloud host out of the box, such as GoGrid, HP Cloud, Google Compute Engine (GCE), Amazon AWS, Joyent, Linode, OpenNebula, ProfitBricks, Proxmox, Saltify, VexxHost, VMWare, etc.
配置雲提供商後,我們可以查詢提供商的可用位置、可用機器映像及其各種大小。
Once the cloud providers are configured, we can query the available location of the provider, available machine images, and its various sizes.
salt-cloud --list-location my-amazon-cloud
salt-cloud --list-images my-amazon-cloud
salt-cloud --list-sizes my-amazon-cloud
Cloud Profile
雲配置文件指定虛擬機映像和大小。它可以在 /etc/salt/cloud.profiles.d/ 下配置。讓我們創建一個簡單的配置文件 simple.conf 。
A Cloud profile specifies the virtual machine image and size. It can be configured under – /etc/salt/cloud.profiles.d/. Let us create a simple profile, simple.conf.
aws_micro:
provider: my-amazon-cloud
image: <image_id>
size: <machine_id e.g. t1.micro>
Virtual Machine
配置好提供程序和配置文件後,我們可以像下面這樣輕鬆地提供一個虛擬機,使用 Salt Cloud。
Once the provider and profiles are configured, we can easily provide a virtual machine using the salt-cloud as shown below.
salt-cloud -p aws_micro master minion1 minion2
其中, p - master、minion1 和 minion2 的配置文件名是新的虛擬機。
Where, p – Profile name master, minion1 and minion2 are the new virtual machines.
可以使用以下命令獲取新創建的虛擬機的詳細信息。
The details of the newly created virtual machine can be obtained by using the following command.
salt-cloud --query
可以使用以下命令銷毀虛擬機 −
The virtual machines can be destroyed using the following command −
slat-cloud -d master minion1
Cloud Map
雲映射是一種特殊格式,可一次創建多個虛擬機。地圖文件的格式是指定配置文件,然後在它下面添加一個虛擬機的列表。
A Cloud Map is a special format to create multiple virtual machines at once. The format of the map file is to specify the profile and then add a list of the virtual machines under it.
示例映射文件如下 −
A sample map file is as follows −
micro:
- web1
- web2
large:
- db1
- db2
地圖文件可以作為參數傳遞給 salt-cloud 命令,以創建虛擬機,如下所示 −
The map file can be passed as an argument to the salt-cloud command to create the virtual machine as follows −
salt-cloud -m /path/to/mapfile