In VMware vSphere, enabling maintenance mode on an ESXi host is a crucial step before performing any maintenance tasks, such as applying updates, performing hardware maintenance, or making configuration changes. Maintenance mode ensures that virtual machines running on the host are gracefully migrated to other hosts in the cluster, ensuring high availability during maintenance. Below are the steps to enable maintenance mode on an ESXi host:
Using vSphere Client:
- Open the vSphere Client and connect to your vCenter Server or directly to the ESXi host.
- In the “Hosts and Clusters” view, select the ESXi host on which you want to enable maintenance mode.
- Right-click on the selected host and choose “Enter Maintenance Mode.”
- A confirmation window will appear, showing you the virtual machines that will be migrated. By default, vCenter Server attempts to automatically migrate powered-on VMs to other hosts in the cluster or datacenter. You can select the checkbox for “Ensure data accessibility” to allow the VMs to continue running even on local storage if they cannot be migrated to other hosts.
- Click “OK” to enable maintenance mode.
- The host will enter maintenance mode, and vCenter Server will migrate the powered-on virtual machines to other available hosts.
Using ESXi Shell (SSH):
- Enable SSH access on the ESXi host. This can be done from the vSphere Client by navigating to the host’s configuration, under “Security Profile,” and starting the SSH service.
- Use an SSH client (e.g., PuTTY) to connect to the ESXi host’s IP address or hostname using the SSH protocol.
- Log in with your ESXi host credentials (root or a user with administrative privileges).
- Enter the following command to enable maintenance mode:
vim-cmd hostsvc/maintenance_mode_enter
Alternatively, you can use the fllowing command to enable maintenance mode and specify a reason:
vim-cmd hostsvc/maintenance_mode_enter "Maintenance Reason"
- Replace “Maintenance Reason” with the reason for enabling maintenance mode.
- The host will enter maintenance mode, and virtual machines will be migrated to other available hosts.
Exiting Maintenance Mode:
To exit maintenance mode and bring the ESXi host back to normal operation:
- If using vSphere Client, right-click on the host and choose “Exit Maintenance Mode.”
- If using ESXi Shell (SSH), use the following command:
vim-cmd hostsvc/maintenance_mode_exit
Once the host exits maintenance mode, it will be ready to resume normal operation, and virtual machines will be allowed to run on it again. Make sure you have adequate knowledge and permissions before making any changes to your ESXi hosts.
To enable maintenance mode on an ESXi host using PowerShell, you can utilize the VMware PowerCLI module. PowerCLI provides cmdlets specifically designed for managing VMware vSphere environments, including ESXi hosts. Below is a PowerShell script that enables maintenance mode on an ESXi host:
# Replace with the IP or hostname of the ESXi host and the necessary credentials
$esxiHost = "ESXi_Host_IP_or_Hostname"
$esxiUsername = "root" # Replace with the ESXi host username
$esxiPassword = "password" # Replace with the ESXi host password
# Connect to the ESXi host using PowerCLI
Connect-VIServer -Server $esxiHost -User $esxiUsername -Password $esxiPassword
# Enable maintenance mode on the ESXi host
Set-VMHost -VMHost $esxiHost -State "Maintenance"
# Disconnect from the ESXi host
Disconnect-VIServer -Server $esxiHost -Force -Confirm:$false
Replace "ESXi_Host_IP_or_Hostname" with the IP address or hostname of the ESXi host you want to put into maintenance mode. Also, replace "root" and "password" with the appropriate ESXi host credentials (username and password).
Save the script with a .ps1 extension, and then run it using PowerShell or the PowerShell Integrated Scripting Environment (ISE).
The script uses the Connect-VIServer cmdlet to establish a connection to the ESXi host using the provided credentials. It then uses the Set-VMHost cmdlet to set the ESXi host’s state to “Maintenance,” effectively enabling maintenance mode. Afterward, it disconnects from the ESXi host using the Disconnect-VIServer cmdlet.
Please ensure that you have VMware PowerCLI installed on the machine where you run the script. You can install it by following the instructions provided by VMware for your specific operating system. Additionally, make sure you have administrative access to the ESXi host and proper permissions to perform maintenance operations.
Always exercise caution while using scripts to modify ESXi host settings, as they can affect the availability and functionality of virtual machines. Verify your script in a test environment before applying it to production systems, and have a proper backup and rollback plan in place.