Monitor Kubernetes cluster by running Boatswain as a DaemonSet
Tags: boatswain, docker, kubernetes
Published: 2020-01-09 09:04:45

---
title: "Monitor Kubernetes cluster by running Boatswain as a DaemonSet"
published: true
description: "Monitor you Kubernetes cluster with Boatswain."
tags: [Boatswain, Docker, Kubernetes]
cover_image: https://thepracticaldev.s3.amazonaws.com/i/vzilip00kj53c15glpb3.png
---

_Originally posted on [Boatswain Blog](https://blog.boatswain.io/post/monitor-k8s-cluster-by-running-boatswain-as-a-daemonset/)._

---

In a [Kubernetes][kubernetes] cluster, each node has a container runtime for running containers in pods. [Kubernetes][kubernetes] supports various [container runtimes](https://kubernetes.io/docs/setup/production-environment/container-runtimes/) including:

- [Docker][docker]
- [containerd][containerd]
- [CRI-O][cri-o]

So if the [Kubernetes][kubernetes] cluster is using [Docker][docker] as container runtime, we could install [Boatswain][boatswain] on the nodes and monitoring its status as well as collecting the container logs.

**NOTE: The example in this article is for experimental purpose and we do not suggest using [Boatswain][boatswain] in any production [Kubernetes][kubernetes] cluster.**

## The DaemonSet in Kubernetes

In [Kubernetes][kubernetes], we could deploy the [Boatswain][boatswain] pod as a [DaemonSet][daemonset]. Unlike a [Deployment][deployment] which we could replicates multiple pods across different nodes, a [DaemonSet][daemonset] makes sure that a single pod runs on the selected nodes even if the node is newly added to the cluster. It is particular useful for storage daemon, log collection and monitoring on the node.

## Running Boatswain as a DaemonSet

Here are the steps about setting up the [Boatswain][boatswain] [DaemonSet][daemonset] in a [Kubernetes][kubernetes] cluster.

1. Create the Boatswain namespace
1. Setup the Boatswain token as a secret
1. Setup the config file of the Boatswain DaemonSet
1. Start the Boatswain DaemonSet

### Create the Boatswain namespace

Let's create a new namespace to separate the [Boatswain][boatswain] setup from those already exist.

```bash
kubectl create namespace boatswain
```

### Setup the Boatswain token as a secret

This token is required for running [Boatswain][boatswain]. We store it as a secret called **boatswain** and later use it in the [DaemonSet][daemonset] configuration.

```bash
kubectl create secret generic boatswain --from-literal=token=<TO_BE_REPLACED> -n boatswain
```

### Setup the config file of the Boatswain DaemonSet

Here comes the [DaemonSet][daemonset] configuration.

`daemon-set.yaml`

```yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: boatswain
  namespace: boatswain
spec:
  selector:
    matchLabels:
      name: boatswain
  template:
    metadata:
      labels:
        name: boatswain
    spec:
      containers:
      - name: boatswain
        image: boatswainio/boatswain:<latest or tag>
        env:
        - name: BOATSWAIN_TOKEN
          valueFrom:
            secretKeyRef:
              name: boatswain
              key: token
        securityContext:
          capabilities:
            add: ["NET_ADMIN"]
        volumeMounts:
          - name: dockersock
            mountPath: "/var/run/docker.sock"
      volumes:
      - name: dockersock
        hostPath:
          path: /var/run/docker.sock
      hostNetwork: true
      hostPID: true
      restartPolicy: Always
      nodeSelector:
        kubernetes.io/hostname: <hostname of the selected node>
```

Before saving the yaml file:

1. Replace the version of [Boatswain][boatswain] at line 17.
1. Set the hostname of the target node which you want to install [Boatswain][boatswain] or remove line 37 and 38 for installation on all nodes.

### Start the Boatswain DaemonSet

Execute the following command to start the [Boatswain][boatswain] [DaemonSet][daemonset].

```bash
kubectl create -f daemon-set.yaml
```

Check the status of the [DaemonSet][daemonset].

```bash
[ykyuen@camus ~]$ kubectl get ds -n boatswain
NAME        DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR                                                        AGE
boatswain   1         1         1       1            1           kubernetes.io/hostname=gke-boatswain-default-pool-v2-1234abcd-5678   52s
```

And we could read the metrics and logs on [Boatswain][boatswain].

![Status of the Kubernetes node](https://thepracticaldev.s3.amazonaws.com/i/kbjqkmea9o1hlkunsvf8.png)

## Summary

The example above shows that it is possible to use [Boatswain][boatswain] to monitor the [Kuberenetes][kubernetes] cluster. But currently it might not able to show the network metrics (confirmed on GKE cluster above). It might be related to the security settings and so far we haven't investigated further about this issue. Moreover, running [Boatswain][boatswain] on all nodes does not work well as the usage will hit the 2GB data limit of the beta trial in a few hours. If you have any suggestions or thoughts, please let us know. :grinning:





[boatswain]: https://boatswain.io/
[kubernetes]: https://kubernetes.io/
[docker]: https://www.docker.com/
[containerd]: https://containerd.io/
[cri-o]: https://cri-o.io/
[daemonset]: https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/
[deployment]: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/