在所有目标机器上安装Docker

openclaw 中文openclaw 2

很高兴为您提供AI小龙虾OpenClaw的批量安装方案,作为一款用于自动生成Shell命令的AI工具,其批量部署确实能极大提升团队效率,以下是几种主流的批量安装方法:

在所有目标机器上安装Docker-第1张图片-OpenClaw下载中文-AI中文智能体

推荐方案:Docker容器化部署(最简便)

准备Docker环境

systemctl start docker
systemctl enable docker

使用Docker Compose批量部署

创建 docker-compose.yml

version: '3.8'
services:
  openclaw:
    image: openclaw/openclaw:latest  # 使用官方镜像
    container_name: openclaw-${HOSTNAME}
    restart: always
    environment:
      - API_KEY=${API_KEY}
      - MODEL=claude-3-opus  # 根据需要修改
    volumes:
      - ./config:/app/config
    ports:
      - "7860:7860"  # Web UI端口

批量启动脚本

#!/bin/bash
# deploy_openclaw.sh
# 定义服务器列表
SERVERS=("server1" "server2" "server3")
API_KEY="your_api_key_here"
for server in "${SERVERS[@]}"; do
  echo "正在部署到 $server"
  # 传输文件
  scp docker-compose.yml $server:/opt/openclaw/
  # 远程执行
  ssh $server << EOF
    cd /opt/openclaw
    echo "API_KEY=$API_KEY" > .env
    docker-compose up -d
    echo "OpenClaw已在 $server 启动"
EOF
done

使用Ansible进行自动化部署

Ansible Playbook配置

创建 openclaw_playbook.yml

---
- name: 批量部署OpenClaw
  hosts: all
  vars:
    openclaw_version: "latest"
    api_key: "{{ lookup('env', 'OPENCLAW_API_KEY') }}"
  tasks:
    - name: 安装系统依赖
      apt:
        name:
          - python3-pip
          - python3-venv
          - git
        state: present
    - name: 创建OpenClaw用户
      user:
        name: openclaw
        system: yes
        shell: /bin/bash
    - name: 克隆OpenClaw仓库
      git:
        repo: "https://github.com/opendilab/OpenClaw.git"
        dest: /opt/openclaw
        version: "{{ openclaw_version }}"
    - name: 创建Python虚拟环境
      pip:
        virtualenv: /opt/openclaw/venv
        virtualenv_python: python3.8
    - name: 安装Python依赖
      pip:
        requirements: /opt/openclaw/requirements.txt
        virtualenv: /opt/openclaw/venv
    - name: 创建配置文件
      template:
        src: templates/config.yaml.j2
        dest: /opt/openclaw/config/config.yaml
        owner: openclaw
        group: openclaw
    - name: 创建systemd服务
      template:
        src: templates/openclaw.service.j2
        dest: /etc/systemd/system/openclaw.service
    - name: 启动OpenClaw服务
      systemd:
        name: openclaw
        state: started
        enabled: yes
        daemon_reload: yes

配置文件模板

templates/config.yaml.j2

model:
  name: "{{ model_name }}"
  api_key: "{{ api_key }}"
server:
  host: "0.0.0.0"
  port: 7860
storage:
  cache_dir: "/var/lib/openclaw/cache"

使用Shell脚本批量安装

主安装脚本

#!/bin/bash
# install_openclaw_batch.sh
set -e
# 配置变量
OPENCLAW_DIR="/opt/openclaw"
PYTHON_VERSION="3.8"
REQUIREMENTS_URL="https://raw.githubusercontent.com/opendilab/OpenClaw/main/requirements.txt"
# 安装函数
install_openclaw() {
    echo "正在在 $(hostname) 上安装OpenClaw..."
    # 1. 更新系统
    apt-get update
    # 2. 安装依赖
    apt-get install -y \
        python${PYTHON_VERSION} \
        python${PYTHON_VERSION}-venv \
        python3-pip \
        git \
        curl
    # 3. 创建目录
    mkdir -p $OPENCLAW_DIR
    cd $OPENCLAW_DIR
    # 4. 克隆代码
    git clone https://github.com/opendilab/OpenClaw.git .
    # 5. 创建虚拟环境
    python${PYTHON_VERSION} -m venv venv
    source venv/bin/activate
    # 6. 安装依赖
    pip install --upgrade pip
    pip install -r requirements.txt
    # 7. 创建配置文件
    cat > config.yaml << EOF
api_key: ${1:-$DEFAULT_API_KEY}
model: "claude-3-sonnet"
port: 7860
EOF
    # 8. 创建启动脚本
    cat > start_openclaw.sh << 'EOF'
#!/bin/bash
cd /opt/openclaw
source venv/bin/activate
python -m openclaw.server --config config.yaml
EOF
    chmod +x start_openclaw.sh
    echo "安装完成!"
}
# 批量执行函数
batch_install() {
    local servers=("$@")
    local api_key=$API_KEY
    for server in "${servers[@]}"; do
        echo "========================================"
        echo "在 $server 上安装OpenClaw"
        echo "========================================"
        # 复制脚本到远程服务器并执行
        scp "$0" "$server:/tmp/install_openclaw.sh"
        ssh "$server" "bash /tmp/install_openclaw.sh --install --api-key='$api_key'"
        # 验证安装
        ssh "$server" "cd /opt/openclaw && source venv/bin/activate && python -c 'import openclaw; print(\"OpenClaw导入成功\")'"
    done
}
# 使用示例
# export API_KEY="your_anthropic_api_key"
# servers=("user@server1" "user@server2" "user@server3")
# batch_install "${servers[@]}"

