【container】【docker-compose】【mysql】【redis】【rabbit mq】【mongo】【elasticsearch】

Nones發表於2024-05-08

@

目錄
  • 寫在前面
  • mysql
  • redis
  • rabbit mq
  • mongo
  • elasticsearch
    • 單節點
    • 多節點
  • 參考資料
    • docker
    • kubernetes
    • helm
    • k3s


寫在前面

  • 相關博文
  • 個人部落格首頁
  • 免責宣告:僅供學習交流使用!開源框架可能存在的風險和相關後果將完全由使用者自行承擔,本人不承擔任何法律責任。

mysql

version: '3'
services:
  mysql:
    image: mysql:8.0.18
    restart: always
    container_name: mysql
    command:
      --default_authentication_plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
      --max_allowed_packet=128M
      --sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
    ports:
      - "3306:3306"
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - $PWD/var/lib/mysql:/var/lib/mysql
      - $PWD/mysqlBackup:/data/mysqlBackup
    environment:
      MYSQL_ROOT_PASSWORD: <root-pass> # mysql root 賬戶的密碼
      MYSQL_DATABASE: <database-name>  # 新建 mysql 資料庫名稱
      MYSQL_USER: <user>         # 新建 mysql 使用者
      MYSQL_PASSWORD: <pass>            # 新建 mysql 密碼 

redis

version: '3'
services:
  redis:
    image: redis # 映象名稱以及版本號
    restart: always  # 失敗後總是重啟
    container_name: redis-6379 # 自定義容器名

    # 資料夾以及檔案對映
    volumes:
      - $PWD/data:/data
      - $PWD/logs:/logs
      # 自定義 redis configuration file
    #  - $PWD/redis.conf:/usr/local/etc/redis/redis.conf 
    #command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
    command: redis-server --requirepass <pass> # redis 密碼 
      # 埠號
    ports:
      - "6379:6379"

rabbit mq

version: '3'
services:
  rabbit-mq:
    hostname: rabbitmq
    # 映象名稱以及版本號
    image: rabbitmq:3-management
    # 失敗後總是重啟
    restart: always
    # 自定義容器名
    container_name: rabbitmq
    # 資料夾以及檔案對映
    volumes:
      - $PWD/etc:/etc/rabbitmq
    #  - $PWD/lib:/var/lib/rabbitmq
      - $PWD/log:/var/log/rabbitmq
    ports:
      # 埠號
      - "5672:5672"
      - "15672:15672"
    environment:
      RABBITMQ_DEFAULT_USER: <user>  # rabbit mq user
      RABBITMQ_DEFAULT_PASS: <pass> # rabbit mq password

mongo

version: '3.1'

services:
  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: <user> # mongo root 使用者名稱 
      MONGO_INITDB_ROOT_PASSWORD: <pass> # mongo root 密碼 
      MONGO_INITDB_DATABASE: <database> # mongo database
    ports:
      - "27017:27017"

elasticsearch

單節點

version: '3'
services:
  es:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.13.2
    container_name: es
    environment:
      - "discovery.type=single-node"
      #- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "ELASTIC_PASSWORD=elastic"
    #volumes:
    #  - ./plugins:/usr/share/elasticsearch/plugins #外掛檔案掛載
    #  - ./data:/usr/share/elasticsearch/data #資料檔案掛載
    #  - ./log:/usr/share/elasticsearch/logs
    ports:
      - 9200:9200

多節點

  • .ven
# Password for the 'elastic' user (at least 6 characters)
ELASTIC_PASSWORD=elastic

# Password for the 'kibana_system' user (at least 6 characters)
KIBANA_PASSWORD=elastic

# Version of Elastic products
STACK_VERSION=8.13.2

# Set the cluster name
CLUSTER_NAME=docker-cluster

# Set to 'basic' or 'trial' to automatically start the 30-day trial
LICENSE=basic
#LICENSE=trial

# Port to expose Elasticsearch HTTP API to the host
ES_PORT=9200
#ES_PORT=127.0.0.1:9200

# Port to expose Kibana to the host
KIBANA_PORT=5601
#KIBANA_PORT=80

# Project namespace (defaults to the current folder name if not set)
#COMPOSE_PROJECT_NAME=myproject

# Increase or decrease based on the available host memory (in bytes)
MEM_LIMIT=1073741824
  • mem limit

設定為至少vm.max_map_count262144編輯
核心設定必須設定為至少用於生產用途。vm.max_map_count262144

[Linux]

1. 若要在實時系統上應用該設定,請執行:
sysctl -w vm.max_map_count=262144

2. 要永久更改設定的值,請更新 中的值。
vm.max_map_count/etc/sysctl.conf
vm.max_map_count=262144

[macOS with Docker] 

1. 在命令列中,執行:
screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty

2. 按Enter鍵並使用配置:sysctlvm.max_map_count
sysctl -w vm.max_map_count=262144

