やりたいこと
おうちKubernetesクラスター内のサービスやIngressリソースが作成・更新されると、それに応じてDNSレコードが自動的に作成・更新されるようにしたい。
ポリシーの設定
ExternalDNSがRoute53リソースレコードセットとホストゾーンを更新することを許可するPolicyを作成します。
https://github.com/kubernetes-sigs/external-dns/blob/master/docs/tutorials/aws.md
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"route53:ChangeResourceRecordSets"
],
"Resource": [
"arn:aws:route53:::hostedzone/*"
]
},
{
"Effect": "Allow",
"Action": [
"route53:ListHostedZones",
"route53:ListResourceRecordSets",
"route53:ListTagsForResource"
],
"Resource": [
"*"
]
}
]
}
ユーザー作成
Kubernetes用のユーザーを作成し、先ほどのポリシーをアタッチします。
External DNSのデプロイ
チュートリアル通りマニフェストを書いていきます。
apiVersion: v1
kind: ServiceAccount
metadata:
name: external-dns
namespace: external-dns
labels:
app.kubernetes.io/name: external-dns
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: external-dns
namespace: external-dns
labels:
app.kubernetes.io/name: external-dns
rules:
- apiGroups: [""]
resources: ["services", "endpoints", "pods", "nodes"]
verbs: ["get", "watch", "list"]
- apiGroups: ["extensions", "networking.k8s.io"]
resources: ["ingresses"]
verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: external-dns-viewer
namespace: external-dns
labels:
app.kubernetes.io/name: external-dns
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: external-dns
subjects:
- kind: ServiceAccount
name: external-dns
namespace: external-dns
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: external-dns
namespace: external-dns
labels:
app.kubernetes.io/name: external-dns
spec:
strategy:
type: Recreate
selector:
matchLabels:
app.kubernetes.io/name: external-dns
template:
metadata:
labels:
app.kubernetes.io/name: external-dns
spec:
serviceAccountName: external-dns
containers:
- name: external-dns
image: registry.k8s.io/external-dns/external-dns:v0.14.0
args:
- --request-timeout=60s
- --source=service
- --source=ingress
- --domain-filter=example.com - 適宜変更
- --provider=aws
- --policy=upsert-only
- --aws-zone-type=public
- --registry=txt
- --txt-owner-id=external-dns
resources:
limits:
cpu: 20m
memory: 300Mi
requests:
cpu: 10m
memory: 200Mi
env:
- name: AWS_DEFAULT_REGION
value: ap-northeast-1
- name: AWS_ACCESS_KEY_ID ----------------|
valueFrom: |
secretKeyRef: |
name: aws-credentials |
key: aws-access-key-id |-------さっき発行したアクセスキー
- name: AWS_SECRET_ACCESS_KEY |
valueFrom: |
secretKeyRef: |
name: aws-credentials |
key: aws-secret-access-key ---------|
アプリケーションのデプロイ
apple.example.comへアクセスするとappleと返すだけのアプリケーションです。
apiVersion: apps/v1
kind: Deployment
metadata:
name: apple
namespace: sandbox
spec:
selector:
matchLabels:
app: apple
template:
metadata:
labels:
app: apple
spec:
containers:
- name: apple
image: hashicorp/http-echo
args: ["-text=apple"]
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 5678
---
apiVersion: v1
kind: Service
metadata:
name: apple
namespace: sandbox
spec:
selector:
app: apple
ports:
- port: 5678
targetPort: 5678
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: apple
namespace: sandbox
labels:
name: apple
spec:
ingressClassName: nginx
rules:
- host: apple.example.com - 適宜変更
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: apple
port:
number: 5678