Hello World to Dapr and Keda

Xin Cheng
3 min readOct 25, 2020

--

Microservice is popular these days, but developing and managing microservice are still hard. Service mesh has become popular these days to solve the problems of managing multitude of microservices. Another interesting project emerges recently: Dapr.

There are some overlap between service mesh and dapr, but they can be used together.

You can test dapr with docker or Kubernetes. I choose Kubernetes because it is closer what will be deployed finally. To start, you need local Kubernets environment and there are lots of choices. Here are 2 articles to decide which one to choose.

In short

Ubuntu: microk8s

Mac: pick docker desktop for mac, enable k8s, or multipass + microk8s

Other linux: kind

Local multi-node: multipass + k3s

I used Ubuntu 20.04 LTS

Local K8S setup

Install

sudo snap install microk8s --classic && \
sudo microk8s.enable dns && \
sudo microk8s.enable registry &&\
sudo microk8s.enable storage

Add user ubuntu to microk8s group

sudo usermod -a -G microk8s ubuntu
sudo chown -f -R ubuntu ~/.kube

Install docker

sudo snap install docker

Add user ubuntu to docker group

sudo groupadd docker
sudo usermod -aG docker ubuntu
newgrp docker

Install kubectl and configure kubectl with microk8s context

sudo snap install kubectl --classic

Now reboot server. Verify with

cd $HOME
mkdir .kube
cd .kube
microk8s config > config
microk8s kubectl get all --all-namespaces

Additional way of using microk8s with multipass-hosted Ubuntu

Install dapr

# dapr cli
wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash
# install helm
sudo snap install helm --classic
# install dapr using helm
helm repo add dapr https://dapr.github.io/helm-charts/
helm repo update
microk8s kubectl create namespace dapr-system
helm install dapr dapr/dapr --namespace dapr-system

Dapr quickstart

Clone dapr quickstart repo

git clone https://github.com/dapr/quickstarts.git
cd quickstarts/hello-kubernetes

Setup redis store

helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
helm install redis bitnami/redis
export REDIS_PASSWORD=$(kubectl get secret — namespace default redis -o jsonpath=”{.data.redis-password}” | base64 — decode)

create redis state store

vi ./deploy/redis.yaml

change

redisHost

redis-master.default.svc.cluster.local:6379

and redisPassword

kubectl apply -f ./deploy/redis.yaml

Deploy node.js app with dapr sidecar (order server)

kubectl apply -f ./deploy/node.yaml

Deploy python app with dapr sidecar (order client)

kubectl apply -f ./deploy/python.yaml
kubectl logs — selector=app=node -c node

Verify with redis cli

kubectl run — namespace default redis-client — rm — tty -i — restart=’Never’ \
— env REDIS_PASSWORD=$REDIS_PASSWORD \
— image docker.io/bitnami/redis:6.0.8-debian-10-r35 — bash

once in the pod

redis-cli -h redis-master -a $REDIS_PASSWORD
CONFIG GET databases
KEYS *
# returns “nodeapp||order”

Redis supports different 5 data types

type “nodeapp||order”
# hash
HGETALL “nodeapp||order”

returns

1) “data”

2) “{\”orderId\”:1137}”

3) “version”

4) “1136”

How client and server communicates through dapr

service invoke api spec

python client is calling

dapr_port = os.getenv(“DAPR_HTTP_PORT”, 3500)
dapr_url = “http://localhost:{}/v1.0/invoke/nodeapp/method/neworder".format(dapr_port)

keda

keda provides ability to scale down to zero (knative is also capable)

Install keda

helm repo add kedacore https://kedacore.github.io/charts
kubectl create namespace keda
helm install keda kedacore/keda -namespace keda

Install kafka

helm repo add incubator http://storage.googleapis.com/kubernetes-charts-incubator
helm install my-kafka incubator/kafka

Deploy keda sample app

git clone https://github.com/onelittlenightmusic/keda-dapr-scaling-sample.git
cd keda-dapr-scaling-sample/3-kafka-pubsub/
kubectl apply -f pubsub/
# verify kafka topics are created
kubectl exec -it my-kafka-0 — /bin/bash
/usr/bin/kafka-consumer-groups -list --bootstrap-server localhost:9092
# apply keda scaler
kubectl apply -f scaler/

Send some messages

kubectl port-forward --address 0.0.0.0 svc/react-form 8081:80

Visit 127.0.0.1:8081 to send kafka message through react-form pub-sub sample. In another shell, you can watch pod status by

kubectl get po -w

node-subscriber- pods are created

Now terminate port-forward command, you will see node-subscriber- pods are terminated.

--

--

Xin Cheng

Multi/Hybrid-cloud, Kubernetes, cloud-native, big data, machine learning, IoT developer/architect, 3x Azure-certified, 3x AWS-certified, 2x GCP-certified