From d95de48d4231d98e5cfe7ca6ce061004af7c68ff Mon Sep 17 00:00:00 2001
From: Brandon High <highb@users.noreply.github.com>
Date: Tue, 10 May 2022 12:22:36 -0700
Subject: [PATCH] Add kubernetes example for hotrod app (#3645)

* Add kubernetes example for hotrod app

Signed-off-by: Brandon High <highb@users.noreply.github.com>

* Remove grafana-agent optional configuration

Signed-off-by: Brandon High <highb@users.noreply.github.com>

* Add redis container to kubernetes deployment example

Also reduces the cpu/memory limits as they were excessive initially.

Signed-off-by: Brandon High <highb@users.noreply.github.com>

* Move kubernetes example into hotrod/kubernetes

Signed-off-by: Brandon High <highb@users.noreply.github.com>

* Add jaeger all-in-one to kubernetes example

Signed-off-by: Brandon High <highb@users.noreply.github.com>

* Add one-liner for port-forward to see traces

Signed-off-by: Brandon High <highb@users.noreply.github.com>

* Tracing port needs to be UDP not TCP

Signed-off-by: Brandon High <highb@users.noreply.github.com>

* Update examples/hotrod/README.md

Co-authored-by: Yuri Shkuro <yurishkuro@users.noreply.github.com>
Signed-off-by: Brandon High <highb@users.noreply.github.com>

* Move up kubernetes section in hotrod example README

Signed-off-by: Brandon High <highb@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Yuri Shkuro <yurishkuro@users.noreply.github.com>
Signed-off-by: Brandon High <highb@users.noreply.github.com>

* Add cleanup to k8s example READMEs

Signed-off-by: Brandon High <highb@users.noreply.github.com>

* Remove redis from hotrod example

Signed-off-by: Brandon High <highb@users.noreply.github.com>

* Use pod local/localhost for jaeger-all-in-one

Signed-off-by: Brandon High <highb@users.noreply.github.com>

Co-authored-by: Yuri Shkuro <yurishkuro@users.noreply.github.com>
Signed-off-by: Albert Teoh <see.kwang.teoh@gmail.com>
---
 examples/hotrod/README.md                     | 14 +++++++
 examples/hotrod/kubernetes/README.md          | 17 ++++++++
 .../kubernetes/base/hotrod/deployment.yaml    | 40 +++++++++++++++++++
 .../kubernetes/base/hotrod/kustomization.yaml |  6 +++
 .../kubernetes/base/hotrod/service.yaml       | 12 ++++++
 .../base/jaeger-all-in-one/kustomization.yaml | 33 +++++++++++++++
 .../base/jaeger-all-in-one/service.yaml       | 16 ++++++++
 examples/hotrod/kubernetes/kustomization.yaml | 12 ++++++
 examples/hotrod/kubernetes/namespace.yaml     |  4 ++
 9 files changed, 154 insertions(+)
 create mode 100644 examples/hotrod/kubernetes/README.md
 create mode 100644 examples/hotrod/kubernetes/base/hotrod/deployment.yaml
 create mode 100644 examples/hotrod/kubernetes/base/hotrod/kustomization.yaml
 create mode 100644 examples/hotrod/kubernetes/base/hotrod/service.yaml
 create mode 100644 examples/hotrod/kubernetes/base/jaeger-all-in-one/kustomization.yaml
 create mode 100644 examples/hotrod/kubernetes/base/jaeger-all-in-one/service.yaml
 create mode 100644 examples/hotrod/kubernetes/kustomization.yaml
 create mode 100644 examples/hotrod/kubernetes/namespace.yaml

diff --git a/examples/hotrod/README.md b/examples/hotrod/README.md
index f369f87b704..305096f7fc9 100644
--- a/examples/hotrod/README.md
+++ b/examples/hotrod/README.md
@@ -28,6 +28,20 @@ to view the traces. A tutorial / walkthrough is available:
 
 Alternatively, you can run each component separately as described below.
 
+### Run everything in Kubernetes
+
+```bash
+kustomize build ./kubernetes | kubectl apply -f -
+kubectl port-forward -n example-hotrod service/example-hotrod 8080:frontend
+# In another terminal
+kubectl port-forward -n example-hotrod service/jaeger 16686:frontend
+
+# To cleanup
+kustomize build ./kubernetes | kubectl delete -f -
+```
+
+Access Jaeger UI at http://localhost:16686 and HotROD app at http://localhost:8080
+
 ### Run Jaeger backend
 
 An all-in-one Jaeger backend is packaged as a Docker container with in-memory storage.
diff --git a/examples/hotrod/kubernetes/README.md b/examples/hotrod/kubernetes/README.md
new file mode 100644
index 00000000000..e58aa2f93a7
--- /dev/null
+++ b/examples/hotrod/kubernetes/README.md
@@ -0,0 +1,17 @@
+# Hot R.O.D. - Rides on Demand on Kubernetes
+
+Example k8s manifests for deploying the [hotrod app](..) to your k8s environment of choice. e.g. minikube, k3s, EKS, GKE
+
+## Usage
+
+```bash
+kustomize build . | kubectl apply -f -
+kubectl port-forward -n example-hotrod service/example-hotrod 8080:frontend
+# In another terminal
+kubectl port-forward -n example-hotrod service/jaeger 16686:frontend
+
+# To cleanup
+kustomize build . | kubectl delete -f -
+```
+
+Access Jaeger UI at <http://localhost:16686> and HotROD app at <http://localhost:8080>
diff --git a/examples/hotrod/kubernetes/base/hotrod/deployment.yaml b/examples/hotrod/kubernetes/base/hotrod/deployment.yaml
new file mode 100644
index 00000000000..420f531816e
--- /dev/null
+++ b/examples/hotrod/kubernetes/base/hotrod/deployment.yaml
@@ -0,0 +1,40 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  labels:
+    app: example-hotrod
+  name: example-hotrod
+spec:
+  replicas: 1
+  selector:
+    matchLabels:
+      app: example-hotrod
+  strategy: {}
+  template:
+    metadata:
+      labels:
+        app: example-hotrod
+    spec:
+      containers:
+      - image: jaegertracing/example-hotrod:latest
+        name: example-hotrod
+        args: ["all"]
+        env:
+          - name: JAEGER_AGENT_HOST
+            value: localhost
+          - name: JAEGER_AGENT_PORT
+            value: "6831"
+        ports:
+          - containerPort: 8080
+            name: frontend
+          - containerPort: 8081
+            name: customer
+          - containerPort: 8083
+            name: route
+        resources:
+          limits:
+            cpu: 100m
+            memory: 100M
+          requests:
+            cpu: 100m
+            memory: 100M
diff --git a/examples/hotrod/kubernetes/base/hotrod/kustomization.yaml b/examples/hotrod/kubernetes/base/hotrod/kustomization.yaml
new file mode 100644
index 00000000000..5b98e944923
--- /dev/null
+++ b/examples/hotrod/kubernetes/base/hotrod/kustomization.yaml
@@ -0,0 +1,6 @@
+apiVersion: kustomize.config.k8s.io/v1beta1
+kind: Kustomization
+
+resources:
+  - deployment.yaml
+  - service.yaml
diff --git a/examples/hotrod/kubernetes/base/hotrod/service.yaml b/examples/hotrod/kubernetes/base/hotrod/service.yaml
new file mode 100644
index 00000000000..7ac8a5cd705
--- /dev/null
+++ b/examples/hotrod/kubernetes/base/hotrod/service.yaml
@@ -0,0 +1,12 @@
+apiVersion: v1
+kind: Service
+metadata:
+  name: example-hotrod
+spec:
+  selector:
+    app: example-hotrod
+  ports:
+    - name: frontend
+      protocol: TCP
+      port: 8080
+      targetPort: frontend
diff --git a/examples/hotrod/kubernetes/base/jaeger-all-in-one/kustomization.yaml b/examples/hotrod/kubernetes/base/jaeger-all-in-one/kustomization.yaml
new file mode 100644
index 00000000000..e64e7bb96d3
--- /dev/null
+++ b/examples/hotrod/kubernetes/base/jaeger-all-in-one/kustomization.yaml
@@ -0,0 +1,33 @@
+apiVersion: kustomize.config.k8s.io/v1beta1
+kind: Kustomization
+
+bases:
+  - ../hotrod
+
+resources:
+  - service.yaml
+
+patches:
+  - target:
+      group: apps
+      version: v1
+      kind: Deployment
+      name: example-hotrod
+    patch: |-
+      - op: add
+        path: /spec/template/spec/containers/-
+        value:
+          image: jaegertracing/all-in-one:latest
+          name: jaeger
+          ports:
+            - containerPort: 6831
+              name: tracing-jaeger
+            - containerPort: 16686
+              name: frontend-jaeger
+          resources:
+            limits:
+              cpu: 100m
+              memory: 100M
+            requests:
+              cpu: 100m
+              memory: 100M
diff --git a/examples/hotrod/kubernetes/base/jaeger-all-in-one/service.yaml b/examples/hotrod/kubernetes/base/jaeger-all-in-one/service.yaml
new file mode 100644
index 00000000000..e893304eb17
--- /dev/null
+++ b/examples/hotrod/kubernetes/base/jaeger-all-in-one/service.yaml
@@ -0,0 +1,16 @@
+apiVersion: v1
+kind: Service
+metadata:
+  name: jaeger
+spec:
+  selector:
+    app: example-hotrod
+  ports:
+    - name: tracing
+      protocol: UDP
+      port: 6831
+      targetPort: tracing-jaeger
+    - name: frontend
+      protocol: TCP
+      port: 16686
+      targetPort: frontend-jaeger
diff --git a/examples/hotrod/kubernetes/kustomization.yaml b/examples/hotrod/kubernetes/kustomization.yaml
new file mode 100644
index 00000000000..84859ec2772
--- /dev/null
+++ b/examples/hotrod/kubernetes/kustomization.yaml
@@ -0,0 +1,12 @@
+apiVersion: kustomize.config.k8s.io/v1beta1
+kind: Kustomization
+
+bases:
+# If you only want the hotrod application, uncomment this line and comment out jaeger-all-in-one
+#  - base/hotrod
+  - base/jaeger
+
+namespace: example-hotrod
+
+resources:
+  - namespace.yaml
diff --git a/examples/hotrod/kubernetes/namespace.yaml b/examples/hotrod/kubernetes/namespace.yaml
new file mode 100644
index 00000000000..e89d2e0c795
--- /dev/null
+++ b/examples/hotrod/kubernetes/namespace.yaml
@@ -0,0 +1,4 @@
+apiVersion: v1
+kind: Namespace
+metadata:
+  name: example-hotrod