第84节 使用Docker或Docker Compose安装Loki


❤️💕💕新时代拥抱云原生,云原生具有环境统一、按需付费、即开即用、稳定性强特点。Myblog:http://nsddd.topopen in new window


[TOC]

选择

如果ni正在评估、测试或开发Loki,则可以使用Docker或Docker Compose安装Loki和Promtail。对于生产环境,我们建议使用Tanka或Helm安装。

使用这些安装说明获得的配置将Loki作为单个二进制文件运行。

Prerequisites 先决条件

Install with Docker Compose

在命令行中运行以下命令。它们适用于Windows或Linux系统。

wget https://raw.githubusercontent.com/grafana/loki/v2.9.1/production/docker-compose.yaml -O docker-compose.yaml
docker-compose -f docker-compose.yaml up

地址是: https://raw.githubusercontent.com/grafana/loki/v2.9.1/production/docker-compose.yaml

Docker Compose 的内容是:

version: "3"

networks:
  loki:

services:
  loki:
    image: grafana/loki:2.9.0
    ports:
      - "3100:3100"
    command: -config.file=/etc/loki/local-config.yaml
    networks:
      - loki

  promtail:
    image: grafana/promtail:2.9.0
    volumes:
      - /var/log:/var/log
    command: -config.file=/etc/promtail/config.yml
    networks:
      - loki

  grafana:
    environment:
      - GF_PATHS_PROVISIONING=/etc/grafana/provisioning
      - GF_AUTH_ANONYMOUS_ENABLED=true
      - GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
    entrypoint:
      - sh
      - -euc
      - |
        mkdir -p /etc/grafana/provisioning/datasources
        cat <<EOF > /etc/grafana/provisioning/datasources/ds.yaml
        apiVersion: 1
        datasources:
        - name: Loki
          type: loki
          access: proxy
          orgId: 1
          url: http://loki:3100
          basicAuth: false
          isDefault: true
          version: 1
          editable: false
        EOF
        /run.sh
    image: grafana/grafana:latest
    ports:
      - "3000:3000"
    networks:
      - loki

node-exporter

node-exporter 是一个开源的工具,用于收集与 Linux 系统上的硬件和操作系统的各种指标有关的信息,并使其能够被 Prometheus 服务获取和存储。它是 Prometheus 监控工具链的一部分。

"exporter" 是一个专门的组件,其主要职责是收集非 Prometheus 系统中的指标并将其转化为 Prometheus 可读的格式。这意味着这些 "exporter" 组件充当了数据来源和 Prometheus 之间的中间层,确保 Prometheus 可以轻松抓取并监控这些数据。

使用 node-exporter 可以提供以下等方面的指标:

  1. CPU 使用情况:用户空间、系统、I/O 等待等。
  2. 内存使用情况:使用量、缓存、交换等。
  3. 磁盘空间:每个分区的使用量、总量、可用量等。
  4. I/O:读写操作、字节等。
  5. 网络:传入/传出数据包、错误等。
  6. 文件系统:i-nodes 使用情况、大小等。
  7. 硬件信息:如 CPU 温度、风扇速度等(取决于系统是否有这些传感器信息)。
  8. 其他系统信息,如运行的进程数量、系统负载等。

使用 node-exporter,运维团队和开发团队可以:

  1. 监控服务器和基础设施的健康情况。
  2. 设置警报,当某些指标超出预设范围时自动通知相关人员。
  3. 对系统性能进行分析和优化。
  4. 了解系统的资源使用情况和趋势,为未来的扩展和升级做准备。

注意,对于 Windows 来说,应该使用 wmi_exporter

# The root of the build/dist directory
IAM_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
[[ -z ${COMMON_SOURCED} ]] && source ${IAM_ROOT}/scripts/install/common.sh

# API Server API Address:Port
INSECURE_OPENIMAPI=${IAM_APISERVER_HOST}:${API_OPENIM_PORT}
INSECURE_OPENIMAUTO=${OPENIM_RPC_AUTH_HOST}:${OPENIM_AUTH_PORT}

CCURL="curl -f -s -XPOST" # Create
UCURL="curl -f -s -XPUT" # Update
RCURL="curl -f -s -XGET" # Retrieve
DCURL="curl -f -s -XDELETE" # Delete

