![]() |
VOOZH | about |
A DaemonSet is a workload object that ensures a copy of a Pod runs on all (or a specific subset of) nodes in a cluster. When a new node joins the cluster, the DaemonSet controller automatically deploys the Pod to it. When a node is removed, the Pod is garbage collected.
DaemonSets are essential for deploying cluster-wide, node-level services. Common use cases include:
Save the following as node-monitor-ds.yaml.
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-monitor-daemonset
spec:
selector:
matchLabels:
app: node-monitor
template:
metadata:
labels:
app: node-monitor
spec:
containers:
- name: monitor-agent
image: busybox
# This command simulates an agent running forever
command: ["/bin/sh", "-c", "while true; do echo 'Monitoring node...'; sleep 10; done"]
$ kubectl apply -f node-monitor-ds.yamlNow, check the status of the DaemonSet and list the Pods it created
$ kubectl get daemonset$ kubectl get pods -o wide
An Init Container is a special container that runs to completion before the main application containers in a Pod are started. You can have multiple Init Containers, and they will run sequentially—the second one will not start until the first one has finished successfully.
Init Containers are perfect for handling prerequisite tasks that must be completed before your main application starts.
A Kubernetes sidecar container is simply another container that will coexist with the primary application container in a pod.
Below is the step-by-step implementation of Kubernetes sidecar container logging:
First, you have to make a file called pod-with-sidecar. yaml and fill it with the information below.
apiVersion: v1
kind: Pod
metadata:
name: my-app-pod
spec:
containers:
- name: main-app
image: your-main-app-image
volumeMounts:
- name: shared-logs
mountPath: /var/log/myapp
# Ensure the app writes logs to /var/log/myapp
- name: log-collector
image: fluent/fluentd:v1.12-1
volumeMounts:
- name: shared-logs
mountPath: /var/log/myapp
resources:
limits:
memory: "200Mi"
cpu: "100m"
env:
- name: FLUENTD_ARGS
value: "--no-supervisor -q"
- name: FLUENTD_CONF
value: "fluent.conf"
volumes:
- name: shared-logs
emptyDir: {}
Then, Make a fluent.conf configuration file for Fluentd and fill it with the following information.
<source>
@type tail
path /var/log/myapp/*.log
pos_file /var/log/fluentd.pos
tag myapp.*
format none
</source>
<match myapp.**>
@type stdout
</match>
You can then run the pod using Kubectl in the following step.
kubectl apply -f pod-with-sidecar.yamlOutput:
To mount the ConfigMap, you have to update pod-with-sidecar. yaml.
apiVersion: v1
kind: Pod
metadata:
name: my-app-pod
spec:
containers:
- name: main-app
image: your-main-app-image
volumeMounts:
- name: shared-logs
mountPath: /var/log/myapp
- name: log-collector
image: fluent/fluentd:v1.12-1
volumeMounts:
- name: shared-logs
mountPath: /var/log/myapp
- name: fluentd-config
mountPath: /fluentd/etc
resources:
limits:
memory: "200Mi"
cpu: "100m"
env:
- name: FLUENTD_ARGS
value: "--no-supervisor -q"
- name: FLUENTD_CONF
value: "fluent.conf"
volumes:
- name: shared-logs
emptyDir: {}
- name: fluentd-config
configMap:
name: fluentd-config
Next, you must verify the pod is operational.
kubectl get podsOutput:
Lastly, you can see the logs that Fluentd generated.
kubectl logs my-app-pod -c log-collectorOutput: