working with daemonset with node
Daemonsets: A DaemonSet ensures that all Nodes run a copy of a Pod. As nodes are added to the cluster, Pods are added to them. Examples of a daemon set would be running your logging or monitoring agent on your nodes.
daemonset.yaml
apiVersion: apps/v1beta2
kind: DaemonSet
metadata:
name: example-daemonset
namespace: default
labels:
k8s-app: example-daemonset
spec:
selector:
matchLabels:
name: example-daemonset
template:
metadata:
labels:
name: example-daemonset
spec:
#nodeSelector: minikube # Specify if you want to run on specific nodes
containers:
- name: example-daemonset
image: busybox
args:
- /bin/sh
- -c
- date; sleep 1000
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
terminationGracePeriodSeconds: 30
daemonset-infra-prod.yaml
apiVersion: apps/v1beta2
kind: DaemonSet
metadata:
name: prod-daemonset
namespace: default
labels:
k8s-app: prod-daemonset
spec:
selector:
matchLabels:
name: prod-daemonset
template:
metadata:
labels:
name: prod-daemonset
spec:
containers:
- name: prod-daemonset
image: busybox
args:
- /bin/sh
- -c
- date; echo "hello prod"; sleep 1000
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
terminationGracePeriodSeconds: 30
nodeSelector:
infra: "production"
daemonset-infra-dev.yaml
apiVersion: apps/v1beta2
kind: DaemonSet
metadata:
name: dev-daemonset
namespace: default
labels:
k8s-app: dev-daemonset
spec:
selector:
matchLabels:
name: dev-daemonset
template:
metadata:
labels:
name: dev-daemonset
spec:
containers:
- name: dev-daemonset
image: busybox
args:
- /bin/sh
- -c
- date; echo "hello dev"; ping xunyangit.wordpress.com -c 4; sleep 10
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
terminationGracePeriodSeconds: 30
nodeSelector:
infra: "development"
create 3 different daemonset
kubectl create -f daemonset.yaml
kubectl create -f daemonset-infra-prod.yaml
kubectl create -f daemonset-infra-development.yaml
localhost:~ xunyang$ kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
minikube Ready master 1d v1.10.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=minikube,node-role.kubernetes.io/master=
localhost:~ xunyang$ kubectl get daemonset
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
dev-daemonset 0 0 0 0 0 infra=development 16m
example-daemonset 1 1 1 1 1 <none> 52m
prod-daemonset 0 0 0 0 0 infra=production 52m
kubectl label node minikube infra=production
localhost:~ xunyang$ kubectl get daemonset
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
dev-daemonset 0 0 0 0 0 infra=development 17m
example-daemonset 1 1 1 1 1 <none> 53m
prod-daemonset 1 1 1 1 1 infra=production 53m
localhost:~ xunyang$ kubectl get daemonset
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
dev-daemonset 1 1 0 1 0 infra=development 18m
example-daemonset 1 1 1 1 1 <none> 54m
prod-daemonset 0 0 0 0 0 infra=production 54m
localhost:~ xunyang$ kubectl get daemonset
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
dev-daemonset 1 1 1 1 1 infra=development 18m
example-daemonset 1 1 1 1 1 <none> 54m
prod-daemonset 0 0 0 0 0 infra=production 54m
check pod logs
localhost:~ xunyang$ kubectl get pod
NAME READY STATUS RESTARTS AGE
dev-daemonset-ktdtn 1/1 Running 1 10m
example-daemonset-46t5j 1/1 Running 2 55m
localhost:~ xunyang$ kubectl logs dev-daemonset-ktdtn
Mon Apr 16 10:27:17 UTC 2018
hello dev
PING xunyangit.wordpress.com (192.0.78.13): 56 data bytes
64 bytes from 192.0.78.13: seq=0 ttl=61 time=158.607 ms
64 bytes from 192.0.78.13: seq=1 ttl=61 time=160.128 ms
64 bytes from 192.0.78.13: seq=2 ttl=61 time=162.701 ms
64 bytes from 192.0.78.13: seq=3 ttl=61 time=160.225 ms
--- xunyangit.wordpress.com ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 158.607/160.415/162.701 ms
localhost:~ xunyang$ kubectl logs dev-daemonset-ktdtn
Mon Apr 16 10:27:49 UTC 2018
hello dev
PING xunyangit.wordpress.com (192.0.78.12): 56 data bytes
64 bytes from 192.0.78.12: seq=0 ttl=61 time=155.594 ms
64 bytes from 192.0.78.12: seq=1 ttl=61 time=154.204 ms
64 bytes from 192.0.78.12: seq=2 ttl=61 time=155.583 ms
64 bytes from 192.0.78.12: seq=3 ttl=61 time=156.444 ms
--- xunyangit.wordpress.com ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 154.204/155.456/156.444 ms