我们在k8s中部署中间件应用,会遇到中间件的依赖关系,例如:我们在部署 kafka
以及 zookeeper
中,需要先启动 zookeeper
,后启动 kafka
。我们要确保 zookeeper
启动后, kafka
在启动。
解决方案:在 kafka
的部署中,增加 zookeeper
的服务检测机制,确保 zookeep
启动后,在启动 kafka
。
使用案例如下:
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: kafka
spec:
replicas: 1
selector:
matchLabels:
name: kafka
template:
metadata:
labels:
name: kafka
app: kafka
spec:
containers:
- name: kafka
image: wurstmeister/kafka
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9092
env:
- name: KAFKA_ADVERTISED_PORT
value: "9092"
- name: KAFKA_ADVERTISED_HOST_NAME
value: "[kafka的service的clusterIP]"
- name: KAFKA_ZOOKEEPER_CONNECT
value: [zookeeper的service的clusterIP]:2181
- name: KAFKA_BROKER_ID
value: "1"
initContainers:
- name: init-redis
image: busybox:1.31
command: ['sh', '-c', 'until nslookup redis-server; do echo waiting for redis; sleep 2; done;']
---
apiVersion: v1
kind: Service
metadata:
name: kafka-service
labels:
app: kafka
spec:
type: NodePort
ports:
- port: 9092
name: kafka-port
targetPort: 9092
nodePort: 30092
protocol: TCP
selector:
app: kafka
上面这一示例定义了一个 init container
,通过 nslookup
检查 zookeeper
服务是否成功启动,检测到 zookeeper
启动了之后,域名解析也就会成功,然后启动 kafka
。
网友评论