Error during checkin with fleet-server

I am trying to enroll an Elastic Agent with Elasticsearch, but my agent is in a Kubernetes cluster. I am using the same YAML that Kibana generated for me, but my container shows this error:
{"log.level":"warn","@timestamp":"2025-03-12T11:04:13.961Z","log.origin":{"function":"github.com/elastic/elastic-agent/internal/pkg/agent/application/gateway/fleet.(*FleetGateway).doExecute","file.name":"fleet/fleet_gateway.go","file.line":183},"message":"Possible transient error during checkin with fleet-server, retrying","log":{"source":"elastic-agent"},"error":{"message":"fail to checkin to fleet-server: all hosts failed: requester 0/1 to host https://172.19.198.21:8220/ errored: Post \"https://172.19.198.21:8220/api/fleet/agents/18102197-709f-4cfe-9fe4-7e9729967535/checkin?\": invalid api key to authenticate with fleet"},"request_duration_ns":6583002,"failed_checkins":1,"retry_after_ns":116975884833,"ecs.version":"1.6.0"}
When I try to enroll on a server that is not in the Kubernetes cluster, it works fine—the token used is valid and functioning. However, when trying from within the cluster, this error appears. I modified the YAML and set it up as a deployment with one replica instead of a DaemonSet.