As for your question, I see that you want to configure a routing rule so that all traffic from an instance is redirected through another instance .
It should not be too complicated to configure, the best example I can imagine at the moment is the configuration used in "Configuring NAT gateway" from Google Help Center documents.
Note the following more specifically (from step 5 in the manual):
$ gcloud compute routes create no-ip-internet-route --network gce-network \ --destination-range 0.0.0.0/0 \ --next-hop-instance nat-gateway \ --next-hop-instance-zone us-central1-a \ --tags no-ip --priority 800
We can break it down to better understand exactly what this rule does:
- We create a route in the "gce-network", and we call it "no-ip-internet-route".
--destination-range 0.0.0.0/0 should indicate that traffic destined for any destination should be affected by this rule. We mean the traffic that will leave our instance.- The
--next-hop-instance nat-gateway should indicate that after the rule starts, traffic should be sent to the nat-gateway instance as the next transition. --next-hop-instance-zone us-central1-a is just to specify the zone of our gateway machine.--tags no-ip --priority 800 used to say that only those that have a no-ip tag should act on it. (while they are on the network in which this rule is created), and we also set a βhigherβ priority for the rule to ensure that it takes precedence over other rules.
Based on the foregoing, any instance that has the no-ip tag will always forward its outgoing traffic to the nat-gateway instance. Thus, the nat-gateway instance automatically becomes the next-hop for all outgoing traffic from the marked instance.
You can use a similar setting to figure out your routing rules and create them based on this, however, note that the "nat-gateway" used in this example has certain features, such as forwarding IP addresses and, in effect, acts as router and becomes the default gateway for instances marked with the rule.
You also asked : how to add an additional ip to my instance using forwarding in the Google Cloud?
Technically, you can simply add your instance to the 'target pool' and then create a forwarding rule to redirect the required traffic to that instance. The forwarding rule will give you an additional IP address. You can also use this for SSH for the instance through the IP address of the forwarding rule as long as you redirect traffic to port 22 or any port that the instance is configured to listen on.
Boyan source share