Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added howto for using header routing with Kubernetes #203

Merged
merged 1 commit into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions walkthroughs/howto-k8s-http-headers/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_output
25 changes: 25 additions & 0 deletions walkthroughs/howto-k8s-http-headers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Overview
This example shows how http routes can use headers for matching incoming requests.

## Prerequisites
[Walkthrough: App Mesh with EKS](../eks/)

## Setup

1. Clone this repository and navigate to the walkthrough/howto-k8s-cloudmap folder, all commands will be ran from this location
2. **Your** account id:
```
export AWS_ACCOUNT_ID=<your_account_id>
```
3. **Region** e.g. us-west-2
```
export AWS_DEFAULT_REGION=us-west-2
```
4. **ENVOY_IMAGE** environment variable is set to App Mesh Envoy, see https://docs.aws.amazon.com/app-mesh/latest/userguide/envoy.html
```
export ENVOY_IMAGE=...
```
5. Deploy
```.
./deploy.sh
```
6 changes: 6 additions & 0 deletions walkthroughs/howto-k8s-http-headers/colorapp/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM python:3

COPY serve.py ./
RUN chmod +x ./serve.py

CMD ["python", "-u", "./serve.py"]
29 changes: 29 additions & 0 deletions walkthroughs/howto-k8s-http-headers/colorapp/serve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3

try:
import os
from http.server import BaseHTTPRequestHandler, HTTPServer
except Exception as e:
print(f'[ERROR] {e}')

COLOR = os.environ.get('COLOR', 'no color!')
print(f'COLOR is {COLOR}')

PORT = int(os.environ.get('PORT', '8080'))
print(f'PORT is {PORT}')

class Handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/ping':
self.send_response(200)
self.end_headers()
return

self.send_response(200)
self.end_headers()
self.wfile.write(bytes(COLOR, 'utf8'))

print('starting server...')
httpd = HTTPServer(('', PORT), Handler)
print('running server...')
httpd.serve_forever()
58 changes: 58 additions & 0 deletions walkthroughs/howto-k8s-http-headers/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

set -e

if [ -z $AWS_ACCOUNT_ID ]; then
echo "AWS_ACCOUNT_ID environment variable is not set."
exit 1
fi

if [ -z $AWS_DEFAULT_REGION ]; then
echo "AWS_DEFAULT_REGION environment variable is not set."
exit 1
fi

if [ -z $ENVOY_IMAGE ]; then
echo "ENVOY_IMAGE environment variable is not set to App Mesh Envoy, see https://docs.aws.amazon.com/app-mesh/latest/userguide/envoy.html"
exit 1
fi

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
PROJECT_NAME="howto-k8s-http-headers"
APP_NAMESPACE=${PROJECT_NAME}
MESH_NAME=${PROJECT_NAME}

ECR_IMAGE_PREFIX="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${PROJECT_NAME}"
FRONT_APP_IMAGE="${ECR_IMAGE_PREFIX}/feapp"
COLOR_APP_IMAGE="${ECR_IMAGE_PREFIX}/colorapp"

deploy_images() {
for app in colorapp feapp; do
aws ecr describe-repositories --repository-name $PROJECT_NAME/$app >/dev/null 2>&1 || aws ecr create-repository --repository-name $PROJECT_NAME/$app
docker build -t ${ECR_IMAGE_PREFIX}/${app} ${DIR}/${app}
$(aws ecr get-login --no-include-email)
docker push ${ECR_IMAGE_PREFIX}/${app}
done
}

deploy_app() {
EXAMPLES_OUT_DIR="${DIR}/_output/"
mkdir -p ${EXAMPLES_OUT_DIR}
eval "cat <<EOF
$(<${DIR}/manifest.yaml.template)
EOF
" >${EXAMPLES_OUT_DIR}/manifest.yaml

kubectl apply -f ${EXAMPLES_OUT_DIR}/manifest.yaml
}

main() {
if [ -z $SKIP_IMAGES ]; then
echo "deploy images..."
deploy_images
fi

deploy_app
}

main
6 changes: 6 additions & 0 deletions walkthroughs/howto-k8s-http-headers/feapp/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM python:3

COPY serve.py ./
RUN chmod +x ./serve.py

CMD ["python", "-u", "./serve.py"]
48 changes: 48 additions & 0 deletions walkthroughs/howto-k8s-http-headers/feapp/serve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3

try:
import os
import socket
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
except Exception as e:
print(f'[ERROR] {e}')

COLOR_HOST = os.environ.get('COLOR_HOST')
print(f'COLOR_HOST is {COLOR_HOST}')

PORT = int(os.environ.get('PORT', '8080'))
print(f'PORT is {PORT}')

class Handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/ping':
self.send_response(200)
self.end_headers()
return

try:
print('Trying to hit ' + COLOR_HOST.split(':')[0])
print(socket.gethostbyname(COLOR_HOST.split(':')[0]))
req = Request(f'http://{COLOR_HOST}')
cool_header = self.headers.get('color_header')
if cool_header is not None:
req.add_header('color_header', cool_header)
res = urlopen(req)
self.send_response(200)
self.end_headers()
self.wfile.write(res.read())

except HTTPError as e:
print(f'[ERROR] {e}')
self.send_error(e.code, e.reason)

except Exception as e:
print(f'[ERROR] {e}')
self.send_error(500, b'Something really bad happened')

print('starting server...')
httpd = HTTPServer(('', PORT), Handler)
print('running server...')
httpd.serve_forever()
Loading