Skip to main content

Kubed - secret sync between k8s clusters in different aws accounts

· 9 min read

Background and architecture​

1) aws account mc-legacy​

img

There's a legacy application running in an AWS account:

  1. Route 53 has a registered domain, <my-company>.io, and a matching hosted zone.
  2. The record for <my-service>.<my-company>.io points to <lb>.<my-company>.io.
  3. The load balancer sends traffic to an Auto Scaling group (ASG).
  4. The ASG manages the EC2 instances that run <my-service>.

2) aws account mc-project​

img

We decide to move the application to Kubernetes, giving each team its own AWS account and cluster:

  1. Create a Network Load Balancer in AWS named <lb-my-project>.
  2. Its target groups point to EC2 instances running the ingress controller pods.
  3. Once a request reaches the ingress controller, the Ingress routes it through a Kubernetes Service to the pods behind <my-service>.

3) combine​

img

The Route 53 record for <my-service>.<my-company>.io now points to the CNAME for <lb-my-project>. Traffic gets through, but TLS isn't terminated.

The <my-company>.io zone is too important to hand over to controllers like external-dns or cert-manager.

At the same time, there can be many project accounts and Kubernetes clusters, and I don't want to copy the TLS Secret by hand.

4) Concept​

img

  1. Create a dedicated management AWS account and Kubernetes cluster.
  2. Install the Secret containing the wildcard TLS certificate for *.<my-company>.io in that cluster.
  3. The Kubed controller syncs the Secret to the other Kubernetes clusters using annotations.
  4. The Ingress references that Secret and terminates TLS traffic.

Configuration​

info

In the examples below, the role for mc-mgmt is called kubed-master and the role for each mc-project-* is called kubed-follower.

AWS IAM RBAC​

info

In the diagram, mc-mgmt is CI and mc-project is Target.

img

Set up cross-account IAM roles and Kubernetes RBAC so Kubed in mc-mgmt can manage Secrets in clusters in other AWS accounts, such as mc-project.

mc-mgmt aka kubed-master​

locals {
kubed_followers_arns = [
for _, id in local.aws_accounts_map : "arn:aws:iam::${id}:role/kubed-follower"
]
}

module "iam_assumable_role_kubed_master" {
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
version = "4.2.0"

create_role = true
role_name = "kubed-master"

provider_url = replace(local.cluster_oidc_issuer_url, "https://", "")

role_policy_arns = [
aws_iam_policy.kubed_master.arn,
aws_iam_policy.kubed_eks.arn,
]

oidc_fully_qualified_subjects = [
"system:serviceaccount:kube-system:kubed"
]
}

data "aws_iam_policy_document" "kubed_master" {
statement {
sid = "kubedMaster"
effect = "Allow"
actions = [
"sts:AssumeRole",
]
resources = local.kubed_followers_arns
}
}

resource "aws_iam_policy" "kubed_master" {
name = "kubed-master"
policy = data.aws_iam_policy_document.kubed_master.json
}

data "aws_iam_policy_document" "kubed_eks" {
statement {
sid = "kubedEks"
effect = "Allow"
actions = [
"eks:DescribeCluster",
"eks:ListClusters"
]
resources = ["*"]
}
}

resource "aws_iam_policy" "kubed_eks" {
name = "kubed-eks"
policy = data.aws_iam_policy_document.kubed_eks.json
}

mc-project aka kubed-follower​

locals {
mc_mgmt_id = "<mc-mgmt_id>"
}

data "aws_iam_policy_document" "kubed_follower_assume" {
statement {
sid = "kubedFollowerAssume"
effect = "Allow"
principals {
identifiers = toset(["arn:aws:iam::${local.mc_mgmt_id}:role/kubed-master"])
type = "AWS"
}
actions = [
"sts:AssumeRole",
]
}
}

data "aws_iam_policy_document" "kubed_follower" {
statement {
sid = "kubedEks"
effect = "Allow"
actions = [
"eks:DescribeCluster",
"eks:ListClusters"
]
resources = ["*"]
}
}


resource "aws_iam_policy" "kubed_follower" {
name = "kubed-follower"
policy = data.aws_iam_policy_document.kubed_follower.json
}


