NCS - Documents
Go to Portal
  • NIPA Cloud Space Overview
    • Welcome to NIPA Cloud Space documentation
    • NCS User Account
      • Create NIPA Cloud Space Account
      • Loging in to Nipa Cloud Space
      • Reset NCS Password
      • Activating Two-Factor Authentication
      • Deactivating Two-Factor Authentication
    • Co-working Projects
      • Create Co-working Project
      • Manage Project Member
      • Exporting Resource List
    • Billing & Wallet
      • Topup a Project Wallet
      • Redeem Voucher
      • Insufficient Wallet Balance
  • COMPUTE
    • Compute Instance
      • What is OS Status?
      • Launching Compute Instance
      • Managing Instance
        • Stop
        • Start
        • Restart
        • Resize (Change Machine Type)
        • Take Snapshot
        • Auto Backup
        • Reset Linux "root" Password
        • Reset Windows "Administrator" Password
      • SSH to Linux Compute Instance
        • Converting Key Pair for PuTTy
        • Windows Client using Key Pair
        • Windows Client using Password
        • MacOS/Linux using Key Pairs
        • MacOS/Linux using Password
      • Remote Desktop to Windows Instance
      • Setting Docker Image Caching
      • Renaming Instance
      • Exporting Instance List
      • How to change Compute Instance hostname
      • How to install QEMU Guest Agent
      • How to fix update kernel for RHEL
      • How to Update Rocky Linux 9
      • How to Upgrade Rocky Linux to 9.4
      • How to change RDP port on Windows
    • Compute Image
      • Create Image From a Bootable Volume
      • Importing Your Own Image
      • Export Image
      • Share Image to Between Projects
      • Exporting Image List
    • Key Pair
      • Managing Key Pair(s)
      • Creating a New Key Pair
      • Importing an Existing Key Pair
    • Deployment Script
      • Managing Deployment Script
      • Create a Deployment Script
      • Clone a Deployment Script
      • Edit a Deployment Script
      • Delete a Deployment Script
  • STORAGE
    • Block Storage
      • Managing Volume(s)
        • Create From Blank
        • Create From an Image
        • Create From a Volume
        • Create From a Snapshot
        • Transferring a Volume
        • Accepting a Tranferred Volume
        • Renaming a Volume
        • How to Change Volume Type
      • Managing Snapshot(s)
        • Create a Snapshot
        • Renaming a Volume Snapshot
      • Exporting Volume and Volume Snapshot List
    • Object Storage (S3)
      • Migrate file S3 AWS to S3 NIPA
      • Create an Object Storage Bucket
      • Delete an Object Storage Bucket
      • Create an Object Storage Sub-User
      • Regenerate Sub-User's Access Key
      • Revoke Sub-User's Access Key
      • Create Bucket Policy
      • Bucket Versioning
      • Access S3 Bucket with Cyberduck
        • Upload Files to a Bucket
        • Share File via Public Link
      • Access S3 Bucket with s3cmd
        • Basic command
        • การ set ACL สำหรับการเปิดใช้งาน Objects แบบ public
        • การสร้าง Presigned URL สำหรับการใช้งานชั่วคราว
      • Mount S3 Bucket on instances with s3fs-fuse
      • Mount the S3 bucket on the Windows
      • Access S3 buckets With AWS S3 Client SDK
        • S3Client Configuration
        • Basic Command
        • Multipart Upload
      • Access S3 buckets with internal network for NCS instance
      • Delete Lifecycle Policies
      • Move Objects Lifecycle Script
      • Configure a static website using S3 Bucket
    • NIPA Drive
      • Purchasing a Drive
  • NETWORKING
    • Networking
      • Managing VPC Network(s)
        • Create a Network
        • DHCP Setting
        • Create Port
        • Create Router
      • Managing Security Group(s)
        • Create a New Security Group
        • Create Security Group Presets
      • Managing External IP(s)
        • Create an External IP
        • Exporting External IP List
      • NAT Gateway with Ubuntu (VM) แบบ Host Route
      • NAT and VPN Gateway on NCS with Pfsense-2.6.0
  • LOAD BALANCING
    • Load Balancer as a Service
      • Create Load Balancer
      • Using Network Load Balancing
      • Using Application Load Balancing
      • Renaming a Load Balancer
      • Exporting Load Balancer List
      • Monitoring Load Balancer Using Prometheus
    • SSL Certificate
      • Import SSL Certificate
  • DATABASE AS A SERVICE
    • SQL Database
      • Create SQL Database Instance
        • Create MySQL Database Instance
      • Manage SQL Database Instance
        • Reboot Database Service
        • Delete Database Instance
        • Online Extend Storage Size
        • Edit Allowed CIDR
      • Auto-Scaling SQL Database Storage
        • Enable Auto-Scaling
        • Disable Auto-Scaling
        • Edit Auto-Scaling
      • Manage SQL Database Root User
        • Enable Root User
        • Reset Root User Password
      • Manage SQL Database Schema
        • Create Database Schema
        • Delete Database Schema
      • Manage SQL Database User
        • Create Database User
        • Delete Database User
        • Reset Password
        • Edit Access
      • Manage SQL Database Backup
        • Create Backup
        • Create A New SQL Database Instance From Backup
        • Delete Backup
      • Manage SQL Database Logs
        • Enable Logs
        • Disable Logs
        • Refresh Logs
        • Load More Logs
      • Manage Monitoring User
        • Create Monitoring User
        • Delete Monitoring User
      • Monitor SQL Database with Percona Monitoring and Management (PMM)
  • SCHEDULING
    • Schedules
      • Create Schedule
    • Jobs
  • Public API
    • What is NCS Public API ?
      • Download NCS Project RC File
      • Getting Start with NCS Public API
        • Using OpenStack Client Tool
        • Using REST API
        • Terraform with Openstack
        • Auto-scaling OpenStack Instances with Senlin and Prometheus
          • Installation Prometheus
          • Installation Alertmanager
  • MIGRATION
    • Migrating Linux VM from vSphere to NCS
    • Migrating Windows VM from vSphere to NCS
  • Customer Support
    • Having Problem Before Access a Project
    • Having Problem In a Project
  • Tutorial
    • My First Website
    • Access MySQL Database With MySQL Workbench
    • Pritunl for VPN server
    • Install Rancher Server with Docker Quick Start
      • Create RKE2 Cluster via Rancher Dashboard
    • Install odoo18 with external database
    • How to use LBaaS for mysql Load Balancing
    • How to use Cloudflare with Nipa Cloud Space
  • Release Notes
    • v5.0.X (v5.0.0-now)
      • v5.0.0
      • v5.1.0
      • v5.2.0
      • v5.2.1
      • v5.2.2
      • v5.2.3
      • v5.2.4
      • v.5.3.0
      • v5.4.0
    • v4.19.X (v4.19.0-v4.19.3)
      • v4.19.0
      • v4.19.1
      • v4.19.2
      • v4.19.3
    • v4.18.X (v4.18.0-v4.18.2)
      • v4.18.0
      • v4.18.1
      • v4.18.2
    • v4.17.X (v4.17.0-v4.17.3)
      • v4.17.0
      • v4.17.0.1
      • v4.17.1
      • v4.17.2
      • v4.17.3
    • v4.16.X (v4.16.0-v4.16.5)
      • v4.16.0
      • v4.16.1
      • v4.16.2
      • v4.16.3
      • v4.16.4
      • v4.16.5
    • v4.15.X (v4.15.0-v4.15.9)
      • v4.15.0
      • v4.15.1
      • v4.15.2
      • v4.15.3
      • v4.15.4
      • v4.15.5
      • v4.15.6
      • v4.15.7
      • v4.15.8
      • v4.15.9
    • v.4.14.X (v4.14.0-v4.14.2)
      • v.4.14.0
      • v4.14.1
      • 4.14.2
