Start and stop a VM

You can stop, start, and restart VMs to support the lifecycle of your applications. For example, you might want to change the number of assigned virtual centralized processing units (vCPUs) or the amount of memory allocated to a VM. You must stop the VM before you edit the configuration. After you finish, you must cycle the power state to apply the changes. This page provides instructions to stop, start, and restart your VMs in such processes.

Before you begin

To use gdcloud command-line interface (CLI) commands, ensure that you have downloaded, installed, and configured the gdcloud CLI. All commands for Distributed Cloud use the gdcloud or kubectl CLI, and require an operating system (OS) environment.

Get the kubeconfig file path

To run commands against the org admin cluster, ensure you have the following resources:

  1. Locate the org admin cluster name, or ask your Platform Administrator (PA) what the cluster name is.

  2. Sign in and generate the kubeconfig file for the org admin cluster if you don't have one.

  3. Use the path to the kubeconfig file of the org admin cluster to replace ORG_ADMIN_KUBECONFIG in these instructions.

If you want to minimize your VM start up time, follow the directions in Minimize the VM start time before you start your VM.

Request permissions and access

To perform the tasks listed in this page, you must have the Project VirtualMachine Admin role. Follow the steps to either verify or have your Project IAM Admin assign you the Project VirtualMachine Admin (project-vm-admin) role in the namespace of the project where the VM resides.

For VM operations using the GDC console or the gdcloud CLI, request your Project IAM Admin to assign you both the Project VirtualMachine Admin role and the Project Viewer (project-viewer) role.xs

Stop a VM

When you stop a virtual machine (VM), the hypervisor deallocates all resources associated with the instance. The VM is shut down.

Stop a VM that's in a running state by using the GDC console or kubectl.

Console

  1. In the navigation menu, click Virtual Machines > Instances.

  2. In the list of VMs, select the checkbox for the running VM that you want to stop.

  3. Click Stop.

  4. In the confirmation dialog, click Stop.

gdcloud

To stop a VM:

gdcloud compute instances stop VM_NAME --project PROJECT
  • Replace VM_NAME with the name of the VM.
  • Replace PROJECT with the name of the GDC project in which the VM lives.

kubectl

  1. Run the following command:

    kubectl --kubeconfig ORG_ADMIN_KUBECONFIG \
       edit virtualmachines.virtualmachine.gdc.goog VM_NAME \
       -n PROJECT
    
  2. Look for the field runningState in the spec of the VM and change the value of the field to Stopped. If runningState is not in the spec add runningState to the spec and set the value to Stopped.

    spec:
       runningState: Stopped
    
  3. Verify the status of the VM:

    kubectl --kubeconfig ORG_ADMIN_KUBECONFIG \
       get virtualmachines.virtualmachine.gdc.goog VM_NAME \
       -n PROJECT
    

    Use these definitions for the preceding variables.

    VariableDefinition
    ORG_ADMIN_KUBECONFIGThe kubeconfig path.
    VM_NAMEThe VM's name.
    PROJECTThe VM's project name.

Start a VM

Start a VM from a stopped state by using the console or kubectl.

Console

  1. In the navigation menu, click Virtual Machines > Instances.

  2. In the list of VMs, select the checkbox for the stopped VM that you want to start.

  3. Click Start.

  4. In the confirmation dialog, click Start.

gdcloud

To start a VM:

gdcloud compute instances start VM_NAME --project PROJECT
  • Replace VM_NAME with the name of the VM.
  • Replace PROJECT with the name of the GDC project in which the VM lives.

kubectl

  1. Run the following command:

    kubectl --kubeconfig ORG_ADMIN_KUBECONFIG \ 
       edit virtualmachines.virtualmachine.gdc.goog VM_NAME \ 
       -n PROJECT
    
  2. Look for the field runningState in the spec of the VM and change the value of the field to Running.

    spec:
       runningState: Running
    
  3. Verify the VM status:

    kubectl --kubeconfig ORG_ADMIN_KUBECONFIG \
       get virtualmachines.virtualmachine.gdc.goog VM_NAME \
       -n PROJECT
    

    Use these definitions for the variables in the example code.

    VariableDefinition
    ORG_ADMIN_KUBECONFIGThe kubeconfig path.
    CLUSTER_NAMEThe name of the org admin cluster.
    NAMESPACEThe VM namespace.
    VM_NAMEThe VM's name.
    PROJECTThe VM's project name.

Restart a VM

When you restart a VM, the machine resets to its initial state and the memory contents of the machine are wiped.

Restart a VM that is in a running state by using the console or kubectl.

Console

  1. In the navigation menu, click Virtual Machines > Instances.

  2. In the list of VMs, select the checkbox for the running VM that you want to restart.

  3. Click Reset.

  4. In the confirmation dialog, click Reset.

gdcloud

To reset a VM:

gdcloud compute instances reset VM_NAME --project PROJECT
  • Replace VM_NAME with the name of the VM.
  • Replace PROJECT with the name of the GDC project in which the VM lives.

kubectl

  1. Get the client certificate and key data, and the org admin cluster's API server. Use these values in the following steps:

    kubectl --kubeconfig ORG_ADMIN_KUBECONFIG config view --raw -o \
       jsonpath='{.users[?(@.name =="CLUSTER_NAME-admin")].user.client-certificate-data}' | \
       base64 -d > cert
    kubectl --kubeconfig ORG_ADMIN_KUBECONFIG config view --raw -o \
       jsonpath='{.users[?(@.name == "CLUSTER_NAME-admin")].user.client-key-data}' | \
       base64 -d > key
    export APISERVER=`kubectl --kubeconfig ORG_ADMIN_KUBECONFIG \
       config view --raw -o \
       jsonpath='{.clusters[?(@.name == "CLUSTER_NAME")].cluster.server}'`
    
  2. Make an API request to restart the VM using the wget command and specify the certificate and key from the previous step:

    wget --method=PUT -O- --no-check-certificate --certificate cert --private-key key --content-on-error=on $APISERVER/apis/virtualmachineoperations.gdc.goog/v1/namespaces/PROJECT/virtualmachines/VM_NAME/restart
    
  3. Verify the VM status:

    kubectl --kubeconfig ORG_ADMIN_KUBECONFIG \
       get virtualmachines.virtualmachine.gdc.goog VM_NAME -n PROJECT
    

    Use these definitions for the variables in the example code.

    VariableDefinition
    ORG_ADMIN_KUBECONFIGThe kubeconfig path.
    CLUSTER_NAMEThe name of the org admin cluster.
    NAMESPACEThe VM namespace.
    VM_NAMEThe VM's name.
    PROJECTThe VM's project name.