Last active
July 28, 2023 10:52
-
-
Save etoews/56d1b2a01daebd9691c62cdcadb1574b to your computer and use it in GitHub Desktop.
Code for Run Once DaemonSet on Kubernetes http://blog.phymata.com/2017/05/13/run-once-daemonset-on-kubernetes/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -euo pipefail | |
# gracefully handle the TERM signal sent when deleting the daemonset | |
trap 'exit' TERM | |
# do the work | |
# the /etc/hosts file of the node is mapped to /node/etc/hosts | |
cat /node/etc/hosts | |
# let the monitoring script know we're done' | |
echo "done" | |
# this is a workaround to prevent the container from exiting | |
# and k8s restarting the daemonset pod | |
while true; do sleep 1; done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: extensions/v1beta1 | |
kind: DaemonSet | |
metadata: | |
name: cat-etc-hosts | |
spec: | |
template: | |
metadata: | |
labels: | |
run-once-daemonset: cat-etc-hosts | |
spec: | |
containers: | |
- name: cat-etc-hosts | |
image: etoews/cat-etc-hosts | |
imagePullPolicy: Always | |
volumeMounts: | |
- name: etc-hosts | |
mountPath: /node/etc/hosts | |
readOnly: true | |
volumes: | |
- name: etc-hosts | |
hostPath: | |
path: /etc/hosts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM alpine:3.5 | |
COPY cat-etc-hosts.sh . | |
RUN chmod u+x cat-etc-hosts.sh | |
CMD ["./cat-etc-hosts.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
function main() { | |
kubectl apply -f daemonset.yaml | |
wait_for_pods cat-etc-hosts | |
wait_for_cats cat-etc-hosts | |
kubectl delete -f daemonset.yaml | |
} | |
function wait_for_pods() { | |
echo -n "waiting for $1 pods to run" | |
PODS=$(kubectl get pods | grep $1 | awk '{print $1}') | |
for POD in ${PODS}; do | |
while [[ $(kubectl get pod ${POD} -o go-template --template "{{.status.phase}}") != "Running" ]]; do | |
sleep 1 | |
echo -n "." | |
done | |
done | |
echo | |
} | |
function wait_for_cats() { | |
echo -n "waiting for $1 daemonset to complete" | |
PODS=$(kubectl get pods | grep $1 | awk '{print $1}') | |
for POD in ${PODS}; do | |
while [[ $(kubectl logs ${POD} --tail 1) != "done" ]]; do | |
sleep 1 | |
echo -n "." | |
done | |
# at this point you could take the output of kubectl logs and do something with it | |
done | |
echo | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
پینترست