apisix/docs/en/latest/getting-started.md

295 lines
10 KiB
Markdown
Raw Normal View History

---
title: Getting Started
---
2019-12-11 13:42:53 +08:00
<!--
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
-->
## Summary
2019-12-11 13:42:53 +08:00
This article is a quick start guide for Apache APISIX. The Quick Start is divided into the following three steps:
2019-12-11 13:42:53 +08:00
1. Install Apache APISIX via [Docker Compose](https://docs.docker.com/compose/).
1. Create a route and bind it with a Upstream.
1. Use `curl` command to verify that the results returned after binding are as expected.
In addition, this article provides some advanced operations on how to use Apache APISIX, including adding authentication, prefixing Route, using the APISIX Dashboard, and troubleshooting.
We will use the following `echo` endpoint as an example, which will return the parameters we passed.
2021-04-02 09:29:40 +08:00
**Request**
2019-12-11 13:42:53 +08:00
The request URL consists of these parameters:
- Protocol: the network transport protocol, `HTTP` protocol is used in our example.
- Port: The port, `80` is used in our example.
- Host: The host, `httpbin.org` is used in our example.
- Path: The path, `/get` is used in our example.
- Query Parameters: the query string, two strings `foo1` and `foo2` are listed in our example.
Run the following command to send the request:
2019-12-11 13:42:53 +08:00
```bash
curl --location --request GET "http://httpbin.org/get?foo1=bar1&foo2=bar2"
2019-12-11 13:42:53 +08:00
```
2021-04-02 09:29:40 +08:00
**Response**
2019-12-11 13:42:53 +08:00
2021-04-02 09:29:40 +08:00
```json
{
"args": {
"foo1": "bar1",
"foo2": "bar2"
},
"headers": {
2021-05-02 09:25:35 +08:00
"Accept": "*/*",
2021-04-02 09:29:40 +08:00
"Host": "httpbin.org",
2021-05-02 09:25:35 +08:00
"User-Agent": "curl/7.29.0",
"X-Amzn-Trace-Id": "Root=1-6088fe84-24f39487166cce1f0e41efc9"
2021-04-02 09:29:40 +08:00
},
"origin": "58.152.81.42",
"url": "http://httpbin.org/get?foo1=bar1&foo2=bar2"
}
```
## Pre-requisites
2021-04-02 09:29:40 +08:00
- Installed [Docker](https://www.docker.com/) and [Docker Compose component](https://docs.docker.com/compose/).
2019-12-11 13:42:53 +08:00
- We use the [curl](https://curl.se/docs/manpage.html) command for API testing. You can also use other tools such as [Postman](https://www.postman.com/) for testing.
2019-12-11 13:42:53 +08:00
:::note Note
If you already have Apache APISIX installed, please skip Step 1, and go to [Step 2](getting-started.md#Step-2-Create-a-Route) directly.
:::
2019-12-11 13:42:53 +08:00
2021-04-02 09:29:40 +08:00
## Step 1: Install Apache APISIX
Thanks to Docker, we can start Apache APISIX and enable it by enabling [Admin API](./admin-api.md).
2019-12-11 13:42:53 +08:00
```bash
# Download the Docker image of Apache APISIX
git clone https://github.com/apache/apisix-docker.git
# Switch the current directory to the apisix-docker/example path
cd apisix-docker/example
# Run the docker-compose command to install Apache APISIX
docker-compose -p docker-apisix up -d
2019-12-11 13:42:53 +08:00
```
It will take some time to download all required files, please be patient.
Once the download is complete, execute the `curl` command on the host running Docker to access the Admin API, and determine if Apache APISIX was successfully started based on the returned data.
2019-12-11 13:42:53 +08:00
```bash
# Note: Please execute the curl command on the host where you are running Docker.
curl "http://127.0.0.1:9080/apisix/admin/services/" -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1'
2019-12-11 13:42:53 +08:00
```
The following data is returned to indicate that Apache APISIX was successfully started:
2019-12-11 13:42:53 +08:00
```json
{
"count":1,
2021-05-02 09:25:35 +08:00
"action":"get",
"node":{
"key":"/apisix/services",
"nodes":{},
"dir":true
}
2019-12-11 13:42:53 +08:00
}
```
2021-04-02 09:29:40 +08:00
## Step 2: Create a Route
2019-12-11 13:42:53 +08:00
Now we have a running instance of Apache APISIX! Next, let's create a Route.
2019-12-11 13:42:53 +08:00
### How it works
2019-12-11 13:42:53 +08:00
Apache APISIX provides users with a powerful [Admin API](./admin-api.md) and [APISIX Dashboard](https://github.com/apache/apisix-dashboard). In this article, we use the Admin API to walk you through the procedures of creating a Route.
2019-12-11 13:42:53 +08:00
We can create a [Route](./architecture-design/route.md) and connect it to an Upstream service(also known as the [Upstream](./architecture-design/upstream.md)). When a `Request` arrives at Apache APISIX, Apache APISIX knows which Upstream the request should be forwarded to.
2019-12-11 13:42:53 +08:00
Because we have configured matching rules for the Route object, Apache APISIX can forward the request to the corresponding Upstream service. The following code is an example of a Route configuration:
2019-12-11 13:42:53 +08:00
2021-04-02 09:29:40 +08:00
```json
{
"methods": ["GET"],
"host": "example.com",
"uri": "/services/users/*",
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
2019-12-11 13:42:53 +08:00
}
2021-04-02 09:29:40 +08:00
}
}
```
2019-12-11 13:42:53 +08:00
This routing configuration means that all matching inbound requests will be forwarded to the Upstream service `httpbin.org:80` when they meet **all** the rules listed below:
2019-12-11 13:42:53 +08:00
- The HTTP method of the request is `GET`.
- The request header contains the `host` field, and its value is `example.com`.
- The request path matches `/services/users/*`, `*` means any subpath, for example `/services/users/getAll?limit=10`.
2019-12-11 13:42:53 +08:00
Once this route is created, we can access the Upstream service using the address exposed by Apache APISIX.
2019-12-11 13:42:53 +08:00
2021-04-02 09:29:40 +08:00
```bash
curl -i -X GET "http://{APISIX_BASE_URL}/services/users/getAll?limit=10" -H "Host: example.com"
2021-04-02 09:29:40 +08:00
```
2019-12-11 13:42:53 +08:00
This will be forwarded to `http://httpbin.org:80/services/users/getAll?limit=10` by Apache APISIX.
2019-12-11 13:42:53 +08:00
### Create an Upstream
After reading the previous section, we know that we must set up an `Upstream` for the `Route`. An Upstream can be created by simply executing the following command:
2019-12-11 13:42:53 +08:00
```bash
curl "http://127.0.0.1:9080/apisix/admin/upstreams/1" -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" -X PUT -d '
2019-12-11 13:42:53 +08:00
{
2021-04-02 09:29:40 +08:00
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
2019-12-11 13:42:53 +08:00
}'
```
We use `roundrobin` as the load balancing mechanism, and set `httpbin.org:80` as our upstream target (Upstream service) with an ID of `1`. For more information on the fields, see [Admin API](./admin-api.md).
2021-04-02 09:29:40 +08:00
:::note Note
Creating an Upstream service is not actually necessary, as we can use [Plugin](./architecture-design/plugin.md) to intercept the request and then respond directly. However, for the purposes of this guide, we assume that at least one Upstream service needs to be set up.
:::
2019-12-11 13:42:53 +08:00
### Bind the Route to the Upstream
2021-04-02 09:29:40 +08:00
We've just created an Upstream service (referencing our backend service), now let's bind a Route for it!
2019-12-11 13:42:53 +08:00
```bash
curl "http://127.0.0.1:9080/apisix/admin/routes/1" -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" -X PUT -d '
2019-12-11 13:42:53 +08:00
{
2021-04-02 09:29:40 +08:00
"uri": "/get",
"host": "httpbin.org",
2021-05-02 09:25:35 +08:00
"upstream_id": "1"
2019-12-11 13:42:53 +08:00
}'
```
## Step 3: Validation
2021-04-02 09:29:40 +08:00
We have created the route and the Upstream service and bound them. Now let's access Apache APISIX to test this route.
2019-12-11 13:42:53 +08:00
```bash
curl -i -X GET "http://127.0.0.1:9080/get?foo1=bar1&foo2=bar2" -H "Host: httpbin.org"
2019-12-11 13:42:53 +08:00
```
It returns data from our Upstream service (actually `httpbin.org`) and the result is as expected.
2019-12-11 13:42:53 +08:00
## Advanced Operations
2019-12-11 13:42:53 +08:00
This section provides some advanced operations such as adding authentication, prefixing Route, using the APISIX Dashboard, and troubleshooting.
2019-12-11 13:42:53 +08:00
### Add Authentication
2019-12-11 13:42:53 +08:00
The route we created in step 2 is public. Thus, **anyone** can access this Upstream service as long as they know the address that Apache APISIX exposes to the outside world. This is unsafe, it creates certain security risks. In a practical application scenario, we need to add authentication to the route.
Now we want only a specific user `John` to have access to this Upstream service, and we need to use [Consumer](./architecture-design/consumer.md) and [Plugin](./architecture-design/plugin.md) to implement authentication.
First, let's use [key-auth](./plugins/key-auth.md) plugin to create a [Consumer](./architecture-design/consumer.md) `John`, we need to provide a specified key.
2019-12-11 13:42:53 +08:00
2021-04-02 09:29:40 +08:00
```bash
curl "http://127.0.0.1:9080/apisix/admin/consumers" -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" -X PUT -d '
2019-12-11 13:42:53 +08:00
{
2021-04-02 09:29:40 +08:00
"username": "john",
"plugins": {
"key-auth": {
2021-05-02 09:25:35 +08:00
"key": "key-of-john"
2019-12-11 13:42:53 +08:00
}
2021-04-02 09:29:40 +08:00
}
2019-12-11 13:42:53 +08:00
}'
```
Next, let's bind `consumer (John)` to the route, we just need to **enable** the [key-auth](./plugins/key-auth.md) plugin.
2019-12-11 13:42:53 +08:00
```bash
curl "http://127.0.0.1:9080/apisix/admin/routes/1" -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" -X PUT -d '
2019-12-11 13:42:53 +08:00
{
2021-04-02 09:29:40 +08:00
"uri": "/get",
"host": "httpbin.org",
"plugins": {
"key-auth": {}
},
2021-05-02 09:25:35 +08:00
"upstream_id": "1"
2019-12-11 13:42:53 +08:00
}'
```
Now when we access the route created in step 2, an **Unauthorized Error** will be triggered.
The correct way to access that route is to add a `Header` named `apikey` with the correct key, as shown in the code below:
2019-12-11 13:42:53 +08:00
```bash
curl -i -X GET http://127.0.0.1:9080/get -H "Host: httpbin.org" -H 'apikey: superSecretAPIKey'
2019-12-11 13:42:53 +08:00
```
### Prefixing a Route
2021-04-02 09:29:40 +08:00
Now, suppose you want to add a prefix to a route (e.g. samplePrefix) and don't want to use the `host` header, then you can use the `proxy-rewrite` plugin to do so.
```bash
curl "http://127.0.0.1:9080/apisix/admin/routes/1" -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" -X PUT -d '
{
2021-04-02 09:29:40 +08:00
"uri": "/samplePrefix/get",
"plugins": {
"proxy-rewrite": {
"regex_uri": ["^/samplePrefix/get(.*)", "/get$1"]
},
2021-04-02 09:29:40 +08:00
"key-auth": {}
},
2021-05-02 09:25:35 +08:00
"upstream_id": "1"
}'
```
You can now use the following command to invoke the route:
```bash
curl -i -X GET "http://127.0.0.1:9080/samplePrefix/get?param1=foo&param2=bar" -H "apikey: key-of-john"
```
2021-04-02 09:29:40 +08:00
### APISIX Dashboard
Apache APISIX provides a [Dashboard](https://github.com/apache/apisix-dashboard) to make our operation more intuitive and easier.
2021-04-02 09:29:40 +08:00
![Dashboard](../../assets/images/dashboard.jpeg)
:::note Note
APISIX Dashboard is an experimental feature for now.
:::
2019-12-11 13:42:53 +08:00
### Troubleshooting
- Make sure that all required ports (**default 9080/9443/2379**) are not used by other systems or processes.
2019-12-11 13:42:53 +08:00
The following are commands to terminate a process that is listening on a specific port (on unix-based systems).
2019-12-11 13:42:53 +08:00
```bash
sudo fuser -k 9443/tcp
```
2021-04-02 09:29:40 +08:00
- If the Docker container keeps restarting or failing, log in to the container and observe the logs to diagnose the problem.
2019-12-11 13:42:53 +08:00
```bash
docker logs -f --tail container_id
```