resource "aws_iam_role" "kubed_follower" {
name = "kubed-follower"
description = "IAM role which allows kubed sync secrets from mc-mgmt to this account"
assume_role_policy = data.aws_iam_policy_document.kubed_follower_assume.json
managed_policy_arns = [aws_iam_policy.kubed_follower.arn]
}

ServiceAccount kubed​

Add this annotation to the kubed-master ServiceAccount:

annotations:
eks.amazonaws.com/role-arn: arn:aws:iam::<mc-mgmt_id>:role/kubed-master

ConfgiMap aws-auth​

apiVersion: v1
kind: ConfigMap
metadata:
name: aws-auth
namespace: kube-system
data:
mapRoles: |
- "groups":
- "system:masters"
"rolearn": "arn:aws:iam::<mc-mgmt_id>:role/kubed-master"
"username": ""
...

Docker image​

The upstream image is based on scratch and contains just the Kubed binary. We also need AWS CLI tools to generate the kubeconfig.

FROM appscode/kubed:v0.13.2 as downloader

FROM alpine:3.17

COPY --from=downloader /kubed /usr/bin/kubed

RUN apk add --no-cache \
curl~=7.86 \
py3-pip~=22.3 \
&& pip install --no-cache-dir \
awscli~=1.27 \
&& curl -Lo aws-iam-authenticator https://github.com/kubernetes-sigs/aws-iam-authenticator/releases/download/v0.5.9/aws-iam-authenticator_0.5.9_linux_amd64 \
&& chmod +x aws-iam-authenticator \
&& mv aws-iam-authenticator /usr/bin/

RUN addgroup --gid 1950 app \
&& addgroup --gid 1000 app \
&& adduser \
--uid 1000 \
--home /home/app \
--shell /bin/ash \
--ingroup app \
--disabled-password \
app \
&& addgroup app app

USER 1000
WORKDIR /home/app

ENTRYPOINT ["/usr/bin/kubed"]

K8s controller​

The mc-mgmt cluster also runs a regular Kubed instance installed with the Helm chart. We reuse its Kubernetes RBAC configuration.

danger

This method can be destructive. A misconfigured kubed-master can reverse the direction of Secret synchronization between namespaces. I recommend running kubed-master in a dedicated cluster.

---
apiVersion: v1
kind: ConfigMap
metadata:
name: kubed-master-aws
namespace: kube-system
data:
credentials: |-
[mc-mgmt]
role_arn = arn:aws:iam::<mc-mgmt_id>:role/kubed-master
web_identity_token_file = /var/run/secrets/eks.amazonaws.com/serviceaccount/token
[mc-project]
role_arn = arn:aws:iam::<mc-project_id>:role/kubed-follower
source_profile = mc-mgmt
role_session_name = mc-project
generate-kubeconfig.sh: |-
#!/bin/bash
set -eux

aws eks update-kubeconfig --name mc-project-k8s --alias mc-project-k8s --profile mc-project

# NOTE: mc-mgmt-k8s should the last due to current-context
aws eks update-kubeconfig --name mc-mgmt-k8s --alias mc-mgmt-k8s --profile mc-mgmt

Synchronizing configuration​

---

apiVersion: v1
kind: ConfigMap
metadata:
name: cross-acc-sync-demo
namespace: prod
annotations:
kubed.appscode.com/sync-contexts: "mc-project-k8s"
data:
foo: bar
spam: eggs

Synchronization behavior​

info

Here, configuration means a ConfigMap or Secret.

  • Sync runs from the namespace named in the controller flags to a namespace with the same name in each follower cluster. Annotations and labels from the source aren't copied to the followers.
  • Restarting the controller pod or updating the source object's .data triggers sync. Deleting the object from a follower cluster doesn't automatically recreate it.
  • Deleting the source object in the master account deletes it from all followers.
  • If a follower runs its own Kubed instance to sync across namespaces, you can annotate the object manually and let that controller distribute it. Updating .data in the master syncs the Secret without overwriting that annotation.

Add the annotation in the follower cluster:

kubectl --context mc-project annotate cm cross-acc-sync-demo "kubed.appscode.com/sync=sync/kubed-master=true"

Label another namespace in the follower cluster so the local Kubed instance syncs the ConfigMap into it.

---

kind: Namespace
apiVersion: v1
metadata:
name: env-ahorbach
labels:
name: env-ahorbach
sync/kubed-master: "true"

Comments and reactions