Can I connect one service account to multiple namespaces in Kubernetes?
Asked Answered
P

1

47

I have couple of namespaces - assume NS1 and NS2. I have serviceaccounts created in those - sa1 in NS1 and sa2 in NS2. I have created roles and rolebindings for sa1 to do stuff within NS1 and sa2 within NS2. What I want is give sa1 certain access within NS2 (say only Pod Reader role).

I am wondering if that's possible or not?

Protract answered 28/12, 2018 at 15:7 Comment(0)
R
83

You can simply reference a ServiceAccount from another namespace in the RoleBinding:

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:
  name: pod-reader
  namespace: ns2
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: pod-reader-from-ns1
  namespace: ns2
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: pod-reader
subjects:
- kind: ServiceAccount
  name: ns1-service-account
  namespace: ns1
Redcap answered 28/12, 2018 at 15:20 Comment(6)
can we apply same concept to make communication between Service [NodePort in ns1] and Ingress [Aws-alb-ingress-controller in ns2] i.e; across 2 different namespace?Grosbeak
@AshishKumar here you can use a service of type ExternalName.Ratty
@Ratty can service of type ExternalName distribute the traffic among multiple pods under same label specified.Grosbeak
@Ashish a service of type external name is nothing more, than a CNAME record in the cluster dns. The balancing is then done by the service it is pointing to.Ratty
Detail: kind: Role in the roleRef section will be removed by k8s. You could just use namespace: n2 in this section.Sauter
There is also a very good and clear article about the topic: octopus.com/blog/k8s-rbac-roles-and-bindings This finally helped me to understand the relationship between service accounts, roles, role bindings, cluster roles and cluster role bindings.Bungle

© 2022 - 2024 — McMap. All rights reserved.