[docker desktop]

1. sudo sysctl -w vm.max_map_count=262144

帶有 Docker 桌面 WSL 2 後端的 Windows編輯
該設定必須在“docker-desktop”WSL 例項中設定,然後 Elasticsearch 容器將正確啟動。有幾種方法可以做到這一點,這取決於 在你的 Windows 版本和 WSL 版本上。vm.max_map_count

1.1 如果你使用的是版本 22H2 之前的 Windows 10,或者如果你使用的是 Windows 10 版本 22H2,請使用 WSL 的內建版本,每次重新啟動 Docker 時都必須手動設定它,然後才能啟動 您的 Elasticsearch 容器,或者(如果您不希望在每次重新啟動時都這樣做)您必須全域性設定 每個 WSL2 例項都已更改。這是因為這些版本的 WSL 不要正確處理 /etc/sysctl.conf 檔案。vm.max_map_count

若要在每次重新啟動時手動設定它,必須在命令提示符下執行以下命令 或每次重啟 Docker 時的 PowerShell 視窗:

wsl -d docker-desktop -u root
sysctl -w vm.max_map_count=262144

1.2 如果你使用的是這些版本的 WSL,並且你不希望每次都執行這些命令 重新啟動 Docker 時,可以使用此設定全域性更改每個 WSL 分配 透過修改 %USERPROFILE%\.wslconfig,如下所示:

[wsl2]
1. kernelCommandLine = "sysctl.vm.max_map_count=262144"
這將導致所有 WSL2 VM 在啟動時分配該設定。

2. 如果你使用的是 Windows 11 或 Windows 10 版本 22H2 並已安裝 Microsoft Store WSL 版本,可以在“docker-desktop”WSL 中修改 /etc/sysctl.conf 分發,也許使用這樣的命令:

wsl -d docker-desktop -u root
vi /etc/sysctl.conf
並附上一行內容如下:

vm.max_map_count = 262144

  • docker-compose
version: "2.2"

