## 2.1、单机部署

**风险提示**

⚠️ 脚本全自动安装，会将所部署机器上的 MySQL、JDK、ES 等进行删除重装，请注意原有服务丢失风险。

### 2.1.1、安装说明

- 以 `v3.0.0-beta.1` 版本为例进行部署；
- 以 CentOS-7 为例，系统基础配置要求 4C-8G；
- 部署完成后，可通过浏览器：`IP:PORT` 进行访问，默认端口是 `8080`，系统默认账号密码: `admin` / `admin2022_`。
- `v3.0.0-beta.2`版本开始，默认账号密码为`admin` / `admin`；
- 本文为单机部署，如需分布式部署，[请联系我们](https://knowstreaming.com/support-center)

**软件依赖**

| 软件名        | 版本要求     | 默认端口 |
| ------------- | ------------ | -------- |
| MySQL         | v5.7 或 v8.0 | 3306     |
| ElasticSearch | v7.6+        | 8060     |
| JDK           | v8+          | -        |
| CentOS        | v6+          | -        |
| Ubuntu        | v16+         | -        |

&nbsp;

### 2.1.2、脚本部署

**在线安装**

```bash
# 在服务器中下载安装脚本, 该脚本中会在当前目录下，重新安装MySQL。重装后的mysql密码存放在当前目录的mysql.password文件中。
wget https://s3-gzpu.didistatic.com/pub/knowstreaming/deploy_KnowStreaming-3.0.0-beta.1.sh

# 执行脚本
sh deploy_KnowStreaming.sh

# 访问地址
127.0.0.1:8080
```

**离线安装**

```bash
# 将安装包下载到本地且传输到目标服务器
wget https://s3-gzpu.didistatic.com/pub/knowstreaming/KnowStreaming-3.0.0-beta.1-offline.tar.gz

# 解压安装包
tar -zxf KnowStreaming-3.0.0-beta.1-offline.tar.gz

# 执行安装脚本
sh deploy_KnowStreaming-offline.sh

# 访问地址
127.0.0.1:8080
```

&nbsp;

### 2.1.3、容器部署

#### 2.1.3.1、Helm

**环境依赖**

- Kubernetes >= 1.14 ，Helm >= 2.17.0

- 默认依赖全部安装，ElasticSearch（3 节点集群模式） + MySQL(单机) + KnowStreaming-manager + KnowStreaming-ui

- 使用已有的 ElasticSearch(7.6.x) 和 MySQL(5.7) 只需调整 values.yaml 部分参数即可

**安装命令**

```bash
# 相关镜像在Docker Hub都可以下载
# 快速安装(NAMESPACE需要更改为已存在的，安装启动需要几分钟初始化请稍等~)
helm install -n [NAMESPACE] [NAME] http://download.knowstreaming.com/charts/knowstreaming-manager-0.1.5.tgz

# 获取KnowStreaming前端ui的service. 默认nodeport方式.
# (http://nodeIP:nodeport，默认用户名密码：admin/admin2022_)
# `v3.0.0-beta.2`版本开始（helm chart包版本0.1.4开始），默认账号密码为`admin` / `admin`；

# 添加仓库
helm repo add knowstreaming http://download.knowstreaming.com/charts

# 拉取最新版本
helm pull knowstreaming/knowstreaming-manager
```

&nbsp;

#### 2.1.3.2、Docker Compose
**环境依赖**

- [Docker](https://docs.docker.com/engine/install/) 
- [Docker Compose](https://docs.docker.com/compose/install/) 


**安装命令**
```bash
# `v3.0.0-beta.2`版本开始(docker镜像为0.2.0版本开始)，默认账号密码为`admin` / `admin`；
# https://hub.docker.com/u/knowstreaming 在此处寻找最新镜像版本
# mysql与es可以使用自己搭建的服务,调整对应配置即可

# 复制docker-compose.yml到指定位置后执行下方命令即可启动
docker-compose up -d
```

**验证安装**
```shell
docker-compose ps
# 验证启动 - 状态为 UP 则表示成功 
        Name                       Command                       State                  Ports
----------------------------------------------------------------------------------------------------
elasticsearch-single    /usr/local/bin/docker-entr ...   Up                      9200/tcp, 9300/tcp
knowstreaming-init      /bin/bash /es_template_cre ...   Up
knowstreaming-manager   /bin/sh /ks-start.sh             Up                      80/tcp
knowstreaming-mysql     /entrypoint.sh mysqld            Up (health: starting)   3306/tcp, 33060/tcp
knowstreaming-ui        /docker-entrypoint.sh ngin ...   Up                      0.0.0.0:80->80/tcp

# 稍等一分钟左右 knowstreaming-init 会退出，表示es初始化完成，可以访问页面
        Name                       Command                  State              Ports
-------------------------------------------------------------------------------------------
knowstreaming-init      /bin/bash /es_template_cre ...   Exit 0
knowstreaming-mysql     /entrypoint.sh mysqld            Up (healthy)   3306/tcp, 33060/tcp
```

**访问**
```http request
http://127.0.0.1:80/
```


**docker-compose.yml**
```yml
version: "2"
services:
  # *不要调整knowstreaming-manager服务名称，ui中会用到
  knowstreaming-manager:
    image: knowstreaming/knowstreaming-manager:latest
    container_name: knowstreaming-manager
    privileged: true
    restart: always
    depends_on:
      - elasticsearch-single
      - knowstreaming-mysql
    expose:
      - 80
    command:
      - /bin/sh
      - /ks-start.sh
    environment:
      TZ: Asia/Shanghai
      # mysql服务地址
      SERVER_MYSQL_ADDRESS: knowstreaming-mysql:3306
      # mysql数据库名
      SERVER_MYSQL_DB: know_streaming
      # mysql用户名
      SERVER_MYSQL_USER: root
      # mysql用户密码
      SERVER_MYSQL_PASSWORD: admin2022_
      # es服务地址
      SERVER_ES_ADDRESS: elasticsearch-single:9200
      # 服务JVM参数
      JAVA_OPTS: -Xmx1g -Xms1g
      # 对于kafka中ADVERTISED_LISTENERS填写的hostname可以通过该方式完成
#    extra_hosts:
#      - "hostname:x.x.x.x"
      # 服务日志路径
#    volumes:
#      - /ks/manage/log:/logs
  knowstreaming-ui:
    image: knowstreaming/knowstreaming-ui:latest
    container_name: knowstreaming-ui
    restart: always
    ports:
      - '80:80'
    environment:
      TZ: Asia/Shanghai
    depends_on:
      - knowstreaming-manager
#    extra_hosts:
#      - "hostname:x.x.x.x"
  elasticsearch-single:
    image: docker.io/library/elasticsearch:7.6.2
    container_name: elasticsearch-single
    restart: always
    expose:
      - 9200
      - 9300
#    ports:
#      - '9200:9200'
#      - '9300:9300'
    environment:
      TZ: Asia/Shanghai
      # es的JVM参数
      ES_JAVA_OPTS: -Xms512m -Xmx512m
      # 单节点配置，多节点集群参考 https://www.elastic.co/guide/en/elasticsearch/reference/7.6/docker.html#docker-compose-file
      discovery.type: single-node
      # 数据持久化路径
#    volumes:
#      - /ks/es/data:/usr/share/elasticsearch/data

  # es初始化服务，与manager使用同一镜像
  # 首次启动es需初始化模版和索引,后续会自动创建
  knowstreaming-init:
    image: knowstreaming/knowstreaming-manager:latest
    container_name: knowstreaming-init
    depends_on:
      - elasticsearch-single
    command:
      - /bin/bash
      - /es_template_create.sh
    environment:
      TZ: Asia/Shanghai
      # es服务地址
      SERVER_ES_ADDRESS: elasticsearch-single:9200

  knowstreaming-mysql:
    image: knowstreaming/knowstreaming-mysql:latest
    container_name: knowstreaming-mysql
    restart: always
    environment:
      TZ: Asia/Shanghai
      # root 用户密码
      MYSQL_ROOT_PASSWORD: admin2022_
      # 初始化时创建的数据库名称
      MYSQL_DATABASE: know_streaming
      # 通配所有host,可以访问远程
      MYSQL_ROOT_HOST: '%'
    expose:
      - 3306
#    ports:
#      - '3306:3306'
      # 数据持久化路径
#    volumes:
#      - /ks/mysql/data:/data/mysql
```

&nbsp;

### 2.1.4、手动部署

**部署流程**

1. 安装 `JDK-11`、`MySQL`、`ElasticSearch` 等依赖服务
2. 安装 KnowStreaming

&nbsp;

#### 2.1.4.1、安装 MySQL 服务

**yum 方式安装**

```bash
# 配置yum源
wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
rpm -ivh mysql57-community-release-el7-9.noarch.rpm

# 执行安装
yum -y install mysql-server mysql-client

# 服务启动
systemctl start mysqld

# 获取初始密码并修改
old_pass=`grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}' | tail -n 1`

mysql -NBe "alter user USER() identified by 'Didi_km_678';" --connect-expired-password -uroot -p$old_pass
```

**rpm 方式安装**

```bash
# 下载安装包
wget https://s3-gzpu.didistatic.com/knowsearch/mysql5.7.tar.gz

# 解压到指定目录
tar -zxf mysql5.7.tar.gz -C /tmp/

# 执行安装
yum -y localinstall /tmp/libaio-*.rpm /tmp/mysql-*.rpm

# 服务启动
systemctl start mysqld


# 获取初始密码并修改
old_pass=`grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}' | tail -n 1`

mysql -NBe "alter user USER() identified by 'Didi_km_678';" --connect-expired-password -uroot -p$old_pass

```

&nbsp;

#### 2.1.4.2、配置 JDK 环境

```bash
# 下载安装包
wget https://s3-gzpu.didistatic.com/pub/jdk11.tar.gz

# 解压到指定目录
tar -zxf jdk11.tar.gz -C /usr/local/

# 更改目录名
mv /usr/local/jdk-11.0.2 /usr/local/java11

# 添加到环境变量
echo "export JAVA_HOME=/usr/local/java11" >> ~/.bashrc
echo "export CLASSPATH=/usr/java/java11/lib" >> ~/.bashrc
echo "export PATH=$JAVA_HOME/bin:$PATH:$HOME/bin" >> ~/.bashrc

source ~/.bashrc

```

&nbsp;

#### 2.1.4.3、ElasticSearch 实例搭建

- ElasticSearch 用于存储平台采集的 Kafka 指标；
- 以下安装示例为单节点模式，如需集群部署可以参考：[Elasticsearch 官方文档](https://www.elastic.co/guide/en/elasticsearch/reference/7.6/elasticsearch-intro.html)

```bash
# 下载安装包
wget https://s3-gzpu.didistatic.com/pub/elasticsearch.tar.gz

# 创建ES数据存储目录
mkdir -p /data/es_data

# 创建ES所属用户
useradd arius

# 配置用户的打开文件数
echo "arius soft nofile 655350" >> /etc/security/limits.conf
echo "arius hard nofile 655350" >> /etc/security/limits.conf
echo "vm.max_map_count = 655360" >> /etc/sysctl.conf
sysctl -p

# 解压安装包
tar -zxf elasticsearch.tar.gz -C /data/

# 更改目录所属组
chown -R arius:arius /data/

# 修改配置文件(参考以下配置)
vim /data/elasticsearch/config/elasticsearch.yml
    cluster.name: km_es
    node.name: es-node1
    node.master: true
    node.data: true
    path.data: /data/es_data
    http.port: 8060
    discovery.seed_hosts: ["127.0.0.1:9300"]

# 修改内存配置
vim /data/elasticsearch/config/jvm.options
    -Xms2g
    -Xmx2g

# 启动服务
su - arius
export JAVA_HOME=/usr/local/java11
sh /data/elasticsearch/control.sh start

# 确认状态
sh /data/elasticsearch/control.sh status
```

&nbsp;

#### 2.1.4.4、KnowStreaming 实例搭建

```bash
# 下载安装包
wget https://s3-gzpu.didistatic.com/pub/knowstreaming/KnowStreaming-3.0.0-beta.1.tar.gz

# 解压安装包到指定目录
tar -zxf KnowStreaming-3.0.0-beta.1.tar.gz -C /data/

# 修改启动脚本并加入systemd管理
cd  /data/KnowStreaming/

# 创建相应的库和导入初始化数据
mysql -uroot -pDidi_km_678 -e "create database know_streaming;"
mysql -uroot -pDidi_km_678  know_streaming < ./init/sql/ddl-ks-km.sql
mysql -uroot -pDidi_km_678  know_streaming < ./init/sql/ddl-logi-job.sql
mysql -uroot -pDidi_km_678  know_streaming < ./init/sql/ddl-logi-security.sql
mysql -uroot -pDidi_km_678  know_streaming < ./init/sql/dml-ks-km.sql
mysql -uroot -pDidi_km_678  know_streaming < ./init/sql/dml-logi.sql

# 创建elasticsearch初始化数据
sh ./bin/init_es_template.sh

# 修改配置文件
vim ./conf/application.yml

# 监听端口
server:
    port: 8080 # web 服务端口
    tomcat:
        accept-count: 1000
        max-connections: 10000

# ES地址
es.client.address: 127.0.0.1:8060

# 数据库配置（一共三处地方，修改正确的mysql地址和数据库名称以及用户名密码）
jdbc-url: jdbc:mariadb://127.0.0.1:3306/know_streaming?.....
username: root
password: Didi_km_678

# 启动服务
cd /data/KnowStreaming/bin/
sh startup.sh
```
