How do I have my GCE instances register with an internal GCP DNS zone? Is there a built in way to do this?
Edit: looks like I can do this https://cloud.google.com/sdk/gcloud/reference/dns/record-sets/transaction/add
For a full guide on this, check out Google Cloud Platform in Action .
You can use the Cloud DNS API to register the instance, which is pretty straight forward. I think the part that isn't well documented would be how to get the right IP address and such in a start-up script.
The following code snippet has some helpers in Node.js that pull down instance names, zones, and IP addresses which you can then use to register with Cloud DNS.
const request = require('request');
const metadataUrl = 'http://metadata.google.internal/computeMetadata/v1/';
const metadataHeader = {'Metadata-Flavor': 'Google'};
const getMetadata = (path) => {
const options = {
url: metadataUrl + path,
headers: metadataHeader
};
return new Promise((resolve, reject) => {
request(options, (err, resp, body) => {
resolve(body) ? err === null : reject(err);
});
});
};
const getInstanceName = () => {
return getMetadata('instance/name');
};
const getInstanceZone = () => {
return getMetadata('instance/zone').then((data) => {
const parts = data.split('/');
return parts[parts.length-1];
})
};
const getInstanceIp = () => {
const path = 'instance/network-interfaces/0/access-configs/0/external-ip';
return getMetadata(path);
};
const getInstanceDetails = () => {
const promises = [getInstanceName(), getInstanceZone(), getInstanceIp()];
return Promise.all(promises).then((data) => {
return {
name: data[0],
zone: data[1],
ip: data[2]
};
});
};
So then in Cloud DNS, you could register using these helpers by doing something like the following (note this was written using an early version of the DNS library -- you can install it by running npm install @google-cloud/dns@0.6.1
).
const dns = require('@google-cloud/dns')({
projectId: 'your-project-id'
});
const zone = dns.zone('mydomain-dot-com');
getInstanceDetails().then((details) => {
return zone.record('a', {
name: [details.name, details.zone].join('-') + '.mydomain.com.',
data: details.ip,
ttl: 86400
});
}).then((record) =>{
return zone.createChange({add: record});
}).then((data) => {
const change = data[0];
console.log('Change created at', change.metadata.startTime,
'as Change ID', change.metadata.id);
console.log('Change status is currently', change.metadata.status);
});
It sounds like you want an internal DNS registration, in which case this happens automatically. See https://cloud.google.com/compute/docs/internal-dns .
For example, if you create an instance named "instance-1", it will be automatically resolvable from any instance in the same network and project as either instance-1
or the FQDN: [HOST_NAME].c.[PROJECT_ID].internal
.
Note: automatic external DNS registration is now in alpha: https://issuetracker.google.com/issues/35904549
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.