github.com/qsunny/k8s@v0.0.0-20220101153623-e6dca256d5bf/examples-master/staging/newrelic/README.md (about) 1 ## New Relic Server Monitoring Agent Example 2 3 This example shows how to run a New Relic server monitoring agent as a pod in a DaemonSet on an existing Kubernetes cluster. 4 5 This example will create a DaemonSet which places the New Relic monitoring agent on every node in the cluster. It's also fairly trivial to exclude specific Kubernetes nodes from the DaemonSet to just monitor specific servers. 6 7 ### Step 0: Prerequisites 8 9 This process will create privileged containers which have full access to the host system for logging. Beware of the security implications of this. 10 11 If you are using a Salt based KUBERNETES\_PROVIDER (**gce**, **vagrant**, **aws**), you should make sure the creation of privileged containers via the API is enabled. Check `cluster/saltbase/pillar/privilege.sls`. 12 13 DaemonSets must be enabled on your cluster. Instructions for enabling DaemonSet can be found [here](https://kubernetes.io/docs/api.md#enabling-the-extensions-group). 14 15 ### Step 1: Configure New Relic Agent 16 17 The New Relic agent is configured via environment variables. We will configure these environment variables in a sourced bash script, encode the environment file data, and store it in a secret which will be loaded at container runtime. 18 19 The [New Relic Linux Server configuration page] 20 (https://docs.newrelic.com/docs/servers/new-relic-servers-linux/installation-configuration/configuring-servers-linux) lists all the other settings for nrsysmond. 21 22 To create an environment variable for a setting, prepend NRSYSMOND_ to its name. For example, 23 24 ```console 25 loglevel=debug 26 ``` 27 28 translates to 29 30 ```console 31 NRSYSMOND_loglevel=debug 32 ``` 33 34 Edit examples/newrelic/nrconfig.env and set up the environment variables for your NewRelic agent. Be sure to edit the license key field and fill in your own New Relic license key. 35 36 Now, let's vendor the config into a secret. 37 38 ```console 39 $ cd examples/newrelic/ 40 $ ./config-to-secret.sh 41 ``` 42 43 <!-- BEGIN MUNGE: EXAMPLE newrelic-config-template.yaml --> 44 45 ```yaml 46 apiVersion: v1 47 kind: Secret 48 metadata: 49 name: newrelic-config 50 type: Opaque 51 data: 52 config: {{config_data}} 53 ``` 54 55 [Download example](newrelic-config-template.yaml?raw=true) 56 <!-- END MUNGE: EXAMPLE newrelic-config-template.yaml --> 57 58 The script will encode the config file and write it to `newrelic-config.yaml`. 59 60 Finally, submit the config to the cluster: 61 62 ```console 63 $ kubectl create -f examples/newrelic/newrelic-config.yaml 64 ``` 65 66 ### Step 2: Create the DaemonSet definition. 67 68 The DaemonSet definition instructs Kubernetes to place a newrelic sysmond agent on each Kubernetes node. 69 70 <!-- BEGIN MUNGE: EXAMPLE newrelic-daemonset.yaml --> 71 72 ```yaml 73 apiVersion: apps/v1 # for k8s versions before 1.9.0 use apps/v1beta2 and before 1.8.0 use extensions/v1beta1 74 kind: DaemonSet 75 metadata: 76 name: newrelic-agent 77 labels: 78 tier: monitoring 79 app: newrelic-agent 80 version: v1 81 spec: 82 selector: 83 matchLabels: 84 name: newrelic 85 template: 86 metadata: 87 labels: 88 name: newrelic 89 spec: 90 # Filter to specific nodes: 91 # nodeSelector: 92 # app: newrelic 93 hostPID: true 94 hostIPC: true 95 hostNetwork: true 96 containers: 97 - resources: 98 requests: 99 cpu: 0.15 100 securityContext: 101 privileged: true 102 env: 103 - name: NRSYSMOND_logfile 104 value: "/var/log/nrsysmond.log" 105 image: newrelic/nrsysmond 106 name: newrelic 107 command: [ "bash", "-c", "source /etc/kube-newrelic/config && /usr/sbin/nrsysmond -E -F" ] 108 volumeMounts: 109 - name: newrelic-config 110 mountPath: /etc/kube-newrelic 111 readOnly: true 112 - name: dev 113 mountPath: /dev 114 - name: run 115 mountPath: /var/run/docker.sock 116 - name: sys 117 mountPath: /sys 118 - name: log 119 mountPath: /var/log 120 volumes: 121 - name: newrelic-config 122 secret: 123 secretName: newrelic-config 124 - name: dev 125 hostPath: 126 path: /dev 127 - name: run 128 hostPath: 129 path: /var/run/docker.sock 130 - name: sys 131 hostPath: 132 path: /sys 133 - name: log 134 hostPath: 135 path: /var/log 136 ``` 137 138 [Download example](newrelic-daemonset.yaml?raw=true) 139 <!-- END MUNGE: EXAMPLE newrelic-daemonset.yaml --> 140 141 The daemonset instructs Kubernetes to spawn pods on each node, mapping /dev/, /run/, /sys/, and /var/log to the container. It also maps the secrets we set up earlier to /etc/kube-newrelic/config, and sources them in the startup script, configuring the agent properly. 142 143 #### DaemonSet customization 144 145 - To include a custom hostname prefix (or other per-container environment variables that can be generated at run-time), you can modify the DaemonSet `command` value: 146 147 ``` 148 command: [ "bash", "-c", "source /etc/kube-newrelic/config && export NRSYSMOND_hostname=mycluster-$(hostname) && /usr/sbin/nrsysmond -E -F" ] 149 ``` 150 151 When the New Relic agent starts, `NRSYSMOND_hostname` is set using the output of `hostname` with `mycluster` prepended. 152 153 154 ### Known issues 155 156 It's a bit cludgy to define the environment variables like we do here in these config files. There is [another issue](https://github.com/kubernetes/kubernetes/issues/4710) to discuss adding mapping secrets to environment variables in Kubernetes. 157 158 <!-- BEGIN MUNGE: GENERATED_ANALYTICS --> 159 []() 160 <!-- END MUNGE: GENERATED_ANALYTICS -->