VMware Cloud Community
bairstowscott
Contributor
Contributor
Jump to solution

Add default gateway for VMKernel port group

Hi

I use this script to create a VMkernel port group but the last part of the script updates gateway address for all VMKernel port groups. How do I modify the  script so it adds the gateway for 55-CHI-VMKernel port group only. Thanks.

$hostname6= 'host1'
$pg = New-VirtualPortGroup -Name  55-CHI-VMKernel -VirtualSwitch "vSwitch1" -VLanId 55

New-VMHostNetworkAdapter -VMHost $hostname6 -PortGroup $pg -VirtualSwitch "vSwitch1" -IP "10.33.1.77" -SubnetMask "255.255.255.0" -ManagementTrafficEnabled $true

#Add gateway IP address
$netMgr = Get-View (Get-VMHost $hostname6 | Get-View).ConfigManager.NetworkSystem
$iproute = New-Object VMware.Vim.HostIpRouteConfig
$iproute.defaultGateway = "10.33.1.3"
$netMgr.UpdateIpRouteConfig($iproute)

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Did you try it like this ?

$hostname6 = 'host1' 
$pg = New-VirtualPortGroup -Name 55 - CHI-VMKernel -VirtualSwitch "vSwitch1" -VLanId 55 New-VMHostNetworkAdapter -VMHost $hostname6 -PortGroup $pg -VirtualSwitch "vSwitch1" -IP "10.33.1.77" -SubnetMask "255.255.255.0" -ManagementTrafficEnabled $true
#Add gateway IP address
$netMgr = Get-View (Get-VMHost $hostname6 | Get-View).ConfigManager.NetworkSystem
$iproute
= New-Object VMware.Vim.HostIpRouteConfig
$iproute.defaultGateway = "10.33.1.3"
$iproute
.GatewayDevice = "vmk0"
$netMgr
.UpdateIpRouteConfig($iproute)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

Are you sure you can have different default gateways for multiple vmkernel portgroups on the same ESXi host ?

Afaik, there is only 1 routing table for all the vmkernel portgroups on an ESXi host.

And that seems to be confirmed by what you can specify on the UpdateIpRouteConfig method, only 1 default gateway.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
bairstowscott
Contributor
Contributor
Jump to solution

Yes, you can have multiple gateways for multiple vmkernel portgroups on the same ESXi host

In our environment, we have three vmkernel port groups on the host. One for vmotion, two for management(on different vSwitches).

The script updates the gateway for all three port groups. Is there a way of updating default gateway for one of the vmkernel?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I see.

Shouldn't you be providing a value (vmkx) for the gatewayDevice property on the HostIpRouteConfig object in that case ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
bairstowscott
Contributor
Contributor
Jump to solution

what's vmkx value? can you give an example?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The name of your VMKernel device (vmk0, vmk1....)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

bairstowscott
Contributor
Contributor
Jump to solution

yes, I can provide VMKernel names. Do you know if there is a way of adding default gateway for vmk0 and vmk1?

Thanks!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you try it like this ?

$hostname6 = 'host1' 
$pg = New-VirtualPortGroup -Name 55 - CHI-VMKernel -VirtualSwitch "vSwitch1" -VLanId 55 New-VMHostNetworkAdapter -VMHost $hostname6 -PortGroup $pg -VirtualSwitch "vSwitch1" -IP "10.33.1.77" -SubnetMask "255.255.255.0" -ManagementTrafficEnabled $true
#Add gateway IP address
$netMgr = Get-View (Get-VMHost $hostname6 | Get-View).ConfigManager.NetworkSystem
$iproute
= New-Object VMware.Vim.HostIpRouteConfig
$iproute.defaultGateway = "10.33.1.3"
$iproute
.GatewayDevice = "vmk0"
$netMgr
.UpdateIpRouteConfig($iproute)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
bigartg
Contributor
Contributor
Jump to solution

after this script the check mark is not enabled:
-Override default gateway for this adapter
0 Kudos
LucD
Leadership
Leadership
Jump to solution

For that you to use the UpdateVirtualNic  method.

$spec = New-Object VMware.Vim.HostVirtualNicSpec
$spec.IpRouteSpec = New-Object VMware.Vim.HostVirtualNicIpRouteSpec
$spec.IpRouteSpec.IpRouteConfig = New-Object VMware.Vim.HostIpRouteConfig
$spec.IpRouteSpec.IpRouteConfig.DefaultGateway = '10.33.1.3'
$spec.Ip = New-Object VMware.Vim.HostIpConfig
$spec.Ip.IpAddress = '10.33.1.77'
$spec.Ip.SubnetMask = '255.255.255.0'
$spec.Ip.Dhcp = $false

$netMgr.UpdateVirtualNic('vmk2', $spec)

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos