index.md 11.9 KB
Newer Older
D
danielgruesso 已提交
1 2 3
# Serverless

> Introduced in GitLab 11.5.
4
> Serverless is currently in [alpha](https://about.gitlab.com/handbook/product/#alpha).
D
Daniel Gruesso 已提交
5

D
danielgruesso 已提交
6 7 8 9
Run serverless workloads on Kubernetes using [Knative](https://cloud.google.com/knative/).

## Overview

D
Daniel Gruesso 已提交
10
Knative extends Kubernetes to provide a set of middleware components that are useful to build modern, source-centric, container-based applications. Knative brings some significant benefits out of the box through its main components:
D
danielgruesso 已提交
11

D
Daniel Gruesso 已提交
12 13 14
- [Build](https://github.com/knative/build): Source-to-container build orchestration.
- [Eventing](https://github.com/knative/eventing): Management and delivery of events.
- [Serving](https://github.com/knative/serving): Request-driven compute that can scale to zero.
D
danielgruesso 已提交
15 16 17

For more information on Knative, visit the [Knative docs repo](https://github.com/knative/docs).

D
Daniel Gruesso 已提交
18 19
With GitLab serverless, you can deploy both functions-as-a-service (FaaS) and serverless applications.

D
danielgruesso 已提交
20 21 22 23
## Requirements

To run Knative on Gitlab, you will need:

D
Daniel Gruesso 已提交
24 25 26 27 28 29 30
1. **Kubernetes Cluster:** An RBAC-enabled Kubernetes cluster is required to deploy Knative.
    The simplest way to get started is to add a cluster using [GitLab's GKE integration](../index.md#adding-and-creating-a-new-gke-cluster-via-gitlab).
1. **Helm Tiller:** Helm is a package manager for Kubernetes and is required to install
    Knative.
1. **Domain Name:** Knative will provide its own load balancer using Istio. It will provide an
    external IP address for all the applications served by Knative. You will be prompted to enter a
    wildcard domain where your applications will be served. Configure your DNS server to use the
D
danielgruesso 已提交
31
    external IP address for that domain.
32
1. **`.gitlab-ci.yml`:** GitLab uses [Kaniko](https://github.com/GoogleContainerTools/kaniko)
D
Daniel Gruesso 已提交
33
    to build the application and the [TriggerMesh CLI](https://github.com/triggermesh/tm) to simplify the
D
danielgruesso 已提交
34
    deployment of knative services and functions.
35
1. **`serverless.yml`** (for [functions only](#deploying-functions)): When using serverless to deploy functions, the `serverless.yml` file
D
Daniel Gruesso 已提交
36 37
    will contain the information for all the functions being hosted in the repository as well as a reference to the
    runtime being used.
38
1. **`Dockerfile`** (for [applications only](#deploying-serverless-applications): Knative requires a `Dockerfile` in order to build your application. It should be included
D
Daniel Gruesso 已提交
39
    at the root of your project's repo and expose port `8080`.
D
danielgruesso 已提交
40 41 42 43

## Installing Knative via GitLab's Kubernetes integration

NOTE: **Note:**
D
Daniel Gruesso 已提交
44
The minimum recommended cluster size to run Knative is 3-nodes, 6 vCPUs, and 22.50 GB memory. **RBAC must be enabled.**
D
danielgruesso 已提交
45

D
Daniel Gruesso 已提交
46 47
1. [Add a Kubernetes cluster](../index.md) and install Helm.
1. Once Helm has been successfully installed, on the Knative app section, enter the domain to be used with
D
danielgruesso 已提交
48 49 50 51
    your application and click "Install".

    ![install-knative](img/install-knative.png)

D
Daniel Gruesso 已提交
52 53
1. After the Knative installation has finished, you can wait for the IP address to be displayed in the
   **Knative IP Address** field or retrieve the Istio Ingress IP address by running the following command:
D
danielgruesso 已提交
54

D
Daniel Gruesso 已提交
55 56 57
   ```bash
   kubectl get svc --namespace=istio-system knative-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip} '
   ```
D
danielgruesso 已提交
58

D
Daniel Gruesso 已提交
59
   Output:
D
danielgruesso 已提交
60

D
Daniel Gruesso 已提交
61 62 63
   ```bash
   35.161.143.124 my-machine-name:~ my-user$
   ```
D
danielgruesso 已提交
64

D
Daniel Gruesso 已提交
65 66 67 68
1. The ingress is now available at this address and will route incoming requests to the proper service based on the DNS
   name in the request. To support this, a wildcard DNS A record should be created for the desired domain name. For example,
   if your Knative base domain is `knative.info` then you need to create an A record with domain `*.knative.info`
   pointing the ip address of the ingress.
D
danielgruesso 已提交
69

D
danielgruesso 已提交
70
    ![dns entry](img/dns-entry.png)
D
danielgruesso 已提交
71

D
Daniel Gruesso 已提交
72 73 74 75
## Deploying Functions

> Introduced in GitLab 11.6.

D
danielgruesso 已提交
76
Using functions is useful for dealing with independent
D
Daniel Gruesso 已提交
77 78 79
events without needing to maintain a complex unified infrastructure. This allows
you to focus on a single task that can be executed/scaled automatically and independently.

D
danielgruesso 已提交
80
Currently the following [runtimes](https://gitlab.com/triggermesh/runtimes) are offered:
81 82 83 84

- node.js
- kaniko

D
danielgruesso 已提交
85
In order to deploy functions to your Knative instance, the following files must be present:
D
Daniel Gruesso 已提交
86

87
1. `.gitlab-ci.yml`: This template allows to define the stage, environment, and
D
Daniel Gruesso 已提交
88 89 90 91 92 93 94 95 96
   image to be used for your functions. It must be included at the root of your repository:

   ```yaml
   stages:
     - deploy

   functions:
     stage: deploy
     environment: test
D
Daniel Gruesso 已提交
97
     image: gcr.io/triggermesh/tm:v0.0.7
D
Daniel Gruesso 已提交
98 99 100 101 102
     script:
       - tm -n "$KUBE_NAMESPACE" set registry-auth gitlab-registry --registry "$CI_REGISTRY" --username "$CI_REGISTRY_USER" --password "$CI_JOB_TOKEN"
       - tm -n "$KUBE_NAMESPACE" --registry-host "$CI_REGISTRY_IMAGE" deploy --wait
   ```

103 104
    The `gitlab-ci.yml` template creates a `Deploy` stage with a `functions` job that invokes the `tm` CLI with the required parameters.

105
2. `serverless.yml`: This file contains the metadata for your functions,
D
danielgruesso 已提交
106
   such as name, runtime, and environment. It must be included at the root of your repository. The following is a sample `echo` function which shows the required structure for the file. You can find the relevant files for this project in the [functions example project](https://gitlab.com/knative-examples/functions).
107

D
Daniel Gruesso 已提交
108
   ```yaml
109
   service: my-functions
D
Daniel Gruesso 已提交
110 111 112 113 114 115 116 117 118
   description: "Deploying functions from GitLab using Knative"

   provider:
     name: triggermesh
     registry-secret: gitlab-registry
     environment:
       FOO: BAR

   functions:
D
danielgruesso 已提交
119
     echo:
120 121 122
       handler: echo
       runtime: https://gitlab.com/triggermesh/runtimes/raw/master/nodejs.yaml
       description: "echo function using node.js runtime"
D
Daniel Gruesso 已提交
123
       buildargs:
124 125 126
        - DIRECTORY=echo
       environment:
        FUNCTION: echo
D
Daniel Gruesso 已提交
127 128
   ```

129

D
danielgruesso 已提交
130
The `serverless.yml` file is referencing both an `echo` directory (under `buildargs`) and an `echo` file (under `handler`) which is a reference to `echo.js` in the [repository](https://gitlab.com/knative-examples/functions). Additionally, it contains three sections with distinct parameters:
131

D
danielgruesso 已提交
132
### `service`
133

D
danielgruesso 已提交
134 135
| Parameter | Description |
|-----------|-------------|
D
danielgruesso 已提交
136 137
| `service` | Name for the Knative service which will serve the function. |
| `description` | A short description of the `service`. |
138 139


D
danielgruesso 已提交
140
### `provider`
141

D
danielgruesso 已提交
142 143
| Parameter | Description |
|-----------|-------------|
144
| `name` | Indicates which provider is used to execute the `serverless.yml` file. In this case, the TriggerMesh `tm` CLI. |
D
danielgruesso 已提交
145
| `registry-secret` | Indicates which registry will be used to store docker images. The sample function is using the GitLab Registry (`gitlab-registry`). A different registry host may be specified using `registry` key in the `provider` object. If changing the default, update the permission and the secret value on the `gitlab-ci.yml` file |
D
danielgruesso 已提交
146
| `environment` | Includes the environment variables to be passed as part of function execution for **all** functions in the file, where `FOO` is the variable name and `BAR` are he variable contents. You may replace this with you own variables. |
147

D
danielgruesso 已提交
148 149
### `functions`

150
In the `serverless.yml` example above, the function name is `echo` and the subsequent lines contain the function attributes.
D
danielgruesso 已提交
151

D
danielgruesso 已提交
152 153 154

| Parameter | Description |
|-----------|-------------|
D
danielgruesso 已提交
155 156 157 158 159
| `handler` | The function's file name. In the example above, both the function name and the handler are the same. |
| `runtime` | The runtime to be used to execute the function. |
| `description` | A short description of the function. |
| `buildargs` | Pointer to the function file in the repo. In the sample the function is located in the `echo` directory. |
| `environment` | Sets an environment variable for the specific function only. |
160

161
After the `gitlab-ci.yml` template has been added and the `serverless.yml` file has been 
162 163
created, each function must be defined as a single file in your repository. Committing a 
function to your project will result in a
D
Daniel Gruesso 已提交
164 165 166 167 168 169
CI pipeline being executed which will deploy each function as a Knative service.
Once the deploy stage has finished, additional details for the function will
appear under **Operations > Serverless**.

![serverless page](img/serverless-page.png)

170 171
This page contains all functions available for the project, the description for
accessing the function, and, if available, the function's runtime information.
D
Daniel Gruesso 已提交
172 173 174 175 176 177 178 179 180
The details are derived from the Knative installation inside each of the project's
Kubernetes cluster.

The function details can be retrieved directly from Knative on the cluster:

```bash
kubectl -n "$KUBE_NAMESPACE" get services.serving.knative.dev
```

D
danielgruesso 已提交
181
The sample function can now be triggered from any HTTP client using a simple `POST` call.
182 183 184

![function exection](img/function-execution.png)

D
Daniel Gruesso 已提交
185 186
Currently, the Serverless page presents all functions available in all clusters registered for the project with Knative installed.

187 188 189 190 191 192
Clicking on the function name will provide additional details such as the
function's URL as well as runtime statistics such as the number of active pods
available to service the request based on load.

![serverless function details](img/serverless-details.png)

D
Daniel Gruesso 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
## Deploying Serverless applications

> Introduced in GitLab 11.5.

NOTE: **Note:**
You can reference the sample [Knative Ruby App](https://gitlab.com/knative-examples/knative-ruby-app) to get started.

Add the following `.gitlab-ci.yml` to the root of your repository
(you may skip this step if using the sample [Knative Ruby App](https://gitlab.com/knative-examples/knative-ruby-app) mentioned above):

```yaml
stages:
  - build
  - deploy

build:
  stage: build
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  only:
    - master
  script:
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE

deploy:
  stage: deploy
  image: gcr.io/triggermesh/tm@sha256:e3ee74db94d215bd297738d93577481f3e4db38013326c90d57f873df7ab41d5
  only:
    - master
  environment: production
  script:
    - echo "$CI_REGISTRY_IMAGE"
    - tm -n "$KUBE_NAMESPACE" --config "$KUBECONFIG" deploy service "$CI_PROJECT_NAME" --from-image "$CI_REGISTRY_IMAGE" --wait
```

D
danielgruesso 已提交
230 231
## Deploy the application with Knative

D
danielgruesso 已提交
232 233
With all the pieces in place, the next time a CI pipeline runs, the Knative application will be deployed. Navigate to
**CI/CD > Pipelines** and click the most recent pipeline.
234 235 236

## Obtain the URL for the Knative deployment

D
danielgruesso 已提交
237
Use the CI/CD deployment job output to obtain the deployment URL. Once all the stages of the pipeline finish, click the **deploy** stage.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

![deploy stage](img/deploy-stage.png)

The output will look like this:

```bash
Running with gitlab-runner 11.5.0~beta.844.g96d88322 (96d88322)
  on docker-auto-scale 72989761
Using Docker executor with image gcr.io/triggermesh/tm@sha256:e3ee74db94d215bd297738d93577481f3e4db38013326c90d57f873df7ab41d5 ...
Pulling docker image gcr.io/triggermesh/tm@sha256:e3ee74db94d215bd297738d93577481f3e4db38013326c90d57f873df7ab41d5 ...
Using docker image sha256:6b3f6590a9b30bd7aafb9573f047d930c70066e43955b4beb18a1eee175f6de1 for gcr.io/triggermesh/tm@sha256:e3ee74db94d215bd297738d93577481f3e4db38013326c90d57f873df7ab41d5 ...
Running on runner-72989761-project-4342902-concurrent-0 via runner-72989761-stg-srm-1541795796-27929c96...
Cloning repository...
Cloning into '/builds/danielgruesso/knative'...
Checking out 8671ad20 as master...
Skipping Git submodules setup
$ echo "$CI_REGISTRY_IMAGE"
registry.staging.gitlab.com/danielgruesso/knative
$ tm -n "$KUBE_NAMESPACE" --config "$KUBECONFIG" deploy service "$CI_PROJECT_NAME" --from-image "$CI_REGISTRY_IMAGE" --wait
Deployment started. Run "tm -n knative-4342902 describe service knative" to see the details
Waiting for ready state.......
Service domain: knative.knative-4342902.knative.info
Job succeeded
```

D
Daniel Gruesso 已提交
263
The second to last line, labeled **Service domain** contains the URL for the deployment. Copy and paste the domain into your
264 265
browser to see the app live.

D
Daniel Gruesso 已提交
266
![knative app](img/knative-app.png)