github.com/teknogeek/dnscontrol/v2@v2.10.1-0.20200227202244-ae299b55ba42/docs/getting-started.md (about)

     1  ---
     2  layout: default
     3  title: Getting Started
     4  ---
     5  # Getting Started
     6  
     7  
     8  ## 1. Install the software
     9  
    10  You can either download the latest
    11  [github release](https://github.com/StackExchange/dnscontrol/releases),
    12  or build from the go source:
    13  
    14      go get github.com/StackExchange/dnscontrol
    15  
    16  The `go get` command will will download the source, compile it, and
    17  install `dnscontrol` in your `bin` directory.
    18  
    19  
    20  ## 2. Create a place for the config files.
    21  
    22  Create a directory where you'll be storing your configuration files.
    23  We highly recommend storing these files in a Git repo, but for
    24  simple tests anything will do.
    25  
    26  Note: Do **not** store your creds.json file in Git unencrypted.
    27  That is unsafe. In fact you should include `creds.json` in your
    28  `.gitignore` file.  We recommend you encrypt the file using something
    29  like [git-crypt](https://www.agwa.name/projects/git-crypt) or
    30  [Blackbox](https://github.com/StackExchange/blackbox).
    31  
    32  Create a subdirectory called `zones` in the same directory as the
    33  configuration files.  (`mkdir zones`).  `zones` is where the BIND
    34  provider writes the zonefiles it creates. Even if you don't
    35  use BIND, it is useful for testing.
    36  
    37  
    38  ## 3. Create the initial `dnsconfig.js`
    39  
    40  `dnsconfig.js` is the main configuration and defines providers, DNS
    41  domains, and so on.
    42  
    43  Start your `dnsconfig.js` file by downloading
    44  [dnsconfig.js-example.txt]({{ site.github.url }}/assets/dnsconfig.js-example.txt))
    45  and renaming it.
    46  
    47  The file looks like:
    48  
    49  {% highlight js %}
    50  
    51  // Providers:
    52  
    53  var REG_NONE = NewRegistrar('none', 'NONE');    // No registrar.
    54  var DNS_BIND = NewDnsProvider('bind', 'BIND');  // ISC BIND.
    55  
    56  // Domains:
    57  
    58  D('example.com', REG_NONE, DnsProvider(DNS_BIND),
    59      A('@', '1.2.3.4')
    60  );
    61  {%endhighlight%}
    62  
    63  You may modify this file to match your particular providers and domains. See [the javascript docs]({{site.github.url}}/js) and  [the provider docs]({{site.github.url}}/provider-list) for more details.
    64  If you are using other providers, you will likely need to make a `creds.json` file with api tokens and other account information. For example, to use both name.com and Cloudflare, you would have:
    65  
    66  {% highlight js %}
    67  {
    68    "cloudflare":{ // provider name to be used in dnsconfig.js
    69      "apitoken": "token" // API token
    70    },
    71    "namecom":{ // provider name to be used in dnsconfig.js
    72      "apikey": "key", // API Key
    73      "apiuser": "username" // username for name.com
    74    }
    75  }
    76  {%endhighlight%}
    77  
    78  There are 2 types of providers:
    79  
    80  A "Registrar" is who you register the domain with.  Start with
    81  `REG_NONE`, which is a provider that never talks to or updates the
    82  registrar.  You can define your registrar later when you want to
    83  use advanced features.
    84  
    85  The `DnsProvider` is the service that actually provides DNS service
    86  (port 53) and may be the same or different company. Even if both
    87  your Registrar and DnsProvider are the same company, two different
    88  definitions must be included in `dnsconfig.js`.
    89  
    90  
    91  ## 4. Create the initial `creds.json`
    92  
    93  `creds.json` stores credentials and a few global settings.
    94  It is only needed if any providers require credentials (API keys,
    95  usernames, passwords, etc.).
    96  
    97  Start your `creds.json` file by downloading
    98  [creds.json-example.txt]({{ site.github.url }}/assets/creds.json-example.txt))
    99  and renaming it.
   100  
   101  The file looks like:
   102  
   103  {% highlight js %}
   104  {
   105    "bind": {
   106    },
   107    "r53_ACCOUNTNAME": {
   108      "KeyId": "change_to_your_keyid",
   109      "SecretKey": "change_to_your_secretkey"
   110    }
   111  }
   112  {%endhighlight%}
   113  
   114  Ignore the `r53_ACCOUNTNAME` section.  It is a placeholder and will be ignored. You
   115  can use it later when you define your first set of API credentials.
   116  
   117  Note that `creds.json` is a JSON file. JSON is very strict about commas
   118  and other formatting.  There are a few different ways to check for typos:
   119  
   120  Python:
   121  
   122      python -m json.tool creds.json
   123  
   124  jq:
   125  
   126      jq < creds.json
   127  
   128  FYI: `creds.json` fields can be an environment variable. The field must begin with a `$` followed by the variable name. No other text. For example:
   129  
   130      "apiuser": "$GANDI_APIUSER",
   131  
   132  ## 5. Test the sample files.
   133  
   134  Before you edit the sample files, verify that the system is working.
   135  
   136  First run `dnscontrol preview` and make sure that it completes with
   137  no errors.  The preview command is the "dry run" mode that shows
   138  what changes need to be made and never makes any actual changes.
   139  It will use APIs if needed to find out what DNS entries currently
   140  exist.
   141  
   142  It should look something like this:
   143  
   144  {% highlight js %}
   145  
   146  $ dnscontrol preview
   147  Initialized 1 registrars and 1 dns service providers.
   148  ******************** Domain: example.com
   149  ----- Getting nameservers from: bind
   150  ----- DNS Provider: bind... 1 correction
   151  #1: GENERATE_ZONEFILE: example.com
   152   (2 records)
   153  
   154  ----- Registrar: none
   155  Done. 1 corrections.
   156  {%endhighlight%}
   157  
   158  Next run `dnscontrol push` to actually make the changes. In this
   159  case, the change will be to create a zone file where one didn't
   160  previously exist.
   161  
   162  {% highlight js %}
   163  $ dnscontrol push
   164  Initialized 1 registrars and 1 dns service providers.
   165  ******************** Domain: example.com
   166  ----- Getting nameservers from: bind
   167  ----- DNS Provider: bind... 1 correction
   168  #1: GENERATE_ZONEFILE: example.com
   169   (2 records)
   170  
   171  CREATING ZONEFILE: zones/example.com.zone
   172  SUCCESS!
   173  ----- Registrar: none
   174  Done. 1 corrections.
   175  {%endhighlight%}
   176  
   177  
   178  ## 6. Make a change.
   179  
   180  Try making a change to `dnsconfig.js`. For example, change the IP
   181  address of in `A('@', '1.2.3.4')` or add an additional A record.
   182  
   183  In our case, we changed the IP address to 10.10.10.10. Previewing
   184  our change looks like this:
   185  
   186  {% highlight js %}
   187  $ dnscontrol preview
   188  Initialized 1 registrars and 1 dns service providers.
   189  ******************** Domain: example.com
   190  ----- Getting nameservers from: bind
   191  ----- DNS Provider: bind... 1 correction
   192  #1: GENERATE_ZONEFILE: example.com
   193  MODIFY A example.com: (1.2.3.4 300) -> (10.10.10.10 300)
   194  
   195  ----- Registrar: none
   196  Done. 1 corrections.
   197  {%endhighlight%}
   198  
   199  Notice that it read the old zone file and was able to produce a
   200  "diff" between the old `A` record and the new one.  If the zonefile
   201  didn't exist, the output would look different because the zone file
   202  was being created from scratch.
   203  
   204  Run `dnscontrol push` to see the system generate a new zone file.
   205  
   206  Other providers use an API do do updates. In those cases the
   207  individual changes will translate into API calls that update the
   208  specific records.
   209  
   210  Take a look at the `zones/example.com.zone` file.  It should look
   211  like:
   212  
   213  {% highlight js %}
   214  $TTL 300
   215  @                IN SOA   DEFAULT_NOT_SET. DEFAULT_NOT_SET. 1 3600 600 604800 1440
   216                   IN A     10.10.10.10
   217  {%endhighlight%}
   218  
   219  You can change the "DEFAULT_NOT_SET" text by following the documentation
   220  for the [BIND provider]({{site.github.url}}/providers/bind) to set
   221  the "master" and "mbox" settings.  Try that now.
   222  
   223  
   224  ## 7. Use your own domains
   225  
   226  Now that we know the system is working for test data, try controlling
   227  a real domain (or a test domain if you have one).
   228  
   229  Set up the provider:  Add the providers's definition to `dnsconfig.js`
   230  and list any credentials in `creds.json`.  Each provider is different.
   231  See [the provider docs]({{site.github.url}}/provider-list) for
   232  specifics.
   233  
   234  Edit the domain: Add the `D()` entry for the domain, or repurpose
   235  the `example.com` domain.  Add individual `A()`, `MX()` and other
   236  records as needed.  Remember that the first parameter to `D()` is
   237  always a Registrar.
   238  
   239  Run `dnscontrol preview` to test your work. It may take a few tries
   240  to list all the DNS records that make up the domain.  When preview
   241  shows no changes required, then you know you are at feature parity.
   242  
   243  The [Migrating]({{site.github.url}}/migrating) doc has advice
   244  about converting from other systems.
   245  You can manually create the `D()` statements, or you can
   246  generate them automatically using the
   247  [dnscontrol get-zones]({{site.github.url}}/get-zones)
   248  command to import the zone from (most) providers and output it as code
   249  that can be added to `dnsconfig.js` and used with very little
   250  modification.
   251  
   252  Now you can make change to the domain(s)  and run `dnscontrol preview`
   253  
   254  
   255  ## 8. Production Advice
   256  
   257  If you are going to use this in production, we highly recommend the following:
   258  
   259  * Store the configuration files in Git.
   260  * Encrypt the `creds.json` file before storing it in Git.
   261  * Use a CI/CD tool like Jenkins to automatically push DNS changes.
   262  * Join the DNSControl community. File [issues and PRs](https://github.com/StackExchange/dnscontrol).