openim::test::check_error() {
  local response=$1
  local err_code=$(echo "$response" | jq '.errCode')
  openim::log::status "Response from user registration: $response"
  if [[ "$err_code" != "0" ]]; then
    openim::log::error_exit "Error occurred: $response, You can read the error code in the API documentation https://docs.openim.io/restapi/errcode"
  else
    openim::log::success "Operation was successful."
  fi
}

# The `openim::test::auth` function serves as a test suite for authentication-related operations.
function openim::test::auth() {
  # 1. Retrieve and set the authentication token.
  openim::test::get_token
  
  # 2. Force logout the test user from a specific platform.
  openim::test::force_logout
  
  # Log the completion of the auth test suite.
  openim::log::success "Auth test suite completed successfully."
}

#################################### Auth Module ####################################

# Define a function to get a token (Admin Token)
openim::test::get_token() {
  token_response=$(${CCURL} "${OperationID}" "${Header}" http://${OPENIM_API_HOST}:${API_OPENIM_PORT}/auth/user_token \
      -d'{"secret": "'"$SECRET"'","platformID": 1,"userID": "openIM123456"}')
  token=$(echo $token_response | grep -Po 'token[" :]+\K[^"]+')
  echo "$token"
}

Header="-HContent-Type: application/json"
OperationID="-HoperationID: 1646445464564"
Token="-Htoken: $(openim::test::get_token)"

# Forces a user to log out from the specified platform by user ID.
openim::test::force_logout() {
  local request_body=$(cat <<EOF
{
  "platformID": 2,
  "userID": "4950983283"
}
EOF
  )
  echo "Requesting force logout for user: $request_body"

  local response=$(${CCURL} "${Token}" "${OperationID}" "${Header}" "http://${OPENIM_API_HOST}:${API_OPENIM_PORT}/auth/force_logout" -d "${request_body}")

  openim::test::check_error "$response"
}


#################################### User Module ####################################

# Registers a new user with provided user ID, nickname, and face URL using the API.
openim::test::user_register() {
  # Assign the parameters to local variables, with defaults if not provided
  local user_id="${1:-${TEST_USER_ID}}"
  local nickname="${2:-cubxxw}"
  local face_url="${3:-https://github.com/cubxxw}"

  # Create the request body using the provided or default values
  local request_body=$(cat <<EOF
{
  "secret": "${SECRET}",
  "users": [
    {
      "userID": "${user_id}",
      "nickname": "${nickname}",
      "faceURL": "${face_url}"
    }
  ]
}
EOF
)

  echo "Request body for user registration: $request_body"

  # Send the registration request
  local user_register_response=$(${CCURL} "${OperationID}" "${Header}" "http://${OPENIM_API_HOST}:${API_OPENIM_PORT}/user/user_register" -d "${request_body}")

  # Check for errors in the response
  openim::test::check_error "$user_register_response"
}

# Checks if the provided list of user IDs exist in the system.
openim::test::check_user_account() {
  local request_body=$(cat <<EOF
{
  "checkUserIDs": [
    "${1}",
    "${MANAGER_USERID_1}",
    "${MANAGER_USERID_2}",
    "${MANAGER_USERID_3}"
  ]
}
EOF
)
  echo "$request_body"

  local response=$(${CCURL} "${Token}" "${OperationID}" "${Header}" "http://${OPENIM_API_HOST}:${API_OPENIM_PORT}/user/account_check" -d "${request_body}")

  openim::test::check_error "$response"
}

# Retrieves a list of users with pagination, limited to a specific number per page.
openim::test::get_users() {
  local request_body=$(cat <<EOF
{
  "pagination": {
    "pageNumber": 1,
    "showNumber": 100
  }
}
EOF
)
  echo "$request_body"

  local response=$(${CCURL} "${Token}" "${OperationID}" "${Header}" "http://${OPENIM_API_HOST}:${API_OPENIM_PORT}/user/get_users" -d "${request_body}")

  openim::test::check_error "$response"
}

# Obtains detailed information for a list of user IDs.
openim::test::get_users_info() {
  local request_body=$(cat <<EOF
{
  "userIDs": [
    "${1}",
    "${MANAGER_USERID_1}"
  ]
}
EOF
)
  echo "$request_body"

  local response=$(${CCURL} "${Token}" "${OperationID}" "${Header}" "http://${OPENIM_API_HOST}:${API_OPENIM_PORT}/user/get_users_info" -d "${request_body}")

  openim::test::check_error "$response"
}

