Skip to content

Instantly share code, notes, and snippets.

@JohnKDay
Created January 18, 2020 00:32
Show Gist options
  • Save JohnKDay/3318b27b45be4f9f7be4bcc8fce7965b to your computer and use it in GitHub Desktop.
Save JohnKDay/3318b27b45be4f9f7be4bcc8fce7965b to your computer and use it in GitHub Desktop.
Compare date and time of all nodes from a Kubernetes container
---
apiVersion: apps/v1beta2
kind: DaemonSet
metadata:
name: datetimecheck
spec:
selector:
matchLabels:
name: datetimecheck
template:
metadata:
labels:
name: datetimecheck
spec:
initContainers:
- name: datechecker
image: busybox
command: ["sh"]
args: ["-c", "hostname; echo \" Website\" $(wget https://google.com -qS 2>&1 | grep -i date | head -1); date -u +\"Container Date: %a, %d %b %Y %T %Z\"; echo"]
containers:
- name: pause
image: gcr.io/google_containers/pause
@JohnKDay
Copy link
Author

JohnKDay commented Jan 18, 2020

Simple yaml file for k8s to create a Daemonset that will run on all of the nodes and check between internet time and local container time derived from k8s node.

kubectl apply -f datecheck.yaml
or

kubectl apply -f \
https://gist.githubusercontent.com/JohnKDay/3318b27b45be4f9f7be4bcc8fce7965b/raw/a9b2c17ebf5da4f0d975847ff6933a1b38963b70/datecheck.yaml

(above script in raw mode)

after a bit check the logs

kubectl logs -l name=datetimecheck --all-containers

Response should look like:

datetimecheck-gd6z2
  Website Date: Sat, 18 Jan 2020 00:40:31 GMT
Container Date: Sat, 18 Jan 2020 00:40:30 UTC

datetimecheck-gpj5l
  Website Date: Sat, 18 Jan 2020 00:40:31 GMT
Container Date: Sat, 18 Jan 2020 00:40:30 UTC

datetimecheck-h8fkr
  Website Date: Sat, 18 Jan 2020 00:40:31 GMT
Container Date: Sat, 18 Jan 2020 00:40:30 UTC

datetimecheck-jpm66
  Website Date: Sat, 18 Jan 2020 00:40:31 GMT
Container Date: Sat, 18 Jan 2020 00:40:31 UTC

datetimecheck-qklcl
  Website Date: Sat, 18 Jan 2020 00:40:31 GMT
Container Date: Sat, 18 Jan 2020 00:40:30 UTC

And don't forget to delete the Daemonset when finished

kubectl delete daemonsets datetimecheck
or delete pods to rerun
kubectl delete pods -l name=datetimecheck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment