[go: up one dir, main page]

Skip to content

Building, running, or deploying functions

After you have created a function project, you can build, run, or deploy your function, depending on your use case.

Running a function

Running a function creates an OCI container image for your function before running the function in your local environment, but does not deploy the function to a cluster. This can be useful if you want to run your function locally for a testing scenario.

Prerequisites

  • You have a Docker daemon on your local machine. This is already provided if you have used the Quickstart installation.

Procedure

The run command builds an image for your function if required, and runs this image locally, instead of deploying it on a cluster.

Run the function locally by running the command inside the project directory. If you have not yet built the function you will need to provide the --registry flag:

cd hello
func run [--registry <registry>]

Note

The coordinates for the image registry can be configured through an environment variable (FUNC_REGISTRY) as well.

Using this command also builds the function if necessary.

You can force a rebuild of the image by running the command:

func run --build

It is also possible to disable the build, by running the command:

func run --build=false

Run the function locally, by running the command inside the project directory:

cd hello
kn func run

Using this command also builds the function if necessary.

You can force a rebuild of the image by running the command:

kn func run --build

It is also possible to disable the build, by running the command:

kn func run --build=false

You can verify that your function has been successfully run by using the invoke command and observing the output:

func invoke
kn func invoke

Expected output

Received response
POST / HTTP/1.1 hello.default.127.0.0.1.sslip.io
  User-Agent: Go-http-client/1.1
  Content-Length: 25
  Accept-Encoding: gzip
  Content-Type: application/json
  K-Proxy-Request: activator
  X-Request-Id: 9e351834-0542-4f32-9928-3a5d6aece30c
  Forwarded: for=10.244.0.15;proto=http
  X-Forwarded-For: 10.244.0.15, 10.244.0.9
  X-Forwarded-Proto: http
Body:

Deploying a function

Deploying a function creates an OCI container image for your function, and pushes this container image to your image registry. The function is deployed to the cluster as a Knative Service. Redeploying a function updates the container image and resulting Service that is running on your cluster. Functions that have been deployed to a cluster are accessible on the cluster just like any other Knative Service.

Prerequisites

  • You have a Docker daemon on your local machine. This is already provided if you have used the Quickstart installation.

  • You have access to a container registry and are able to push images to this registry. Note that some image registries set newly pushed images to private by default. If you are deploying a function for the first time, you may need to ensure that your images are set to public.

Procedure

The deploy command uses the function project name as the Knative Service name. When the function is built, the project name and the image registry name are used to construct a fully qualified image name for the function.

Deploy the function by running the command inside the project directory:

func deploy --registry <registry>

Deploy the function by running the command inside the project directory:

kn func deploy --registry <registry>

Expected output

    🙌 Function image built: <registry>/hello:latest
    ✅ Function deployed in namespace "default" and exposed at URL:
    http://hello.default.127.0.0.1.sslip.io

You can verify that your function has been successfully deployed by using the invoke command and observing the output:

func invoke
kn func invoke

Expected output

Received response
POST / HTTP/1.1 hello.default.127.0.0.1.sslip.io
  User-Agent: Go-http-client/1.1
  Content-Length: 25
  Accept-Encoding: gzip
  Content-Type: application/json
  K-Proxy-Request: activator
  X-Request-Id: 9e351834-0542-4f32-9928-3a5d6aece30c
  Forwarded: for=10.244.0.15;proto=http
  X-Forwarded-For: 10.244.0.15, 10.244.0.9
  X-Forwarded-Proto: http
Body:

Building a function

Building a function creates an OCI container image for your function that can be pushed to a container registry. It does not run or deploy the function, which can be useful if you want to build a container image for your function locally, but do not want to automatically run the function or deploy it to a cluster, for example, in a testing scenario.

Prerequisites

  • You have a Docker daemon on your local machine. This is already provided if you have used the Quickstart installation.

Procedure

The build command uses the project name and the image registry name to construct a fully qualified container image name for the function. If the function project has not previously been built, you are prompted to provide an image registry.

To build the function, run the following command:

func build

To build the function, run the following command:

kn func build

Note

The coordinates for the image registry can be configured through an environment variable (FUNC_REGISTRY) as well.

We use analytics and cookies to understand site traffic. Information about your use of our site is shared with Google for that purpose. Learn more.

× OK