I am trying to create a K8s cluster in Azure AKS and when cluster is ready I can see couple of resources are created within the default
namespace. Example secret, configmap:
As a security recommendation NO k8s resources should be created under the default
namespace so how to avoid it? It's created by default during cluster creation.
I have found the same question asked here:
User srbose-msft (Microsoft employee) explained the principle of operation very well:
In Kubernetes, a
ServiceAccount controller
manages the ServiceAccounts inside namespaces, and ensures a ServiceAccount named "default" exists in every active namespace. [Reference]TokenController runs as part of
kube-controller-manager
. It acts asynchronously. It watches ServiceAccount creation and creates a corresponding ServiceAccount token Secret to allow API access. [Reference] Thus, the secret for the default ServiceAccount token is also created.Trusting the custom CA from an application running as a pod usually requires some extra application configuration. You will need to add the CA certificate bundle to the list of CA certificates that the TLS client or server trusts. For example, you would do this with a golang TLS config by parsing the certificate chain and adding the parsed certificates to the
RootCAs
field in thetls.Config
struct.You can distribute the CA certificate as a ConfigMap that your pods have access to use. [Reference] AKS implements this in all active namespaces through ConfigMaps named
kube-root-ca.crt
in these namespaces.You shall also find a Service named
kubernetes
in the default namespace. It has a ServiceType of ClusterIP and exposes the API Server Endpoint also namedkubernetes
internally to the cluster in the default namespace.All the resources mentioned above will be created by design at the time of cluster creation and their creation cannot be prevented. If you try to remove these resources manually, they will be recreated to ensure desired goal state by the
kube-controller-manager
.
Additionally:
The Kubernetes clusters should not use the default namespace Policy is still in Preview. Currently the schema does not explicitly allow for Kubernetes resources in the default namespace to be excluded during policy evaluation. However, at the time of writing, the schema allows for
labelSelector.matchExpressions[].operator
which can be set toNotIn
with appropriatelabelSelector.matchExpressions[].values
for the Service default/kubernetes with label:
component=apiserver
The default
ServiceAccount
, the defaultServiceAccount token Secret
and theRootCA ConfigMap
themselves are not created with any labels and hence cannot to added to this list. If this is impeding your use-case I would urge you to share your feedback at https://techcommunity.microsoft.com/t5/azure/ct-p/Azure