Powered by GitBook
On this page
  • Prerequisites
  • วิธีการสร้าง instance
  • การ attach floating ip เข้ากับ instance
  • วิธีการลบ Instance

Was this helpful?

  1. Public API
  2. What is NCS Public API ?
  3. Getting Start with NCS Public API

Terraform with Openstack

ใน Tutorials นี้จะเป็นการใช้ Terraform ในการสร้าง instance สำหรับผู้ที่ไม่เคยใช้ Terraform ในการทำ โดยจะเริ่ม Guide ตั้งเเต่สิ่งที่ต้องเตรียมก่อนสร้าง(Prerequisites) การหา data เเละ Resource ที่จำเป็น

PreviousUsing REST APINextAuto-scaling OpenStack Instances with Senlin and Prometheus

Last updated 5 months ago

Was this helpful?

Prerequisites

  • มี instance หรือ Devices ที่ติดตั้ง Terraform ()

  • การกำหนด Provider ()

  • Data ที่ต้องใช้ใน Terraform (สามารถหาได้จาก )

    • Provider

      1. username and password : นำมาจากที่ผู้ใช้กรอกเข้าเว็ป portal

      2. tenant_id or tenant_name: เอาไว้ระบุ project ที่เราจะใช้งาน Terraform สามารถหาได้จาก project_id หรือ project_name

      3. Terraform version : รองรับversion >= 1.8.4

      4. Openstack version : รองรับversion ~> 2.0.0

      5. auth_url : ต้องใช้ url นี้ ในการ Authentication

      6. region : เลือกพื้นที่ หรือ เขตในการสร้าง instance

    • Instance

      1. image_id หรือ image_name : ใช้สำหรับกำหนด OS ให้กับ instance

      2. flavor_id หรือ flavor_name : ใช้สำหรับกำหนด Machine Type ให้กับ instance

      3. availability_zone : ระบุพื้นที่ที่กำลังใช้งาน

        • NCP-BKK

        • NCP-BKK2

        • NCP-NON

        • NCP-KKN

      4. key_pair : จำเป็นต้องสร้าง key pair ก่อนจึงจะนำไปใช้งานได้

        1. ซึ่งเมื่อสร้างเสร็จเเล้วจะได้ไฟล์ .pem สามารถใช้ remote เข้า instance ด้วยคำสั่ง

        ssh -i [target = pathfile] username@hostname

      5. security_groups : สามารถระบุชื่อ security group เพื่อนำมาใช้ได้

    • Network

      1. network_id : เป็น id networkเเต่ละตัว ซึ่งสามารถนำไปใช้ในการสร้าง port เเละ subnet

      2. pool : เป็น External network ที่เป็น public ของเเต่ละ AZ ที่เราสามารถนำมาใช้สร้าง floating ip ใน Terraform

      3. port_id : id port ของ instance ตัวนั้นๆ

