简体   繁体   中英

Kubernetes - Can't curl to Service, but I can to Pod

I have Elasticsearch Pod, and a Service. I also have another Pod (same namespace - default) called "website", that want to get the html content of the Elasticsearch (using curl).

When I access the "website" Pod (using exec), and run $curl -v elasticsearch-service ,I get:

curl -v elasticsearch-service
* Rebuilt URL to: elasticsearch-service/
*   Trying 10.111.159.30...
* TCP_NODELAY set

note: 10.111.159.30 is the elasticsearch-service IP.

But when I run $curl 172.15.0.5:9200 (POD IP), I get the correct output:

 $curl 172.15.0.5:9200
{
  "name" : "1X7ZARS",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "V8GX-5STSoW_PJ8Qguu7Jw",
  "version" : {
    "number" : "6.6.1",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "1fd8f69",
    "build_date" : "2019-02-13T17:10:04.160291Z",
    "build_snapshot" : false,
    "lucene_version" : "7.6.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

For some reason, Elasticsearch's service, doesn't redirect to the els app.

This is the yaml of the Pod:

apiVersion: v1
kind: Pod
metadata:
  name: elasticsearch
spec:
  containers:
  - name: elasticsearch
    image: yageldhn/els-java8:6.6.1
    ports:
    - containerPort: 9200
    - containerPort: 9300
    env:
    - name: discovery.type
      value: "single-node"

This is the yaml of the service:

apiVersion: v1
kind: Service
metadata:
  name: elasticsearch-service
spec:
  ports:
  - port: 9200
    name: serving
    protocol: TCP
  - port: 9300
    name: node2node
    protocol: TCP
  selector:
    app: elasticsearch

I'm pretty new with K8s, what I did wrong?

As you see from your service yaml file, you added

  selector:
    app: elasticsearch

which will search app: elasticsearch matched labels in order to create endpoints. You need to label your pod as below:

apiVersion: v1
kind: Pod
metadata:
  name: elasticsearch
  labels:
    app: elasticsearch  # Should match with service selector
spec:
  containers:
  - name: elasticsearch
    image: yageldhn/els-java8:6.6.1
    ports:
    - containerPort: 9200
    - containerPort: 9300
    env:
    - name: discovery.type
      value: "single-node"

Then if you run

kubectl get endpoints elasticsearch-service

You will see that endpoints created for your applications

NAME                    ENDPOINTS                         AGE
elasticsearch-service   172.17.0.3:9200,172.17.0.3:9300   1m

For more info check this documentation

You need to label your pod with app: elasticsearch because of the selector in you service definition: selector: app: elasticsearch

Edit: There is a pretty good explanation in the official docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM