# 使用stream模块实现负载均衡

修改nginx配置文件，从`stream-enabled`目录中读取负载均衡的配置文件

```bash
sudo vi /etc/nginx/nginx.conf
```

{% code title="/etc/nginx/nginx.conf" %}

```
stream {
        include /etc/nginx/stream-enabled/*;
}
```

{% endcode %}

```bash
sudo mkdir /etc/nginx/stream-enabled/
sudo mkdir /etc/nginx/stream-available/
```

创建一个负载均衡配置文件：

```bash
sudo vi /etc/nginx/stream-available/kubernetes
```

```
upstream kubernetes {
	server 10.100.0.117:6443;
	server 10.100.0.118:6443;
}

server {
	listen 6443;
	proxy_pass kubernetes;
}
```

检查端口`6443`是否开始监听

```
$ netstat -nptl

roto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      891/sshd
tcp        0      0 0.0.0.0:6443            0.0.0.0:*               LISTEN      27342/nginx -g daem
```

检查数据是否转发过来：

```
$ curl -k https://localhost:6443
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "forbidden: User \"system:anonymous\" cannot get path \"/\"",
  "reason": "Forbidden",
  "details": {

  },
  "code": 403
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.tanmer.cn/nginx/shi-yong-stream-mo-kuai-shi-xian-fu-zai-jun-heng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
