部署 Elastic Heartbeat 到 K8S

情景

既然已经在 K8S 中 部署了 ES,那么该生态中的应用也体验一下,比如服务拨测 Heartbeat,定期探测服务访问是否成功。

前提条件

操作步骤

1. 初始化数据 {#Init}

由于部署采集器,需要向 ES 初始化数据。

请替换 <kibana><elasticsearch> 的真实地址。

docker run \
docker.elastic.co/beats/heartbeat:7.3.2 \
setup -E setup.kibana.host=<kibana>:5601 \
-E output.elasticsearch.hosts=["<elasticsearch>:9200"]
1
2
3
4

最好使用一次性的 Pod 来创建,如此 ES 和 Kibana 地址可直接使用 K8S 内部的 Sevice 替代。

2. 部署 Heartbeat 到 K8S {#Deploy}

2.1 准备配置文件

下载配置文件

wget https://raw.githubusercontent.com/elastic/beats/7.3/deploy/docker/heartbeat.docker.yml
1

新增 ConfigMap 资源

apiVersion: v1
data:
  heartbeat.yml: |-
    heartbeat.monitors:
    - type: http
      schedule: '@every 30s'
      urls:
        - https://dhcp.cn

    - type: icmp
      schedule: '@every 30s'
      hosts:
        - dhcp.cn


    processors:
    - add_cloud_metadata: ~

    output.elasticsearch:
      hosts: ["elasticsearch-master:9200"]
kind: ConfigMap
metadata:
  name: heartbeat-docker-yml
  namespace: default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

2.2 新增 Deployment 部署 Heartbeat

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  labels:
    k8s-app: heartbeat
    qcloud-app: heartbeat
  name: heartbeat
  namespace: default
spec:
 replicas: 1
  selector:
    matchLabels:
      k8s-app: heartbeat
      qcloud-app: heartbeat
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        k8s-app: heartbeat
        qcloud-app: heartbeat
    spec:
      containers:
      - env:
        - name: PATH
          value: /usr/share/heartbeat:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
        - name: ELASTIC_CONTAINER
          value: "true"
        image: docker.elastic.co/beats/heartbeat:7.3.2
        imagePullPolicy: IfNotPresent
        name: heartbeat
        resources:
          limits:
            cpu: 500m
            memory: 1Gi
          requests:
            cpu: 250m
            memory: 256Mi
        volumeMounts:
        - mountPath: /usr/share/heartbeat/heartbeat.yml
          name: heartbeat-docker-yml
          readOnly: true
          subPath: heartbeat.yml
        workingDir: /usr/share/heartbeat
      volumes:
      - configMap:
          defaultMode: 420
          name: heartbeat-docker-yml
        name: heartbeat-docker-yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

3. 访问测试 {#Test}

检查部署状态

# kubectl get pods -l k8s-app=heartbeat -o wide
NAME                         READY   STATUS    RESTARTS   AGE   IP             NODE       NOMINATED NODE   READINESS GATES
heartbeat-5bc8bfdf95-zr66t   2/2     Running   0          49m   172.16.7.162   10.0.0.6   <none>           <none>
1
2
3

访问 Kibana ,查看拨测详情

-w1665

reference