Getting Started: DeviceShifu
Helloworld device
Steps
- Prepare the EdgeDevice: Docker image
package main import ( "fmt" "net/http" ) func process_hello(w http.ResponseWriter, req *http.Request) { fmt.Fprintln(w, "Hello_world from device via shifu!") } func headers(w http.ResponseWriter, req *http.Request) { for name, headers := range req.Header { for _, header := range headers { fmt.Fprintf(w, "%v: %v\n", name, header) } } } func main() { http.HandleFunc("/hello", process_hello) http.HandleFunc("/headers", headers) http.ListenAndServe(":11111", nil) }go mod init helloworld# syntax=docker/dockerfile:1 FROM golang:1.17-alpine WORKDIR /app COPY go.mod ./ RUN go mod download COPY *.go ./ RUN go build -o /helloworld EXPOSE 11111 CMD [ "/helloworld" ]docker build --tag helloworld-device:v0.0.1 . - Prepare the configuration for the EdgeDevice:
apiVersion: apps/v1 kind: Deployment metadata: labels: app: helloworld name: helloworld namespace: devices spec: replicas: 1 selector: matchLabels: app: helloworld template: metadata: labels: app: helloworld spec: containers: - image: helloworld-device:v0.0.1 name: helloworld ports: - containerPort: 11111apiVersion: shifu.edgenesis.io/v1alpha1 kind: EdgeDevice metadata: name: edgedevice-helloworld namespace: devices spec: sku: "Hello World" connection: Ethernet address: helloworld.devices.svc.cluster.local:11111 protocol: HTTP status: edgedevicephase: "Pending"apiVersion: v1 kind: Service metadata: labels: app: helloworld name: helloworld namespace: devices spec: ports: - port: 11111 protocol: TCP targetPort: 11111 selector: app: helloworld type: LoadBalancer - Prepare the configurations for Shifu to create the DeviceShifu
apiVersion: v1 kind: ConfigMap metadata: name: helloworld-configmap-0.0.1 namespace: default data: # device name and image address driverProperties: | driverSku: Hello World driverImage: helloworld-device:v0.0.1 # available instructions instructions: | hello: # telemetry retrieval methods # in this example, a device_health telemetry is collected by calling hello instruction every 1 second telemetries: | device_health: properties: instruction: hello initialDelayMs: 1000 intervalMs: 1000apiVersion: apps/v1 kind: Deployment metadata: labels: app: edgedevice-helloworld-deployment name: edgedevice-helloworld-deployment namespace: default spec: replicas: 1 selector: matchLabels: app: edgedevice-helloworld-deployment template: metadata: labels: app: edgedevice-helloworld-deployment spec: containers: - image: edgehub/deviceshifu-http:v0.0.1 name: deviceshifu-http ports: - containerPort: 8080 volumeMounts: - name: edgedevice-config mountPath: "/etc/edgedevice/config" readOnly: true env: - name: EDGEDEVICE_NAME value: "edgedevice-helloworld" - name: EDGEDEVICE_NAMESPACE value: "devices" volumes: - name: edgedevice-config configMap: name: helloworld-configmap-0.0.1 serviceAccountName: edgedevice-mockdevice-saapiVersion: v1 kind: Service metadata: labels: app: edgedevice-helloworld-deployment name: edgedevice-helloworld-service namespace: default spec: ports: - port: 80 protocol: TCP targetPort: 8080 selector: app: edgedevice-helloworld-deployment type: LoadBalancer - Create new DeviceShifu
kind load docker-image helloworld-device:v0.0.1kubectl apply -f <working_dir>/helloworld-device/configurationkubectl run nginx --image=nginx:1.21kubectl get pods --all-namespacesNAMESPACE NAME READY STATUS RESTARTS AGE crd-system crd-controller-manager-7bc78896b9-sq72b 2/2 Running 0 28m default edgedevice-helloworld-deployment-6464b55979-hbdhr 1/1 Running 0 27m default nginx 1/1 Running 0 8s devices helloworld-5f467bf5db-f5hxh 1/1 Running 0 25m kube-system coredns-558bd4d5db-qqx92 1/1 Running 0 30m kube-system coredns-558bd4d5db-zlw8b 1/1 Running 0 30m kube-system etcd-kind-control-plane 1/1 Running 0 30m kube-system kindnet-ndrnh 1/1 Running 0 30m kube-system kube-apiserver-kind-control-plane 1/1 Running 0 30m kube-system kube-controller-manager-kind-control-plane 1/1 Running 0 30m kube-system kube-proxy-qkswm 1/1 Running 0 30m kube-system kube-scheduler-kind-control-plane 1/1 Running 0 30m local-path-storage local-path-provisioner-547f784dff-44xnv 1/1 Running 0 30mkubectl get edgedevice --namespace devices edgedevice-helloworld NAME AGE edgedevice-helloworld 22mkubectl describe edgedevice --namespace devices edgedevice-helloworldkubectl exec -it --namespace default nginx -- bash/# curl http://edgedevice-helloworld-service:80/helloHello_world from device via shifu!kubectl logs edgedevice-helloworld-deployment-6464b55979-hbdhr
Last updated