# Error: fail to enroll: fail to execute request to fleet-server: status code: 400, fleet-server returned an error: BadRequest

**URL:** https://discuss.elastic.co/t/error-fail-to-enroll-fail-to-execute-request-to-fleet-server-status-code-400-fleet-server-returned-an-error-badrequest/302402
**Category:** Elastic Cloud on Kubernetes (ECK)
**Tags:** fleet
**Created:** [April 14, 2022, 8:26am UTC](https://discuss.elastic.co/t/error-fail-to-enroll-fail-to-execute-request-to-fleet-server-status-code-400-fleet-server-returned-an-error-badrequest/302402 "2022-04-14T08:26:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![tirelibirefe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tirelibirefe/32/71511_2.png) [@tirelibirefe](https://discuss.elastic.co/u/tirelibirefe)
#### Post date: [April 14, 2022, 8:26am UTC](https://discuss.elastic.co/t/error-fail-to-enroll-fail-to-execute-request-to-fleet-server-status-code-400-fleet-server-returned-an-error-badrequest/302402/1 "2022-04-14T08:26:08Z")

</div>

Hello,  
I am not able to run Fleet on Kubernetes.

Here is my config:

```auto
apiVersion: agent.k8s.elastic.co/v1alpha1
kind: Agent
metadata:
  name: $FLEETSERVERNAME
  namespace: $NAMESPACE
spec:
  version: 8.1.2
  mode: fleet
  fleetServerEnabled: true
  kibanaRef:
    name: $KIBANANAME
    namespace: $NAMESPACE
  elasticsearchRefs:
  - name: $ESCLUSTERNAME
    namespace: $NAMESPACE
  deployment:
    replicas: 1
    podTemplate:
      spec:
        serviceAccountName: elastic-agent
        automountServiceAccountToken: true
        securityContext:
          runAsUser: 0
        containers:
          - name: agent
            volumeMounts:
            - mountPath: /mnt/elastic-internal/kibana-association/logging/kibana/certs
              name: kibana-kb-es-ca
              readOnly: true
            - mountPath: /mnt/elastic-internal/fleetserver-association/logging/fleet-server/certs
              name: fleet-server-agent-http-certs-internal
              readOnly: true
            env:
              - name: FLEET_ENROLLMENT_TOKEN
                value: $FLEETENROLLMENTTOKEN
              - name: ELASTICSEARCH_HOST
                value: https://$ESCLUSTERNAME-es-http.$NAMESPACE.svc
              - name: ELASTICSEARCH_PORT
                value: "9200"
              - name: ELASTICSEARCH_USERNAME
                value: elastic
              - name: ELASTICSEARCH_PASSWORD
                value: $ESPASSWORD
              - name: FLEET_SERVER_SERVICE_TOKEN
                value: $FLEETSERVERSERVICETOKEN
              - name: FLEET_SERVER_POLICY_ID
                value: "eck-fleet-server"
              - name: NODE_NAME
                valueFrom:
                  fieldRef:
                    fieldPath: spec.nodeName
        volumes:
        - name: kibana-kb-es-ca
          secret:
            secretName: kibana-monitoring
        - name: fleet-server-agent-http-certs-internal
          secret:
            secretName: fleet-server-agent-http-certs-internal

```

...and the logs:

```auto
{"log.level":"info","@timestamp":"2022-04-14T08:20:15.789Z","log.origin":{"file.name":"cmd/run.go","file.line":185},"message":"Shutting down Elastic Agent and sending last events...","ecs.version":"1.6.0"}
{"log.level":"info","@timestamp":"2022-04-14T08:20:15.789Z","log.origin":{"file.name":"operation/operator.go","file.line":216},"message":"waiting for installer of pipeline 'default' to finish","ecs.version":"1.6.0"}
{"log.level":"info","@timestamp":"2022-04-14T08:20:15.790Z","log.origin":{"file.name":"process/app.go","file.line":176},"message":"Signaling application to stop because of shutdown: fleet-server--8.1.2","ecs.version":"1.6.0"}
{"log.level":"info","@timestamp":"2022-04-14T08:20:17.291Z","log.origin":{"file.name":"status/reporter.go","file.line":236},"message":"Elastic Agent status changed to: 'online'","ecs.version":"1.6.0"}
{"log.level":"info","@timestamp":"2022-04-14T08:20:17.291Z","log.origin":{"file.name":"cmd/run.go","file.line":193},"message":"Shutting down completed.","ecs.version":"1.6.0"}
{"log.level":"info","@timestamp":"2022-04-14T08:20:17.292Z","log.logger":"api","log.origin":{"file.name":"api/server.go","file.line":66},"message":"Stats endpoint (/usr/share/elastic-agent/state/data/tmp/elastic-agent.sock) finished: accept unix /usr/share/elastic-agent/state/data/tmp/elastic-agent.sock: use of closed network connection","ecs.version":"1.6.0"}
Error: fail to enroll: fail to execute request to fleet-server: status code: 400, fleet-server returned an error: BadRequest
For help, please see our troubleshooting guide at https://www.elastic.co/guide/en/fleet/8.1/fleet-troubleshooting.html
Error: enrollment failed: exit status 1
For help, please see our troubleshooting guide at https://www.elastic.co/guide/en/fleet/8.1/fleet-troubleshooting.html

```

Could you please advise which request supposed as bad by Fleet?

Thanks & Regards

---

<div class="post-metadata">

### Author: ![pebrc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pebrc/32/101790_2.png) [@pebrc](https://discuss.elastic.co/u/pebrc)
#### Post date: [April 19, 2022, 1:04pm UTC](https://discuss.elastic.co/t/error-fail-to-enroll-fail-to-execute-request-to-fleet-server-status-code-400-fleet-server-returned-an-error-badrequest/302402/2 "2022-04-19T13:04:11Z")

</div>

What I don't understand about your configuration is why are you recreating all these internal volume mounts and Elasticsearch output configuration environment variables? And where is the `FLEET_ENROLLMENT_TOKEN` coming from, have you created that ahead of time? I suspect this is causing the error if the token does not exist or is invalid?

The idea behind `elasticsearchRef` and `kibanaRef` is that you don't have to worry about all these configuration settings and the ECK operator manages those instead for you.

So a minimal Fleet Server Agent spec looks like this (taken directly from our Quickstart)

```nohighlight
apiVersion: agent.k8s.elastic.co/v1alpha1
kind: Agent
metadata:
  name: fleet-server-quickstart
  namespace: default
spec:
  version: 8.1.2
  kibanaRef:
    name: kibana-quickstart
  elasticsearchRefs:
  - name: elasticsearch-quickstart
  mode: fleet
  fleetServerEnabled: true
  deployment:
    replicas: 1
    podTemplate:
      spec:
        serviceAccountName: elastic-agent
        automountServiceAccountToken: true
        securityContext:
          runAsUser: 0

```

Have a look at [Quickstart | Elastic Cloud on Kubernetes [master] | Elastic](https://www.elastic.co/guide/en/cloud-on-k8s/master/k8s-elastic-agent-fleet-quickstart.html) for the full example.

You then also need an Agents and Elasticsearch and Kibana which is all in the Quickstart.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [May 17, 2022, 1:05pm UTC](https://discuss.elastic.co/t/error-fail-to-enroll-fail-to-execute-request-to-fleet-server-status-code-400-fleet-server-returned-an-error-badrequest/302402/3 "2022-05-17T13:05:07Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
