> For the complete documentation index, see [llms.txt](https://gushi10546.gitbook.io/aptos-kai-fa-zhe-wen-dang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gushi10546.gitbook.io/aptos-kai-fa-zhe-wen-dang/jie-dian/fullnode-for-devnet/zai-cgp-shang-yun-xing-quan-jie-dian.md).

# 在 CGP 上运行全节点

本教程介绍了如何配置和部署公共全节点以使用 Google Cloud (GCP) 连接到 Aptos 开发网。与在笔记本电脑上运行相比，在云中运行全节点通常可以提供更好的稳定性和可用性。如果您正在寻找部署生产级全节点，我们建议您将其部署在云上。

注意：如果您想要其他部署替代方案，请阅读运行全节点，使用云需要付费，具体取决于您的配置方式。

###

### **先决条件**

您可以运行本指南中的命令，从您想要的任何机器上在 Google Kubernetes Engine 上部署您的完整节点。从 [GCP](https://cloud.google.com/compute)、[Google Cloud Shell](https://cloud.google.com/shell) 或您的个人计算机上的虚拟机。

Cloud Shell 预装了以下软件包。如果您选择使用 Cloud Shell，请务必查看有关 [ephermability 的文档](https://cloud.google.com/shell/docs/using-cloud-shell#choosing_ephemeral_mode)。但是，如果您是从笔记本电脑或另一台机器上运行安装，则需要安装：

* Terraform 1.1.7:<https://www.terraform.io/downloads.html>
* Kubernetes cli:<https://kubernetes.io/docs/tasks/tools/>
* Google Cloud cli:<https://cloud.google.com/sdk/docs/install-sdk>

安装 gcloud CLI 后，使用 gcloud 登录 GCP

(<https://cloud.google.com/sdk/gcloud/reference/auth/login>)

```
$ gcloud auth login --update-adc
```

如果您已经设置了 GCP 帐户，请直接跳至“[入门](https://aptos.dev/nodes/full-node/run-a-fullnode-on-gcp#getting-started)”，如果还没有，请按照以下部分创建和配置您的 GCP 帐户。

### **GCP 设置**

**注册 90 天免费试用**

Google Cloud 为每位新用户提供 [90 天 300 美元的免费试用](https://cloud.google.com/free/docs/gcp-free-tier/#free-trial)。这 300 美元将作为积分存入您的帐户，您可以使用它们来了解 Google Cloud 产品。请注意，您在注册免费试用时需要添加付款信息。这是出于身份验证目的，[在您升级到付费帐户并用完积分之前不会产生费用](https://cloud.google.com/free/docs/gcp-free-tier/#:~:text=Don%27t%20worry%2C%20setting,90%2Dday%20period)。）。免费试用版中不提供某些 GCP 功能，例如 GPU 和 Windows 服务器。

[Sign up for the $300 in credits here.](https://cloud.google.com/free)

[在此处注册 300 美元的积分。](https://cloud.google.com/free)

####

**创建一个新的 GCP 项目**

您还需要在 GCP Console 上或使用 Google Cloud CLI 中的 glcoud 命令创建一个新项目。不过，在您这样做之前，熟悉 [GCP 上的资源层次结构](https://cloud.google.com/resource-manager/docs/cloud-platform-resource-hierarchy)可能会有所帮助。

[按照这些说明设置新项目](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project)

**启用计费/升级您的帐户**

您仍然可以使用免费试用积分，但启用计费功能可以让您完全访问 GCP 的所有功能，并且不会遇到任何节点中断。

[按照此处列出的步骤升级您的帐户。](https://cloud.google.com/free/docs/gcp-free-tier#how-to-upgrade)

####

**更多 GCP 资源**

这应该足以让您的 GCP 设置准备好开始部署您的全节点。但是，如果您是 GCP 的新手，您可能需要查看我们的一些[快速入门指南](https://cloud.google.com/docs/get-started/quickstarts)和 [Google Cloud Skills Boost](https://www.cloudskillsboost.google/catalog)

### **入门**

您可以使用 Aptos 全节点 Terraform 模块在 GCP 上部署公共全节点，本指南假设您已经设置了 GCP 帐户，并创建了一个用于部署 Aptos 全节点的新项目。如果您不这样做，请查看上述有关 [GCP 设置的说明](https://aptos.dev/nodes/full-node/run-a-fullnode-on-gcp#gcp-setup)。

1. 为您的配置创建一个工作目录。

   * 选择一个工作区名称，例如`devnet`。注意：这定义了 terraform 工作空间名称，该名称又用于形成资源名称。

   ```
   $ export WORKSPACE=devnet
   ```

   * 为工作区创建目录

   ```
   $ mkdir -p ~/$WORKSPACE
   ```
2. 创建一个存储桶，用于在 Google Cloud Storage 上存储 Terraform 状态。使用控制台或此 gcs 命令创建存储桶。存储桶的名称必须是唯一的。在此处查看 Google Cloud Storage 文档：

   <https://cloud.google.com/storage/docs/creating-buckets#prereq-cli>

   ```
   $ gsutil mb gs://BUCKET_NAME
   # for example
   $ gsutil mb gs://<project-name>-aptos-terraform-dev
   ```
3. 在您的工作目录中创建名为 `main.tf` 的 Terraform 文件

   ```
   $ cd ~/$WORKSPACE
   $ touch main.tf
   ```
4. 修改 `main.tf`文件以配置 Terraform，并从 Terraform 模块创建全节点。 `main.tf` 的示例内容

   ```
   terraform {
     required_version = "~> 1.2.0"
     backend "gcs" {
       bucket = "BUCKET_NAME" # bucket name created in step 2
       prefix = "state/fullnode"
     }
   }

   module "fullnode" {
     # download Terraform module from aptos-labs/aptos-core repo
     source        = "github.com/aptos-labs/aptos-core.git//terraform/fullnode/gcp?ref=main"
     region        = "us-central1"  # Specify the region
     zone          = "c"            # Specify the zone suffix
     project       = "gcp-fullnode" # Specify your GCP project name
     era           = 1              # bump era number to wipe the chain
     image_tag     = "devnet"       # Specify the docker image tag to use, replace to `testnet` or other tag if needed

     fullnode_helm_values = {
       chain = {
       name = "devnet"              # replace with `ait3` or other values if connecting to different networks.
       }
     }
   }
   ```
5. 在 `main.tf`文件的同一目录中初始化 Terraform

```
$ terraform init
```

**这应该会在 `.terraform` 文件夹中为您下载所有 terraform 依赖项。**

1. 创建一个新的 Terraform 工作区以隔离您的环境：

   ```
   $ terraform workspace new $WORKSPACE
   # This command will list all workspaces
   $ terraform workspace list
   ```
2. 应用配置。

   ```
   $ terraform apply
   ```

这可能需要一段时间才能完成（10 - 20 分钟），Terraform 将在您的云帐户上创建所有资源。

### **验证**

一旦 Terraform 应用完成，您可以按照此部分验证您的部署。

1. 配置您的 Kubernetes 客户端以访问您刚刚部署的集群

   ```
   $ gcloud container clusters get-credentials aptos-$WORKSPACE --zone <region_zone_name> --project <project_name>
   # for example:
   $ gcloud container clusters get-credentials aptos-devnet --zone us-central1-a --project aptos-fullnode
   ```
2. 检查您的全节点 Pod 现在是否正在运行（这可能需要几分钟）：

   ```
   $ kubectl get pods -n aptos
   ```
3. 获得您全节点IP

   ```
   $ kubectl get svc -o custom-columns=IP:status.loadBalancer.ingress -n aptos
   ```
4. 检查 REST API，确保 ledge 版本正在增加。

   ```
   $ curl http://<IP>/v1
   # Example command syntax: curl <http://104.198.36.142/v1>
   ```
5. 为了验证您的[全节点的正确性](https://aptos.dev/tutorials/run-a-fullnode/#verify-the-correctness-of-your-fullnode)，如全节点文档中所述，您需要在一个 ssh 终端中直接设置端口转发机制到 aptos pod，并在另一个 ssh 终端中对其进行测试

* 设置到 aptos-fullnode pod 的端口转发。使用 `kubectl get pods -n aptos` 获取 pod 的名称

  ```
  $ kubectl port-forward -n aptos <pod-name> 9101:9101
  # for example:
  $ kubectl port-forward -n aptos devnet0-aptos-fullnode-0 9101:9101
  ```
* 打开一个新的 ssh 终端。执行以下 curl 调用以验证正确性

  ```
  $ curl -v <http://0:9101/metrics> 2> /dev/null | grep "aptos_state_sync_version{type=\\"synced\\"}"

  $ curl -v <http://0:9101/metrics> 2> /dev/null | grep "aptos_connections{direction=\\"outbound\\""
  ```
* 完成后通过在终端中输入 control-c 退出端口转发

###

### **使用新版本更新全节点**

可能有两种类型的版本，一种带有数据擦除以启动区块链，一种只是软件更新。

**使用数据擦除升级**

1. 您可以在 `main.tf`中增加`era`编号以触发新数据卷的创建，这将在新数据库上启动节点。
2. 更新 `main.tf`中的 `image_tag`
3. 更新全节点的 Terraform 模块，在 `main.tf` 文件的同一目录中运行它

   ```
   $ terraform get -update
   ```
4. 应用 Terraform 更改

```
$ terraform apply
```

####

**升级无数据擦除**

1. 更新 `main.tf`中的`image_tag`
2. 更新全节点的 Terraform 模块，在  `main.tf`文件的同一目录中运行它

   ```
   $ terraform get -update
   ```
3. 应用 Terraform 更改

```
$ terraform apply
# if you didn't update the image tag, terraform will show nothing to change, in this case, force helm update
$ terraform apply -var force_helm_update=true
```

###

### **配置身份和种子对等点**

**静态身份**

如果您想使用静态身份配置您的节点，请查看完整[节点高级指南](https://aptos.dev/tutorials/run-a-fullnode#advanced-guide)以了解如何生成密钥，并按照以下说明在您的 terraform 文件中进行配置。

1. 按照[此处](https://aptos.dev/tutorials/run-a-fullnode#create-a-static-identity-for-a-fullnode)的指南生成您自己的私钥并提取对等 ID
2. 修改 `main.tf`以在 `fullnode_helm_values` 中添加`fullnode_identity`，这将配置 fullnode 的键，例如：

   <pre><code><strong>module "fullnode" {
   </strong>  # download Terraform module from aptos-labs/aptos-core repo
     source        = "github.com/aptos-labs/aptos-core.git//terraform/fullnode/gcp?ref=main"
     region        = "us-central1"  # Specify the region
     zone          = "c"            # Specify the zone suffix
     project       = "gcp-fullnode" # Specify your GCP project name
     era           = 1              # bump era number to wipe the chain
     image_tag     = "devnet"       # Specify the docker image tag to use

     fullnode_helm_values = {
       chain = {
         name = "devnet"
       }
       # create fullnode from this identity config, so it will always have same peer id and address
       fullnode_identity = {
         type = "from_config"
         key = "B8BD811A91D8E6E0C6DAC991009F189337378760B55F3AD05580235325615C74"
         peer_id = "ca3579457555c80fc7bb39964eb298c414fd60f81a2f8eedb0244ec07a26e575"
       }
     }
   }
   </code></pre>
3. 应用 Terraform 更改

   ```
   $ terraform apply
   ```

####

**添加上游种子节点**

您可以添加上游种子对等点以允许您的节点状态从特定的全节点同步，这在全节点由于拥塞而无法连接到网络时很有帮助。

1. 获取上游节点 ID 信息，您可以使用我们在[全节点教程](/aptos-kai-fa-zhe-wen-dang/gai-nian/quan-jie-dian.md)中列出的信息；或从 [Aptos Discord](http://discord.gg/aptoslabs) `#advertise-full-node` 频道获取一个，这些是我们社区托管的节点。

   ```
   module "fullnode" {
       # download Terraform module from aptos-labs/aptos-core repo
       source        = "github.com/aptos-labs/aptos-core.git//terraform/fullnode/gcp?ref=main"
       region        = "us-central1"  # Specify the region
       zone          = "c"            # Specify the zone suffix
       project       = "gcp-fullnode" # Specify your GCP project name
       era           = 1              # bump era number to wipe the chain
       image_tag     = "dev_5b525691" # Specify the docker image tag to use

       fullnode_helm_values = {
         # add a list of peers as upstream
         aptos_chains = {
           devnet = {
             seeds = {
               "bb14af025d226288a3488b4433cf5cb54d6a710365a2d95ac6ffbd9b9198a86a" = {
               addresses = ["/dns4/pfn0.node.devnet.aptoslabs.com/tcp/6182/noise-ik/bb14af025d226288a3488b4433cf5cb54d6a710365a2d95ac6ffbd9b9198a86a/handshake/0"]
               role = "Upstream"
               },
               "7fe8523388084607cdf78ff40e3e717652173b436ae1809df4a5fcfc67f8fc61" = {
               addresses = ["/dns4/pfn1.node.devnet.aptoslabs.com/tcp/6182/noise-ik/7fe8523388084607cdf78ff40e3e717652173b436ae1809df4a5fcfc67f8fc61/handshake/0"]
               role = "Upstream"
               },
               "f6b135a59591677afc98168791551a0a476222516fdc55869d2b649c614d965b" = {
               addresses = ["/dns4/pfn2.node.devnet.aptoslabs.com/tcp/6182/noise-ik/f6b135a59591677afc98168791551a0a476222516fdc55869d2b649c614d965b/handshake/0"]
               role = "Upstream"
               }
             }
           }
         }
       }
     }
   ```

   如果您要连接到其他网络，请确保将 `aptos_chains.devnet`  更新到相应的网络
2. 应用 Terraform 更改

```
$ terraform apply
```

###

### **检查日志记录**

要检查 pod 的日志，请使用以下命令

```
# Get a list of the pods
$ kubectl get pods -n aptos

# Get logs of the pod
$ kubectl logs <pod-name> -n aptos
# for example:
$ kubectl logs devnet0-aptos-fullnode-0 -n aptos
```

使用 GKE 时，集群和 pod 的日志会自动显示在 Google Cloud 控制台中。从控制台菜单中，选择 `Kubernetes Engine`。从侧面菜单中，选择`Workloads`。您将看到列出的集群中的所有 pod。

![https://aptos.dev/assets/images/tutorial-gcp-logging1-b2eed3ddcdc3f32fa0a98d156eb576c3.png](https://aptos.dev/assets/images/tutorial-gcp-logging1-b2eed3ddcdc3f32fa0a98d156eb576c3.png)

`devnet0-aptos-fullnode` 是运行 aptos fullnode 容器的 pod。单击吊舱以查看详细信息。您将看到有关 pod 的一些指标和其他详细信息。

![https://aptos.dev/assets/images/tutorial-gcp-logging2-7c7394435178b639ad3c49537270c5ca.png](https://aptos.dev/assets/images/tutorial-gcp-logging2-7c7394435178b639ad3c49537270c5ca.png)

单击  `LOGS`选项卡可直接从 pod 中查看日志。如果 pod 中有错误，您将在此处看到它们。

![https://aptos.dev/assets/images/tutorial-gcp-logging3-ce881ec60dd529f88fde99a3197bf2f1.png](https://aptos.dev/assets/images/tutorial-gcp-logging3-ce881ec60dd529f88fde99a3197bf2f1.png)

单击`open in new window`图标以在日志资源管理器中查看日志。此屏幕允许在日志中进行高级搜索。

![https://aptos.dev/assets/images/tutorial-gcp-logging4-b79b3db6552a833070107f26138f72da.png](https://aptos.dev/assets/images/tutorial-gcp-logging4-b79b3db6552a833070107f26138f72da.png)

日志仪表板中提供了其他日志记录见解

![https://aptos.dev/assets/images/tutorial-gcp-logging5-78873facafd1c13b8963cf9520972f8f.png](https://aptos.dev/assets/images/tutorial-gcp-logging5-78873facafd1c13b8963cf9520972f8f.png)

其他[功能](https://cloud.google.com/logging/docs)可通过 [Cloud Logging](https://cloud.google.com/logging) 获得，包括创建基于日志的指标、日志接收器和日志存储桶。

###

### **检查监控**

谷歌云从集群中捕获许多指标，并让它们在控制台中轻松查看。从控制台菜单中，选择 `Kubernetes Engine`。单击 aptos 部署到的集群。点击右上角的`Operations`链接。单击“`Metrics`”子选项卡以查看特定的集群指标。

![https://aptos.dev/assets/images/tutorial-gcp-mon1-8e519d888d8278e3905c2a853c735011.png](https://aptos.dev/assets/images/tutorial-gcp-mon1-8e519d888d8278e3905c2a853c735011.png)

点击顶部的“`View in Cloud Monitoring`”链接以查看集群的内置 GKE [仪表板](https://cloud.google.com/stackdriver/docs/solutions/gke/observing)。

![https://aptos.dev/assets/images/tutorial-gcp-mon2-daf3266735be444309cf6445df4e1c68.png](https://aptos.dev/assets/images/tutorial-gcp-mon2-daf3266735be444309cf6445df4e1c68.png)

Google Cloud [Monitoring](https://cloud.google.com/monitoring) 还有许多其他功能可以轻松监控集群和 Pod。您可以为服务配置正常运行时间检查，并在指标达到特定阈值时配置警报。

### **故障排除**

常见的故障排除解决方案。

**Terraform“连接被拒绝”错误消息**

运行 terraform 时，命令出错，并显示连接被拒绝错误消息。

```
Error: Get "<http://localhost/api/v1/namespaces/aptos>": dial tcp 127.0.0.1:80: connect: connection refused
```

这可能意味着安装状态与存储桶中保存的 terraform 状态文件不同步。 （在 `terraform init`语句期间配置）。如果集群或其他组件在 terraform 之外被删除，则可能会发生这种情况。或者如果 terraform 出现错误并且没有完成。使用以下命令检查状态。删除与错误消息相关的状态。您可能需要运行 terraform destroy、清理环境并再次运行 terraform 脚本。

```
terraform state list

terraform state rm <state>
```

### **全节点“NoAvailablePeers”错误消息**

如果您的节点无法状态同步，并且日志显示“NoAvailablePeers”，则可能是由于网络拥塞。您可以尝试为您的全节点添加一些额外的上游对等节点以进行状态同步。请参阅指南[添加上游种子节点](/aptos-kai-fa-zhe-wen-dang/jie-dian/fullnode-for-devnet/yong-aptos-yuan-huo-docker-lai-da-jian-quan-jie-dian.md#zeng-jia-shang-you-zhong-zi-peer)。
