server වැඩ්ඩෝ එන්ට .

Honey Bunch

Well-known member
  • Nov 24, 2018
    17,255
    26,708
    113
    Kandy
    terraform walin puluwan wei neda

    To shut down VM instances using Terraform, you can leverage the cloud provider's APIs to manage the instances. Here's an example using Terraform with the Google Cloud Platform (GCP):

    1. Set up the GCP provider in your Terraform configuration. Create a file named `main.tf` and add the following content:

    ```hcl
    provider "google" {
    credentials = file("<path-to-your-service-account-key>")
    project = "<your-gcp-project>"
    region = "<your-gcp-region>"
    }
    ```

    Replace `<path-to-your-service-account-key>` with the path to your GCP service account key JSON file, `<your-gcp-project>` with your GCP project ID, and `<your-gcp-region>` with the desired region where your VM instances are located.

    2. Create a file named `variables.tf` and define the variables needed for your Terraform configuration. In this case, we need a variable to specify the number of VM instances to shut down:

    ```hcl
    variable "num_instances" {
    description = "Number of VM instances to shut down"
    default = 20
    }
    ```

    3. Add the following resource block to your `main.tf` file to define the instances you want to shut down:

    ```hcl
    resource "google_compute_instance" "vm_instance" {
    count = var.num_instances
    name = "vm-instance-${count.index + 1}"
    zone = "<your-vm-zone>"
    machine_type = "<your-machine-type>"
    # Other instance configuration options
    }
    ```

    Replace `<your-vm-zone>` with the zone where your VM instances are located (e.g., `us-central1-a`), and `<your-machine-type>` with the desired machine type for your instances (e.g., `n1-standard-1`).

    4. Add the following resource block to your `main.tf` file to shut down the instances:

    ```hcl
    resource "null_resource" "shutdown" {
    count = var.num_instances

    provisioner "local-exec" {
    command = "gcloud compute instances stop ${google_compute_instance.vm_instance[count.index].name} --zone=${google_compute_instance.vm_instance[count.index].zone} --project=${var.project_id}"
    }
    }
    ```

    This resource block uses a `null_resource` with a `local-exec` provisioner to execute a local command for each instance. It uses the `gcloud compute instances stop` command to shut down the instances individually.

    5. Run `terraform init` in the terminal to initialize your Terraform configuration.

    6. Run `terraform apply` to deploy the changes and shut down the instances.

    Terraform will execute the specified command for each VM instance, shutting them down one by one.
     

    Honey Bunch

    Well-known member
  • Nov 24, 2018
    17,255
    26,708
    113
    Kandy
    ඒකේ සර්වර් හදලා තියෙන්නේ kvm da ehemanam script ekakinuth kara ganna pluwan
     

    Geeth_Nuwan

    Well-known member
  • Dec 11, 2007
    330
    261
    63
    Kegalle
    vm එකක server 20 ද server එකක vm 20ද ??

    Industry එෙකදි තනි සර්වර් එකක් උඩ VM දුවන්නෙ නැහැ. අඩුම සර්වර් 3ක්වත් තියෙන cluster එකක් හදල ඒකෙ තමයි දුවන්නෙ. (shared resource pool එකක) එක සර්වර් එකක් ඩවුන් කරන්න ඔ්න නම් රන් වෙන VM ටික ඩවුන් නොකර ‍cluster එක‍ෙ ‍ වෙන සර්වර් එකකට දාන්න පුලුවන්. (Live migration/Hot migration). අපි ඕන තරම් එහෙම කරනවා users ලා ට දැනෙන්නෙවත් නැහැ.

    -------------
    ඔයාගෙ කේස් එකෙදි නම්ඔ ය ඔක්කොම එකපාර down කරන්න අවශ්‍ය නම් script එකක් හදාගන්න. VM මොනවද hypervisor එක මොකක්ද කියල තව විස්තර කීව නම් උදවු කරන්න පුලුවන්