docker compose 访问宿主机服务
Docker Compose是一个工具,可以用于定义和运行多个Docker容器的应用程序。如果您想在Docker Compose中访问宿主机服务,需要使用特殊的网络模式 - host模式。通过这种模式,Docker Compose可以使用宿主机网络堆栈,并直接访问宿主机服务。在docker-compose.yml文件中添加以下行:
network_mode: "host"
然后,您可以使用宿主机IP地址来访问宿主机服务。例如,如果您正在运行一个Web服务器,可以使用以下URL访问:
http://<host_ip>:<port>
注意,在host模式下,容器将共享主机的网络命名空间,因此它将看到主机上的所有网络接口和端口。这可能会产生一些安全风险,因此请谨慎使用host模式。如果可能,最好使用bridge网络模式来隔离容器和宿主机的网络。
docker-compose 使用宿主机网络
Docker Compose 是一个工具,用于定义并管理 Docker 应用程序和服务的配置。当涉及到宿主机网络(Host Network)时,Docker Compose 中可以设置 networks
部分来连接服务到宿主机网络,这样容器之间的通信就会直接通过主机而不是 Docker 网络。
例如,在 docker-compose.yml
文件中,你可以这样配置:
version: '3'
services:
service_name:
networks:
- host_network
networks:
host_network:
driver: host
在这个例子中,service_name
容器会加入名为 host_network
的网络,并使用 driver: host
指定它将连接到主机的网络堆栈。这样做有以下优势:
- 更高的性能:容器可以直接访问宿主机的 IP 地址,无需额外的网络代理,对于端口映射和数据共享更方便。
- 调试简单:宿主机网络下的容器更容易进行调试,因为它们暴露了真实的网络环境。
然而,注意以下几点:
- 宿主机网络不是默认选项,如果不清楚需求,通常会让服务运行在 Docker 自带的网络模式。
- 这种方式可能导致安全风险,因为它允许容器直接访问宿主机资源,所以应谨慎使用。
docker-compose 快速部署Prometheus,监控docker 容器, 宿主机,ceph -- cluster集群
好的,下面是一个快速部署 Prometheus 监控 docker 容器、宿主机和 Ceph 集群的 docker-compose.yaml 文件:
version: '3'
services:
prometheus:
image: prom/prometheus
ports:
- 9090:9090
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
command:
- '--config.file=/etc/prometheus/prometheus.yml'
depends_on:
- node-exporter
- cadvisor
- ceph-exporter
node-exporter:
image: prom/node-exporter
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
command:
- '--path.procfs=/host/proc'
- '--path.sysfs=/host/sys'
- '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc)($$|/)'
cadvisor:
image: google/cadvisor
ports:
- 8080:8080
volumes:
- /:/rootfs:ro
- /var/run:/var/run:rw
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
ceph-exporter:
image: galexrt/ceph-exporter:latest
environment:
CLUSTER_NAME: mycluster
CEPH_USERNAME: admin
CEPH_KEYRING_BASE64: <base64-encoded ceph.client.admin.keyring>
ports:
- 9128:9128
其中,Prometheus 监控的配置文件 prometheus.yml 可以参考下面的示例:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'docker'
metrics_path: '/metrics'
static_configs:
- targets: ['node-exporter:9100']
labels:
group: 'docker'
- job_name: 'node'
metrics_path: '/metrics'
static_configs:
- targets: ['node-exporter:9100']
labels:
group: 'node'
- job_name: 'cadvisor'
metrics_path: '/metrics'
static_configs:
- targets: ['cadvisor:8080']
labels:
group: 'docker'
- job_name: 'ceph'
metrics_path: '/metrics'
static_configs:
- targets: ['ceph-exporter:9128']
labels:
group: 'ceph'
在这个配置文件中,我们定义了四个 job:
- prometheus:监控 Prometheus 本身的指标。
- docker:监控 Docker 容器运行时的指标。
- node:监控宿主机的指标。
- ceph:监控 Ceph 集群的指标。
对于 docker 和 node,它们都使用了 node-exporter 来收集指标。而对于 ceph,我们使用了 ceph-exporter 来收集指标。
在部署前,需要将 ceph.client.admin.keyring 文件的内容进行 base64 编码,并将编码后的内容填入 docker-compose.yaml 文件中的 CEHP_KEYRING_BASE64 环境变量中。
部署完成后,可以通过访问 http://localhost:9090 来访问 Prometheus 的 Web 界面。在这里,你可以查看所有收集到的指标,并且可以创建自己的监控规则和仪表盘。
相关推荐















