246 lines
8.7 KiB
YAML
246 lines
8.7 KiB
YAML
---
|
|
- name: Docker Swarm Migration Analysis for ash3c
|
|
hosts: ash3c
|
|
become: yes
|
|
gather_facts: yes
|
|
|
|
vars:
|
|
analysis_results: []
|
|
|
|
tasks:
|
|
# 基础检查
|
|
- name: Check if Docker is installed
|
|
command: which docker
|
|
register: docker_installed
|
|
failed_when: false
|
|
changed_when: false
|
|
|
|
- name: Fail if Docker not installed
|
|
fail:
|
|
msg: "Docker is not installed on {{ inventory_hostname }}"
|
|
when: docker_installed.rc != 0
|
|
|
|
# 检查当前 Swarm 状态
|
|
- name: Check Docker Swarm status
|
|
shell: docker info --format "{{ '{{' }}.Swarm.LocalNodeState{{ '}}' }}"
|
|
register: swarm_status
|
|
|
|
- name: Display current Swarm status
|
|
debug:
|
|
msg: "🔍 Current Swarm Status: {{ swarm_status.stdout }}"
|
|
|
|
# 获取所有容器的详细信息
|
|
- name: Get all containers (running and stopped)
|
|
shell: docker ps -a --format "{{ '{{' }}.Names{{ '}}' }}"
|
|
register: all_containers
|
|
|
|
- name: Get basic container information
|
|
shell: |
|
|
echo "=== Container: {{ item }} ==="
|
|
docker inspect {{ item }} | jq -r '
|
|
.[0] |
|
|
"Image: " + .Config.Image,
|
|
"Status: " + .State.Status,
|
|
"RestartPolicy: " + .HostConfig.RestartPolicy.Name,
|
|
"NetworkMode: " + .HostConfig.NetworkMode,
|
|
"Ports: " + (.NetworkSettings.Ports | keys | join(", ")),
|
|
"Volumes: " + ([.Mounts[]? | .Source + ":" + .Destination + ":" + .Mode] | join(" ")),
|
|
"Memory: " + (.HostConfig.Memory | tostring),
|
|
"CPUs: " + (.HostConfig.NanoCpus | tostring)
|
|
'
|
|
echo "---"
|
|
register: container_details
|
|
loop: "{{ all_containers.stdout_lines }}"
|
|
when: all_containers.stdout_lines | length > 0
|
|
|
|
- name: Display container details
|
|
debug:
|
|
msg: "{{ item.stdout }}"
|
|
loop: "{{ container_details.results }}"
|
|
when: container_details is defined
|
|
|
|
# 检查 Docker Compose 文件
|
|
- name: Find docker-compose files
|
|
find:
|
|
paths:
|
|
- /root
|
|
- /home
|
|
- /opt
|
|
patterns:
|
|
- "docker-compose.yml"
|
|
- "docker-compose.yaml"
|
|
- "compose.yml"
|
|
- "compose.yaml"
|
|
recurse: yes
|
|
register: compose_files
|
|
|
|
- name: Display found compose files
|
|
debug:
|
|
msg: "📄 Found compose files: {{ item.path }}"
|
|
loop: "{{ compose_files.files }}"
|
|
when: compose_files.files | length > 0
|
|
|
|
# 分析网络配置
|
|
- name: Get Docker networks
|
|
shell: docker network ls --format "{{ '{{' }}.Name{{ '}}' }}\t{{ '{{' }}.Driver{{ '}}' }}\t{{ '{{' }}.Scope{{ '}}' }}"
|
|
register: docker_networks
|
|
|
|
- name: Display Docker networks
|
|
debug:
|
|
msg: "🌐 Docker Networks: {{ docker_networks.stdout_lines }}"
|
|
|
|
# 检查卷使用情况
|
|
- name: Get Docker volumes
|
|
shell: docker volume ls --format "{{ '{{' }}.Name{{ '}}' }}\t{{ '{{' }}.Driver{{ '}}' }}"
|
|
register: docker_volumes
|
|
|
|
- name: Display Docker volumes
|
|
debug:
|
|
msg: "💾 Docker Volumes: {{ docker_volumes.stdout_lines }}"
|
|
|
|
# 检查容器资源使用
|
|
- name: Get container resource usage
|
|
shell: docker stats --no-stream --format "{{ '{{' }}.Name{{ '}}' }}\t{{ '{{' }}.CPUPerc{{ '}}' }}\t{{ '{{' }}.MemUsage{{ '}}' }}\t{{ '{{' }}.NetIO{{ '}}' }}\t{{ '{{' }}.BlockIO{{ '}}' }}"
|
|
register: container_stats
|
|
when: all_containers.stdout_lines | length > 0
|
|
|
|
- name: Display container stats
|
|
debug:
|
|
msg: "📊 Container Resource Usage: {{ container_stats.stdout_lines }}"
|
|
when: container_stats is defined
|
|
|
|
# 分析服务类型和 Swarm 适用性
|
|
- name: Analyze containers for Swarm suitability
|
|
set_fact:
|
|
swarm_analysis: |
|
|
🔍 SWARM MIGRATION ANALYSIS FOR {{ inventory_hostname }}
|
|
================================================
|
|
|
|
Current Swarm Status: {{ swarm_status.stdout }}
|
|
Total Containers: {{ all_containers.stdout_lines | length }}
|
|
|
|
📋 CONTAINER ANALYSIS:
|
|
{% for container in container_details.results %}
|
|
|
|
Container: {{ container.item }}
|
|
{% set details = container.stdout.split('\n') %}
|
|
{% for line in details %}
|
|
{{ line }}
|
|
{% endfor %}
|
|
|
|
SWARM SUITABILITY ASSESSMENT:
|
|
{% if 'restart=always' in container.stdout or 'restart=unless-stopped' in container.stdout %}
|
|
✅ Good restart policy for Swarm
|
|
{% else %}
|
|
⚠️ Consider adding restart policy
|
|
{% endif %}
|
|
|
|
{% if 'NetworkMode: bridge' in container.stdout or 'NetworkMode: host' in container.stdout %}
|
|
⚠️ May need network configuration for Swarm
|
|
{% else %}
|
|
✅ Custom network - good for Swarm
|
|
{% endif %}
|
|
|
|
{% if '/var/run/docker.sock' in container.stdout %}
|
|
❌ Uses Docker socket - NOT suitable for Swarm
|
|
{% elif 'bind' in container.stdout %}
|
|
⚠️ Uses bind mounts - consider using volumes
|
|
{% else %}
|
|
✅ Good volume configuration
|
|
{% endif %}
|
|
|
|
{% endfor %}
|
|
|
|
💡 RECOMMENDATIONS:
|
|
|
|
SUITABLE FOR SWARM:
|
|
{% for container in container_details.results %}
|
|
{% if '/var/run/docker.sock' not in container.stdout %}
|
|
- {{ container.item }}: Ready for Swarm migration
|
|
{% endif %}
|
|
{% endfor %}
|
|
|
|
NEEDS MODIFICATION:
|
|
{% for container in container_details.results %}
|
|
{% if '/var/run/docker.sock' in container.stdout %}
|
|
- {{ container.item }}: Uses Docker socket - keep as standalone
|
|
{% elif 'bind' in container.stdout %}
|
|
- {{ container.item }}: Convert bind mounts to volumes
|
|
{% endif %}
|
|
{% endfor %}
|
|
|
|
NEXT STEPS:
|
|
1. Initialize Swarm: docker swarm init
|
|
2. Create overlay networks for services
|
|
3. Convert suitable containers to services
|
|
4. Set up service discovery and load balancing
|
|
5. Configure secrets and configs management
|
|
when: container_details is defined
|
|
|
|
- name: Display Swarm analysis
|
|
debug:
|
|
msg: "{{ swarm_analysis }}"
|
|
when: swarm_analysis is defined
|
|
|
|
# 生成迁移脚本建议
|
|
- name: Generate migration script suggestions
|
|
set_fact:
|
|
migration_script: |
|
|
#!/bin/bash
|
|
# Docker Swarm Migration Script for {{ inventory_hostname }}
|
|
# Generated on {{ ansible_date_time.iso8601 }}
|
|
|
|
echo "🚀 Starting Docker Swarm migration..."
|
|
|
|
# Initialize Swarm (if not already done)
|
|
if [ "{{ swarm_status.stdout }}" != "active" ]; then
|
|
echo "Initializing Docker Swarm..."
|
|
docker swarm init
|
|
fi
|
|
|
|
# Create overlay networks
|
|
echo "Creating overlay networks..."
|
|
docker network create -d overlay --attachable app-network
|
|
|
|
# Example service creation (modify as needed)
|
|
{% for container in container_details.results if container_details is defined %}
|
|
{% if '/var/run/docker.sock' not in container.stdout %}
|
|
echo "Converting {{ container.item }} to Swarm service..."
|
|
# docker service create --name {{ container.item }}-svc \
|
|
# --network app-network \
|
|
# --replicas 1 \
|
|
# [ADD_YOUR_SPECIFIC_OPTIONS] \
|
|
# [IMAGE_NAME]
|
|
{% endif %}
|
|
{% endfor %}
|
|
|
|
echo "✅ Migration script template generated!"
|
|
echo "Please review and customize before running."
|
|
when: container_details is defined
|
|
|
|
- name: Display migration script
|
|
debug:
|
|
msg: "{{ migration_script }}"
|
|
when: migration_script is defined
|
|
|
|
# 保存分析结果到文件
|
|
- name: Save analysis results to file
|
|
copy:
|
|
content: |
|
|
{{ swarm_analysis }}
|
|
|
|
MIGRATION SCRIPT:
|
|
{{ migration_script }}
|
|
dest: "/tmp/swarm-analysis-{{ inventory_hostname }}-{{ ansible_date_time.epoch }}.txt"
|
|
when: swarm_analysis is defined and migration_script is defined
|
|
|
|
- name: Analysis complete
|
|
debug:
|
|
msg: |
|
|
🎉 Analysis complete!
|
|
Results saved to: /tmp/swarm-analysis-{{ inventory_hostname }}-{{ ansible_date_time.epoch }}.txt
|
|
|
|
Summary:
|
|
- Total containers analyzed: {{ all_containers.stdout_lines | length }}
|
|
- Compose files found: {{ compose_files.files | length }}
|
|
- Current Swarm status: {{ swarm_status.stdout }} |