วิธีการสร้าง instance

  1. สร้างไฟล์ openstack_provider.tf

touch openstack_provider.tf
  1. เเก้ไขไฟล์ openstack_provider.tf ดังนี้

# Define required providers   #กำหนด Provider สำหรับใช้งาน
terraform {
  required_version = ">= 1.8.2" 
  required_providers {
    openstack = {
      source  = "terraform-provider-openstack/openstack"
      version = "~> 2.0.0"
    }
  }
}

# Configure the OpenStack Provider
provider "openstack" {
  user_name   = "xxxxxx"  #user name ใช้ user เดียวกันกับที่ login ใน portal
  password    = "xxxxxx"   #password ใช้ password เดียวกันกับที่ login ใน portal
  tenant_name     = "xxxxxx"   #เป็น ID ของ tenant หากเว้นไว้จะใช้จากตัวเเปร OS_PROJECT_NAME ของ environment
  auth_url      = " https://identity-api.nipa.cloud"
  region        = "NCP-TH"  #เขตหรือพื้นที่ใช้สำหรับสร้างตัว instance 
}
  1. ใช้คำสั่ง terraform init เพื่อเริ่มใช้งาน terraform เป็นขั้นตอนแรกที่สำคัญในการเริ่มต้นใช้งาน Terraform เนื่องจากมันจะจัดเตรียมสภาพแวดล้อมให้พร้อมสำหรับการดำเนินการคำสั่ง Terraform อื่น ๆ เช่น terraform plan หรือ terraform apply

terraform init

เมื่อ init สำเร็จจะปรากฎ ผลลัพธ์ดังรูป

folder .terraform และ file .terraform.lock.hcl ที่เกิดขึ้นจากการใช้คำสั่ง terraform initเมื่อทำการ init เรียบร้อย ก็จะสามารถเพิ่ม resource ต่างๆ เพื่อใช้จัดการ infrastructure ด้วย terraform ได้

  1. สร้างไฟล์ ที่ชื่อว่า compute_instance.tf

touch compute_instance.tf
  1. ทำการเเก้ไขไฟล์ compute_instance.tf

