0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

おうちKubernetesにExternal DNSを設定する

Posted at

やりたいこと

おうち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用のユーザーを作成し、先ほどのポリシーをアタッチします。
スクリーンショット 2024-01-06 164537.png

アクセスキーを発行します。
スクリーンショット 2024-01-06 164731.png

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

ちゃんと登録されてました。
スクリーンショット 2024-01-06 191157.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?