8000 Add example of developing multiple functions locally (#99) · jrmfg/functions-framework-python@c247550 · GitHub
[go: up one dir, main page]

Skip to content

Commit c247550

Browse files
authored
Add example of developing multiple functions locally (GoogleCloudPlatform#99)
* Add example of developing multiple functions locally * Apply suggestions from code review
1 parent ad3406d commit c247550

File tree

7 files changed

+315
-0
lines changed

7 files changed

+315
-0
lines changed

examples/skaffold/README.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Developing multiple functions on the same host using Minikube and Skaffold
2+
3+
## Introduction
4+
5+
This example shows you how to develop multiple Cloud Functions to a single host
6+
using Minikube and Skaffold.
7+
8+
The example will focus on:
9+
* taking two separate Cloud Functions (defined in the same file)
10+
* building them each individually with Cloud Buildpacks and the Functions Framework
11+
* deploying them to a local Kubernetes cluster with `minikube` and `skaffold`
12+
* including live reloading!
13+
14+
## Install `minikube`
15+
*Note: If on Cloud Shell, `minikube` is pre-installed.*
16+
17+
Install `minikube` via the instructions for your platform at <https://minikube.sigs.k8s.io/docs/start/>
18+
19+
Confirm that `minikube` is installed:
20+
21+
```bash
22+
minikube version
23+
```
24+
25+
You should see output similar to:
26+
27+
```terminal
28+
minikube version: v1.15.1
29+
commit: 23f40a012abb52eff365ff99a709501a61ac5876
30+
```
31+
32+
## Start `minikube`
33+
34+
This starts `minikube` using the default profile:
35+
36+
```bash
37+
minikube start
38+
```
39+
40+
This may take a few minutes.
41+
42+
*Note: If on Cloud Shell, you may be asked to enable Cloud Shell to make API calls*
43+
44+
You should see output similar to:
45+
46+
```terminal
47+
😄 minikube v1.15.1 on Debian 10.6
48+
▪ MINIKUBE_FORCE_SYSTEMD=true
49+
▪ MINIKUBE_HOME=/google/minikube
50+
▪ MINIKUBE_WANTUPDATENOTIFICATION=false
51+
✨ Automatically selected the docker driver
52+
👍 Starting control plane node minikube in cluster minikube
53+
🚜 Pulling base image ...
54+
💾 Downloading Kubernetes v1.19.4 preload ...
55+
🔥 Creating docker container (CPUs=2, Memory=4000MB) ...
56+
🐳 Preparing Kubernetes v1.19.4 on Docker 19.03.13 ...
57+
🔎 Verifying Kubernetes components...
58+
🌟 Enabled addons: storage-provisioner, default-storageclass
59+
🏄 Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
60+
```
61+
62+
## Install the `ingress` addon for `minikube`
63+
64+
This allows `minikube` to handle external traffic:
65+
66+
```bash
67+
minikube addons enable ingress
68+
```
69+
70+
You should see output similar to:
71+
72+
```terminal
73+
🔎 Verifying ingress addon...
74+
🌟 The 'ingress' addon is enabled
75+
```
76+
77+
## Install `skaffold`
78+
*Note: If on Cloud Shell, `skaffold` is pre-installed.*
79+
80+
Install `skaffold` via the instructions for your platform at <https://skaffold.dev/docs/install/>
81+
82+
Confirm that `skaffold` is installed:
83+
84+
```bash
85+
skaffold version
86+
```
87+
88+
You should see output similar to:
89+
90+
```terminal
91+
v1.16.0
92+
```
93+
94+
## Start `skaffold`
95+
96+
Start `skaffold` with:
97+
98+
```bash
99+
skaffold dev
100+
```
101+
102+
You should see output similar to:
103+
104+
```terminal
105+
Starting deploy...
106+
Waiting for deployments to stabilize...
107+
- deployment/hello is ready. [1/2 deployment(s) still pending]
108+
- deployment/goodbye is ready.
109+
Deployments stabilized in 1.154162006s
110+
Watching for changes...
111+
```
112+
113+
This command will continue running indefinitely, watching for changes and redeploying as necessary.
114+
115+
## Call your Cloud Functions
116+
117+
Leaving the previous command running, in a **new terminal**, call your functions. To call the `hello` function:
118+
119+
```bash
120+
curl `minikube ip`/hello
121+
```
122+
123+
You should see output similar to:
124+
125+
```terminal
126+
Hello, World!
127+
```
128+
129+
To call the `goodbye` function:
130+
131+
```bash
132+
curl `minikube ip`/goodbye
133+
```
134+
135+
You should see output similar to:
136+
137+
```terminal
138+
Goodbye, World!
139+
```

examples/skaffold/k8s/goodbye.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2020 Google LLC
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+
apiVersion: v1
16+
kind: Service
17+
metadata:
18+
name: goodbye
19+
spec:
20+
ports:
21+
- port: 8080
22+
name: http
23+
type: LoadBalancer
24+
selector:
25+
app: goodbye
26+
---
27+
apiVersion: apps/v1
28+
kind: Deployment
29+
metadata:
30+
name: goodbye
31+
spec:
32+
selector:
33+
matchLabels:
34+
app: goodbye
35+
template:
36+
metadata:
37+
labels:
38+
app: goodbye
39+
spec:
40+
containers:
41+
- name: goodbye
42+
image: example-goodbye-image
43+
env:
44+
- name: PORT
45+
value: "8080"
46+
ports:
47+
- containerPort: 8080

examples/skaffold/k8s/hello.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2020 Google LLC
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+
apiVersion: v1
16+
kind: Service
17+
metadata:
18+
name: hello
19+
spec:
20+
ports:
21+
- port: 8080
22+
name: http
23+
type: LoadBalancer
24+
selector:
25+
app: hello
26+
---
27+
apiVersion: apps/v1
28+
kind: Deployment
29+
metadata:
30+
name: hello
31+
spec:
32+
selector:
33+
matchLabels:
34+
app: hello
35+
template:
36+
metadata:
37+
labels:
38+
app: hello
39+
spec:
40+
containers:
41+
- name: hello
42+
image: example-hello-image
43+
env:
44+
- name: PORT
45+
value: "8080"
46+
ports:
47+
- containerPort: 8080

examples/skaffold/k8s/ingress.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2020 Google LLC
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+
apiVersion: extensions/v1beta1
16+
kind: Ingress
17+
metadata:
18+
name: scaffold-example-ingress
19+
spec:
20+
rules:
21+
- http:
22+
paths:
23+
- path: /hello
24+
backend:
25+
serviceName: hello
26+
servicePort: 8080
27+
- path: /goodbye
28+
backend:
29+
serviceName: goodbye
30+
servicePort: 8080

examples/skaffold/main.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2020 Google LLC
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+
def hello(request):
17+
"""Return a friendly HTTP greeting."""
18+
return "Hell 10000 o, World!"
19+
20+
21+
def goodbye(request):
22+
"""Return a friendly HTTP goodbye."""
23+
return "Goodbye, World!"

examples/skaffold/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Add any Python requirements here

examples/skaffold/skaffold.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2020 Google LLC
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+
apiVersion: skaffold/v2beta9
16+
kind: Config
17+
build:
18+
artifacts:
19+
- image: example-hello-image
20+
buildpacks:
21+
builder: "gcr.io/buildpacks/builder:v1"
22+
env:
23+
- "GOOGLE_FUNCTION_TARGET=hello"
24+
- image: example-goodbye-image
25+
buildpacks:
26+
builder: "gcr.io/buildpacks/builder:v1"
27+
env:
28+
- "GOOGLE_FUNCTION_TARGET=goodbye"

0 commit comments

Comments
 (0)
0