Last updated
Last updated
Để có thể quản lý vServer với Terraform bạn cần cài đặt Terraform CLI theo hướng dẫn
Để có thể thực hiện quản lý vContainer với Terraform bạn cần tạo Service account từ Root account trên trang chủ IAM (xem hướng dẫn cách tạo Service account và sử dụng IAM ), trong trường hợp này lấy ví dụ cho việc bạn muốn tạo Server với Terraform cần có các quyền (Policy) sau:
CreateServer
GetServer
hoặc bạn có thể cấp quyền vServerFullAccess (nên có quyền vServerReadOnlyAccess để get thông tin resource sau khi tạo xong). Tham khảo thêm về cách phân quyền cho từng Resource, Action tương ứng tại .
Ghi chú: Mỗi hành động quản lý tài nguyên khác nhau sẽ yêu cầu các quyền khác nhau, vì thế điều cần thiết là phải thiết lập một bộ phân quyền hợp lý phù hợp với nhu cầu kinh doanh của bạn.
Sau khi cấp quyền IAM cho account cần sử dụng Terraform, bạn cần tạo một thư mục chứa file Terraform để cài đặt các thông số trên đó, bạn có thể tải file Example từ VNG Cloud repo của chúng tôi tại
Sau khi tải thư mục Example về máy, người dùng mở file (theo đường dẫn /examples/variable.tf), sau đó thay đổi các thông tin cần thiết như sau:
Client_id: Lấy tại trang chủ IAM/ Service account/ Tab Security credentials
Client_secret: Lấy khi khởi tạo Service account tại trang chủ IAM hoặc có thể reset lại tại trang IAM/ Service account/ Tab Security credentials
module "k8s" { source = "./modules/vng-cloud-k8s" }
module "vlb" { source = "./modules/vng-cloud-vlb" }
chỉ để lại:
module "vserver" { source = "./modules/vng-cloud-vserver" }
root_disk_size: chỉ định dung lượng ổ root disk, ví dụ: 20
data_disk_size: chỉ định dung lượng ổ root disk, ví dụ: 50
Bước 7: Khởi chạy terraform command
Hệ thống sẽ trả ra kết quả:
Sau đó, bạn để xem những thay đổi sẽ được áp dụng trên những resource mà terraform đang quản lý bạn có thể chạy:
Cuối cùng bạn chọn chạy dòng lệnh
và chọn YES để thực hiện việc khởi tạo vServer thông qua Terraform
Bước 4: Kiểm tra lại thông tin file (theo đường dẫn examples/main.tf), trường hợp này bạn cần xóa đi các dóng bên dưới:
Bước 5: Sau đó truy cập vào thư mục vng-cloud-vserver/ examples/ modules/ vng-cloud-vserver/, và mở file :
project_id: thông tin project của bạn, bạn có thể lấy ở trên vServer Portal, Ví dụ: pro-462803f3-6858-466f-bf05-df2b33faa360:
image_id: hệ điều hành để khởi tạo vServer ví dụ như: img-b5bf635e-0456-4765-b493-31d5fcfc05aa (1_Ubuntu-22.04x64) ... bạn có thể xem danh sách Id khi tạo vServer trên Portal/
flavor_id: cấu hình vServer mà bạn sẽ khởi tạo ví dụ: flav-e2028a81-cc75-47e4-8af1-9eef2f857f84 (s-general-2x4) ,... bạn có thể xem danh sách khi tạo vServer trên Portal/ .
volume_type_name: chỉ định IOPS cho root disk và data disk, ví dụ: SSD-3000, SSD-200, SSD-400, bạn có thể xem danh sách Volume Type trên vServer Portal/
network_id: chỉ định network id mà vServer sẽ được tạo trên đó, bạn có thể lấy từ tab , nếu chưa khởi tạo bất cứ network nào bạn có thể xem hướng dẫn
subnet_id: chỉ định subnet id mà vServer sẽ được tạo trên đó, bạn có thể lấy từ , nếu chưa khởi tạo bất cứ subnet nào bạn có thể xem hướng dẫn tại
ssh_key_id: chỉ định ssh key sẽ được inject vào vServer, bạn có thể lấy ở , nếu chưa khởi tạo bất kì ssh key nào bạn có thể xem tại :
security_group_id_list: chỉ định danh sách security group id cần gắn vào vServer, bạn có thể lấy ở , nếu cần tạo thêm security group bạn có thể xem tại
Bước 6: Kiểm tra lại thông tin file (theo đường dẫn terraform-provider-vngcloud/examples/modules/vng-cloud-vserver/), trong file chúng tôi để sẵn một số câu lệnh: Create Server, Create Volume, Attach volume to Server, trường hợp này để Tạo mới Server bạn chỉ cần để lại resource Create Server theo hướng dẫn bên dưới:
Sau khi hoàn tất các thông tin trên, để terraform khởi tạo và tải VNG Cloud provider về đồng thời thiết lập các thông tin cần thiết chạy lệnh bên dưới, lưu ý khi chạy cần đứng tại thư mục terraform-provider-vngcloud/ /:
Bước 8: Bạn có thể lên để xem Server đang được khởi tạo từ Terraform:
variable "project_id"
{ type
= string default = "pro-462803f3-6858-466f-bf05-df2b33faa360"}variable "s_general_4x8"
{ type
= string default = "flav-05f97524-0410-46a4-87a8-af92aa759231"}variable "ubuntu_20_04"
{ type
= string default = "img-a34d639b-e070-46ff-8b91-addf4fac45b4"}
variable "ssd_3000"
{ type
= string default = "3000"}variable "root_disk_size"
{ type
= number default = 20}variable "data_disk_size"
{ type
= number default = 50}
variable "network_id"
{ type
= string default = "net-22581aed-a65d-4b1e-86d3-102d68e148e0"}variable "subnet_id"
{ type
= string default = "sub-5f101cba-7ce0-4084-8576-06b8dbfb298a"
variable "ssh_key_id"
{ type
= string default = "ssh-b4fbf87a-d9bc-4f04-9ea1-39e086f443de"}variable "security_group_id_list"
{ type
= list(string) default = [ "secg-28e91c47-11b1-4cc1-8e24-dd174882708d" ]}
data "vngcloud_vserver_volume_type_zone"
"volume_type_zone"
{ name = "SSD" project_id = var.project_id}data "vngcloud_vserver_volume_type"
"volume_type"
{ name = var.ssd_3000 project_id = var.project_id volume_type_zone_id = data.vngcloud_vserver_volume_type_zone.volume_type_zone.id}
resource "vngcloud_vserver_server"
"server"
{ count = var.server_count project_id = var.project_id name = "vngcloud-tinbhn22-${count.index}" encryption_volume = false attach_floating = true flavor_id = var.s_general_4x8 image_id = var.ubuntu_20_04 network_id = var.network_id root_disk_size = var.root_disk_size root_disk_type_id = data.vngcloud_vserver_volume_type.volume_type.id security_group = var.security_group_id_list subnet_id = var.subnet_id action = "start" # user_name = "stackops" # user_password = "Vng@Cloud3030" # expire_password = false # ssh_key = var.ssh_key_id #user_data_base64_encode = var.user_data_base64_encode #user_data = "${data.template_cloudinit_config.user_data.rendered}" lifecycle { create_before_destroy = true }}
terraform init
vnglab:vngcloud cbr09$ terraform init
Initializing the backend...
Initializing provider plugins...- Finding vngcloud/vngcloud
versions matching "0.0.5"...- Installing vngcloud/vngcloud
v0.0.5...- Installed vngcloud/vngcloud
v0.0.5 (self-signed, key ID A6A27B3126EF15EB)
Partner and community providers are signed by their developers.If you'd like to know more
about provider signing, you can read
about it here:https://www.terraform.io/docs/cli/plugins/signing.html
Terraform has created a lock file
.terraform.lock.hcl to record the providerselections it made above. Include this file
in
your version control repositoryso that Terraform can guarantee to make
the same selections by default whenyou run "terraform init"
in
the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan"
to seeany changes that are required for
your infrastructure. All Terraform commandsshould now work.
If you ever set
or change modules or backend configuration for
Terraform,rerun this command
to reinitialize your working directory. If you forget, othercommands will detect it and remind you to do
so if
necessary.
terraform plan
terraform apply
terraform { required_providers { vngcloud = { source
= "vngcloud/vngcloud" version = "1.1.0" } } # backend "s3" { # skip_credentials_validation = true # skip_metadata_api_check = true # skip_region_validation = true # bucket = "bucket-name" # endpoint = "
" # key = "terraform.tfstate" # region = "HCM01" # access_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # }}
provider "vngcloud"
{ token_url = "
" client_id = var.client_id client_secret = var.client_secret vserver_base_url = "
" vlb_base_url = "
"}
module "vserver"
{ source
= "./modules/vng-cloud-vserver"}