Best practices for defining Kubernetes namespaces and labels

3/6/2018

I am looking for some good examples about structuring namespaces in Kubernetes clusters, and an appropriate way to label my pods for long term maintainability.

Can you recommend some resources, or examples from real world applications that follow some solid practices?

-- Igor Šarčević
kubernetes

2 Answers

3/6/2018

A good starting point is Ahmet Alp Balkan's resource "kubernetes-network-policy-recipes".

https://github.com/ahmetb/kubernetes-network-policy-recipes/raw/master/img/1.gif

You can see examples of labels like one allowing traffic only to a port of an application:

https://github.com/ahmetb/kubernetes-network-policy-recipes/raw/712388dccdf9f9cbb6b9683c4e7564b6569d5060/img/9.gif

kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: api-allow-5000
spec:
  podSelector:
    matchLabels:
      app: apiserver
  ingress:
  - ports:
    - port: 5000
    from:
    - podSelector:
        matchLabels:
          role: monitoring

That helps illustrating a good label policy, ie one which helps your use case (that is: what you are trying to do as a network policy)

-- VonC
Source: StackOverflow

8/31/2018

Namespaces:

  • I recommend grouping resources by namespaces for "resources you can just delete altogether".

  • Most notably, Kubernetes Policy objects (like RBAC, PodSecurityPolicy, NetworkPolicy, ResourceQuota) are per-namespace. So "namespaces" are often for organizational/team boundary.

Labels:

  • These can be applied to any kind of object (incl. namespaces)
  • Think of labels that indicate logical grouping of objects.
  • For example you can have an app=paymentservice label for everything that empowers this app (Pods, Service, Secret)
  • You can also use labels to indicate:
    • version/commit numbers: gitcommit=235ab3f
    • purpose (especially useful while testing namespaces etc) purpose=testing
    • organizational boundary (again, for namespaces) team=payments
-- AhmetB - Google
Source: StackOverflow