Skip to content

Commit ad65372

Browse files
committed
Add example of developing multiple functions locally
1 parent ab372f5 commit ad65372

File tree

7 files changed

+314
-0
lines changed

7 files changed

+314
-0
lines changed

examples/skaffold/README.md

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

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 "Hello, 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)