mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-28 01:31:17 +00:00
Make the example work on kubernetes (#1463)
* Make the example work on kubernetes * Example app: Launch weave if not running.
This commit is contained in:
@@ -2,7 +2,7 @@ CC=gcc
|
||||
CFLAGS=-g -lpthread
|
||||
BUILD_IN_CONTAINER=true
|
||||
|
||||
all: .qotd.marker .app.marker .client.marker .searchapp.marker .shout.marker .frontend.marker .echo.marker .trace_app.marker
|
||||
all: .qotd.marker .app.marker .client.marker .searchapp.marker .shout.marker .frontend.marker .frontend-k8s.marker .echo.marker .trace_app.marker
|
||||
|
||||
searchapp/searchapp: searchapp/app.go
|
||||
shout/shout: shout/shout.go
|
||||
@@ -29,6 +29,7 @@ endif
|
||||
.searchapp.marker: searchapp/* searchapp/searchapp
|
||||
.shout.marker: shout/* shout/shout
|
||||
.frontend.marker: frontend/*
|
||||
.frontend-k8s.marker: frontend-k8s/*
|
||||
.echo.marker: echo/*
|
||||
.trace_app.marker: trace_app/*
|
||||
.%.marker:
|
||||
|
||||
@@ -5,6 +5,7 @@ import requests
|
||||
import random
|
||||
import threading
|
||||
import logging
|
||||
import argparse
|
||||
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from flask import Flask
|
||||
@@ -16,8 +17,6 @@ redis = Redis(host='redis', port=6379)
|
||||
pool = ThreadPoolExecutor(max_workers=10)
|
||||
sessions = threading.local()
|
||||
|
||||
searchapps = ['http://searchapp:8080/']
|
||||
|
||||
def do_redis():
|
||||
redis.incr('hits')
|
||||
return redis.get('hits')
|
||||
@@ -25,7 +24,7 @@ def do_redis():
|
||||
def do_qotd():
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
try:
|
||||
s.connect(("qotd", 4446))
|
||||
s.connect((args.qotd, 4446))
|
||||
s.send("Hello")
|
||||
return s.recv(1024)
|
||||
finally:
|
||||
@@ -34,11 +33,13 @@ def do_qotd():
|
||||
def do_search():
|
||||
if getattr(sessions, 'session', None) == None:
|
||||
sessions.session = requests.Session()
|
||||
r = sessions.session.get(random.choice(searchapps))
|
||||
r = sessions.session.get(args.search)
|
||||
return r.text
|
||||
|
||||
def do_echo(text):
|
||||
r = requests.get("http://echo/", data=text)
|
||||
if getattr(sessions, 'session', None) == None:
|
||||
sessions.session = requests.Session()
|
||||
r = sessions.session.get(args.echo, data=text)
|
||||
return r.text
|
||||
|
||||
def ignore_error(f):
|
||||
@@ -69,6 +70,13 @@ def root():
|
||||
return result
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-redis', default="redis.weave.local")
|
||||
parser.add_argument('-search', default="http://search.weave.local:80/")
|
||||
parser.add_argument('-qotd', default="qotd.weave.local")
|
||||
parser.add_argument('-echo', default="http://echo.weave.local:80/")
|
||||
args = parser.parse_args()
|
||||
|
||||
logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s', level=logging.INFO)
|
||||
WSGIRequestHandler.protocol_version = "HTTP/1.1"
|
||||
app.run(host="0.0.0.0", port=80, debug=True)
|
||||
|
||||
11
experimental/example/frontend-k8s/Dockerfile
Normal file
11
experimental/example/frontend-k8s/Dockerfile
Normal file
@@ -0,0 +1,11 @@
|
||||
FROM ubuntu
|
||||
MAINTAINER Weaveworks Inc <help@weave.works>
|
||||
RUN apt-get update && \
|
||||
apt-get install -y nginx && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
RUN rm /etc/nginx/sites-available/default && \
|
||||
ln -sf /dev/stdout /var/log/nginx/access.log && \
|
||||
ln -sf /dev/stderr /var/log/nginx/error.log
|
||||
COPY default.conf /etc/nginx/conf.d/
|
||||
EXPOSE 80
|
||||
CMD ["nginx", "-g", "daemon off;"]
|
||||
7
experimental/example/frontend-k8s/default.conf
Normal file
7
experimental/example/frontend-k8s/default.conf
Normal file
@@ -0,0 +1,7 @@
|
||||
server {
|
||||
listen 80;
|
||||
|
||||
location / {
|
||||
proxy_pass http://app.default.svc.cluster.local;
|
||||
}
|
||||
}
|
||||
22
experimental/example/k8s/app-deployment.yaml
Normal file
22
experimental/example/k8s/app-deployment.yaml
Normal file
@@ -0,0 +1,22 @@
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: app
|
||||
spec:
|
||||
replicas: 2
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
name: app
|
||||
spec:
|
||||
containers:
|
||||
- name: app
|
||||
image: tomwilkie/app
|
||||
imagePullPolicy: IfNotPresent
|
||||
args:
|
||||
- -echo=http://echo.default.svc.cluster.local/
|
||||
- -qotd=qotd.default.svc.cluster.local
|
||||
- -redis=redis.default.svc.cluster.local
|
||||
- -search=http://search.default.svc.cluster.local/
|
||||
ports:
|
||||
- containerPort: 80
|
||||
11
experimental/example/k8s/app-svc.yaml
Normal file
11
experimental/example/k8s/app-svc.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: app
|
||||
spec:
|
||||
type: LoadBalancer
|
||||
ports:
|
||||
- name: http
|
||||
port: 80
|
||||
selector:
|
||||
name: app
|
||||
17
experimental/example/k8s/client-deployment.yaml
Normal file
17
experimental/example/k8s/client-deployment.yaml
Normal file
@@ -0,0 +1,17 @@
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: client
|
||||
spec:
|
||||
replicas: 2
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
name: client
|
||||
spec:
|
||||
containers:
|
||||
- name: client
|
||||
image: tomwilkie/client
|
||||
imagePullPolicy: IfNotPresent
|
||||
args:
|
||||
- -target=frontend.default.svc.cluster.local
|
||||
17
experimental/example/k8s/echo-deployment.yaml
Normal file
17
experimental/example/k8s/echo-deployment.yaml
Normal file
@@ -0,0 +1,17 @@
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: echo
|
||||
spec:
|
||||
replicas: 1
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
name: echo
|
||||
spec:
|
||||
containers:
|
||||
- name: echo
|
||||
image: tomwilkie/echo
|
||||
imagePullPolicy: IfNotPresent
|
||||
ports:
|
||||
- containerPort: 80
|
||||
11
experimental/example/k8s/echo-svc.yaml
Normal file
11
experimental/example/k8s/echo-svc.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: echo
|
||||
spec:
|
||||
type: LoadBalancer
|
||||
ports:
|
||||
- name: http
|
||||
port: 80
|
||||
selector:
|
||||
name: echo
|
||||
17
experimental/example/k8s/elasticsearch-deployment.yaml
Normal file
17
experimental/example/k8s/elasticsearch-deployment.yaml
Normal file
@@ -0,0 +1,17 @@
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: elasticsearch
|
||||
spec:
|
||||
replicas: 1
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
name: elasticsearch
|
||||
spec:
|
||||
containers:
|
||||
- name: elasticsearch
|
||||
image: elasticsearch
|
||||
imagePullPolicy: IfNotPresent
|
||||
ports:
|
||||
- containerPort: 9200
|
||||
11
experimental/example/k8s/elasticsearch-svc.yaml
Normal file
11
experimental/example/k8s/elasticsearch-svc.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: elasticsearch
|
||||
spec:
|
||||
type: LoadBalancer
|
||||
ports:
|
||||
- name: http
|
||||
port: 9200
|
||||
selector:
|
||||
name: elasticsearch
|
||||
17
experimental/example/k8s/frontend-deployment.yaml
Normal file
17
experimental/example/k8s/frontend-deployment.yaml
Normal file
@@ -0,0 +1,17 @@
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: frontend
|
||||
spec:
|
||||
replicas: 2
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
name: frontend
|
||||
spec:
|
||||
containers:
|
||||
- name: frontend
|
||||
image: tomwilkie/frontend-k8s
|
||||
imagePullPolicy: IfNotPresent
|
||||
ports:
|
||||
- containerPort: 80
|
||||
11
experimental/example/k8s/frontend-svc.yaml
Normal file
11
experimental/example/k8s/frontend-svc.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: frontend
|
||||
spec:
|
||||
type: LoadBalancer
|
||||
ports:
|
||||
- name: http
|
||||
port: 80
|
||||
selector:
|
||||
name: frontend
|
||||
17
experimental/example/k8s/qotd-deployment.yaml
Normal file
17
experimental/example/k8s/qotd-deployment.yaml
Normal file
@@ -0,0 +1,17 @@
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: qotd
|
||||
spec:
|
||||
replicas: 1
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
name: qotd
|
||||
spec:
|
||||
containers:
|
||||
- name: qotd
|
||||
image: tomwilkie/qotd
|
||||
imagePullPolicy: IfNotPresent
|
||||
ports:
|
||||
- containerPort: 4446
|
||||
11
experimental/example/k8s/qotd-svc.yaml
Normal file
11
experimental/example/k8s/qotd-svc.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: qotd
|
||||
spec:
|
||||
type: LoadBalancer
|
||||
ports:
|
||||
- name: qotd
|
||||
port: 4446
|
||||
selector:
|
||||
name: qotd
|
||||
17
experimental/example/k8s/redis-deployment.yaml
Normal file
17
experimental/example/k8s/redis-deployment.yaml
Normal file
@@ -0,0 +1,17 @@
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: redis
|
||||
spec:
|
||||
replicas: 1
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
name: redis
|
||||
spec:
|
||||
containers:
|
||||
- name: redis
|
||||
image: redis
|
||||
imagePullPolicy: IfNotPresent
|
||||
ports:
|
||||
- containerPort: 6379
|
||||
11
experimental/example/k8s/redis-svc.yaml
Normal file
11
experimental/example/k8s/redis-svc.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: redis
|
||||
spec:
|
||||
type: LoadBalancer
|
||||
ports:
|
||||
- name: redis
|
||||
port: 6379
|
||||
selector:
|
||||
name: redis
|
||||
21
experimental/example/k8s/search-deployment.yaml
Normal file
21
experimental/example/k8s/search-deployment.yaml
Normal file
@@ -0,0 +1,21 @@
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: search
|
||||
spec:
|
||||
replicas: 1
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
name: search
|
||||
spec:
|
||||
containers:
|
||||
- name: search
|
||||
image: tomwilkie/searchapp
|
||||
imagePullPolicy: IfNotPresent
|
||||
ports:
|
||||
- containerPort: 80
|
||||
args:
|
||||
- -addr=:80
|
||||
- -target=http://elasticsearch.default.svc.cluster.local:9200
|
||||
|
||||
11
experimental/example/k8s/search-svc.yaml
Normal file
11
experimental/example/k8s/search-svc.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: search
|
||||
spec:
|
||||
type: LoadBalancer
|
||||
ports:
|
||||
- name: http
|
||||
port: 80
|
||||
selector:
|
||||
name: search
|
||||
@@ -4,6 +4,10 @@ set -ex
|
||||
|
||||
readonly ARG="$1"
|
||||
|
||||
if ! $(weave status 1>/dev/null 2>&1); then
|
||||
weave launch
|
||||
fi
|
||||
|
||||
eval $(weave env)
|
||||
|
||||
start_container() {
|
||||
@@ -24,7 +28,7 @@ start_container() {
|
||||
}
|
||||
|
||||
start_container 1 elasticsearch elasticsearch
|
||||
start_container 2 tomwilkie/searchapp searchapp
|
||||
start_container 2 tomwilkie/searchapp search
|
||||
start_container 1 redis redis
|
||||
start_container 1 tomwilkie/qotd qotd
|
||||
start_container 1 tomwilkie/echo echo
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
FROM progrium/busybox
|
||||
MAINTAINER Weaveworks Inc <help@weave.works>
|
||||
WORKDIR /home/weave
|
||||
ADD searchapp entrypoint.sh /home/weave/
|
||||
ADD searchapp /home/weave/
|
||||
EXPOSE 8080
|
||||
ENTRYPOINT ["/home/weave/entrypoint.sh"]
|
||||
ENTRYPOINT ["/home/weave/searchapp"]
|
||||
|
||||
@@ -19,23 +19,11 @@ import (
|
||||
|
||||
func main() {
|
||||
var (
|
||||
addr = flag.String("addr", ":8080", "HTTP listen address")
|
||||
addr = flag.String("addr", ":80", "HTTP listen address")
|
||||
target = flag.String("target", "http://elasticsearch.weave.local:9200", "Elastic hostname")
|
||||
)
|
||||
flag.Parse()
|
||||
|
||||
var targets []string
|
||||
for _, s := range os.Args[1:] {
|
||||
target, err := normalize(s)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Printf("target %s", target)
|
||||
targets = append(targets, target)
|
||||
}
|
||||
if len(targets) <= 0 {
|
||||
log.Fatal("no targets")
|
||||
}
|
||||
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
hostname = strings.ToLower(strings.Replace(hostname, " ", "-", -1)) // lol
|
||||
@@ -45,7 +33,7 @@ func main() {
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("Handling %v", r)
|
||||
read(targets, &reads)
|
||||
read([]string{*target}, &reads)
|
||||
fmt.Fprintf(w, "%s %d", hostname, atomic.LoadUint64(&reads))
|
||||
})
|
||||
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
#!/bin/sh
|
||||
exec /home/weave/searchapp elasticsearch.weave.local
|
||||
Reference in New Issue
Block a user