Kubernetes-无法适应服务,但可以适应Pod

时间:2019-03-08 11:28:19

标签: elasticsearch curl kubernetes

我有Elasticsearch Pod和一项服务。我还有一个名为“网站”的Pod(相同的命名空间-默认),它想要获取Elasticsearch的html内容(使用curl)。

当我(使用exec)访问“网站” Pod并运行$curl -v elasticsearch-service时,我得到:

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

注意:10.111.159.30是elasticsearch-service IP。

但是当我运行$curl 172.15.0.5:9200(POD IP)时,我得到了正确的输出:

 $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"
}

由于某种原因,Elasticsearch的服务未重定向到els应用。

这是豆荚的味道:

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"

这是服务的Yaml:

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

我对K8很陌生,我做错了什么?

2 个答案:

答案 0 :(得分:2)

从服务yaml文件中看到的,您已添加

  selector:
    app: elasticsearch

,它将搜索app: elasticsearch个匹配的标签以创建端点。您需要按如下所示标记您的广告连播:

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"

然后,如果您运行

kubectl get endpoints elasticsearch-service

您将看到为您的应用程序创建的端点

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

有关更多信息,请检查此documentation

答案 1 :(得分:-1)

由于服务定义中的选择器,您需要使用 app:elasticsearch 标记广告连播:     selector: app: elasticsearch

编辑:官方docs

有一个很好的解释。