使用Kubernetes部署(大规模集群)

Kubernetes Deployment配置

# openclaw-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: openclaw
spec:
  replicas: 3  # 实例数量
  selector:
    matchLabels:
      app: openclaw
  template:
    metadata:
      labels:
        app: openclaw
    spec:
      containers:
      - name: openclaw
        image: openclaw/openclaw:latest
        env:
        - name: API_KEY
          valueFrom:
            secretKeyRef:
              name: openclaw-secret
              key: api-key
        ports:
        - containerPort: 7860
        resources:
          requests:
            memory: "512Mi"
            cpu: "250m"
          limits:
            memory: "1Gi"
            cpu: "500m"
---
apiVersion: v1
kind: Service
metadata:
  name: openclaw-service
spec:
  selector:
    app: openclaw
  ports:
    - protocol: TCP
      port: 80
      targetPort: 7860
  type: LoadBalancer

配置管理工具整合

使用Puppet

# modules/openclaw/manifests/init.pp
class openclaw (
  String $api_key,
  String $version = 'latest',
) {
  package { ['python3', 'python3-pip', 'git']:
    ensure => installed,
  }
  vcsrepo { '/opt/openclaw':
    ensure   => present,
    provider => git,
    source   => 'https://github.com/opendilab/OpenClaw.git',
    revision => $version,
  }
  file { '/opt/openclaw/config.yaml':
    ensure  => file,
    content => template('openclaw/config.yaml.erb'),
    require => Vcsrepo['/opt/openclaw'],
  }
  exec { 'install-openclaw-deps':
    command => '/usr/bin/pip3 install -r /opt/openclaw/requirements.txt',
    unless  => '/usr/bin/pip3 list | grep openclaw',
    require => Package['python3-pip'],
  }
}

验证批量安装结果

批量验证脚本

#!/bin/bash
# verify_installation.sh
SERVERS=("server1" "server2" "server3")
PORT=7860
for server in "${SERVERS[@]}"; do
    echo "验证 $server 上的OpenClaw..."
    # 检查服务状态
    if ssh "$server" "systemctl is-active openclaw" | grep -q "active"; then
        echo "✓ 服务正在运行"
    else
        echo "✗ 服务未运行"
    fi
    # 测试API端点
    response=$(curl -s -o /dev/null -w "%{http_code}" "http://$server:$PORT/health")
    if [ "$response" = "200" ]; then
        echo "✓ API端点正常"
    else
        echo "✗ API端点异常: HTTP $response"
    fi
done

最佳实践建议

  1. 环境一致性:使用相同的Python版本和依赖版本
  2. 密钥管理:通过环境变量或密钥管理服务传递API密钥
  3. 监控配置:批量部署时应包含监控和日志收集
  4. 回滚策略:保留旧版本,确保可快速回滚
  5. 网络考虑:内网部署可配置镜像源加速依赖下载

常见问题解决

# 如果遇到依赖问题
pip install --upgrade setuptools wheel
# 如果遇到权限问题
sudo chown -R $(whoami) /opt/openclaw
# 批量更新所有实例
for server in ${SERVERS[@]}; do
    ssh $server "cd /opt/openclaw && git pull && pip install -r requirements.txt"
done

选择哪种方案取决于您的具体需求:

  • 少量服务器:推荐Docker方案
  • 大规模部署:推荐Kubernetes或Ansible
  • 混合环境:推荐使用Shell脚本结合配置管理

需要更详细的某部分配置,请告知具体需求!

标签: Docker 安装

抱歉,评论功能暂时关闭!