Kubed - secret sync between k8s clusters in different aws accounts
Background and architecture
1) aws account mc-legacy

There's a legacy application running in an AWS account:
- Route 53 has a registered domain,
<my-company>.io, and a matching hosted zone. - The record for
<my-service>.<my-company>.iopoints to<lb>.<my-company>.io. - The load balancer sends traffic to an Auto Scaling group (ASG).
- The ASG manages the EC2 instances that run
<my-service>.
2) aws account mc-project

We decide to move the application to Kubernetes, giving each team its own AWS account and cluster:
- Create a Network Load Balancer in AWS named
<lb-my-project>. - Its target groups point to EC2 instances running the ingress controller pods.
- Once a request reaches the ingress controller, the Ingress routes it through a Kubernetes Service to the pods behind
<my-service>.
3) combine

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

- Create a dedicated management AWS account and Kubernetes cluster.
- Install the Secret containing the wildcard TLS certificate for
*.<my-company>.ioin that cluster. - The Kubed controller syncs the Secret to the other Kubernetes clusters using annotations.
- The Ingress references that Secret and terminates TLS traffic.
Configuration
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
In the diagram, mc-mgmt is CI and mc-project is Target.

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
- Terraform
- Trust policy
- Policy kubed-master
- Policy kubed-eks
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
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam:::<mc-mgmt_id>:oidc-provider/oidc.eks.<aws_region>.amazonaws.com/id/<mc-mgmt_oidc>"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"oidc.eks.<aws_region>.amazonaws.com/id/<mc-mgmt_oidc>:sub": "system:serviceaccount:kube-system:kubed"
}
}
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "kubedMaster",
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": [
"arn:aws:iam::<mc-project_id>:role/kubed-follower"
]
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "kubedEks",
"Effect": "Allow",
"Action": [
"eks:ListClusters",
"eks:DescribeCluster"
],
"Resource": "*"
}
]
}
mc-project aka kubed-follower
- Terraform
- Trust policy
- Policy kubed-eks
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]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "kubedFollowerAssume",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam:::<mc-mgmt_id>:role/kubed-master"
},
"Action": "sts:AssumeRole"
}
]
}
{
"Statement": [
{
"Action": [
"eks:ListClusters",
"eks:DescribeCluster"
],
"Effect": "Allow",
"Resource": "*",
"Sid": "kubedEks"
}
],
"Version": "2012-10-17"
}
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
- kubed master
- kubed follower
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": ""
...
apiVersion: v1
kind: ConfigMap
metadata:
name: aws-auth
namespace: kube-system
data:
mapRoles: |
- "groups":
- "system:masters"
"rolearn": "arn:aws:iam::<mc-project_id>:role/kubed-follower"
"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.
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.
- ConfigMap
- Deployment
---
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
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: kubed-master
namespace: kube-system
labels:
app.kubernetes.io/name: kubed-master
app.kubernetes.io/instance: kubed-master
app.kubernetes.io/version: "v0.13.2"
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: kubed-master
app.kubernetes.io/instance: kubed-master
template:
metadata:
labels:
app.kubernetes.io/name: kubed-master
app.kubernetes.io/instance: kubed-master
spec:
serviceAccountName: kubed
# NOTE: kubeconfig-generator
initContainers:
- name: kubeconfig-generator
image: docker.io/my-company/kubed:v0.13.2
volumeMounts:
- name: kubeconfig
mountPath: /srv/kubed
- name: aws-config
mountPath: /home/app/.aws
- name: kubed-master-aws
mountPath: /home/app/scripts
command:
- sh
- -c
- |
mkdir -p /home/app/.aws &&
cp /home/app/scripts/credentials /home/app/.aws &&
sh /home/app/scripts/generate-kubeconfig.sh &&
cp /home/app/.kube/config /srv/kubed/kubeconfig
containers:
- name: kubed-master
image: docker.io/my-company/kubed:v0.13.2
args:
- run
- --v=3
- --secure-port=8443
- --audit-log-path=-
- --tls-cert-file=/var/serving-cert/tls.crt
- --tls-private-key-file=/var/serving-cert/tls.key
- --use-kubeapiserver-fqdn-for-aks=true
- --enable-analytics=false
- --cluster-name=mc-mgmt
- --kubeconfig-file=/srv/kubed/kubeconfig
- --config-source-namespace=prod
ports:
- containerPort: 8443
resources: {}
volumeMounts:
- name: kubeconfig
mountPath: /srv/kubed
- name: aws-config
mountPath: /home/app/.aws
- name: scratch
mountPath: /tmp
- name: serving-cert
mountPath: /var/serving-cert
volumes:
# NOTE: we'd like to generate kubeconfig during initContainer
- name: kubeconfig
emptyDir: {}
# secret:
# secretName: kubed # NOTE: contains data.kubeconfig
- name: aws-config
emptyDir: {}
- name: kubed-master-aws
configMap:
name: kubed-master-aws
defaultMode: 0550
- name: scratch
emptyDir: {}
- name: serving-cert
secret:
defaultMode: 420
secretName: kubed-apiserver-cert
securityContext:
fsGroup: 1000
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
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
.datatriggers 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
.datain 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"