resource "openstack_networking_port_v2" "port_for_test" {
  name       = "openstack_networking_floatingip_associate_v2_port"
  network_id = "{Network_id}" # network id
}
resource "openstack_compute_instance_v2" "myinstance" {
  name            = "myinstance"   #ชื่อของ instance
  flavor_id       = "{flavor_id}" #ใส่ instance flavor id ที่ต้องการ
  key_pair        = "my_key_pair_name"   #ใส่ชื่อ key pair ที่ generate จาก Portal
  security_groups = ["default"]
  availability_zone = "NCP-BKK" #สามารถเลือก availability_zone ได้

  block_device {
    uuid                  = "af3ea4e9-f664-4801-9b1a-59aa3c206342"
    source_type           = "image"
    volume_size           = 10
    boot_index            = 0
    destination_type      = "volume"
  }

  network {
    port = openstack_networking_port_v2.port_for_test.id
  }
}
  1. ใช้คำสั่ง terraform plan เพื่อตรวจสอบ script เเละดูว่ามีการเปลี่ยนแปลงใดจะเกิดขึ้นเมื่อใช้คำสั่ง terraform apply

terraform plan
  1. ใช้คำสั่ง terraform apply เพื่อสร้าง instance

terraform apply

หลังจากใช้คำสั่ง terraform จะถามว่าต้องการ perform action หรือไม่ ถ้าต้องการให้พิมพ์ yes ถ้าไม่ต้องการให้พิมพ์ no หากสร้าง instance สำเร็จจะได้ผลลัพธ์ดังรูป

การ attach floating ip เข้ากับ instance

  1. สร้างไฟล์ openstack_networking_floatingip.tf

touch openstack_networking_floatingip.tf
  1. เเก้ไขไฟล์ openstack_networking_floatingip.tf

resource "openstack_networking_floatingip_v2" "floatip_1" {
  pool = "Standard_Public_IP_Pool_NON"  #ให้กรอกชื่อของ pool ที่มีอยู่ โดยจะต้องอยู่ใน AZ เดียวกัย Instances
}
  1. สร้างไฟล์ openstack_networking_floatingip_associate.tf

touch openstack_networking_floatingip_associate.tf
  1. เเก้ไขไฟล์ openstack_networking_floatingip_associate.tf

resource "openstack_networking_floatingip_associate_v2" "floating_ip_associate" {
  floating_ip = openstack_networking_floatingip_v2.floatip_1.address    #เป็นการนำ floating ip จาก resource ในข้อสองมาใส่
  port_id     = openstack_networking_port_v2.port_for_test.id  
}
  1. ใช้คำสั่ง terraform plan

terraform plan
  1. ใช้คำสั่ง terraform apply เพื่อสร้าง floating ip เเละผูกเข้ากับ instance

terraform apply

หลังจากใช้คำสั่ง terraform จะถามว่าต้องการ perform action หรือไม่ ถ้าต้องการให้พิมพ์ yes ถ้าไม่ต้องการให้พิมพ์ no

หลังจากใช้คำสั่งเเล้วจะปรากฎผลลัพธ์ ดังรูป

วิธีการลบ Instance

terraform destroy คำสั่งสำหรับลบ infrastructure ที่ถูกสร้างขึ้นทั้งหมดที่ถูกสร้างขึ้นด้วยไฟล์ .tf ใน folder นั้นๆ

terraform destroy

หลังจากใช้คำสั่ง terraform จะถามว่าต้องการ perform action หรือไม่ ถ้าต้องการ ให้พิมพ์ yes ถ้าไม่ต้องการ ให้พิมพ์ no

จะได้ผลลัพธ์ดังรูป

Note : สามารถอ่านข้อมูลเพิ่มเติมได้ที่

Note : สามารถอ่านข้อมูลเพิ่มเติมได้ที่ ,

โดย instance ที่สร้างจะปรากฎในหน้า ที่หัวข้อ COMPUTE > Instances

เมื่อตรวจสอบ Instance ในหน้า จะพบว่า Instance จะมี External Ip ที่ทำให้ instance สามารถเข้าถูกเข้าถึงได้จากภายนอก

How to install
OpenStack Provider
space.nipa.cloud
https://identity-api.nipa.cloud
openstack provider
openstack_compute_instance_v2
openstack_networking_port_v2
space.nipa.cloud
space.nipa.cloud