github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/docs/user_docs/kubeblocks-for-kafka/cluster-management/connect-to-a-cluster.md (about) 1 --- 2 title: Connect to a Kafka cluster 3 description: Guide for cluster creation for kafka 4 keywords: [kafka, cluster, connect, network] 5 sidebar_position: 2 6 sidebar_label: Connect 7 --- 8 ## Overview 9 10 Before you connect to the Kafka cluster, you must check your network environment, and from which network you would like to connect to the cluster. 11 There are four scenarios of connecting. 12 13 * Connect to the cluster within the same Kubernetes cluster. 14 * Connect to a kafka cluster from outside of the Kubernetes cluster but in the same VPC. 15 * Connect to a kafka cluster from public internet. 16 17 ## Connect to a kafka cluster within the Kubernetes cluster 18 19 Within the same Kubernetes cluster, you can directly access the Kafka cluster with clusterIp service:9092 20 21 ***Steps:*** 22 23 1. Get the address of the Kafka ClusterIP service port No.. 24 25 ```bash 26 kubectl get svc 27 ``` 28 29 *Example:* 30 31 ```bash 32 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE 33 kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 9d 34 ivy85-broker-headless ClusterIP None <none> 9092/TCP,9093/TCP,9094/TCP,5556/TCP 7d16h 35 ivy85-broker ClusterIP 10.43.8.124 <none> 9093/TCP,9092/TCP,5556/TCP 7d16h 36 ``` 37 38 2. Connect to the kafka cluster with the port No.. 39 40 Below is an example of connecting with the official client script. 41 42 a. Start client pod 43 44 ```bash 45 kubectl run kafka-producer --restart='Never' --image docker.io/bitnami/kafka:3.3.2-debian-11-r54 --command -- sleep infinity 46 kubectl run kafka-consumer --restart='Never' --image docker.io/bitnami/kafka:3.3.2-debian-11-r54 --command -- sleep infinity 47 ``` 48 49 b. Login to kafka-producer 50 51 ```bash 52 kubectl exec -ti kafka-producer -- bash 53 ``` 54 55 c. Create topic 56 57 ```bash 58 kafka-topics.sh --create --topic quickstart-events --bootstrap-server xxx-broker:9092 59 ``` 60 61 d. Create producer 62 63 ```bash 64 kafka-console-producer.sh --topic quickstart-events --bootstrap-server xxx-broker:9092 65 ``` 66 67 e. Enter:"Hello, KubeBlocks" and press Enter. 68 69 f. Start a new terminal session and login to kafka-consumer. 70 71 ```bash 72 kubectl exec -ti kafka-consumer -- bash 73 ``` 74 75 g. Create consumer and specify consuming topic, and consuming message from the beginning. 76 77 ```bash 78 kafka-console-consumer.sh --topic quickstart-events --from-beginning --bootstrap-server xxx-broker:9092 79 ``` 80 81 And you get the output 'Hello, KubeBlocks'. 82 83 ## Connect to a Kafka cluster from outside of the Kubernetes cluster but in the same VPC 84 85 If you use AWS EKS, you may want to access to the Kafka cluster from EC2 instance. This section shows how to perform the connection. 86 87 ***Steps:*** 88 89 1. Set the value of host-network-accessible as true. 90 91 <Tabs> 92 <TabItem value="kbcli" label="kbcli" default> 93 94 ```bash 95 kbcli cluster create kafka --host-network-accessible=true 96 ``` 97 98 </TabItem> 99 <TabItem value="kubectl" label="kubectl" > 100 101 ```bash 102 kubectl apply -f - <<EOF 103 apiVersion: apps.kubeblocks.io/v1alpha1 104 kind: Cluster 105 metadata: 106 name: kafka 107 namespace: default 108 spec: 109 affinity: 110 podAntiAffinity: Preferred 111 tenancy: SharedNode 112 topologyKeys: 113 - kubernetes.io/hostname 114 clusterDefinitionRef: kafka 115 clusterVersionRef: kafka-3.3.2 116 componentSpecs: 117 - componentDefRef: kafka-server 118 monitor: false 119 name: broker 120 replicas: 1 121 resources: 122 limits: 123 cpu: "1" 124 memory: 1Gi 125 requests: 126 cpu: "1" 127 memory: 1Gi 128 serviceAccountName: kb-sa-kafka 129 services: 130 - annotations: 131 service.beta.kubernetes.io/aws-load-balancer-type: nlb 132 service.beta.kubernetes.io/aws-load-balancer-internal: "true" 133 name: vpc 134 serviceType: LoadBalancer 135 tls: false 136 terminationPolicy: Delete 137 EOF 138 ``` 139 140 </TabItem> 141 142 </Tabs> 143 144 2. Get the corresponding ELB address. 145 146 ```bash 147 kubectl get svc 148 ``` 149 150 image.png 151 152 Information to be noticed: 153 154 * poplar50-broker: broker build-in advertised.listeners, service name 155 * a0e01377fa33xxx-xxx.cn-northwest-1.elb.amazonaws.com.cn: The ELB address which can be accessed from outside of the Kubernetes cluster within the same VPC. 156 157 3. Configure hostname mapping. 158 159 a. Login to the EC2 instance. 160 b. Check ELB address IP address. 161 162 ```bash 163 nslookup a0e01377fa33xxx-xxx.cn-northwest-1.elb.amazonaws.com.cn 164 ``` 165 166 image. 167 c. Configure /etc/hosts mapping. 168 169 ```bash 170 vi /etc/hosts 171 # at the bottom, add the address. 172 52.83.xx.xx poplar50-broker 173 ``` 174 175 4. Use ELB address to connect. In the above example, it is a0e01377fa33xxx-xxx.cn-northwest-1.elb.amazonaws.com.cn:9092 176 177 ## Connect to a Kafka cluster from public internet 178 179 ***Steps:*** 180 181 1. Set the --publicly-accessible value as true when creating cluster. 182 183 <Tabs> 184 <TabItem value="kbcli" label="kbcli" default> 185 186 ```bash 187 kbcli cluster create kafka --publicly-accessible=true 188 ``` 189 190 </TabItem> 191 192 <TabItem value="kubectl" label="kubectl" > 193 194 ```bash 195 kubectl apply -f - <<EOF 196 apiVersion: apps.kubeblocks.io/v1alpha1 197 kind: Cluster 198 metadata: 199 name: kafka 200 namespace: default 201 spec: 202 affinity: 203 podAntiAffinity: Preferred 204 tenancy: SharedNode 205 topologyKeys: 206 - kubernetes.io/hostname 207 clusterDefinitionRef: kafka 208 clusterVersionRef: kafka-3.3.2 209 componentSpecs: 210 - componentDefRef: kafka-server 211 monitor: false 212 name: broker 213 replicas: 1 214 resources: 215 limits: 216 cpu: "1" 217 memory: 1Gi 218 requests: 219 cpu: "1" 220 memory: 1Gi 221 serviceAccountName: kb-sa-kafka 222 services: 223 - annotations: 224 service.beta.kubernetes.io/aws-load-balancer-type: nlb 225 service.beta.kubernetes.io/aws-load-balancer-internal: "false" 226 name: vpc 227 serviceType: LoadBalancer 228 tls: false 229 terminationPolicy: Delete 230 EOF 231 ``` 232 233 </TabItem> 234 235 </Tabs>