Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit b89b7e7

Browse files
Instructions for OC Web example in Kubernetes (#37)
1 parent 5cb6119 commit b89b7e7

7 files changed

Lines changed: 256 additions & 13 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ dist/
4343

4444
#istanbul files
4545
coverage/
46+
47+
#static JS files in examples
48+
static/

examples/initial_load/Dockerfile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2019, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http:#www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# First build the server binary
16+
FROM golang:alpine AS build
17+
RUN apk update && apk add --no-cache git && apk add --no-cache ca-certificates
18+
WORKDIR /root
19+
ADD go.mod .
20+
ADD go.sum .
21+
ADD server.go .
22+
RUN GO111MODULE=on go get -d -v
23+
RUN GO111MODULE=on CGO_ENABLED=0 go build -o example_server
24+
25+
# Then put the server binary and needed files into an empty container
26+
FROM scratch
27+
COPY index.html /
28+
COPY ./static/*.js /static/
29+
COPY --from=build ./root/example_server /
30+
ENTRYPOINT ["/example_server"]
31+
EXPOSE 8000

examples/initial_load/README.md

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ Then create a `config.yaml` file following the
2424
receivers:
2525
opencensus:
2626
address: "127.0.0.1:55678"
27-
27+
cors_allowed_origins:
28+
- http://localhost:*
2829
exporters:
2930
# Pick and configure an exporter e.g. stackdriver, zipkin, aws-xray, honeycomb
3031
```
@@ -63,6 +64,85 @@ the trace viewer for the exporter you set up and view the trace of your initial
6364
page load. It will be named `Nav./index.html` (or just `Nav./` if you left off
6465
the actual `index.html` part when you visited the URL).
6566

66-
## Deploying to Kubernetes
67+
## Deploying to GKE (Kubernetes on Google Cloud Platform)
68+
69+
### 1. Install needed tools
70+
71+
Install [gcloud](https://cloud.google.com/sdk/install).
72+
Then run `gcloud components install kubectl` to install `kubectl`.
73+
74+
### 2. Set up GKE cluster and configure container registry
75+
76+
To create a cluster with the following commands:
77+
78+
```bash
79+
gcloud services enable container.googleapis.com
80+
gcloud container clusters create opencensus-web-demo --enable-autoupgrade --num-nodes=1 --zone=us-central1-a
81+
```
82+
You also need to enable Google Container Registry (GCR) on your GCP project and configure the docker CLI to authenticate to GCR:
83+
84+
```bash
85+
gcloud services enable containerregistry.googleapis.com
86+
gcloud auth configure-docker -q
87+
```
88+
89+
### 3. Deploy the OpenCensus Agent
90+
91+
To deploy the agent, run the following commands:
92+
93+
```bash
94+
# Get the project you are using with gcloud
95+
PROJECT_ID="$(gcloud config list --format 'value(core.project)')"
96+
97+
# Substitute the project ID in the k8s config and deploy it
98+
cat ./kubernetes/oc-agent-cors.template.yaml | \
99+
sed "s/{{PROJECT-ID}}/$PROJECT_ID/" | \
100+
kubectl apply -f -
101+
```
102+
Note that this uses the [omnition/opencensus-agent](./kubernetes/agent-cors.yaml)
103+
container from the Docker Hub. You can also build your own container by
104+
following the
105+
[OpenCensus Agent](https://github.com/census-instrumentation/opencensus-service#opencensus-agent)
106+
docs.
107+
108+
### 5. Build the demo application container
109+
110+
First build the OpenCensus Web scripts that will be deployed with the
111+
application:
112+
113+
```bash
114+
npm run build:prod --prefix=../../packages/opencensus-web-all
115+
mkdir -p static
116+
cp ../../packages/opencensus-web-all/dist/*.js ./static
117+
```
118+
Then build the server container and push it to GCR:
119+
120+
```bash
121+
PROJECT_ID="$(gcloud config list --format 'value(core.project)')"
122+
docker build . -t gcr.io/$PROJECT_ID/oc-web-initial-load:latest
123+
gcloud docker -- push gcr.io/$PROJECT_ID/oc-web-initial-load:latest
124+
```
125+
126+
### 4. Deploy the demo application
127+
128+
Run the command `kubectl get svc oc-agent-service` to check if the
129+
`EXTERNAL-IP` column has been filled in. If it is still pending, then wait a bit
130+
and run it again until it's available.
131+
132+
Once the agent has an external IP, you can deploy the example service that uses
133+
it by running the following commands:
134+
135+
```bash
136+
PROJECT_ID="$(gcloud config list --format 'value(core.project)')"
137+
AGENT_IP="$(kubectl get svc oc-agent-service \
138+
-o=custom-columns="IP ADDRESS:.status.loadBalancer.ingress[*].ip" | \
139+
tail -n 1)"
140+
cat ./kubernetes/initial-load-demo.template.yaml | \
141+
sed "s/{{PROJECT-ID}}/$PROJECT_ID/; s/{{AGENT-IP}}/$AGENT_IP/" | \
142+
kubectl apply -f -
143+
```
144+
145+
Then run `kubectl get svc oc-web-initial-load-service` to see the external IP
146+
address for the demo app, which you can then visit in your browser.
67147

68-
TODO(draffensperger): develop example Kubernetes deployment instructions.
148+
You can then view traces in the [Stackdriver Trace](https://console.cloud.google.com/traces/traces) console in GCP. The traces will be named `Nav./`.

examples/initial_load/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ <h1>OpenCensus Web Hello World!</h1>
2323

2424
<script>
2525
traceparent = "{{.Traceparent}}";
26-
ocwAgent = "{{.AgentEndpoint}}";
26+
ocwAgent = "http://{{.AgentHostAndPort}}";
2727
</script>
2828
<script src="{{.OcwScriptEndpoint}}/initial-load-all.js" async></script>
2929
<footer>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2019, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http:#www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
---
16+
apiVersion: extensions/v1beta1
17+
kind: Deployment
18+
metadata:
19+
name: oc-web-initial-load-deployment
20+
spec:
21+
replicas: 1
22+
template:
23+
metadata:
24+
labels:
25+
app: oc-web-initial-load
26+
spec:
27+
containers:
28+
- name: oc-web-initial-load-container
29+
image: gcr.io/{{PROJECT-ID}}/oc-web-initial-load:latest
30+
args: [
31+
"--listen=:8000",
32+
"--agent={{AGENT-IP}}:80",
33+
"--ocw_script_prefix=/static",
34+
]
35+
ports:
36+
- containerPort: 8000
37+
---
38+
apiVersion: v1
39+
kind: Service
40+
metadata:
41+
name: oc-web-initial-load-service
42+
spec:
43+
type: LoadBalancer
44+
selector:
45+
app: oc-web-initial-load
46+
ports:
47+
- protocol: TCP
48+
port: 80
49+
targetPort: 8000
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright 2019, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http:#www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
---
16+
apiVersion: v1
17+
kind: ConfigMap
18+
metadata:
19+
name: oc-agent-conf
20+
namespace: default
21+
data:
22+
oc-agent-config: |
23+
receivers:
24+
opencensus:
25+
address: ":55678"
26+
cors_allowed_origins:
27+
- "*"
28+
exporters:
29+
stackdriver:
30+
project: {{PROJECT-ID}}
31+
enable_tracing: true
32+
enable_metrics: false
33+
---
34+
apiVersion: extensions/v1beta1
35+
kind: Deployment
36+
metadata:
37+
name: oc-agent-deployment
38+
spec:
39+
replicas: 1
40+
template:
41+
metadata:
42+
labels:
43+
app: oc-agent-deployment
44+
spec:
45+
containers:
46+
- name: oc-agent-container
47+
image: omnition/opencensus-agent:latest
48+
volumeMounts:
49+
- name: oc-agent-config-vol
50+
readOnly: true
51+
mountPath: /conf
52+
args: ["--config=/conf/oc-agent-config.yaml"]
53+
ports:
54+
- containerPort: 55678
55+
volumes:
56+
- name: oc-agent-config-vol
57+
configMap:
58+
name: oc-agent-conf
59+
items:
60+
- key: oc-agent-config
61+
path: oc-agent-config.yaml
62+
---
63+
apiVersion: v1
64+
kind: Service
65+
metadata:
66+
name: oc-agent-service
67+
spec:
68+
type: LoadBalancer
69+
selector:
70+
app: oc-agent-deployment
71+
ports:
72+
- protocol: TCP
73+
port: 80
74+
targetPort: 55678

examples/initial_load/server.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,26 @@ import (
3131
)
3232

3333
// Use a local agent by default to help in local development.
34-
var agentEndpoint = flag.String("agent", "http://localhost:55678", "HTTP(S) endpoint of the OpenCensus agent")
34+
var agentHostAndPort = flag.String("agent", "localhost:55678", "Host:port of OpenCensus agent")
3535

3636
// Use local webpack server on port 8080 by default.
37-
var ocwScriptEndpoint = flag.String("ocw_script", "http://localhost:8080", "HTTP(S) endpoint that serves OpenCensus Web JS script")
37+
var ocwScriptEndpoint = flag.String("ocw_script_prefix", "http://localhost:8080", "HTTP(S) endpoint that serves OpenCensus Web JS script")
3838

39-
var listenAddr = flag.String("listen", "localhost:8000", "")
39+
var listenAddr = flag.String("listen", ":8000", "")
4040

4141
// Data rendered to the HTML template
4242
type pageData struct {
4343
Traceparent string
44-
AgentEndpoint string
45-
OcwScriptEndpoint string
44+
AgentHostAndPort template.URL
45+
OcwScriptEndpoint template.URL
4646
}
4747

4848
func main() {
49-
exp, err := ocagent.NewExporter(ocagent.WithInsecure(), ocagent.WithServiceName("hello-server"))
49+
flag.Parse()
50+
exp, err := ocagent.NewExporter(
51+
ocagent.WithInsecure(),
52+
ocagent.WithAddress(*agentHostAndPort),
53+
ocagent.WithServiceName("hello-server"))
5054
if err != nil {
5155
log.Fatalf("Failed to create the agent exporter: %v", err)
5256
}
@@ -61,6 +65,8 @@ func main() {
6165

6266
serveMux := http.NewServeMux()
6367
serveMux.HandleFunc("/", handleRequest)
68+
fs := http.FileServer(http.Dir("static"))
69+
serveMux.Handle("/static/", http.StripPrefix("/static/", fs))
6470

6571
var handler http.Handler = serveMux
6672
handler = &ochttp.Handler{
@@ -74,7 +80,7 @@ func main() {
7480
handler = ensureTraceHeader(handler)
7581

7682
fmt.Printf("OC Web initial load example server listening on %v\n", *listenAddr)
77-
http.ListenAndServe(*listenAddr, handler)
83+
log.Fatal(http.ListenAndServe(*listenAddr, handler))
7884
}
7985

8086
// Adds a random traceparent header if none is specified. This is needed
@@ -104,8 +110,8 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
104110
_, renderSpan := trace.StartSpan(r.Context(), "Render template")
105111
data := pageData{
106112
Traceparent: r.Header.Get("traceparent"),
107-
AgentEndpoint: *agentEndpoint,
108-
OcwScriptEndpoint: *ocwScriptEndpoint,
113+
AgentHostAndPort: template.URL(*agentHostAndPort),
114+
OcwScriptEndpoint: template.URL(*ocwScriptEndpoint),
109115
}
110116
tmpl.Execute(w, data)
111117
renderSpan.End()

0 commit comments

Comments
 (0)