이 연습에서는 클러스터에 Dapr 애플리케이션을 배포합니다. Dapr 애플리케이션은 MQTT 브로커에 게시된 시뮬레이션된 MQTT 데이터를 사용하고, 창 작업 함수를 적용한 다음, 결과를 MQTT 브로커에 다시 게시합니다. 게시된 출력은 메시지 빈도와 크기를 줄이기 위해 대용량 데이터를 에지에 집계하는 방법을 나타냅니다. Dapr 애플리케이션은 상태 비저장이며 MQTT 브로커 상태 저장소를 사용하여 창 계산에 필요한 과거 값을 캐시합니다.
Dapr 애플리케이션에서는 다음 단계를 수행합니다.
- 센서 데이터에 대한
sensor/data
항목을 구독합니다. - 토픽에서 데이터를 받으면 MQTT broker 상태 저장소에 게시됩니다.
- 10초마다, 상태 저장소에서 데이터를 가져오고 지난 30초의 타임스탬프가 지정된 모든 센서 데이터에 대해 최소, 최대, 평균, 중앙값 및 75번째 백분위수 값을 계산합니다.
- 30초보다 오래된 데이터는 상태 저장소에서 만료됩니다.
- 결과는 JSON 형식으로
sensor/window_data
항목에 게시됩니다.
참고 항목
이 자습서에서는 Dapr CloudEvents 사용하지 않도록 설정하여 원시 MQTT를 통해 게시 및 구독할 수 있게 합니다.
필수 조건
- Azure IoT 작업 설치 - 빠른 시작: K3s를 사용하여 GitHub Codespaces에서 Azure IoT 작업 실행
- MQTT 브로커 Dapr 구성 요소 설치됨 - MQTT 브로커 Dapr 구성 요소 설치
Dapr 애플리케이션 배포
이 시점에서 Dapr 애플리케이션을 배포할 수 있습니다. 구성 요소를 등록해도 컨테이너에 패키지된 연결된 이진 파일이 배포되지 않습니다. 애플리케이션과 함께 이진 파일을 배포하려면, 배포를 사용하여 컨테이너화된 Dapr 애플리케이션과 두 구성 요소를 함께 그룹화할 수 있습니다.
시작하려면 다음 정의를 사용하는 yaml 파일을 만듭니다.
구성 요소 | 설명 |
---|---|
volumes.mqtt-client-token |
MQTT 브로커 및 상태 저장소를 사용하여 Dapr 플러그형 구성 요소를 인증하는 데 사용되는 SAT |
volumes.aio-internal-ca-cert-chain |
MQTT 브로커 TLS 인증서의 유효성을 검사하는 트러스트 체인 |
containers.mq-event-driven |
미리 빌드된 dapr 애플리케이션 컨테이너 |
다음 배포 yaml을 이름이
app.yaml
인 파일에 저장합니다.apiVersion: v1 kind: ServiceAccount metadata: name: dapr-client namespace: azure-iot-operations annotations: aio-broker-auth/group: dapr-workload --- apiVersion: apps/v1 kind: Deployment metadata: name: mq-event-driven-dapr namespace: azure-iot-operations spec: selector: matchLabels: app: mq-event-driven-dapr template: metadata: labels: app: mq-event-driven-dapr annotations: dapr.io/enabled: "true" dapr.io/inject-pluggable-components: "true" dapr.io/app-id: "mq-event-driven-dapr" dapr.io/app-port: "6001" dapr.io/app-protocol: "grpc" spec: serviceAccountName: dapr-client volumes: # SAT token used to authenticate between Dapr and the MQTT broker - name: mqtt-client-token projected: sources: - serviceAccountToken: path: mqtt-client-token audience: aio-internal expirationSeconds: 86400 # Certificate chain for Dapr to validate the MQTT broker - name: aio-ca-trust-bundle configMap: name: azure-iot-operations-aio-ca-trust-bundle containers: - name: mq-event-driven-dapr image: ghcr.io/azure-samples/explore-iot-operations/mq-event-driven-dapr:latest
다음 명령을 실행하여 애플리케이션을 배포합니다.
kubectl apply -f app.yaml
애플리케이션이 배포되었는지 확인합니다. 다음 명령과 같이 짧은 간격 후에 Pod가 모든 컨테이너가 준비되었다고 보고해야 합니다.
kubectl get pods -l app=mq-event-driven-dapr -n azure-iot-operations
출력은 다음과 같습니다.
NAME READY STATUS RESTARTS AGE mq-event-driven-dapr 3/3 Running 0 30s
시뮬레이터 배포
Kubernetes 워크로드를 배포하여 테스트 데이터를 시뮬레이션합니다.
sensor/data
토픽에 대해 MQTT 클라이언트를 사용하여 MQTT 브로커에 주기적으로 샘플 온도, 진동 및 압력 판독값을 전송하여 센서를 시뮬레이션합니다.
IoT 작업 탐색 리포지토리에서 시뮬레이터를 배포합니다 .
kubectl apply -f https://raw.githubusercontent.com/Azure-Samples/explore-iot-operations/main/tutorials/mq-event-driven-dapr/simulate-data.yaml
시뮬레이터가 올바르게 실행되고 있는지 확인합니다.
kubectl logs deployment/mqtt-publisher-deployment -n azure-iot-operations -f
출력은 다음과 같습니다.
Get:1 http://deb.debian.org/debian stable InRelease [151 kB] Get:2 http://deb.debian.org/debian stable-updates InRelease [52.1 kB] Get:3 http://deb.debian.org/debian-security stable-security InRelease [48.0 kB] Get:4 http://deb.debian.org/debian stable/main amd64 Packages [8780 kB] Get:5 http://deb.debian.org/debian stable-updates/main amd64 Packages [6668 B] Get:6 http://deb.debian.org/debian-security stable-security/main amd64 Packages [101 kB] Fetched 9139 kB in 3s (3570 kB/s) ... Messages published in the last 10 seconds: 10 Messages published in the last 10 seconds: 10 Messages published in the last 10 seconds: 10
MQTT 클라이언트 배포
MQTT 브리지가 작동하는지 확인하려면 클러스터에 MQTT 클라이언트를 배포합니다.
이름이
client.yaml
인 새 파일에서 클라이언트 배포를 지정합니다.apiVersion: v1 kind: ServiceAccount metadata: name: mqtt-client namespace: azure-iot-operations --- apiVersion: v1 kind: Pod metadata: name: mqtt-client namespace: azure-iot-operations spec: serviceAccountName: mqtt-client containers: - image: alpine name: mqtt-client command: ["sh", "-c"] args: ["apk add mosquitto-clients mqttui && sleep infinity"] volumeMounts: - name: mqtt-client-token mountPath: /var/run/secrets/tokens - name: aio-ca-trust-bundle mountPath: /var/run/certs/aio-internal-ca-cert/ volumes: - name: mqtt-client-token projected: sources: - serviceAccountToken: path: mqtt-client-token audience: aio-internal expirationSeconds: 86400 - name: aio-ca-trust-bundle configMap: name: azure-iot-operations-aio-ca-trust-bundle
kubectl을 사용하여 배포 파일을 적용합니다.
kubectl apply -f client.yaml
출력 확인:
pod/mqtt-client created
Dapr 애플리케이션 출력 확인
Mosquitto 클라이언트 Pod에 대한 셸을 엽니다.
kubectl exec --stdin --tty mqtt-client -n azure-iot-operations -- sh
Dapr 애플리케이션에서 게시된 출력을 관찰하려면
sensor/window_data
항목을 구독합니다.mosquitto_sub -L mqtt://aio-broker/sensor/window_data
애플리케이션이 10초마다 다양한 센서에 대한 슬라이딩 윈도우 계산을 출력하는지 확인합니다.
{ "timestamp": "2023-11-16T21:59:53.939690+00:00", "window_size": 30, "temperature": { "min": 553.024, "max": 598.907, "mean": 576.4647857142858, "median": 577.4905, "75_per": 585.96125, "count": 28 }, "pressure": { "min": 290.605, "max": 299.781, "mean": 295.521, "median": 295.648, "75_per": 297.64050000000003, "count": 28 }, "vibration": { "min": 0.00124192, "max": 0.00491257, "mean": 0.0031171810714285715, "median": 0.003199235, "75_per": 0.0038769150000000003, "count": 28 } }
선택 사항 - Dapr 애플리케이션 만들기
이 자습서에서는 Dapr 애플리케이션의 미리 빌드된 컨테이너를 사용합니다. 코드를 직접 수정하고 빌드하려면 다음 단계를 수행합니다.
필수 조건
- Docker - 애플리케이션 컨테이너 빌드
- 컨테이너 레지스트리 - 애플리케이션 컨테이너 호스팅
애플리케이션 빌드
IoT 작업 탐색 리포지토리를 복제합니다.
git clone https://github.com/Azure-Samples/explore-iot-operations
Dapr 자습서 디렉터리로 변경합니다.
cd explore-iot-operations/tutorials/mq-event-driven-dapr/src
Docker 이미지를 빌드합니다.
docker build docker build . -t mq-event-driven-dapr
Kubernetes 클러스터에서 애플리케이션을 사용하려면 Azure Container Registry 같은 컨테이너 레지스트리에 이미지를 푸시해야 합니다. minikube, Docker 등의 로컬 컨테이너 레지스트리에 푸시할 수도 있습니다.
docker tag mq-event-driven-dapr <container-alias> docker push <container-alias>
새로 만든 이미지를 끌어오도록
app.yaml
을 업데이트합니다.
문제 해결
애플리케이션이 시작되지 않거나 컨테이너가 표시되는 CrashLoopBackoff
경우 컨테이너 로그에 daprd
유용한 정보가 포함된 경우가 많습니다.
다음 명령을 실행하여 daprd 구성 요소에 대한 로그를 봅니다.
kubectl logs -l app=mq-event-driven-dapr -n azure-iot-operations -c daprd