Aquesta és una revisió antiga del document —-
portainer
gestor de contenedores de Docker
server
- docker-compose.yml
services: portainer: #image: portainer/portainer-ce:sts image: portainer/portainer-ee:2.40.0 restart: always network_mode: host volumes: - /var/run/docker.sock:/var/run/docker.sock - ./data:/data container_name: portainer ports: - 9443:9443 - 8000:8000 # Remove if you do not intend to use Edge Agents extra_hosts: # crear entrada a /etc/hosts - "agent.s4.local:host-gateway" - "agent.arsys.local:host-gateway"
agent (in remote server)
- fijamos la IP interna:
- docker-compose.yml
name: portainer services: agent: image: portainer/agent:sts restart: always networks: portainer_agent_net: ipv4_address: 172.20.0.5 volumes: - /var/run/docker.sock:/var/run/docker.sock - /var/lib/docker/volumnes:/var/lib/docker/volumes - /:/host container_name: portainer-agent networks: portainer_agent_net: ipam: config: - subnet: 172.20.0.0/16
- abrimos un tunel desde el servidor:
# -f hace que se quede en background ssh -N -f -L 0.0.0.0:9002:172.20.0.5:9001 s4.fidmag.org # podemos localizarlo después con: ps aux | grep ssh # o eliminarlo directamente: fuser -k 9002/tcp
- 9002: puerto tunel a servidor
- 172.20.0.5: ip contenedor agente (puede variar si queremos + de 1)
- configuramos el entorno en portainer server
- nombre_host_local:puerto
autossh
- también se puede hacer a través de:
# Instalación (en Ubuntu/Debian) sudo apt install autossh # Crear el túnel persistente autossh -M 0 -f -N -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -L 9002:172.20.0.5:9001 usuario@IP-SERVIDOR-REMOTO
- para hacerlo persistente:
- /etc/systemd/system/autossh-portainer.service
[Unit] Description=Tuneles AutoSSH persistentes para Portainer Agents After=network-online.target Wants=network-online.target [Service] Type=simple # IMPORTANTE: Pon tu usuario local de Linux (el que tiene las claves SSH configuradas) User=TU_USUARIO_LOCAL Environment="AUTOSSH_GATETIME=0" # Un solo comando levanta ambos puertos (9002 y 9003) ExecStart=/usr/bin/autossh -M 0 -N -q -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -L 0.0.0.0:9002:172.20.0.5:9001 -L 0.0.0.0:9003:172.20.0.6:9001 usuario_remoto@IP_DE_S4 Restart=always RestartSec=10 [Install] WantedBy=multi-user.target
# 1. Recargar systemd para que detecte el nuevo archivo sudo systemctl daemon-reload # 2. HABILITAR el servicio (esto hace que arranque tras un REINICIO) sudo systemctl enable autossh-portainer.service # 3. Arrancar el servicio ahora mismo (por primera vez) sudo systemctl start autossh-portainer.service
- necesario acceso por ssh-key sin contraseña
otros comandos
- tunel activo?:
netstat -tunlp | grep 9002 # O también: ss -tunlp | grep 9002
- ip contenedor:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' portainer-agent
agente
services: portainer-agent: image: portainer/agent:latest container_name: portainer-agent restart: always volumes: - /var/run/docker.sock:/var/run/docker.sock # - /var/lib/docker/volumes:/var/lib/docker/volumes ports: - "9001:9001" # IMPORTANTE: En entornos muy restrictivos, a veces requiere modo privilegiado privileged: true
saltar "http VS https" en agente remoto tunelizado + 2 portainer servers
portainer degrada a http las conexiones locales.
- crear 127.0.0.1 agent.remote.local en
/etc/hosts - modificar portainer server para añadir:
extra_hosts: - "agent.s4.local:host-gateway"
- esto evita que se use el DNS de Docker que no encontraba ese servidor
- montar el tunel:
ssh -N -L 0.0.0.0:9002:172.20.0.6:9001 server.com
- hemos montado un segundo agente (portainer solo adminte 1 conexión con un agente, y una vez emparejado no soporta más):
name: portainer services: agent-fidmag: image: portainer/agent:sts restart: always networks: portainer_agent_net: ipv4_address: 172.20.0.5 volumes: - /var/run/docker.sock:/var/run/docker.sock - /var/lib/docker/volumnes:/var/lib/docker/volumes - /:/host container_name: portainer-agent-fidmag agent-mate: image: portainer/agent:sts restart: always networks: portainer_agent_net: ipv4_address: 172.20.0.6 volumes: - /var/run/docker.sock:/var/run/docker.sock - /var/lib/docker/volumnes:/var/lib/docker/volumes - /:/host container_name: portainer-agent-mate networks: portainer_agent_net: ipam: config: - subnet: 172.20.0.0/16
reset contraseña
#!/bin/bash docker pull portainer/helper-reset-password docker run --rm -v ./data:/data portainer/helper-reset-password