Start and stop a VM

This document explains how to start, stop and restart a virtual machine (VM) instance in Google Distributed Cloud (GDC) air-gapped.

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 document is for developers in platform administrator or application operator groups that need to start, stop and restart VM instances in a Distributed Cloud environment. For more information, see Audiences for GDC air-gapped documentation.

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 Management API server, ensure you have the following resources:

  1. Sign in and generate the kubeconfig file for the Management API server if you don't have one.

  2. Use the path to the kubeconfig file of the Management API server to replace MANAGEMENT_API_SERVER 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 IAM roles

Contact your Project IAM Admin to request the following roles on your project:

  • Virtual Machine Project Admin (project-vm-admin): create, modify, list, and delete VMs in the project namespace.

  • Project Viewer(project-viewer): view all resources within the project namespaces.

All VM roles must bind to the namespace of the project where the VM resides. Follow the steps to verify your access.

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 MANAGEMENT_API_SERVER_KUBECONFIG \
       -n PROJECT \
       edit virtualmachines.virtualmachine.gdc.goog VM_NAME
    
  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 MANAGEMENT_API_SERVER_KUBECONFIG \
       -n PROJECT \
       get virtualmachines.virtualmachine.gdc.goog VM_NAME
    

    Use these definitions for the preceding variables.

    VariableDefinition
    MANAGEMENT_API_SERVER_KUBECONFIGThe Management API server kubeconfig file.
    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 MANAGEMENT_API_SERVER_KUBECONFIG \
      -n PROJECT \
      edit virtualmachines.virtualmachine.gdc.goog VM_NAME
    
  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 MANAGEMENT_API_SERVER_KUBECONFIG \
      -n PROJECT \
      get virtualmachines.virtualmachine.gdc.goog VM_NAME
    

    Use these definitions for the variables in the example code.

    VariableDefinition
    MANAGEMENT_API_SERVER_KUBECONFIGThe Management API server kubeconfig file.
    MANAGEMENT_API_SERVER_NAMEThe name of the Management API server.
    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 Management API server. Use these values in the following steps:

    kubectl --kubeconfig MANAGEMENT_API_SERVER_KUBECONFIG \
      config view --raw -o \
      jsonpath='{.users[?(@.name =="MANAGEMENT_API_SERVER_NAME-admin")].user.client-certificate-data}' \
      | base64 -d > cert
    kubectl --kubeconfig MANAGEMENT_API_SERVER_KUBECONFIG \
      config view --raw -o \
      jsonpath='{.users[?(@.name == "MANAGEMENT_API_SERVER_NAME-admin")].user.client-key-data}' \
      | base64 -d > key
    export APISERVER=`kubectl --kubeconfig MANAGEMENT_API_SERVER_KUBECONFIG \
       config view --raw -o \
       jsonpath='{.clusters[?(@.name == "MANAGEMENT_API_SERVER_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 MANAGEMENT_API_SERVER_KUBECONFIG \
      -n PROJECT \
      get virtualmachines.virtualmachine.gdc.goog VM_NAME
    

    Use these definitions for the variables in the example code.

    VariableDefinition
    MANAGEMENT_API_SERVER_KUBECONFIGThe Management API server kubeconfig file.
    MANAGEMENT_API_SERVER_NAMEThe name of the Management API server.
    VM_NAMEThe VM's name.
    PROJECTThe VM's project name.