# Retrieves the online status for a list of user IDs.
openim::test::get_users_online_status() {
  local request_body=$(cat <<EOF
{
  "userIDs": [
    "${TEST_USER_ID}",
    "${MANAGER_USERID_1}",
    "${MANAGER_USERID_2}",
    "${MANAGER_USERID_3}"
  ]
}
EOF
)
  echo "$request_body"

  local response=$(${CCURL} "${Token}" "${OperationID}" "${Header}" "http://${OPENIM_API_HOST}:${API_OPENIM_PORT}/user/get_users_online_status" -d "${request_body}")

  openim::test::check_error "$response"
}

# Updates the information for a user, such as nickname and face URL.
openim::test::update_user_info() {
  local request_body=$(cat <<EOF
{
  "userInfo": {
    "userID": "${TEST_USER_ID}",
    "nickname": "openimbot",
    "faceURL": "https://github.com/openimbot"
  }
}
EOF
)
  echo "$request_body"

  local response=$(${CCURL} "${Token}" "${OperationID}" "${Header}" "http://${OPENIM_API_HOST}:${API_OPENIM_PORT}/user/update_user_info" -d "${request_body}")

  openim::test::check_error "$response"
}

# Gets the online status for users that a particular user has subscribed to.
openim::test::get_subscribe_users_status() {
  local request_body=$(cat <<EOF
{
  "userID": "${TEST_USER_ID}"
}
EOF
)
  echo "$request_body"

  local response=$(${CCURL} "${Token}" "${OperationID}" "${Header}" "http://${OPENIM_API_HOST}:${API_OPENIM_PORT}/user/get_subscribe_users_status" -d "${request_body}")

  openim::test::check_error "$response"
}

# Subscribes to the online status of a list of users for a particular user ID.
openim::test::subscribe_users_status() {
  local request_body=$(cat <<EOF
{
  "userID": "9168684795",
  "userIDs": [
    "7475779354",
    "6317136453",
    "8450535746"
  ],
  "genre": 1
}
EOF
)
  echo "$request_body"

  local response=$(${CCURL} "${Token}" "${OperationID}" "${Header}" "http://${OPENIM_API_HOST}:${API_OPENIM_PORT}/user/subscribe_users_status" -d "${request_body}")

  openim::test::check_error "$response"
}

# Sets the global message receiving option for a user, determining their messaging preferences.
openim::test::set_global_msg_recv_opt() {
  local request_body=$(cat <<EOF
{
  "userID": "${TEST_USER_ID}",
  "globalRecvMsgOpt": 0
}
EOF
)
  echo "$request_body"

  local response=$(${CCURL} "${Token}" "${OperationID}" "${Header}" "http://${OPENIM_API_HOST}:${API_OPENIM_PORT}/user/set_global_msg_recv_opt" -d "${request_body}")

  openim::test::check_error "$response"
}

# [openim::test::user function description]
# The `openim::test::user` function serves as a test suite for user-related operations. 
# It sequentially invokes all user-related test functions to ensure the API's user operations are functioning correctly.
function openim::test::user() {
  # 1. Register a test user.
  local USER_ID=$RANDOM
  local TEST_USER_ID=$RANDOM
  openim::test::user_register "${USER_ID}" "user01" "new_face_url"
  openim::test::user_register "${TEST_USER_ID}" "user01" "new_face_url"
  # 2. Check if the test user's account exists.
  openim::test::check_user_account "${TEST_USER_ID}"
  
  # 3. Retrieve a list of users.
  openim::test::get_users
  
  # 4. Get detailed information for the test user.
  openim::test::get_users_info "${TEST_USER_ID}"
  
  # 5. Check the online status of the test user.
  openim::test::get_users_online_status
  
  # 6. Update the test user's information.
  openim::test::update_user_info
  
  # 7. Get the status of users subscribed by the test user.
  openim::test::get_subscribe_users_status
  
  # 8. Subscribe the test user to a set of user statuses.
  openim::test::subscribe_users_status
  
  # 9. Set the message receiving option for the test user.
  openim::test::set_global_msg_recv_opt
  
  # Log the completion of the user test suite.
  openim::log::success "User test suite completed successfully."
}

END 链接