Does endpoints in k8s need namespaces?

Hi KodeKloud Team:

Recently I’m studying for my coming CKA exman. I I had the doubt if endpoints needs in some cases to have a namespace value in the metadata data section.

Cheers

They are namespaced objects, in all cases.

1 Like

Hi @rob_kodekloud

So if you put an endpoint without namespace (Assuming the namespace works in the default namespace), does that endpoint will only work in the default namespace ? I’m asking because what should I do if it is an external service with and specific namespace ( like: seal for example), and I need to create an endpoint. Should the endpoint be in the seal namespace, or in the default namesapce ?

By “namespaced objects”, we mean that the given object (in this case endpoints) must exist in a namespace.

If you create such objects without a

metadata:
  namespace: some-namespace

then it will be created in whatever the current namespace is in your context.

That is,
If you did

kubectl config set-context --current --namespace namespace-x
kubectl apply -f endpoint-without-namespace.yaml

it would be created in namespace-x

Then

kubectl config set-context --current --namespace namespace-y
kubectl apply -f endpoint-without-namespace.yaml

then another endpoint with the same spec would be created in namespace-y

Then

kubectl apply -n namespace-z -f endpoint-without-namespace.yaml

Then it would always be created in namespace-z regardless of what your context is set to.

Applies to all namespaced objects, like pods, services deployments etc.etc., and of course these namespaces must already exist or you get an error.

If you have not set a context, then the namespace is default

1 Like

Many thanks @Alistair_KodeKloud for your detail response.

Have a nice day!