同一名称空间中的两个Kubernetes部署无法通信

时间:2019-05-09 09:27:07

标签: kubernetes

我正在将ELK堆栈(oss)部署到kubernetes集群。 Elasticsearch部署和服务正确启动,并且API可达。 Kibana部署开始,但无法访问elasticsearch:

从Kibana容器日志中:

{"type":"log","@timestamp":"2019-05-08T22:49:26Z","tags":["error","elasticsearch","admin"],"pid":1,"message":"Request error, retrying\nHEAD http://elasticsearch:9200/ => getaddrinfo ENOTFOUND elasticsearch elasticsearch:9200"}
{"type":"log","@timestamp":"2019-05-08T22:50:44Z","tags":["warning","elasticsearch","admin"],"pid":1,"message":"Unable to revive connection: http://elasticsearch:9200/"}
{"type":"log","@timestamp":"2019-05-08T22:50:44Z","tags":["warning","elasticsearch","admin"],"pid":1,"message":"No living connections"}

两个部署都在相同的名称空间“可观察性”中。我还尝试将Elasticsearch容器引用为 elasticsearch.observability.svc.cluster.local ,但它也无法正常工作。

我做错了什么?如何从kibana容器中引用elasticsearch容器?

更多信息:

  

kubectl --context = 19team-observability-admin-context -n可观察性获取窗格

NAME                            READY     STATUS    RESTARTS   AGE
elasticsearch-9d495b84f-j2297   1/1       Running   0          15s
kibana-65bc7f9c4-s9cv4          1/1       Running   0          15s
  

kubectl --context = 19team-observability-admin-context -n可观察性获取服务

NAME            TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)                         AGE
elasticsearch   NodePort   10.104.250.175   <none>        9200:30083/TCP,9300:30059/TCP   1m
kibana          NodePort   10.102.124.171   <none>        5601:30124/TCP                  1m

我用命令启动容器

  

kubectl --context = 19team-observability-admin-context -n可观察性应用-f。\ elasticsearch.yaml -f。\ kibana.yaml

elasticsearch.yaml

apiVersion: v1
kind: Service
metadata:
  name: elasticsearch
  namespace: observability
spec:
  type: NodePort
  ports:
  - name: "9200"
    port: 9200
    targetPort: 9200
  - name: "9300"
    port: 9300
    targetPort: 9300
  selector:
    app: elasticsearch
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: elasticsearch
  namespace: observability
spec:
  replicas: 1
  selector:
    matchLabels:
      app: elasticsearch
  template:
    metadata:
      labels:
        app: elasticsearch
    spec:
      initContainers:
      - name: set-vm-max-map-count
        image: busybox
        imagePullPolicy: IfNotPresent
        command: ['sysctl', '-w', 'vm.max_map_count=262144']
        securityContext:
          privileged: true
        resources:
          requests:
            memory: "512Mi"
            cpu: "1"
          limits:
            memory: "724Mi"
            cpu: "1"
      containers:
      - name: elasticsearch
        image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.7.1
        ports:
        - containerPort: 9200
        - containerPort: 9300
        resources:
          requests:
            memory: "3Gi"
            cpu: "1"
          limits:
            memory: "3Gi"
            cpu: "1"

kibana.yaml

apiVersion: v1
kind: Service
metadata:
  name: kibana
  namespace: observability
spec:
  type: NodePort
  ports:
  - name: "5601"
    port: 5601
    targetPort: 5601
  selector:
    app: observability_platform_kibana
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: observability_platform_kibana
  name: kibana
  namespace: observability
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: observability_platform_kibana
    spec:
      containers:
      - env:
        # THIS IS WHERE WE SET CONNECTION BETWEEN KIBANA AND ELASTIC
        - name: ELASTICSEARCH_HOSTS
          value: http://elasticsearch:9200
        - name: SERVER_NAME
          value: kibana
        image: docker.elastic.co/kibana/kibana-oss:6.7.1
        name: kibana
        ports:
        - containerPort: 5601
        resources:
          requests:
            memory: "512Mi"
            cpu: "1"
          limits:
            memory: "724Mi"
            cpu: "1"
      restartPolicy: Always

更新1

按照 gonzalesraul 的建议,我已经创建了第二个ClusterIP类型的弹性服务:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: elasticsearch
  name: elasticsearch-local
  namespace: observability
spec:
  type: ClusterIP
  ports:
  - port: 9200
    protocol: TCP
    targetPort: 9200
  selector:
    app: elasticsearch

服务已创建:

  

kubectl --context = 19team-observability-admin-context -n可观察性获取服务

NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                         AGE
elasticsearch         NodePort    10.106.5.94     <none>        9200:31598/TCP,9300:32018/TCP   26s
elasticsearch-local   ClusterIP   10.101.178.13   <none>        9200/TCP                        26s
kibana                NodePort    10.99.73.118    <none>        5601:30004/TCP                  26s

将弹性标为“ http://elasticsearch-local:9200

不幸的是,它在kibana容器中不起作用:

{"type":"log","@timestamp":"2019-05-09T10:13:54Z","tags":["warning","elasticsearch","admin"],"pid":1,"message":"Unable to revive connection: http://elasticsearch-local:9200/"}

2 个答案:

答案 0 :(得分:0)

请勿使用NodePort服务,而应使用ClusterIP。如果您需要将服务公开为Nodeport,请另外创建另一个服务,例如:

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: elasticsearch
  name: elasticsearch-local
  namespace: observability
spec:
  type: ClusterIP
  ports:
  - port: 9200
    protocol: TCP
    targetPort: 9200
  selector:
    app: elasticsearch

然后更新kibana清单以指向ClusterIP服务:

# ...
# THIS IS WHERE WE SET CONNECTION BETWEEN KIBANA AND ELASTIC
- name: ELASTICSEARCH_HOSTS
  value: http://elasticsearch-local:9200
# ...

nodePort服务不会在kubernetes上创建“ dns条目”(例如elasticsearch.observability.svc.cluster.local)

答案 1 :(得分:0)

kibana.yaml中编辑服务器名称值,并将其设置为kibana:5601

我认为,如果您不这样做,默认情况下它将尝试转到端口80。

这就是现在kibana.yaml的样子:

...
spec:
  containers:
  - env:
    - name: ELASTICSEARCH_HOSTS
      value: http://elasticsearch:9200
    - name: SERVER_NAME
      value: kibana:5601
    image: docker.elastic.co/kibana/kibana-oss:6.7.1
    imagePullPolicy: IfNotPresent
    name: kibana
 ...

这是现在的输出:

{"type":"log","@timestamp":"2019-05-09T10:37:16Z","tags":["status","plugin:console@6.7.1","info"],"pid":1,"state":"green","message":"Status changed from uninitialized to green - Ready","prevState":"uninitialized","prevMsg":"uninitialized"}
{"type":"log","@timestamp":"2019-05-09T10:37:16Z","tags":["status","plugin:interpreter@6.7.1","info"],"pid":1,"state":"green","message":"Status changed from uninitialized to green - Ready","prevState":"uninitialized","prevMsg":"uninitialized"}
{"type":"log","@timestamp":"2019-05-09T10:37:16Z","tags":["status","plugin:metrics@6.7.1","info"],"pid":1,"state":"green","message":"Status changed from uninitialized to green - Ready","prevState":"uninitialized","prevMsg":"uninitialized"}
{"type":"log","@timestamp":"2019-05-09T10:37:16Z","tags":["status","plugin:tile_map@6.7.1","info"],"pid":1,"state":"green","message":"Status changed from uninitialized to green - Ready","prevState":"uninitialized","prevMsg":"uninitialized"}
{"type":"log","@timestamp":"2019-05-09T10:37:16Z","tags":["status","plugin:timelion@6.7.1","info"],"pid":1,"state":"green","message":"Status changed from uninitialized to green - Ready","prevState":"uninitialized","prevMsg":"uninitialized"}
{"type":"log","@timestamp":"2019-05-09T10:37:16Z","tags":["status","plugin:elasticsearch@6.7.1","info"],"pid":1,"state":"green","message":"Status changed from yellow to green - Ready","prevState":"yellow","prevMsg":"Waiting for Elasticsearch"}
{"type":"log","@timestamp":"2019-05-09T10:37:17Z","tags":["listening","info"],"pid":1,"message":"Server running at http://0:5601"}

更新

我刚刚在裸机集群(通过kubeadm引导)上对其进行了测试,然后再次工作。

这是输出:

{"type":"log","@timestamp":"2019-05-09T11:09:59Z","tags":["warning","elasticsearch","admin"],"pid":1,"message":"No living connections"}
{"type":"log","@timestamp":"2019-05-09T11:10:01Z","tags":["warning","elasticsearch","admin"],"pid":1,"message":"Unable to revive connection: http://elasticsearch:9200/"}
{"type":"log","@timestamp":"2019-05-09T11:10:01Z","tags":["warning","elasticsearch","admin"],"pid":1,"message":"No living connections"}
{"type":"log","@timestamp":"2019-05-09T11:10:04Z","tags":["status","plugin:elasticsearch@6.7.1","info"],"pid":1,"state":"green","message":"Status changed from red to green - Ready","prevState":"red","prevMsg":"Unable to connect to Elasticsearch."}
{"type":"log","@timestamp":"2019-05-09T11:10:04Z","tags":["info","migrations"],"pid":1,"message":"Creating index .kibana_1."}
{"type":"log","@timestamp":"2019-05-09T11:10:06Z","tags":["info","migrations"],"pid":1,"message":"Pointing alias .kibana to .kibana_1."}
{"type":"log","@timestamp":"2019-05-09T11:10:06Z","tags":["info","migrations"],"pid":1,"message":"Finished in 2417ms."}
{"type":"log","@timestamp":"2019-05-09T11:10:06Z","tags":["listening","info"],"pid":1,"message":"Server running at http://0:5601"}

请注意,它从“无活动连接”传递到“正在运行”。我正在GCP上运行节点。我必须打开防火墙才能正常工作。您的环境如何?