0
0
GCPcloud~5 mins

Routes and routing in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Routes in Google Cloud help direct network traffic from one place to another. Routing decides how data moves between virtual machines or to the internet, making sure it reaches the right destination.
When you want to control how traffic flows between virtual machines in your cloud network.
When you need to send traffic from your cloud network to the internet or other networks.
When you want to block or allow certain traffic by directing it through specific paths.
When you have multiple networks and want to connect them with clear traffic rules.
When you want to troubleshoot network issues by checking how routes are set up.
Commands
This command creates a route named 'example-route' in the 'default' network. It directs traffic destined for the 10.0.0.0/24 range to the virtual machine 'example-vm' in the specified zone.
Terminal
gcloud compute routes create example-route --network default --destination-range 10.0.0.0/24 --next-hop-instance example-vm --next-hop-instance-zone us-central1-a
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/my-project/global/routes/example-route].
--network - Specifies the network where the route is created.
--destination-range - Defines the IP range this route applies to.
--next-hop-instance - Sets the VM that traffic will be sent to.
--next-hop-instance-zone - Specifies the zone of the next hop instance.
This command lists the route named 'example-route' to verify it was created and see its details.
Terminal
gcloud compute routes list --filter="name=example-route"
Expected OutputExpected
NAME NETWORK DEST_RANGE NEXT_HOP_INSTANCE NEXT_HOP_IP PRIORITY TAGS example-route default 10.0.0.0/24 projects/my-project/zones/us-central1-a/instances/example-vm 1000
--filter - Filters the list to show only the route with the specified name.
This command deletes the route named 'example-route' without asking for confirmation, cleaning up the route after use.
Terminal
gcloud compute routes delete example-route --quiet
Expected OutputExpected
Deleted [https://www.googleapis.com/compute/v1/projects/my-project/global/routes/example-route].
--quiet - Skips confirmation prompt for deletion.
Key Concept

If you remember nothing else from this pattern, remember: routes tell your cloud network where to send traffic based on destination addresses.

Common Mistakes
Using the wrong network name when creating a route.
The route will not apply to the intended network, so traffic won't be directed correctly.
Always check and specify the correct network name with the --network flag.
Setting an incorrect destination IP range.
Traffic outside the correct range won't follow the route, causing connectivity issues.
Verify the destination range matches the IP addresses you want to route.
Not specifying the correct next hop instance or zone.
Traffic will not reach the intended virtual machine, breaking communication.
Use the exact VM name and zone where the next hop instance is located.
Summary
Create routes to control where network traffic goes in your cloud network.
Use 'gcloud compute routes create' with destination range and next hop to set routes.
List routes to check their details and delete them when no longer needed.