Google Cloud DNS入门





0/5 (0投票)
本文档演示了如何进行身份验证、创建托管区域以及创建资源记录
您可以通过 gcloud
命令行工具或直接使用 REST API 来使用 Google Cloud DNS API。本文档演示了如何进行身份验证、创建托管区域以及创建资源记录。命令行工具要求您安装 Google Cloud SDK。REST API 示例演示了如何启用 API、创建 HTML 页面以及使用 Google JavaScript 客户端库调用 API。
必备组件
要启用 Google Cloud DNS,您必须创建一个 Google Developers Console 项目或使用现有项目。在项目中,您必须 启用 Cloud DNS API,在提示时接受服务条款协议,并在尚未完成的情况下为您的项目启用结算。记下项目 ID,以便在下一节中授权请求时使用。
用户必须在 Google Developers Console 中项目的团队区域内获得授权。他们需要拥有所有者或编辑者权限才能进行更改,或需要查看者权限才能列出或查看当前的 DNS 配置。
命令行工具需要 Google Cloud SDK。运行 gcloud components update dns
命令以确保您的环境中已包含 DNS 命令。
推荐:您应该 验证您要管理的域的所有权。
授权请求
在读取或修改 DNS 设置之前,您必须使用 REST API 或 gcloud
命令行工具进行身份验证。
命令行
设置命令行工具并授权其代表您发出 API 请求
-
安装 Cloud SDK。如果您已安装 SDK,请运行
gcloud components update dns
命令以确保您的环境中已包含 DNS 命令。 -
命令行身份验证
gcloud auth login
-
在浏览器中显示的授权对话框中点击接受。点击接受后,您可以关闭浏览器窗口。
- 接下来,该命令会提示您输入项目 ID。输入您之前在 Developers Console 中创建的值。
您的 gcloud
命令行工具现在已准备就绪,并将针对您输入اً的项目 ID 发出所有请求。稍后可以通过重新运行此命令或为命令指定 --project=NEW_PROJ_ID
参数来更改此值。
JavaScript
启用 API
在使用 REST API 之前,您必须在 Google Developers Console 中启用 Google Cloud DNS API,并创建一个客户端 ID 以允许您的示例应用程序连接到 Google API。本文档假设您在 https://:8080
上测试示例。
- 如果您还没有 Google 帐户,请注册一个 Google 帐户。
- 在 Developers Console 中启用 Google Cloud DNS API。您可以选择一个现有的 Compute Engine 或 App Engine 项目,也可以创建一个新项目。
- 如果您需要向 REST API 发出请求,则需要创建 OAuth 2.0 ID
- 在“API 和身份验证”部分的“凭据”页面上,点击“创建新的客户端 ID”。
- 选择“Web 应用程序”。
- 在“授权的 JavaScript 来源”中,输入您将提供此示例的主机,例如
https://:8080
。 - 点击“创建客户端 ID”。
- 请记下您将在后续步骤中使用的项目中的以下信息
- 客户端 ID (
xxxxxx.apps.googleusercontent.com
)。 - 您希望使用的项目 ID。您可以在 Developers Console 的“概览”页面的顶部找到该 ID。您也可以让用户在您的应用程序中提供他们想要使用的项目名称。
- 客户端 ID (
现在 API 已为您的项目启用,您将配置您的应用程序以代表用户访问 REST API。
设置 OAuth 2.0 流
本示例演示了一个简单的 OAuth 2.0 流,使用 JavaScript 请求用户授权您的应用程序代表他们发出请求。用户必须在 {{ console_name }} 的团队区域中拥有项目的“所有者”或“编辑者”访问权限。OAuth 2.0 范围 https://www.googleapis.com/auth/ndev.clouddns.readwrite
授予您的应用程序读取和写入 Cloud DNS API 的权限。
<button id="authButton">Authorize</button> <script type="text/javascript"> // Array of scopes var authParams = { 'response_type' : 'token', // Retrieves an access token only 'client_id' : 'xxxxxxx.apps.googleusercontent.com', // Client ID from Developers Console 'immediate' : false, // For the demo, force the auth window every time 'scope' : ['https://www.googleapis.com/auth/ndev.clouddns.readwrite'] // Array of scopes }; function myCallback(authResult){ if (authResult && authResult['access_token']) { // User granted authorization // Set the token for later API calls gapi.auth.setToken(authResult); ... // Next step, load API and query API } else { // Authorization failed or user declined } } // Attach a click even to an authorization button. Use clicks to trigger // the flow so that the pop-up window succeeds. document.getElementById('authButton').onclick = function(){ gapi.auth.authorize(authParams, myCallback) }; </script>
现在您已通过身份验证,可以与 Cloud DNS API 进行交互了。
创建托管区域和记录
现在您的应用程序拥有访问令牌,可以管理区域和记录了。以下示例演示了如何创建一个托管区域,然后在该区域内创建一个 A
记录。
命令行
创建一个托管区域并传递必需的参数。区域名称必须是小写字母、字母数字字符,并且可以包含连字符。
gcloud dns managed-zone create --dns_name="example.com." --description="A test zone" examplezonename
接下来,在 examplezonename
区域中创建一个 DNS 记录
gcloud dns records --zone=examplezonename edit
在出现的编辑器中,将显示一个包含添加和删除部分的更改示例。您可以编辑此 JSON 格式并输入您的添加、删除或两者。
{ "additions": [ { "kind": "dns#resourceRecordSet", "name": "example.com.", "rrdatas": [ "ns-cloud1.googledomains.com. dns-admin.google.com. 1 21600 3600 1209600 300" ], "ttl": 21600, "type": "SOA" }, ... { INSERT YOUR ADDITIONS HERE } ], "deletions": [ { "kind": "dns#resourceRecordSet", "name": "example.com.", "rrdatas": [ "ns-cloud1.googledomains.com. dns-admin.google.com. 0 21600 3600 1209600 300" ], "ttl": 21600, "type": "SOA" }, ... { INSERT YOUR DELETIONS HERE } ] }
在保存更改之前,请验证您的语法,以确保在命令失败时不会丢失更改。
编辑完成后,保存更改并退出编辑器。该命令将发送您的更改。
JavaScript
加载 Cloud DNS API 方法,并调用 gapi.client.dns.managedzones.create()
方法来创建托管区域,然后使用返回的 ID 在该区域内创建记录
<script type="text/javascript"> var projectName = 'YOUR_PROJECT_NAME'; var zoneName = 'YOUR_ZONE_NAME'; // Hard coded for the example: var recordToCreate = { 'additions' : [{ 'name' : 'example.com.', 'type' : 'A', 'ttl' : 21600, 'rrdatas' : ['1.2.3.4 2.3.4.5'] }] // Can also specify a list of deletions // , 'deletions' : [{ // Same format as additions }] } gapi.client.load('dns', 'v1beta1', function() { createZone(); } function createZone() { // Configure a request to the Managedzones.create API method and pass the // required and optional parameters var request = gapi.client.dns.managedzones.create({ 'project' : projectName, 'resource' : { 'name' : zoneName, 'dnsName' : 'example.com.', 'description' : 'A managed zone for example.com' } }); // Execute the request and provide a callback function request.execute(function(resp) { // Insert the response into a div for viewing. document.getElementById('response-payload').innerText = 'managedZones.create response:<pre>' + JSON.stringify(resp) + '</pre>'; // Get managed zone ID: var zoneId = resp.id; createRecord(zoneId); }); } /** * Create a basic record in the given zone * * @param zoneId The identifier for the zone that was returned during * the previous step in this example. */ function createRecord(zoneId) { var request = gapi.client.dns.changes.create({ 'project' : projectName, 'managedZone' : zoneName, 'body' : recordToCreate }); request.execute(function(resp){ document.getElementById('response2-payload').innerText = 'changes.create response:<pre>' + JSON.stringify(resp) + '</pre>'; }); } </script> <div id="response-payload"></div> <div id="response2-payload"></div>
故障排除
verifyManagedZoneDnsNameOwnership
请在 http://www.google.com/webmasters/verification/ 验证 'your_domain' 域(或其父域)的所有权,然后重试。
收到此错误时,您必须 验证域所有权。
accessNotConfigured
访问未配置
要解决此错误,您必须为您的项目 启用 Cloud DNS API。
inactiveBillingState
项目 project_name
在计费状态非活动时无法接受请求。计费状态可能需要几分钟才能更新。
在 Developers Console 中项目的“设置”部分中为您的项目启用计费。
后续步骤
本文档中的简单示例演示了如何通过使用命令行工具或 REST API 快速开始使用 Cloud DNS API。此外,还有 客户端库可帮助您创建直接使用 REST API 的应用程序。
要了解有关 Cloud DNS 的更多信息,请参阅以下主题
除非另有说明,本页的代码示例根据 Apache 2.0 许可证 授权。