2023-02-06 22:50:36 +08:00
|
|
|
|
[#]: subject: "Use Terraform to manage an OpenStack cluster"
|
|
|
|
|
[#]: via: "https://opensource.com/article/23/1/terraform-manage-openstack-cluster"
|
|
|
|
|
[#]: author: "AJ Canlas https://opensource.com/users/ajscanlas"
|
|
|
|
|
[#]: collector: "lkxed"
|
2023-02-22 08:50:30 +08:00
|
|
|
|
[#]: translator: "geekpi"
|
2023-03-01 11:53:43 +08:00
|
|
|
|
[#]: reviewer: "wxy"
|
|
|
|
|
[#]: publisher: "wxy"
|
|
|
|
|
[#]: url: "https://linux.cn/article-15586-1.html"
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
使用 Terraform 来管理 OpenStack 集群
|
2023-02-06 22:50:36 +08:00
|
|
|
|
======
|
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
> Terraform 是一种声明性语言,可以作为你正在建设的基础设施的蓝图。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
在拥有一个 OpenStack 生产环境和家庭实验室一段时间后,我可以肯定地说,从管理员和租户的角度置备工作负载和管理它是很重要的。
|
|
|
|
|
|
|
|
|
|
Terraform 是一个开源的基础设施即代码(IaC)软件工具,用于 <ruby>置备<rt>provisioning</rt></ruby>网络、服务器、云平台等。Terraform 是一种声明性语言,可以作为你正在建设的基础设施的蓝图。你可以用 Git 来管理它,它有一个强大的 [GitOps][1] 使用场景。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
本文介绍了使用 Terraform 管理 OpenStack 集群的基础知识。我使用 Terraform 重新创建了 OpenStack 演示项目。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
### 安装 Terraform
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
我使用 CentOS 作为跳板机运行 Terraform。根据官方文档,第一步是添加 Hashicorp 仓库:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
2023-03-01 11:53:43 +08:00
|
|
|
|
$ sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
|
2023-02-06 22:50:36 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
接下来,安装 Terraform:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ sudo dnf install terraform -y
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
验证安装:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ terraform –version
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
如果你看到返回的版本号,那么你已经安装了 Terraform。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
### 为 OpenStack 提供者创建一个 Terraform 脚本
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
在 Terraform 中,你需要一个 <ruby>提供者<rt>provider</rt></ruby>,它是一个转换器,Terraform 调用它将你的 `.tf` 转换为对你正在协调的平台的 API 调用。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
有三种类型的提供者:官方、合作伙伴和社区:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
- 官方提供者由 Hashicorp 维护。
|
|
|
|
|
- 合作伙伴提供者由与 Hashicorp 合作的技术公司维护。
|
|
|
|
|
- 社区提供者是由开源社区成员维护的。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
在这个 [链接][2] 中有一个很好的 OpenStack 的社区提供者。要使用这个提供者,请创建一个 `.tf` 文件,并命名为 `main.tf`。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ vi main.tf
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
在 `main.tf` 中添加以下内容:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
terraform {
|
|
|
|
|
required_version = ">= 0.14.0"
|
|
|
|
|
required_providers {
|
|
|
|
|
openstack = {
|
|
|
|
|
source = "terraform-provider-openstack/openstack"
|
|
|
|
|
version = "1.49.0"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
provider "openstack" {
|
|
|
|
|
user_name = “OS_USERNAME”
|
|
|
|
|
tenant_name = “OS_TENANT”
|
|
|
|
|
password = “OS_PASSWORD”
|
|
|
|
|
auth_url = “OS_AUTH_URL”
|
|
|
|
|
region = “OS_REGION”
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
你需要修改 `OS_USERNAME`、`OS_TENANT`、`OS_PASSWORD`、`OS_AUTH_URL` 和 `OS_REGION` 变量才能工作。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
### 创建一个 Terraform 管理文件
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
OpenStack 管理文件的重点是置备外部网络、路由、用户、镜像、租户配置文件和配额。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
此示例提供风格,连接到外部网络的路由、测试镜像、租户配置文件和用户。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
首先,为置备资源创建一个 `AdminTF` 目录:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ mkdir AdminTF
|
|
|
|
|
|
|
|
|
|
$ cd AdminTF
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
在 `main.tf` 中,添加以下内容:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
terraform {
|
|
|
|
|
required_version = ">= 0.14.0"
|
|
|
|
|
required_providers {
|
|
|
|
|
openstack = {
|
|
|
|
|
source = "terraform-provider-openstack/openstack"
|
|
|
|
|
version = "1.49.0"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
provider "openstack" {
|
|
|
|
|
user_name = “OS_USERNAME”
|
|
|
|
|
tenant_name = “admin”
|
|
|
|
|
password = “OS_PASSWORD”
|
|
|
|
|
auth_url = “OS_AUTH_URL”
|
|
|
|
|
region = “OS_REGION”
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "openstack_compute_flavor_v2" "small-flavor" {
|
|
|
|
|
name = "small"
|
|
|
|
|
ram = "4096"
|
|
|
|
|
vcpus = "1"
|
|
|
|
|
disk = "0"
|
|
|
|
|
flavor_id = "1"
|
|
|
|
|
is_public = "true"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "openstack_compute_flavor_v2" "medium-flavor" {
|
|
|
|
|
name = "medium"
|
|
|
|
|
ram = "8192"
|
|
|
|
|
vcpus = "2"
|
|
|
|
|
disk = "0"
|
|
|
|
|
flavor_id = "2"
|
|
|
|
|
is_public = "true"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "openstack_compute_flavor_v2" "large-flavor" {
|
|
|
|
|
name = "large"
|
|
|
|
|
ram = "16384"
|
|
|
|
|
vcpus = "4"
|
|
|
|
|
disk = "0"
|
|
|
|
|
flavor_id = "3"
|
|
|
|
|
is_public = "true"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "openstack_compute_flavor_v2" "xlarge-flavor" {
|
|
|
|
|
name = "xlarge"
|
|
|
|
|
ram = "32768"
|
|
|
|
|
vcpus = "8"
|
|
|
|
|
disk = "0"
|
|
|
|
|
flavor_id = "4"
|
|
|
|
|
is_public = "true"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "openstack_networking_network_v2" "external-network" {
|
|
|
|
|
name = "external-network"
|
|
|
|
|
admin_state_up = "true"
|
|
|
|
|
external = "true"
|
|
|
|
|
segments {
|
|
|
|
|
network_type = "flat"
|
|
|
|
|
physical_network = "physnet1"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "openstack_networking_subnet_v2" "external-subnet" {
|
|
|
|
|
name = "external-subnet"
|
|
|
|
|
network_id = openstack_networking_network_v2.external-network.id
|
|
|
|
|
cidr = "10.0.0.0/8"
|
|
|
|
|
gateway_ip = "10.0.0.1"
|
|
|
|
|
dns_nameservers = ["10.0.0.254", "10.0.0.253"]
|
|
|
|
|
allocation_pool {
|
|
|
|
|
start = "10.0.0.1"
|
|
|
|
|
end = "10.0.254.254"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "openstack_networking_router_v2" "external-router" {
|
|
|
|
|
name = "external-router"
|
|
|
|
|
admin_state_up = true
|
|
|
|
|
external_network_id = openstack_networking_network_v2.external-network.id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "openstack_images_image_v2" "cirros" {
|
|
|
|
|
name = "cirros"
|
|
|
|
|
image_source_url = "https://download.cirros-cloud.net/0.6.1/cirros-0.6.1-x86_64-disk.img"
|
|
|
|
|
container_format = "bare"
|
|
|
|
|
disk_format = "qcow2"
|
|
|
|
|
|
|
|
|
|
properties = {
|
|
|
|
|
key = "value"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "openstack_identity_project_v3" "demo-project" {
|
|
|
|
|
name = "Demo"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "openstack_identity_user_v3" "demo-user" {
|
|
|
|
|
name = "demo-user"
|
|
|
|
|
default_project_id = openstack_identity_project_v3.demo-project.id
|
|
|
|
|
password = "demo"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
### 创建一个租户 Terraform 文件
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
作为一个 <ruby>租户<rt>Tenant</rt></ruby>,你通常会创建虚拟机。你还为这些虚拟机创建网络和安全组。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
这个例子使用上面由 Admin 文件创建的用户。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
首先,创建一个 `TenantTF` 目录,用于与租户相关的置备:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ mkdir TenantTF
|
|
|
|
|
$ cd TenantTF
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
在 `main.tf` 中,添加以下内容:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
terraform {
|
|
|
|
|
required_version = ">= 0.14.0"
|
|
|
|
|
required_providers {
|
|
|
|
|
openstack = {
|
|
|
|
|
source = "terraform-provider-openstack/openstack"
|
|
|
|
|
version = "1.49.0"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
provider "openstack" {
|
|
|
|
|
user_name = “demo-user”
|
|
|
|
|
tenant_name = “demo”
|
|
|
|
|
password = “demo”
|
|
|
|
|
auth_url = “OS_AUTH_URL”
|
|
|
|
|
region = “OS_REGION”
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "openstack_compute_keypair_v2" "demo-keypair" {
|
|
|
|
|
name = "demo-key"
|
|
|
|
|
public_key = "ssh-rsa ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resource "openstack_networking_network_v2" "demo-network" {
|
|
|
|
|
name = "demo-network"
|
|
|
|
|
admin_state_up = "true"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "openstack_networking_subnet_v2" "demo-subnet" {
|
|
|
|
|
network_id = openstack_networking_network_v2.demo-network.id
|
|
|
|
|
name = "demo-subnet"
|
|
|
|
|
cidr = "192.168.26.0/24"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "openstack_networking_router_interface_v2" "demo-router-interface" {
|
|
|
|
|
router_id = “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”
|
|
|
|
|
subnet_id = openstack_networking_subnet_v2.demo-subnet.id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "openstack_compute_instance_v2" "demo-instance" {
|
|
|
|
|
name = "demo"
|
|
|
|
|
image_id = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
|
|
|
|
|
flavor_id = "3"
|
|
|
|
|
key_pair = "demo-key"
|
|
|
|
|
security_groups = ["default"]
|
|
|
|
|
|
|
|
|
|
metadata = {
|
|
|
|
|
this = "that"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
network {
|
|
|
|
|
name = "demo-network"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
### 初始化你的 Terraform
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
创建 Terraform 文件后,你需要初始化 Terraform。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
对于管理员:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ cd AdminTF
|
|
|
|
|
|
|
|
|
|
$ terraform init
|
|
|
|
|
|
|
|
|
|
$ terraform fmt
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
对于租户:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ cd TenantTF
|
|
|
|
|
|
|
|
|
|
$ terraform init
|
|
|
|
|
|
|
|
|
|
$ terraform fmt
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
命令解释:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
- `terraform init` 从镜像源下载提供者用于置备此项目。
|
2023-02-24 08:56:51 +08:00
|
|
|
|
- `terraform fmt` 格式化文件,以便在仓库中使用。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
### 创建一个 Terraform 计划
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
接下来,为你创建一个 <ruby>计划<rt>plan</rt></ruby>,看看将创建哪些资源。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
对于管理员:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ cd AdminTF
|
|
|
|
|
|
|
|
|
|
$ terraform validate
|
|
|
|
|
|
|
|
|
|
$ terraform plan
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
对于租户:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ cd TenantTF
|
|
|
|
|
|
|
|
|
|
$ terraform validate
|
|
|
|
|
|
|
|
|
|
$ terraform plan
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
命令解释:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
- `terraform validate` 验证 `.tf` 语法是否正确。
|
|
|
|
|
- `terraform plan` 在缓存中创建一个计划文件,所有管理的资源在创建和销毁时都可以被跟踪。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
### 应用你的第一个 TF
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
要部署资源,使用 `terraform apply` 命令。该命令应用计划文件中的所有资源状态。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
对于管理员:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ cd AdminTF
|
|
|
|
|
|
|
|
|
|
$ terraform apply
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
对于租户:
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ cd TenantTF
|
|
|
|
|
|
|
|
|
|
$ terraform apply
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 08:56:51 +08:00
|
|
|
|
### 接下来的步骤
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
2023-03-01 11:53:43 +08:00
|
|
|
|
之前,我写了一篇关于在树莓派上部署最小 OpenStack 集群的 [文章][3]。你可以找到更详细的 [Terraform 和 Ansible][4] 配置,并通过 GitLab 实现一些 CI/CD。
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
via: https://opensource.com/article/23/1/terraform-manage-openstack-cluster
|
|
|
|
|
|
|
|
|
|
作者:[AJ Canlas][a]
|
|
|
|
|
选题:[lkxed][b]
|
2023-02-24 08:56:51 +08:00
|
|
|
|
译者:[geekpi](https://github.com/geekpi)
|
2023-03-01 11:53:43 +08:00
|
|
|
|
校对:[wxy](https://github.com/wxy)
|
2023-02-06 22:50:36 +08:00
|
|
|
|
|
|
|
|
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
|
|
|
|
|
|
|
|
[a]: https://opensource.com/users/ajscanlas
|
|
|
|
|
[b]: https://github.com/lkxed
|
|
|
|
|
[1]: https://opensource.com/article/21/3/gitops
|
|
|
|
|
[2]: https://registry.terraform.io/providers/terraform-provider-openstack/openstack/1.49.0
|
|
|
|
|
[3]: https://opensource.com/article/20/12/openstack-raspberry-pi
|
|
|
|
|
[4]: https://www.ansible.com/blog/ansible-vs.-terraform-demystified?intcmp=7013a000002qLH8AAM
|
2023-03-01 11:53:43 +08:00
|
|
|
|
[0]: https://img.linux.net.cn/data/attachment/album/202303/01/114855zdkhdhsdoojmrqx2.jpg
|