How to update Google Cloud DNS with ephemeral IP for an instance

前端 未结 3 1721
清酒与你
清酒与你 2020-12-30 10:00

I have couple of instances on GCE that I don\'t really need static addresses for, but I still need to make them accessible via dns name. Since ephemeral external ip addresse

3条回答
  •  悲哀的现实
    2020-12-30 10:14

    I'm going to take a slight spin on the answer from @MaZe. Also, I'll show using systemd so this script starts automatically at startup if you're on Ubuntu or another distro that uses systemd.

    #!/bin/bash
    
    EXISTING=`gcloud dns record-sets list --zone="{your domain}" | grep xxx.yyyy.com | awk '{print $4}'`
    NEW=`gcloud compute instances describe {your instance} --zone={your zone} | grep natIP | awk -F': ' '{print $2}'`
    gcloud dns record-sets transaction start -z={your domain}
    gcloud dns record-sets transaction remove -z={your domain} \
        --name="xxx.yyyy.com." \
        --type=A \
        --ttl=300 "$EXISTING"
    gcloud dns record-sets transaction add -z={your domain} \
       --name="xxx.yyyy.com." \
       --type=A \
       --ttl=300 "$NEW"
    gcloud dns record-sets transaction execute -z={your domain}
    

    Save it to /path/to/script.sh and start it up in systemd:

    [Unit]
    Description=Set xxx.yyyy.com to the correct external ip address of this instance
    After=network.target auditd.service
    
    [Service]
    ExecStart=/path/to/script.sh
    Type=oneshot
    
    [Install]
    WantedBy=multi-user.target
    

    Save it in /etc/systemd/system as filename.service and enabled it with:

    sudo systemctl enable filename.service
    

提交回复
热议问题