概要
OCI Container Engine for Kubernetes (OKE)のkube-dns(CoreDNS)についてVCN内リソースの名前解決と異なるnamespace内リソースの名前解決について各設定を確認しました。
構成
OKEのCNIはFlannelを使用してます。また仮想クラスタのオプションもありますが今回は標準クラスタの構成を使用しました。
OKE内PodからVCNリソースのMySQL DBへのアクセス
各コンポーネントでどのように動いているか確認します。
- Podは/etc/resolv.confに設定されたnameserverに問合せします。
このnameserver(10.96.5.5)はkube-dnsです。
user@ubuntu:~# cat /etc/resolv.conf
search mgmt.svc.cluster.local svc.cluster.local cluster.local sydvcn1010500.oraclevcn.com sydsub10105650.sydvcn1010500.oraclevcn.com
nameserver 10.96.5.5
options ndots:5
- Svc kube-dns(10.96.5.5)のEndpointsはCoreDNS(10.244.0.14)です。
[user@linux]$ kubectl describe service/kube-dns -n kube-system
Name: kube-dns
Namespace: kube-system
Labels: k8s-app=kube-dns
kubernetes.io/cluster-service=true
kubernetes.io/name=CoreDNS
Annotations: <none>
Selector: k8s-app=kube-dns
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.96.5.5
IPs: 10.96.5.5
Port: dns 53/UDP
TargetPort: 53/UDP
Endpoints: 10.244.0.14:53
Port: dns-tcp 53/TCP
TargetPort: 53/TCP
Endpoints: 10.244.0.14:53
Port: metrics 9153/TCP
TargetPort: 9153/TCP
Endpoints: 10.244.0.14:9153
Session Affinity: None
Events: <none>
- CoreDNS(10.244.0.14)のPod内容です。
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-system coredns-76d9448ddc-bm4l7 1/1 Running 3 62d 10.244.0.14 10.105.65.182 <none> <none>
- 自ドメイン外の名前解決はforwardに設定された/etc/resolv.confを参照します。
[user@linux]$ kubectl describe configmaps coredns -n kube-system
Name: coredns
Namespace: kube-system
Labels: <none>
Annotations: <none>
Data
====
Corefile:
----
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
import custom/*.server
BinaryData
====
Events: <none>
- /etc/resolv.confはworker nodeの設定を参照します。以下のようにOCIのDNS(169.254.169.254)を指しています。
[user@workernode ~]$ cat /etc/resolv.conf
; Any changes made to this file will be overwritten whenever the
; DHCP lease is renewed. To persist changes you must update the
; /etc/oci-hostname.conf file. For more information see
;[https://docs.cloud.oracle.com/iaas/Content/Network/Tasks/managingDHCP.htm#notes]
;
# Generated by NetworkManager
search sydvcn1010500.oraclevcn.com sydsub10105650.sydvcn1010500.oraclevcn.com
nameserver 169.254.169.254
-
以上の結果、Podからはkube-dns→CoreDNS→OCI DNSを経由してMySQL DBの名前解決を行うことによりアクセスが可能となります。
user@ubuntu:~# mysql -h mysql01.sydsub1010520.sydvcn1010500.oraclevcn.com -P 3306 -u admin -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 66
Server version: 8.0.34-u3-cloud MySQL Enterprise - Cloud
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
- ExternalName Serviceを使用することによってFQDNを使用せずアクセスすることが可能です。
以下のようにPodと同じnamespaceでsvc ext-mysql01を設定しFQDNとしてMySQL DBを指定します。
apiVersion: v1
kind: Service
metadata:
name: ext-mysql01
namespace: mgmt
spec:
type: ExternalName
externalName: mysql01.sydsub1010520.sydvcn1010500.oraclevcn.com
apply後、kubectl get svcで登録されたことを確認します。
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
mgmt ext-custaweb-svc ExternalName <none> custaweb-svc.custa.svc.cluster.local <none>
ExternalName Serviceを設定することによりアクセス先のFQDNやIPアドレスが変わってもアプリケーションの変更を抑えることが可能です。
user@ubuntu:~# mysql -h ext-mysql01 -P 3306 -u admin -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 74
Server version: 8.0.34-u3-cloud MySQL Enterprise - Cloud
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
OKE内Podから異なるnamespace内リソースの名前解決
Podからはkube-dns→CoreDNSを経由して異なるnamespace内リソースの名前解決を行うことによりアクセスが可能となります。
FQDNはサービス名.namespace名.svc.cluster.localとなります。
user@ubuntu:~# curl -I http://custaweb-svc.custa.svc.cluster.local:8092
HTTP/1.1 200 OK
異なるnamespace内リソースの名前解決においてもExternalName Serviceを使用することによってFQDNを使用せずアクセスすることが可能です。
apiVersion: v1
kind: Service
metadata:
name: ext-custaweb-svc
namespace: mgmt
spec:
type: ExternalName
externalName: custaweb-svc.custa.svc.cluster.local
apply後、kubectl get svcで登録されたことを確認します。
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
mgmt ext-custaweb-svc ExternalName <none> custaweb-svc.custa.svc.cluster.local
ExternalNameを使用したアクセス結果です。
user@ubuntu:~# curl -I http://ext-custaweb-svc:8092
HTTP/1.1 200 OK