簡體   English   中英

使用 docker-compose 在本地掛載 AWS EFS

[英]Locally mount AWS EFS using docker-compose

我想知道是否有一種簡單的方法可以使用 Amazon EFS(彈性文件系統)在本地 docker-compose 設置中作為卷安裝。

原因是對於本地開發,創建的卷會保留在我的筆記本電腦上 - 如果我要更換機器,我將無法訪問任何底層數據。 雲 NFS 可以解決這個問題,因為它可以從任何地方隨時可用。

AWS 文檔( https://docs.aws.amazon.com/efs/latest/ug/efs-onpremises.html )似乎建議使用 AWS Direct Connect / VPN - 有沒有辦法通過打開端口來避免這種情況2049(NFS 流量)在偵聽所有 IP 地址的安全組中,並將該安全組應用於新創建的 EFS?

這是我的 docker-compose.yml:

version: "3.2"
 
services:
  postgres_db:
    container_name: "postgres"
    image: "postgres:13"
    ports:
      - 5432:5432
    volumes:
      - type: volume
        source: postgres_data
        target: /var/lib/postgresql/data
        volume:
          nocopy: true
    environment: 
      POSTGRES_USER: 'admin'
      POSTGRES_PASSWORD: "password"
 
volumes: 
  postgres_data:
    driver_opts:
      type: "nfs"
      o: "addr=xxx.xx.xx.xx,nolock,soft,rw"
      device: ":/docker/example"

我收到以下錯誤:

ERROR: for postgres_db  Cannot start service postgres_db: error while mounting volume '/var/lib/docker/volumes/flowstate_example/_data': failed to mount local volume: mount :/docker/example:/var/lib/docker/volumes/flowstate_example/_data, data: addr=xxx.xx.xx.xx,nolock,soft: connection refused

我認為我的筆記本電腦連接不是 AWS EFS VPC 的一部分,因此無法掛載 EFS。

對於添加的上下文,我希望將網絡抓取設置 dockerize 並將數據量保存在雲中,以便我可以從任何地方連接到它。

EFS 假設 nfs4,所以:

version: '3.8'

services:

  postgres:
    volumes:
      postgres_data:/var/lib/postgresql/data

volumes: 

  postgres_data:
    driver_opts:
      type: "nfs4"
      o: "addr=xxx.xx.xx.xx,nolock,soft,rw"
      device: ":/docker/postgres_data"

當然,引用的 nfs-export/path 必須存在。 Swarm 不會自動創建不存在的文件夾。

在重新創建堆棧之前,請確保手動刪除任何這種錯誤類型/名稱的舊 docker 卷(在所有 swarm 節點上!):

docker volume rm $(docker volume ls -f name=postgres_data -q)

理解這一點很重要:Docker NFS 卷實際上只是聲明在哪里可以找到數據。 當您更新 docker-compose.yml 時它不會更新,因此您必須刪除該卷,以便出現任何新配置

見輸出

docker service ps stack_postgres --no-trunc

有關為什么無法安裝卷的更多信息

還要確保您可以通過mount -t nfs4 ...掛載 nfs-export

請參閱showmount -e your.efs.ip.address

volumes:
  nginx_test_vol:
    driver_opts:
      type: "nfs"
      o: "addr=fs-xxxxxxxxxxxxxxxxxx.efs.us-east-1.amazonaws.com,rw,nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport"
      device: ":/nginx-test"

這讓我很好地使用它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM