Kubernetes节点无法访问外部Kafka群集

时间:2018-05-30 01:30:03

标签: docker kubernetes flannel

我有一个Kubernetes集群,其中包含多个需要连接到远程Kafka集群的java微服务。服务器位于DigitalOcean中,可在其专用网络内完全访问。 Kafka群集不使用Kubernetes,它不在群集中。

我使用kubeadm启动整个群集,这是有关群集的信息:

# kubectl cluster-info
Kubernetes master is running at https://10.132.113.68:6443
KubeDNS is running at https://10.132.113.68:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

群集版本

# kubectl version
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.2", GitCommit:"81753b10df112992bf51bbc2c2f85208aad78335", GitTreeState:"clean", BuildDate:"2018-04-27T09:22:21Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.7", GitCommit:"dd5e1a2978fd0b97d9b78e1564398aeea7e7fe92", GitTreeState:"clean", BuildDate:"2018-04-18T23:58:35Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}

这用于建立网络:

kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=10.132.113.68 --kubernetes-version stable-1.8

所有豆荚成功推出:

# kubectl get pods --all-namespaces
NAMESPACE     NAME                                                        READY     STATUS      RESTARTS   AGE
default       datadog-agent-5cht2                                         1/1       Running     0          12d
default       datadog-agent-5r7rw                                         1/1       Running     0          12d
default       datadog-agent-b7t5q                                         1/1       Running     0          12d
default       vizix-services-7bdccb48c4-2q8js                             1/1       Running     0          19m
default       vizix-tools-cpr88                                           0/1       Completed   0          12d
kube-system   etcd-kubctl-s-2vcpu-4gb-nyc3-01-master                      1/1       Running     0          27d
kube-system   kube-apiserver-kubctl-s-2vcpu-4gb-nyc3-01-master            1/1       Running     0          27d
kube-system   kube-controller-manager-kubctl-s-2vcpu-4gb-nyc3-01-master   1/1       Running     0          27d
kube-system   kube-dns-6f4fd4bdf-f7ssn                                    3/3       Running     0          27d
kube-system   kube-flannel-ds-dm5w4                                       1/1       Running     0          27d
kube-system   kube-flannel-ds-ns58w                                       1/1       Running     0          27d
kube-system   kube-flannel-ds-prnvf                                       1/1       Running     1          27d
kube-system   kube-flannel-ds-xck8p                                       1/1       Running     0          27d
kube-system   kube-proxy-2xrhl                                            1/1       Running     0          27d
kube-system   kube-proxy-lnt9r                                            1/1       Running     0          27d
kube-system   kube-proxy-m74ms                                            1/1       Running     0          27d
kube-system   kube-proxy-vqdxt                                            1/1       Running     0          27d
kube-system   kube-scheduler-kubctl-s-2vcpu-4gb-nyc3-01-master            1/1       Running     0          27d
kube-system   kubernetes-dashboard-5bd6f767c7-7qp75                       1/1       Running     0          26d

当使用PING或Telnet时,需要连接到Kafka的POD可以到达集群:

# kubectl exec -it vizix-services-7bdccb48c4-2q8js bash
bash-4.2# ping 10.132.123.177
PING 10.132.123.177 (10.132.123.177) 56(84) bytes of data.
64 bytes from 10.132.123.177: icmp_seq=1 ttl=63 time=0.540 ms
64 bytes from 10.132.123.177: icmp_seq=2 ttl=63 time=0.518 ms
64 bytes from 10.132.123.177: icmp_seq=3 ttl=63 time=0.432 ms
64 bytes from 10.132.123.177: icmp_seq=4 ttl=63 time=0.527 ms
^C
--- 10.132.123.177 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.432/0.504/0.540/0.045 ms
bash-4.2# telnet 10.132.123.177 9092
Trying 10.132.123.177...
Connected to 10.132.123.177.
Escape character is '^]'.
^CConnection closed by foreign host.
bash-4.2#

但JAVA应用程序不能。仅使用泊坞窗,它可以正常连接,但是当Kubernetes启动吊舱时,它不能。

2018-05-30 01:25:06,993+0000 WARN  [localhost-startStop-1] com.tierconnect.riot.commons.services.broker.KafkaPublisher:: - 
Check if exists a connection to kafka server 10.132.123.177:9092 and services is able to publish to kafka. 

Kubernetes有什么东西可以阻止某些特定的应用程序协议从一个节点连接到外部主机吗?

这是服务的部署YAML文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: microservice1
  labels:
    app: services
spec:
  replicas: 1
  selector:
    matchLabels:
      app: services
  template:
    metadata:
      labels:
        app: services
    spec:
      containers:
      - name: microservice1
        image: random/java-image:v6.5.2
        env:
      - name: KAFKA_SERVERS
        value: "10.132.123.177:9092"

1 个答案:

答案 0 :(得分:0)

我使用端点和服务解决了这个问题。这种连接方式是由Kubernetes管理的:

    ---
    kind: "Service"
    apiVersion: "v1"
    metadata:
      name: "kafka"
    spec:
      ports:
        -
          name: "kafka"
          protocol: "TCP"
          port: 9092
          targetPort: 9092
          nodePort: 0
    ---
    kind: "Endpoints"
    apiVersion: "v1"
    metadata:
      name: "kafka"
    subsets:
      -
        addresses:
          -
            ip: "10.128.0.2"
        ports:
          -
            port: 9092
            name: "kafka"