diff --git a/examples/kubernetes/README.md b/examples/kubernetes/README.md new file mode 100644 index 0000000..2caed41 --- /dev/null +++ b/examples/kubernetes/README.md @@ -0,0 +1,33 @@ +# Running Examples on KinD + +Prerequisites: + +- kind +- dapr cli +- kubectl + +## Setup local registry and kind + +```shell +./setup_kind.sh +``` + +## Run any component on kubernetes + +> Note: Some components may require a dependency like redis/kafka and others. So you need to install it first. + +```shell +./run.sh state.memory +``` + +## See it working + +Forward local 3500 port to remote daprd port and make requests: + +```sh +curl -X POST -H "Content-Type: application/json" -d '[{ "key": "name", "value": "Bruce Wayne", "metadata": { "ttlInSeconds": "60"}}]' http://localhost:3500/v1.0/state/default +``` + +```sh +curl --silent http://localhost:3500/v1.0/state/default/name |base64 --decode +``` diff --git a/examples/kubernetes/deployment.yaml b/examples/kubernetes/deployment.yaml new file mode 100644 index 0000000..37654bd --- /dev/null +++ b/examples/kubernetes/deployment.yaml @@ -0,0 +1,33 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: __COMPONENT__ + labels: + app: __COMPONENT__ +spec: + replicas: 1 + selector: + matchLabels: + app: __COMPONENT__ + template: + metadata: + labels: + app: __COMPONENT__ + annotations: + dapr.io/unix-domain-socket-path: "/tmp/dapr-components-sockets" ## required, the default path where Dapr uses for registering components. + dapr.io/app-id: "__COMPONENT__" + dapr.io/enabled: "true" + spec: + volumes: ## required, the sockets volume + - name: dapr-unix-domain-socket + emptyDir: {} + containers: + - name: component + image: localhost:5001/__COMPONENT__:latest + imagePullPolicy: Always + volumeMounts: # required, the sockets volume mount + - name: dapr-unix-domain-socket + mountPath: /tmp/dapr-components-sockets + env: + - name: DAPR_COMPONENT_SOCKET_PATH # Tells the component where the sockets should be created. + value: /tmp/dapr-components-sockets/pluggable.sock diff --git a/examples/kubernetes/run.sh b/examples/kubernetes/run.sh new file mode 100755 index 0000000..15697db --- /dev/null +++ b/examples/kubernetes/run.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +COMPONENT=${1:-state.memory} +COMPONENT_IMAGE=$(echo $COMPONENT |sed -e s/"\."/"-"/gi) +docker build -t $COMPONENT_IMAGE -f ../Dockerfile ../.. --build-arg COMPONENT=$COMPONENT +docker tag $COMPONENT_IMAGE localhost:5001/$COMPONENT_IMAGE:latest +docker push localhost:5001/$COMPONENT_IMAGE:latest +kubectl apply -f ../$COMPONENT/component.yml +cat deployment.yaml | sed s/__COMPONENT__/$COMPONENT_IMAGE/gi | kubectl apply -f - \ No newline at end of file diff --git a/examples/kubernetes/setup_kind.sh b/examples/kubernetes/setup_kind.sh new file mode 100755 index 0000000..3bd94fc --- /dev/null +++ b/examples/kubernetes/setup_kind.sh @@ -0,0 +1,44 @@ +### Local registry setup +set -o errexit + +# create registry container unless it already exists +reg_name='kind-registry' +reg_port='5001' +if [ "$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)" != 'true' ]; then + docker run \ + -d --restart=always -p "127.0.0.1:${reg_port}:5000" --name "${reg_name}" \ + registry:2 +fi + +# create a cluster with the local registry enabled in containerd +cat <