Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
Kubernetes Cronjob labeling

As I have seen few related posts but none answered my question, I thought I would ask a new question based on suggestions from other users as well here.

I have the need to make a selector label for a network policy for a running cronjob that is responsible to connect to some other services within the cluster, as far as I know there is no easy straight forward way to make a selector label for the jobs pod as that would be problematic with duplicate job labels if they ever existed. Not sure why the cronjob can't have a selector itself, and then can be applied to the job and the pod.

also there might be a possibility to just set this cronjob in its own namespace and then allow all from that one namespace to whatever needed in the network policy but does not feel like the right way to overcome that problem.

Using k8s v1.20

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

First of all, to select pods (spawned by your CronJob) that should be allowed by the NetworkPolicy as ingress sources or egress destinations, you may set specific label for those pods.

You can easily set a label for Jobs spawned by CronJob using labels field (another example with an explanation can be found in the OpenShift CronJobs documentation):

---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: mysql-test
spec:
...
  jobTemplate:             
    spec:
      template:
        metadata:
          labels:          
            workload: cronjob # Sets a label for jobs spawned by this CronJob.
            type: mysql       # Sets another label for jobs spawned by this CronJob.
...

Pods spawned by this CronJob will have the labels type=mysql and workload=cronjob, using this labels you can create/customize your NetworkPolicy:

$ kubectl get pods --show-labels
NAME                          READY   STATUS      RESTARTS   AGE    LABELS
mysql-test-1615216560-tkdvk   0/1     Completed   0          2m2s   ...,type=mysql,workload=cronjob
mysql-test-1615216620-pqzbk   0/1     Completed   0          62s    ...,type=mysql,workload=cronjob
mysql-test-1615216680-8775h   0/1     Completed   0          2s     ...,type=mysql,workload=cronjob

$ kubectl describe pod mysql-test-1615216560-tkdvk
Name:         mysql-test-1615216560-tkdvk
Namespace:    default
...
Labels:       controller-uid=af99e9a3-be6b-403d-ab57-38de31ac7a9d
              job-name=mysql-test-1615216560
              type=mysql
              workload=cronjob
...

For example this mysql-workload NetworkPolicy allows connections to all pods in the mysql namespace from any pod with the labels type=mysql and workload=cronjob (logical conjunction) in a namespace with the label namespace-name=default :
NOTE: Be careful to use correct YAML (take a look at this namespaceSelector and podSelector example).

---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: mysql-workload
  namespace: mysql
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          namespace-name: default
      podSelector:
        matchLabels:
          type: mysql
          workload: cronjob

To use network policies, you must be using a networking solution which supports NetworkPolicy:

Network policies are implemented by the network plugin. To use network policies, you must be using a networking solution which supports NetworkPolicy. Creating a NetworkPolicy resource without a controller that implements it will have no effect.

You can learn more about creating Kubernetes NetworkPolicies in the Network Policies documentation.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda