Skip to content

Instantly share code, notes, and snippets.

@bantic
Last active January 28, 2022 11:57
Show Gist options
  • Save bantic/7a714ab4be6dfb61a1cada8b597cabe1 to your computer and use it in GitHub Desktop.
Save bantic/7a714ab4be6dfb61a1cada8b597cabe1 to your computer and use it in GitHub Desktop.

Revisions

  1. bantic revised this gist Jan 28, 2022. No changes.
  2. bantic revised this gist Jan 28, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ Example showing how to use a StatefulSet in coordination with a CronJob to run a
    Download k8s.yaml locally and then run:

    ```
    $ kubectl apply -f k8s.yaml && \
    kubectl apply -f k8s.yaml && \
    kubectl rollout status statefulset/web && \
    sleep 65 && \
    for podNum in 0 1 2; do echo -n "Pod $podNum: "; kubectl exec pod/web-$podNum -- ls -1 / | grep cron || echo "<notfound>"; done
  3. bantic revised this gist Jan 28, 2022. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions k8s.yaml
    Original file line number Diff line number Diff line change
    @@ -7,13 +7,13 @@ metadata:
    spec:
    selector:
    matchLabels:
    app: nginx
    app: nginx # has to match .spec.template.metadata.labels
    serviceName: "nginx"
    replicas: 3
    replicas: 3 # by default is 1
    template:
    metadata:
    labels:
    app: nginx
    app: nginx # has to match .spec.selector.matchLabels
    spec:
    containers:
    - name: nginx
    @@ -38,5 +38,5 @@ spec:
    command:
    - /bin/sh
    - -c
    - kubectl exec pod/web-1 -- "touch /FROMCRON"
    restartPolicy: OnFailure
    - kubectl exec pod/web-1 -- touch ran-cron
    restartPolicy: OnFailure
  4. bantic created this gist Jan 28, 2022.
    23 changes: 23 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    Example showing how to use a StatefulSet in coordination with a CronJob to run a scheduled command on a *specific* pod.

    Download k8s.yaml locally and then run:

    ```
    $ kubectl apply -f k8s.yaml && \
    kubectl rollout status statefulset/web && \
    sleep 65 && \
    for podNum in 0 1 2; do echo -n "Pod $podNum: "; kubectl exec pod/web-$podNum -- ls -1 / | grep cron || echo "<notfound>"; done
    ```

    Expected output:
    ```
    statefulset.apps/web created
    cronjob.batch/crontest created
    Waiting for 3 pods to be ready...
    Waiting for 2 pods to be ready...
    Waiting for 1 pods to be ready...
    partitioned roll out complete: 3 new pods have been updated...
    Pod 0: <notfound>
    Pod 1: ran-cron // <-- cronjob runs against pod 1 only
    Pod 2: <notfound>
    ```
    42 changes: 42 additions & 0 deletions k8s.yaml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    # Slightly modified from the StatefulSet example at
    # https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
    name: web
    spec:
    selector:
    matchLabels:
    app: nginx
    serviceName: "nginx"
    replicas: 3
    template:
    metadata:
    labels:
    app: nginx
    spec:
    containers:
    - name: nginx
    image: alpine:latest
    command: [ "/bin/sh", "-c", "--" ]
    args: [ "while true; do sleep 30; done;" ]
    ---
    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
    name: crontest
    spec:
    schedule: "* * * * *"
    jobTemplate:
    spec:
    template:
    spec:
    containers:
    - name: crontest
    image: alpine/k8s:1.20.7
    imagePullPolicy: IfNotPresent
    command:
    - /bin/sh
    - -c
    - kubectl exec pod/web-1 -- "touch /FROMCRON"
    restartPolicy: OnFailure