From bc1a145aad41a003c33c4a81056e9708245a536f Mon Sep 17 00:00:00 2001 From: Marcos Candeia Date: Mon, 17 Oct 2022 16:33:18 -0300 Subject: [PATCH 1/2] Add k8s examples Signed-off-by: Marcos Candeia --- examples/kubernetes/README.md | 33 ++++++++++++++++++++++ examples/kubernetes/deployment.yaml | 33 ++++++++++++++++++++++ examples/kubernetes/run.sh | 9 ++++++ examples/kubernetes/setup_kind.sh | 44 +++++++++++++++++++++++++++++ examples/state.memory/component.yml | 1 + socket_unix.go | 4 ++- 6 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 examples/kubernetes/README.md create mode 100644 examples/kubernetes/deployment.yaml create mode 100755 examples/kubernetes/run.sh create mode 100755 examples/kubernetes/setup_kind.sh 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..2223ba8 --- /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 --no-cache +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 < Date: Mon, 24 Oct 2022 16:32:56 -0300 Subject: [PATCH 2/2] Remove cache flag Signed-off-by: Marcos Candeia --- examples/kubernetes/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/kubernetes/run.sh b/examples/kubernetes/run.sh index 2223ba8..15697db 100755 --- a/examples/kubernetes/run.sh +++ b/examples/kubernetes/run.sh @@ -2,7 +2,7 @@ COMPONENT=${1:-state.memory} COMPONENT_IMAGE=$(echo $COMPONENT |sed -e s/"\."/"-"/gi) -docker build -t $COMPONENT_IMAGE -f ../Dockerfile ../.. --build-arg COMPONENT=$COMPONENT --no-cache +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