Skip to content
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
139 changes: 139 additions & 0 deletions examples/skaffold/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Developing multiple functions on the same host using Minikube and Skaffold

## Introduction

This example shows you how to develop multiple Cloud Functions to a single host
using Minikube and Skaffold.

The example will focus on:
* taking two separate Cloud Functions (defined in the same file)
* building them each individually with Cloud Buildpacks and the Functions Framework
* deploying them to a local Kubernetes cluster with `minikube` and `skaffold`
* including live reloading!

## Install `minikube`
*Note: If on Cloud Shell, `minikube` is pre-installed.*

Install `minikube` via the instructions for your platform at <https://minikube.sigs.k8s.io/docs/start/>

Confirm that `minikube` is installed:

```bash
minikube version
```

You should see output similar to:

```terminal
minikube version: v1.15.1
commit: 23f40a012abb52eff365ff99a709501a61ac5876
```

## Start `minikube`

This starts `minikube` using the default profile:

```bash
minikube start
```

This may take a few minutes.

*Note: If on Cloud Shell, you may be asked to enable Cloud Shell to make API calls*

You should see output similar to:

```terminal
😄 minikube v1.15.1 on Debian 10.6
▪ MINIKUBE_FORCE_SYSTEMD=true
▪ MINIKUBE_HOME=/google/minikube
▪ MINIKUBE_WANTUPDATENOTIFICATION=false
✨ Automatically selected the docker driver
👍 Starting control plane node minikube in cluster minikube
🚜 Pulling base image ...
💾 Downloading Kubernetes v1.19.4 preload ...
🔥 Creating docker container (CPUs=2, Memory=4000MB) ...
🐳 Preparing Kubernetes v1.19.4 on Docker 19.03.13 ...
🔎 Verifying Kubernetes components...
🌟 Enabled addons: storage-provisioner, default-storageclass
🏄 Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
```

## Install the `ingress` addon for `minikube`

This allows `minikube` to handle external traffic:

```bash
minikube addons enable ingress
```

You should see output similar to:

```terminal
🔎 Verifying ingress addon...
🌟 The 'ingress' addon is enabled
```

## Install `skaffold`
*Note: If on Cloud Shell, `skaffold` is pre-installed.*

Install `skaffold` via the instructions for your platform at <https://skaffold.dev/docs/install/>

Confirm that `skaffold` is installed:

```bash
skaffold version
```

You should see output similar to:

```terminal
v1.16.0
```

## Start `skaffold`

Start `skaffold` with:

```bash
skaffold dev
```

You should see output similar to:

```terminal
Starting deploy...
Waiting for deployments to stabilize...
- deployment/hello is ready. [1/2 deployment(s) still pending]
- deployment/goodbye is ready.
Deployments stabilized in 1.154162006s
Watching for changes...
```

This command will continue running indefinitely, watching for changes and redeploying as necessary.

## Call your Cloud Functions

Leaving the previous command running, in a **new terminal**, call your functions. To call the `hello` function:

```bash
curl `minikube ip`/hello
```

You should see output similar to:

```terminal
Hello, World!
```

To call the `goodbye` function:

```bash
curl `minikube ip`/goodbye
```

You should see output similar to:

```terminal
Goodbye, World!
```
47 changes: 47 additions & 0 deletions examples/skaffold/k8s/goodbye.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: Service
metadata:
name: goodbye
spec:
ports:
- port: 8080
name: http
type: LoadBalancer
selector:
app: goodbye
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: goodbye
spec:
selector:
matchLabels:
app: goodbye
template:
metadata:
labels:
app: goodbye
spec:
containers:
- name: goodbye
image: example-goodbye-image
env:
- name: PORT
value: "8080"
ports:
- containerPort: 8080
47 changes: 47 additions & 0 deletions examples/skaffold/k8s/hello.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: Service
metadata:
name: hello
spec:
ports:
- port: 8080
name: http
type: LoadBalancer
selector:
app: hello
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello
spec:
selector:
matchLabels:
app: hello
template:
metadata:
labels:
app: hello
spec:
containers:
- name: hello
image: example-hello-image
env:
- name: PORT
value: "8080"
ports:
- containerPort: 8080
30 changes: 30 additions & 0 deletions examples/skaffold/k8s/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: scaffold-example-ingress
spec:
rules:
- http:
paths:
- path: /hello
backend:
serviceName: hello
servicePort: 8080
- path: /goodbye
backend:
serviceName: goodbye
servicePort: 8080
23 changes: 23 additions & 0 deletions examples/skaffold/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def hello(request):
"""Return a friendly HTTP greeting."""
return "Hello, World!"


def goodbye(request):
"""Return a friendly HTTP goodbye."""
return "Goodbye, World!"
1 change: 1 addition & 0 deletions examples/skaffold/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Add any Python requirements here
28 changes: 28 additions & 0 deletions examples/skaffold/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: skaffold/v2beta9
kind: Config
build:
artifacts:
- image: example-hello-image
buildpacks:
builder: "gcr.io/buildpacks/builder:v1"
env:
- "GOOGLE_FUNCTION_TARGET=hello"
- image: example-goodbye-image
buildpacks:
builder: "gcr.io/buildpacks/builder:v1"
env:
- "GOOGLE_FUNCTION_TARGET=goodbye"