mirror of https://github.com/dapr/samples.git
Updated Readme and makefile for kubernetes events sample
This commit is contained in:
parent
e8299c39f7
commit
47f59ce313
|
@ -1,18 +1,27 @@
|
|||
# Read Kubernetes Events
|
||||
# Read Kubernetes events
|
||||
|
||||
This tutorial will show an example of running Dapr with a Kubernetes Events Input bindg. You'll be deploying the [Node](./node) application along with components [described](./node/components/).
|
||||
|
||||
## Sample info
|
||||
| Attribute | Details |
|
||||
|--------|--------|
|
||||
| Dapr runtime version | v0.10.0 |
|
||||
| Language | Node.js |
|
||||
| Environment | Local or Kubernetes |
|
||||
|
||||
|
||||
## Prerequisites
|
||||
This quickstart requires you to have the following installed on your machine:
|
||||
This sample requires you to have the following installed on your machine:
|
||||
- [Docker](https://docs.docker.com/get-docker/)
|
||||
- [Dapr](https://github.com/dapr/cli/tree/release-0.10) v0.10.0+
|
||||
- [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
|
||||
- A Kubernetes cluster, such as [Minikube](https://github.com/dapr/docs/blob/master/getting-started/cluster/setup-minikube.md), [AKS](https://github.com/dapr/docs/blob/master/getting-started/cluster/setup-aks.md) or [GKE](https://cloud.google.com/kubernetes-engine/)
|
||||
|
||||
Also, unless you have already done so, clone the repository with the quickstarts and ````cd```` into the right directory:
|
||||
Also, unless you have already done so, clone the repository with the samples and ````cd```` into the right directory:
|
||||
```
|
||||
git clone [-b <dapr_version_tag>] https://github.com/dapr/samples.git
|
||||
git clone https://github.com/dapr/samples.git
|
||||
cd samples/read-kubernetes-events
|
||||
```
|
||||
> **Note**: Use `git clone https://github.com/dapr/samples.git` when using the edge version of dapr runtime.
|
||||
|
||||
## Step 1 - Make sure that your kubectl client is working
|
||||
|
||||
|
@ -71,7 +80,7 @@ This simple gets the request, prints a log line and the request body in the cons
|
|||
cd ../components/
|
||||
```
|
||||
|
||||
Here the yaml file defines the component that Dapr has to register with the particular configuration.
|
||||
Here the yaml file defines the component that Dapr has to register with the particular configuration. The binding spec can be seen [here](https://github.com/dapr/docs/blob/master/reference/specs/bindings/kubernetes.md).
|
||||
|
||||
```yaml
|
||||
apiVersion: dapr.io/v1alpha1
|
||||
|
@ -96,7 +105,7 @@ The sample uses default kubectl config from the local machine and does not need
|
|||
|
||||
* Navigate to Node subscriber directory in your CLI:
|
||||
```bash
|
||||
cd ../node
|
||||
cd ./node
|
||||
```
|
||||
* Install dependencies:
|
||||
```bash
|
||||
|
@ -121,6 +130,7 @@ The application is watching the namespace `kube-events`. If it is already presen
|
|||
```bash
|
||||
kubectl create ns kube-events
|
||||
```
|
||||
|
||||
* Deploy a quick Kubernetes hello-world application in the created namespace:
|
||||
|
||||
```bash
|
||||
|
@ -284,6 +294,193 @@ kubectl delete ns kube-events
|
|||
|
||||
## Step 3 - Running in kubernetes cluster
|
||||
|
||||
### Prerequisites
|
||||
Apart from the previous requisites the following are needed.
|
||||
1. Need a shell capable of running make for building and pushing container to Docker Hub repo.
|
||||
2. `makefile` also uses `sed` command for editing file on the fly.
|
||||
3. [Docker Hub](https://hub.docker.com) account.
|
||||
|
||||
1. You will be using the same app that was used in Step 2 to test locally. For running the application in Kubernetes, a container is needed.
|
||||
2. Once the repo has been cloned, change to the read-kubernetes-events directory
|
||||
```bash
|
||||
cd read-kubernetes-events
|
||||
```
|
||||
3. Set the environment variable DOCKER_REPO to your docker hub username
|
||||
```bash
|
||||
export DOCKER_REPO=<REPO>
|
||||
```
|
||||
4. Build the application container
|
||||
```bash
|
||||
make build
|
||||
```
|
||||
|
||||
The output should be of the form
|
||||
```
|
||||
docker build -f node/Dockerfile node/. -t docker.io/<REPO>/k8s-events-node:edge
|
||||
Sending build context to Docker daemon 2.627MB
|
||||
Step 1/6 : FROM node:8-alpine
|
||||
---> 2b8fcdc6230a
|
||||
Step 2/6 : WORKDIR /app
|
||||
---> Using cache
|
||||
---> b8d2c304c3f0
|
||||
Step 3/6 : COPY . .
|
||||
---> Using cache
|
||||
---> 7a9c2ce6a9d6
|
||||
Step 4/6 : RUN npm install
|
||||
---> Using cache
|
||||
---> f7e6ebbee818
|
||||
Step 5/6 : EXPOSE 3000
|
||||
---> Using cache
|
||||
---> d3756d97db07
|
||||
Step 6/6 : CMD [ "node", "app.js" ]
|
||||
---> Using cache
|
||||
---> 908b65d9d01f
|
||||
Successfully built 908b65d9d01f
|
||||
Successfully tagged <REPO>/k8s-events-node:edge
|
||||
```
|
||||
5. Push the container to your docker hub repository
|
||||
```bash
|
||||
make push
|
||||
```
|
||||
> Note: you might need to login to your docker hub repo. [Docker Hub quickstart](https://docs.docker.com/docker-hub/)
|
||||
|
||||
6. Create namespace `kube-events` if not present:
|
||||
|
||||
```bash
|
||||
kubectl create ns kube-events
|
||||
```
|
||||
7. Apply the kubernetes configuration to your cluster using the kubectl command.
|
||||
```yaml
|
||||
apiVersion: dapr.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: kube-events
|
||||
namespace: kube-events
|
||||
spec:
|
||||
type: bindings.kubernetes
|
||||
metadata:
|
||||
- name: namespace
|
||||
value: kube-events
|
||||
- name: resyncPreiodInSec
|
||||
value: "5"
|
||||
```
|
||||
```bash
|
||||
kubectl apply -f ./deploy/kubernetes.yaml
|
||||
```
|
||||
|
||||
8. View the `./deploy/node.yaml` file. It does the following
|
||||
* Creates a role called `events-reader` with permission to `get, list and watch` `events` resource. More details can be found [here](https://github.com/dapr/docs/blob/master/reference/specs/bindings/kubernetes.md).
|
||||
* Creates a role binding called `read-events` which binds default ServiceAccount in `kube-events` namespace to the Role previously created.
|
||||
* Create a service called `events-nodeapp`.
|
||||
* Creates a deployment called `events-nodeapp` with reference to be container created and pushed in steps 4 and 5.
|
||||
|
||||
The container referred to is
|
||||
```
|
||||
image: DOCKER_REPO/k8s-events-node:edge # When applying using make, the DOCKER_REPO is replaced with the environment variable
|
||||
```
|
||||
The DOCKER_REPO name needs to be replaced with the name of your DOCKER_REPO for which an environment variable was created. The process is encapsulated in a make command.
|
||||
|
||||
9. To apply the `node.yaml` configuration to kubernetes cluster, make sure you are within the folder `read-kubernetes-events` and run
|
||||
```bash
|
||||
make apply-node-app-k8s
|
||||
```
|
||||
The output will be of the form
|
||||
|
||||
```
|
||||
sed -e s"/DOCKER_REPO/<REPO>/g" ./deploy/node.yaml | kubectl apply -f -
|
||||
role.rbac.authorization.k8s.io/events-reader created
|
||||
rolebinding.rbac.authorization.k8s.io/read-events created
|
||||
service/events-nodeapp created
|
||||
deployment.apps/events-nodeapp created
|
||||
```
|
||||
|
||||
10. You can now observe the logs
|
||||
|
||||
```bash
|
||||
kubectl get pods -n kube-events
|
||||
```
|
||||
|
||||
Output will be of the form:
|
||||
|
||||
```
|
||||
NAME READY STATUS RESTARTS AGE
|
||||
events-nodeapp-69cdb56c6d-m7qd8 2/2 Running 0 3m56s
|
||||
```
|
||||
|
||||
Run:
|
||||
```
|
||||
kubectl -n kube-events logs -f events-nodeapp-69cdb56c6d-m7qd8 node
|
||||
```
|
||||
|
||||
Output should be of the form
|
||||
|
||||
```
|
||||
Hello from Kube Events!
|
||||
{ event: 'add',
|
||||
oldVal:
|
||||
{ metadata: { creationTimestamp: null },
|
||||
involvedObject: {},
|
||||
source: {},
|
||||
firstTimestamp: null,
|
||||
lastTimestamp: null,
|
||||
eventTime: null,
|
||||
reportingComponent: '',
|
||||
reportingInstance: '' },
|
||||
newVal:
|
||||
{ metadata:
|
||||
{ name: 'events-nodeapp.162cd2271581f9dc',
|
||||
namespace: 'kube-events',
|
||||
selfLink: '/api/v1/namespaces/kube-events/events/events-nodeapp.162cd2271581f9dc',
|
||||
uid: 'e2167021-6e23-43d2-afcc-0104b6a31ab2',
|
||||
resourceVersion: '822570',
|
||||
creationTimestamp: '2020-08-20T00:23:53Z',
|
||||
managedFields: [Array] },
|
||||
involvedObject:
|
||||
{ kind: 'Deployment',
|
||||
namespace: 'kube-events',
|
||||
name: 'events-nodeapp',
|
||||
uid: 'c61035f6-0f29-4062-a5d1-4c3963c919ac',
|
||||
apiVersion: 'apps/v1',
|
||||
resourceVersion: '822564' },
|
||||
reason: 'ScalingReplicaSet',
|
||||
message: 'Scaled up replica set events-nodeapp-69cdb56c6d to 1',
|
||||
source: { component: 'deployment-controller' },
|
||||
firstTimestamp: '2020-08-20T00:23:53Z',
|
||||
lastTimestamp: '2020-08-20T00:23:53Z',
|
||||
count: 1,
|
||||
type: 'Normal',
|
||||
eventTime: null,
|
||||
reportingComponent: '',
|
||||
reportingInstance: '' } }
|
||||
```
|
||||
|
||||
11. Cleanup
|
||||
|
||||
* Delete the applied configuration `node.yaml`
|
||||
```bash
|
||||
make delete-node-app-k8s
|
||||
```
|
||||
|
||||
Output should be of the form:
|
||||
```
|
||||
sed -e s"/DOCKER_REPO/<REPO>/g" ./deploy/node.yaml | kubectl delete -f -
|
||||
role.rbac.authorization.k8s.io "events-reader" deleted
|
||||
rolebinding.rbac.authorization.k8s.io "read-events" deleted
|
||||
service "events-nodeapp" deleted
|
||||
deployment.apps "events-nodeapp" deleted
|
||||
```
|
||||
|
||||
* Delete the applied configuration `kubernetes.yaml`
|
||||
```
|
||||
kubectl delete -f ./deploy/kubernetes.yaml
|
||||
```
|
||||
|
||||
* Delete namepace (if it was created for this sample):
|
||||
|
||||
```bash
|
||||
kubectl delete ns kube-events
|
||||
```
|
||||
|
||||
|
||||
## Next steps
|
||||
- Explore additional [samples](../README.md#Samples-in-this-repository) and deploy them locally or on Kubernetes.
|
||||
|
|
|
@ -1,30 +1,27 @@
|
|||
# Template to be used with associate make command. Or manually replace DOCKER_REPO in container definintion below.
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: Role
|
||||
metadata:
|
||||
namespace: kube-events
|
||||
name: events-reader
|
||||
rules:
|
||||
- apiGroups: [""] # "" indicates the core API group
|
||||
- apiGroups: [""]
|
||||
resources: ["events"]
|
||||
verbs: ["get", "watch", "list"]
|
||||
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
# This role binding allows "jane" to read pods in the "default" namespace.
|
||||
# You need to already have a Role named "pod-reader" in that namespace.
|
||||
kind: RoleBinding
|
||||
metadata:
|
||||
name: read-events
|
||||
namespace: kube-events
|
||||
subjects:
|
||||
# You can specify more than one "subject"
|
||||
- kind: ServiceAccount
|
||||
name: default
|
||||
namespace: kube-events
|
||||
roleRef:
|
||||
# "roleRef" specifies the binding to a Role / ClusterRole
|
||||
kind: Role #this must be Role or ClusterRole
|
||||
name: events-reader # this must match the name of the Role or ClusterRole you wish to bind to
|
||||
kind: Role
|
||||
name: events-reader
|
||||
apiGroup: ""
|
||||
|
||||
---
|
||||
|
@ -68,7 +65,7 @@ spec:
|
|||
spec:
|
||||
containers:
|
||||
- name: node
|
||||
image: dapriosamples/k8s-events-node:edge
|
||||
image: DOCKER_REPO/k8s-events-node:edge # When applying using make, the DOCKER_REPO is replaced with the environment variable
|
||||
ports:
|
||||
- containerPort: 3000
|
||||
imagePullPolicy: Always
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
DOCKER_IMAGE_PREFIX ?=k8s-events-
|
||||
APPS ?=node
|
||||
|
||||
SAMPLE_REGISTRY ?=docker.io/dapriosamples
|
||||
SAMPLE_REGISTRY ?=docker.io/${DOCKER_REPO}
|
||||
REL_VERSION ?=edge
|
||||
|
||||
# Add latest tag if LATEST_RELEASE is true
|
||||
|
@ -50,3 +50,13 @@ endef
|
|||
|
||||
# Generate docker image push targets
|
||||
$(foreach ITEM,$(APPS),$(eval $(call genDockerImagePush,$(ITEM),$(REL_VERSION))))
|
||||
|
||||
# Apply the node app to k8s after making changes to DOCKER_REPO
|
||||
.PHONY: apply-node-app-k8s
|
||||
apply-node-app-k8s:
|
||||
sed -e s"/DOCKER_REPO/${DOCKER_REPO}/g" ./deploy/node.yaml | kubectl apply -f -
|
||||
|
||||
# Delete the node app from k8s after making changes to DOCKER_REPO
|
||||
.PHONY: delete-node-app-k8s
|
||||
delete-node-app-k8s:
|
||||
sed -e s"/DOCKER_REPO/${DOCKER_REPO}/g" ./deploy/node.yaml | kubectl delete -f -
|
||||
|
|
Loading…
Reference in New Issue