services:
  setup:
    image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
    volumes:
      - ./certs:/usr/share/elasticsearch/config/certs
    user: "0"
    command: >
      bash -c '
        if [ x${ELASTIC_PASSWORD} == x ]; then
          echo "Set the ELASTIC_PASSWORD environment variable in the .env file";
          exit 1;
        elif [ x${KIBANA_PASSWORD} == x ]; then
          echo "Set the KIBANA_PASSWORD environment variable in the .env file";
          exit 1;
        fi;
        if [ ! -f config/certs/ca.zip ]; then
          echo "Creating CA";
          bin/elasticsearch-certutil ca --silent --pem -out config/certs/ca.zip;
          unzip config/certs/ca.zip -d config/certs;
        fi;
        if [ ! -f config/certs/certs.zip ]; then
          echo "Creating certs";
          echo -ne \
          "instances:\n"\
          "  - name: es01\n"\
          "    dns:\n"\
          "      - es01\n"\
          "      - localhost\n"\
          "    ip:\n"\
          "      - 127.0.0.1\n"\
          "  - name: es02\n"\
          "    dns:\n"\
          "      - es02\n"\
          "      - localhost\n"\
          "    ip:\n"\
          "      - 127.0.0.1\n"\
          "  - name: es03\n"\
          "    dns:\n"\
          "      - es03\n"\
          "      - localhost\n"\
          "    ip:\n"\
          "      - 127.0.0.1\n"\
          > config/certs/instances.yml;
          bin/elasticsearch-certutil cert --silent --pem -out config/certs/certs.zip --in config/certs/instances.yml --ca-cert config/certs/ca/ca.crt --ca-key config/certs/ca/ca.key;
          unzip config/certs/certs.zip -d config/certs;
        fi;
        echo "Setting file permissions"
        chown -R root:root config/certs;
        find . -type d -exec chmod 750 \{\} \;;
        find . -type f -exec chmod 640 \{\} \;;
        echo "Waiting for Elasticsearch availability";
        until curl -s --cacert config/certs/ca/ca.crt https://es01:9200 | grep -q "missing authentication credentials"; do sleep 30; done;
        echo "Setting kibana_system password";
        until curl -s -X POST --cacert config/certs/ca/ca.crt -u "elastic:${ELASTIC_PASSWORD}" -H "Content-Type: application/json" https://es01:9200/_security/user/kibana_system/_password -d "{\"password\":\"${KIBANA_PASSWORD}\"}" | grep -q "^{}"; do sleep 10; done;
        echo "All done!";
      '
    healthcheck:
      test: ["CMD-SHELL", "[ -f config/certs/es01/es01.crt ]"]
      interval: 1s
      timeout: 5s
      retries: 120

  es01:
    depends_on:
      setup:
        condition: service_healthy
    image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
    volumes:
      - ./certs:/usr/share/elasticsearch/config/certs
      - ./esdata01:/usr/share/elasticsearch/data
    ports:
      - ${ES_PORT}:9200
    environment:
      - node.name=es01
      - cluster.name=${CLUSTER_NAME}
      - cluster.initial_master_nodes=es01,es02,es03
      - discovery.seed_hosts=es02,es03
      - ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
      - bootstrap.memory_lock=true
      - xpack.security.enabled=true
      - xpack.security.http.ssl.enabled=true
      - xpack.security.http.ssl.key=certs/es01/es01.key
      - xpack.security.http.ssl.certificate=certs/es01/es01.crt
      - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.enabled=true
      - xpack.security.transport.ssl.key=certs/es01/es01.key
      - xpack.security.transport.ssl.certificate=certs/es01/es01.crt
      - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.verification_mode=certificate
      - xpack.license.self_generated.type=${LICENSE}
    mem_limit: ${MEM_LIMIT}
    ulimits:
      memlock:
        soft: -1
        hard: -1
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
        ]
      interval: 10s
      timeout: 10s
      retries: 120

  es02:
    depends_on:
      - es01
    image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
    volumes:
      - ./certs:/usr/share/elasticsearch/config/certs
      - ./esdata02:/usr/share/elasticsearch/data
    environment:
      - node.name=es02
      - cluster.name=${CLUSTER_NAME}
      - cluster.initial_master_nodes=es01,es02,es03
      - discovery.seed_hosts=es01,es03
      - bootstrap.memory_lock=true
      - xpack.security.enabled=true
      - xpack.security.http.ssl.enabled=true
      - xpack.security.http.ssl.key=certs/es02/es02.key
      - xpack.security.http.ssl.certificate=certs/es02/es02.crt
      - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.enabled=true
      - xpack.security.transport.ssl.key=certs/es02/es02.key
      - xpack.security.transport.ssl.certificate=certs/es02/es02.crt
      - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.verification_mode=certificate
      - xpack.license.self_generated.type=${LICENSE}
    mem_limit: ${MEM_LIMIT}
    ulimits:
      memlock:
        soft: -1
        hard: -1
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
        ]
      interval: 10s
      timeout: 10s
      retries: 120

  es03:
    depends_on:
      - es02
    image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
    volumes:
      - ./certs:/usr/share/elasticsearch/config/certs
      - ./esdata03:/usr/share/elasticsearch/data
    environment:
      - node.name=es03
      - cluster.name=${CLUSTER_NAME}
      - cluster.initial_master_nodes=es01,es02,es03
      - discovery.seed_hosts=es01,es02
      - bootstrap.memory_lock=true
      - xpack.security.enabled=true
      - xpack.security.http.ssl.enabled=true
      - xpack.security.http.ssl.key=certs/es03/es03.key
      - xpack.security.http.ssl.certificate=certs/es03/es03.crt
      - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.enabled=true
      - xpack.security.transport.ssl.key=certs/es03/es03.key
      - xpack.security.transport.ssl.certificate=certs/es03/es03.crt
      - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.verification_mode=certificate
      - xpack.license.self_generated.type=${LICENSE}
    mem_limit: ${MEM_LIMIT}
    ulimits:
      memlock:
        soft: -1
        hard: -1
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
        ]
      interval: 10s
      timeout: 10s
      retries: 120

  kibana:
    depends_on:
      es01:
        condition: service_healthy
      es02:
        condition: service_healthy
      es03:
        condition: service_healthy
    image: docker.elastic.co/kibana/kibana:${STACK_VERSION}
    volumes:
      - ./certs:/usr/share/kibana/config/certs
      - ./kibanadata:/usr/share/kibana/data
    ports:
      - ${KIBANA_PORT}:5601
    environment:
      - SERVERNAME=kibana
      - ELASTICSEARCH_HOSTS=https://es01:9200
      - ELASTICSEARCH_USERNAME=kibana_system
      - ELASTICSEARCH_PASSWORD=${KIBANA_PASSWORD}
      - ELASTICSEARCH_SSL_CERTIFICATEAUTHORITIES=config/certs/ca/ca.crt
    mem_limit: ${MEM_LIMIT}
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "curl -s -I http://localhost:5601 | grep -q 'HTTP/1.1 302 Found'",
        ]
      interval: 10s
      timeout: 10s
      retries: 120


參考資料

docker

  • docker docs
  • docker desktop 代替
  • colima 工具
  • docker 中文文件
  • docker 參考文件
  • runoob docker 文件

kubernetes

  • kubernetes 中文文件
  • kubernetes 中文文件2
  • kubernetes 參考文件
  • kubernetes 參考
  • kubernetes 參考

helm

  • helm 中文文件

k3s

  • K3s-輕量級K8s、Kuboard皮膚,安裝使用教程
  • kuboard
  • k3s

相關文章