简体   繁体   English

GCP:如何动态更改 VM 实例的公共 IP 并保持连接

[英]GCP: How to dynamically change an VM Instance's public IP and keep the connection

We have many VM instances in Compute Engine used to scrape, they can get blocked in some sites and then we try to change the IP using NordVPN.我们在 Compute Engine 中有许多用于抓取的 VM 实例,它们可能会在某些站点中被阻止,然后我们尝试使用 NordVPN 更改 IP。 We are trying to create a Python script to automate the IP change when we detect we're blocked.我们正在尝试创建一个 Python 脚本,以便在我们检测到我们被阻止时自动更改 IP。 Currently, we are using this Python package that we recently found: NordVPN-switcher , but we are getting the next error:目前,我们正在使用我们最近发现的这个 Python package : NordVPN-switcher ,但我们收到下一个错误:

Connecting you to Denver ...
An unknown error occurred while connecting to a different server! 

An unknown error occurred while connecting to a different server! Retrying with a different server...

Traceback (most recent call last):
  File "demo.py", line 13, in <module>
    rotate_VPN(instructions)  # refer to the instructions variable here
  File "/home/eduardo_santos_housecallprosolut/.local/lib/python3.8/site-packages/nordvpn_switcher/nordvpn_switch.py", line 514, in rotate_VPN
    raise Exception("Unable to connect to a new server. Please check your internet connection.\n")
Exception: Unable to connect to a new server. Please check your internet connection.

Note: We have an internet connection.注意:我们有互联网连接。

VM instances also have NordVPN installed, if we try manually we can change it, but as we are connected to the instance using SSH, at the moment we change the IP the connection is lost.虚拟机实例也安装了 NordVPN,如果我们手动尝试,我们可以更改它,但由于我们使用 SSH 连接到实例,此时我们更改 IP 连接丢失。

Then, the current problems are:那么,目前的问题是:

  1. How to dynamically change the IP of an instance properly?如何正确动态更改实例的IP?
  2. How to keep a connection after the change occurs.更改发生后如何保持连接。

Note: The scrapers and all the logic is dockerized, and the Python version is 3.9注意: scrapers和所有的逻辑都是docker化的,Python版本是3.9

As I mentioned at the beginning, we have many machines used for scrape, we would like to keep a registry of the IPs used in each one in order to have a better assignation, probably using a Redis DB oa small collection in MongoDB.正如我一开始提到的,我们有很多机器用于scrape,我们希望保留每台使用的IP的注册表以便更好地分配,可能使用Redis DB oa small collection in MongoDB。 What do you think about it?你怎么看待这件事? What is a good way to de develop this?什么是开发这个的好方法?

Thank you so much.太感谢了。

How to dynamically change the IP of an instance properly?如何正确动态更改实例的IP?

There is no supported method.没有支持的方法。 Any existing connections will break/fail once the IP address changes.一旦 IP 地址更改,任何现有连接都将中断/失败。 Software that uses IP will need to be written to handle connection failures and attempt to reconnect.需要编写使用 IP 的软件来处理连接失败并尝试重新连接。 This type of feature is common with cell phone applications but less so in the desktop/server world.这种类型的功能在手机应用程序中很常见,但在桌面/服务器世界中则较少。

An important point with Google Cloud (and most of the cloud vendors) is that your VM does not have a public IP address assigned to a network interface.谷歌云(以及大多数云供应商)的重要一点是,您的虚拟机没有分配给网络接口的公共 IP 地址。 The public IP address is assigned to one side of a one-to-one NAT.公共 IP 地址分配给一对一 NAT 的一侧。 This means IP address change notifications within the OS and applications will not happen.这意味着不会发生操作系统和应用程序内的 IP 地址更改通知。

Google provides a CLI, SDKs and APIs that can be used to programmatically change the IP address assigned to an instance. Google 提供了 CLI、SDK 和 API,可用于以编程方式更改分配给实例的 IP 地址。

How to keep a connection after the change occurs.更改发生后如何保持连接。

Two strategies:两种策略:

  1. Add another network interface with a public IP address that does not change.添加另一个具有不变的公共 IP 地址的网络接口。 Connect to the VM using that IP address.使用该 IP 地址连接到 VM。

  2. Create a pool of public IP address that you will use.创建一个您将使用的公共 IP 地址池。 Use a VPN such as WireGuard which has excellent features for following connection address changes.使用具有跟踪连接地址更改的出色功能的 VPN,例如 WireGuard。 Connect via the VPN using the VM's private IP address which does not change when the public IP address is changed.使用 VM 的私有 IP 地址通过 VPN 连接,该地址在公共 IP 地址更改时不会更改。

I would use the first strategy as that has less complexity and fewer potential problems.我会使用第一种策略,因为它的复杂性和潜在问题更少。 However, once you understand how WireGuard manages connections and implements signatures instead of IP addresses, there are numerous possibilities for connection management.但是,一旦您了解了 WireGuard 如何管理连接并实现签名而不是 IP 地址,连接管理就有很多可能性。

I tried this tonight on a VM with a public IP, and then I remove the public IP and it continued to work.我今晚在具有公共 IP 的 VM 上尝试了这个,然后我删除了公共 IP 并且它继续工作。 It could be the solution!这可能是解决方案!

You can use IAP to connect to your vm.您可以使用 IAP连接到您的虚拟机。 Do it in a terminal with gcloud like this:像这样在带有 gcloud 的终端中执行此操作:

gcloud compute ssh --tunnel-through-iap -project=$project_name --zone=us-west2-a $instance_name

Let me know.让我知道。 If it doesn't work for you, I will delete the answer如果它不适合你,我将删除答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM