# ElasticClient unable to connect to Elasticsearch server running on k8s

**URL:** <https://discuss.elastic.co/t/elasticclient-unable-to-connect-to-elasticsearch-server-running-on-k8s/356283>\
**Category:** Elasticsearch\
**Tags:** docker\
**Created:** [March 27, 2024, 12:35pm UTC](https://discuss.elastic.co/t/elasticclient-unable-to-connect-to-elasticsearch-server-running-on-k8s/356283 "2024-03-27T12:35:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![roamik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roamik/32/133035_2.png) [@roamik](https://discuss.elastic.co/u/roamik)\
**Post date:** [March 27, 2024, 12:35pm UTC](https://discuss.elastic.co/t/elasticclient-unable-to-connect-to-elasticsearch-server-running-on-k8s/356283/1 "2024-03-27T12:35:03Z")

</div>

Hi  
I have deployed Elasticsearch locally to Docker Desktop Kubernetes as following:  
install operator:  
kubectl create -f {linktoEck}/eck/2.11.1/crds.yaml  
kubectl apply -f {linktoEck}/eck/2.11.1/operator.yaml

then I deployed my .Net service which have a connection line to ElasticClient in Program.cs:

```auto
var connSettings = new ConnectionSettings(new Uri("https://elasticlocal.com/"))
    .BasicAuthentication("elastic", "testPassword")
    .ThrowExceptions()
    .EnableApiVersioningHeader();

builder.Services.AddSingleton<IElasticClient>(new ElasticClient(connSettings));

```

but when running this code with ExceptionDispatchInfo:

```auto
 var bulkAllObservable = _elasticClient.BulkAll(personIndexes, b => b
 .Index("persons"));

```

I get an exception telling this:

```auto
Unsuccessful () low level call on GET: /
# Audit trail of this API call:
# OriginalException: System.Net.Http.HttpRequestException: Connection refused (elasticlocal.com:443)
 ---> System.Net.Sockets.SocketException (111): Connection refused
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
   at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|285_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(QueueItem queueItem)
   at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at Elasticsearch.Net.HttpConnection.RequestAsync[TResponse](RequestData requestData, CancellationToken cancellationToken)

```

Elasticsearch file:

```auto
apiVersion: elasticsearch.k8s.elastic.co/v1
kind: Elasticsearch
metadata:
  name: elastic-cluster
spec:
  version: 8.12.2
  nodeSets:
  - name: default
    count: 1
    config:
      node.store.allow_mmap: false
  http:
    tls:
      selfSignedCertificate:
        subjectAltNames:
        - ip: 0.0.0.0
        - dns: elasticlocal.com

```

[elasticlocal.com](http://elasticlocal.com) is handled by Ingress and is working fine (I am able to connect via Postman - [elasticlocal.com](http://elasticlocal.com):

```auto
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: elastic-cluster
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/backend-protocol: HTTPS
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - elasticlocal.com
    secretName: my-new-tls-secret
  rules:
  - host: elasticlocal.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: elastic-cluster-es-http
            port:
              number: 9200

```

I have tried disabling certificates and it worked fine via Postman requests and via curl  
but from .Net service it still give the same error

---

<div class="post-metadata">

**Author:** ![roamik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roamik/32/133035_2.png) [@roamik](https://discuss.elastic.co/u/roamik)\
**Post date:** [March 27, 2024, 4:21pm UTC](https://discuss.elastic.co/t/elasticclient-unable-to-connect-to-elasticsearch-server-running-on-k8s/356283/2 "2024-03-27T16:21:54Z")

</div>

Actually now I am able to connect with using this configuration in code

```auto
var connSettings = new ConnectionSettings(new Uri("https://elastic-cluster-es-http.default.svc.cluster.local:9200"))
    .BasicAuthentication("elastic", "testPassword")
    .ThrowExceptions()
    .EnableApiVersioningHeader()
    .ServerCertificateValidationCallback((o, certificate, chain, errors) => true)
    .ServerCertificateValidationCallback(CertificateValidations.AllowAll);

```

---

<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:** [April 24, 2024, 4:22pm UTC](https://discuss.elastic.co/t/elasticclient-unable-to-connect-to-elasticsearch-server-running-on-k8s/356283/3 "2024-04-24T16:22:09Z")

</div>

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