IBM Cloud DNS Services を VPC からプライベートな名前解決に使う

#概要
こちらの続きです。
[IBM Cloud の VPC と VPC と Classic Infrastructure を Transit Gateway で接続する - Qiita](https://qiita.com/khayama/items/8d1e4ac3e67717f1cecd#cli-%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB)

こちらの構成を検証します。

[Resolving DNS names with DNS Services](https://cloud.ibm.com/docs/dns-svcs?topic=dns-svcs-about-dns-services#resolving-dns-names-with-dns-services)
![Resolving DNS names with DNS Services](https://cloud.ibm.com/docs-content/v1/content/eb5f6cdae5cf1dfb5b881b3059a3fe71403a3422/dns-svcs/images/dns-svcs-overview.png)

#IBM Cloud DNS Services
## CLI プラグインインストール

```shell

ibmcloud plugin install cloud-dns-services
ibmcloud plugin show cloud-dns-services
```

## リソースグループ設定

```shell

ibmcloud target -g khayama
```

##サービスインスタンス作成
```shell

ibmcloud dns instance-create khayama-dns standard-dns
ibmcloud dns instance-target khayama-dns
```

##ゾーン作成

```shell

export zoneid=`ibmcloud dns zone-create ibmcloud.local --output json | jq -r .id`
```

##レコード作成
```shell

ibmcloud dns resource-record-create $zoneid --type A --name khayama-vsi1 --ipv4 192.168.10.10
ibmcloud dns resource-record-create $zoneid --type A --name khayama-vsi2 --ipv4 192.168.20.20
```

## VPC からのアクセスを許可
```shell

export crn1=`ibmcloud is vpcs --json | jq -r '.[] | select (.name=="khayama-vpc-1") | .crn'`
export crn2=`ibmcloud is vpcs --json | jq -r '.[] | select (.name=="khayama-vpc-2") | .crn'`

ibmcloud dns permitted-network-add $zoneid --type vpc --vpc-crn $crn1
ibmcloud dns permitted-network-add $zoneid --type vpc --vpc-crn $crn2
```

# VPC VSI の設定
こちらのドキュメントを参考に設定します。

> [Updating the DNS resolver for your VSI](https://cloud.ibm.com/docs/dns-svcs?topic=dns-svcs-updating-dns-resolver)

デフォルトは「Public DNS Resolver」が以下のように設定されます。

```shell

[root@khayama-vsi1 ~]# cat /etc/resolv.conf 
; generated by /usr/sbin/dhclient-script
nameserver 161.26.0.10
nameserver 161.26.0.11
```

DHCP で DNS サーバーが上書きされないように
「IBM Cloud DNS Services」をプライベートな名前解決に使う設定をおこないます。(CentOS 7.x)


```shell

cat <<EOF > /etc/dhcp/dhclient.conf
supersede domain-name-servers 161.26.0.7, 161.26.0.8;
supersede domain-search "ibmcloud.local";
EOF
```

DHCP をリロードします。

```shell

dhclient -v -r eth0; dhclient -v eth0
```

DNS 設定が反映されたことを確認します。

```shell

[root@khayama-vsi1 ~]# cat /etc/resolv.conf 
; generated by /usr/sbin/dhclient-script
search ibmcloud.local.
nameserver 161.26.0.7
nameserver 161.26.0.8
```

# 名前解決の確認
以下のように確認できます。

```shell

[root@khayama-vsi1 ~]# ping google.com -c 4
PING google.com (172.217.17.142) 56(84) bytes of data.
64 bytes from ams15s30-in-f14.1e100.net (172.217.17.142): icmp_seq=1 ttl=55 time=14.1 ms
64 bytes from ams15s30-in-f14.1e100.net (172.217.17.142): icmp_seq=2 ttl=55 time=14.2 ms
64 bytes from ams15s30-in-f14.1e100.net (172.217.17.142): icmp_seq=3 ttl=55 time=14.3 ms
64 bytes from ams15s30-in-f14.1e100.net (172.217.17.142): icmp_seq=4 ttl=55 time=14.3 ms

--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 14.133/14.241/14.303/0.109 ms
```

```shell

[root@khayama-vsi1 ~]# ping khayama-vsi2 -c 4
PING khayama-vsi2.ibmcloud.local (192.168.20.20) 56(84) bytes of data.
64 bytes from 192.168.20.20 (192.168.20.20): icmp_seq=1 ttl=55 time=1.38 ms
64 bytes from 192.168.20.20 (192.168.20.20): icmp_seq=2 ttl=55 time=1.52 ms
64 bytes from 192.168.20.20 (192.168.20.20): icmp_seq=3 ttl=55 time=1.36 ms
64 bytes from 192.168.20.20 (192.168.20.20): icmp_seq=4 ttl=55 time=1.40 ms

--- khayama-vsi2.ibmcloud.local ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 1.369/1.419/1.521/0.071 ms
```

```shell

[root@khayama-vsi2 ~]# ping khayama-vsi1 -c 4
PING khayama-vsi1.ibmcloud.local (192.168.10.10) 56(84) bytes of data.
64 bytes from 192.168.10.10 (192.168.10.10): icmp_seq=1 ttl=55 time=1.42 ms
64 bytes from 192.168.10.10 (192.168.10.10): icmp_seq=2 ttl=55 time=1.38 ms
64 bytes from 192.168.10.10 (192.168.10.10): icmp_seq=3 ttl=55 time=1.23 ms
64 bytes from 192.168.10.10 (192.168.10.10): icmp_seq=4 ttl=55 time=1.36 ms

--- khayama-vsi1.ibmcloud.local ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 1.239/1.353/1.421/0.073 ms
```

# 参考

- [Getting started with IBM Cloud DNS Services](https://cloud.ibm.com/docs/dns-svcs?topic=dns-svcs-getting-started)