|
| 1 | +# Run/Test local build of agent on k8s cluster |
| 2 | + |
| 3 | +## Prerequisites |
| 4 | + |
| 5 | +- Install [skaffold](https://skaffold.dev/docs/install/) |
| 6 | +- Install [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl) |
| 7 | +- Install a local k8s distribution and create a cluster: |
| 8 | + - [kind](https://kind.sigs.k8s.io/docs/user/quick-start/#installation) |
| 9 | + - [k3d](https://k3d.io/v5.5.1/#installation) |
| 10 | + - [minikube](https://minikube.sigs.k8s.io/docs/start/) (not tested) |
| 11 | + |
| 12 | +## Quickstart (the hard way) |
| 13 | + |
| 14 | +#### Standalone or managed mode |
| 15 | +There are 2 distinct profiles available (pretty self-explanatory): |
| 16 | +- `elastic-agent-standalone` |
| 17 | +- `elastic-agent-managed` |
| 18 | + |
| 19 | +One of those profiles must always be specified in a skaffold command (using the `-p` or `--profile` option), for sake of brevity we are gonna list only examples with standalone profile |
| 20 | + |
| 21 | +### Prepare environment variables |
| 22 | +In order to run agent on a local k8s cluster we need to set some environment variables in `deploy/skaffold/.env` (we can use `.env.example`). Those environment variables must point to a running elastic stack installation. |
| 23 | + |
| 24 | +#### Standalone mode |
| 25 | +```shell |
| 26 | +# standalone elastic agent vars |
| 27 | +ES_HOST=https://<elasticsearch host>:443 |
| 28 | +ES_USERNAME=elastic |
| 29 | +ES_PASSWORD=changeme |
| 30 | +``` |
| 31 | + |
| 32 | +#### Managed mode |
| 33 | +```shell |
| 34 | +# managed elastic agent vars |
| 35 | +FLEET_URL=https://<fleet host>:443 |
| 36 | +FLEET_ENROLLMENT_TOKEN=<enrollment token> |
| 37 | +``` |
| 38 | + |
| 39 | +### Run agent |
| 40 | + |
| 41 | +In order to just deploy agent on your local cluster, open a terminal and then execute |
| 42 | + |
| 43 | + |
| 44 | +```shell |
| 45 | +skaffold run -p elastic-agent-standalone |
| 46 | +``` |
| 47 | +and the output should be something similar to this: |
| 48 | +```shell |
| 49 | +Generating tags... |
| 50 | + ... |
| 51 | +Checking cache... |
| 52 | + - docker.elastic.co/beats/elastic-agent: Found Locally |
| 53 | +Starting test... |
| 54 | +Starting pre-render hooks... |
| 55 | +Completed pre-render hooks |
| 56 | +Tags used in deployment: |
| 57 | + ... |
| 58 | +Starting deploy... |
| 59 | + ... |
| 60 | + - serviceaccount/elastic-agent-standalone created |
| 61 | + - role.rbac.authorization.k8s.io/elastic-agent-standalone created |
| 62 | + - role.rbac.authorization.k8s.io/elastic-agent-standalone-kubeadm-config created |
| 63 | + - clusterrole.rbac.authorization.k8s.io/elastic-agent-standalone created |
| 64 | + - rolebinding.rbac.authorization.k8s.io/elastic-agent-standalone created |
| 65 | + - rolebinding.rbac.authorization.k8s.io/elastic-agent-standalone-kubeadm-config created |
| 66 | + - clusterrolebinding.rbac.authorization.k8s.io/elastic-agent-standalone created |
| 67 | + - configmap/agent-node-datastreams created |
| 68 | + - configmap/fleet-es-configmap-95bb9gfkkt created |
| 69 | + - daemonset.apps/elastic-agent-standalone created |
| 70 | +Waiting for deployments to stabilize... |
| 71 | +Deployments stabilized in 14.411071ms |
| 72 | +You can also run [skaffold run --tail] to get the logs |
| 73 | +``` |
| 74 | + |
| 75 | +Once you are done, remove the deployments with: |
| 76 | +```shell |
| 77 | +skaffold delete -p elastic-agent-standalone |
| 78 | +``` |
| 79 | + |
| 80 | + |
| 81 | +### Debug agent |
| 82 | +The elastic agent can be debugged by connecting a debugger client to the debugger port (forwarded automatically on localhost). |
| 83 | + |
| 84 | +If we run the command below (the `--tail=false` is there only to disable the streaming of logs on stdout): |
| 85 | +```shell |
| 86 | +skaffold debug -p elastic-agent-standalone --tail=false |
| 87 | +``` |
| 88 | +we should have something similar to the output below |
| 89 | + |
| 90 | +```shell |
| 91 | +Generating tags... |
| 92 | + ... |
| 93 | +Checking cache... |
| 94 | + ... |
| 95 | +Starting pre-render hooks... |
| 96 | +Completed pre-render hooks |
| 97 | +Tags used in deployment: |
| 98 | + ... |
| 99 | +Starting deploy... |
| 100 | +Loading images into kind cluster nodes... |
| 101 | + ... |
| 102 | +Images loaded in 35.802479ms |
| 103 | + ... <a bunch of k8s resources created> |
| 104 | +Waiting for deployments to stabilize... |
| 105 | +Deployments stabilized in 7.654873ms |
| 106 | +Listing files to watch... |
| 107 | + - docker.elastic.co/beats/elastic-agent |
| 108 | +Press Ctrl+C to exit |
| 109 | +Not watching for changes... |
| 110 | +WARN[0003] unable to get owner from reference: {apps/v1 DaemonSet elastic-agent-standalone 0c579ec4-319a-4e85-99ec-c409260cb6ce 0xc000f950a0 0xc000f950a1} subtask=-1 task=DevLoop |
| 111 | +Port forwarding pod/elastic-agent-standalone-lgr9d in namespace kube-system, remote port 56268 -> http://127.0.0.1:56268 |
| 112 | +``` |
| 113 | +The last line of the output tells us where to connect our debugger ;) |
| 114 | +(it's always 56268 unless that port is busy, in which case skaffold will try to increment it until it finds one that can be bound). |
| 115 | +The debug session will continue until we hit `Ctrl+C` to stop the debug and start the cleanup |
| 116 | + |
| 117 | +## Quickstart (the easy way) |
| 118 | + |
| 119 | +Check [Google cloud code extension](https://cloud.google.com/code/docs) to do away with the terminal and to have Run/Debug configuration directly in your IDE (if supported) |
| 120 | + |
0 commit comments