天天看點

Diagrams 系統架構圖繪制工具: 使用 Python 繪制漂亮的系統架構圖Diagrams

Diagrams 系統架構圖繪制工具: 使用 Python 繪制漂亮的系統架構圖Diagrams

Diagrams

Diagram as Code.

Diagrams lets you draw the cloud system architecture in Python code. It was born for prototyping a new system architecture design without any design tools. You can also describe or visualize the existing system architecture as well. Diagrams currently supports main major providers including:

AWS

,

Azure

,

GCP

,

Kubernetes

,

Alibaba Cloud

,

Oracle Cloud

etc... It also supports

On-Premise

nodes,

SaaS

and major

Programming

frameworks and languages.

Diagram as Code also allows you to track the architecture diagram changes in any version control system.

NOTE: It does not control any actual cloud resources nor does it generate cloud formation or terraform code. It is just for drawing the cloud system architecture diagrams.

Getting Started

It requires Python 3.6 or higher, check your Python version first.

It uses Graphviz to render the diagram, so you need to install Graphviz to use diagrams. After installing graphviz (or already have it), install the diagrams.

Graphviz下載下傳位址: https://graphviz.gitlab.io/download/

macOS users can download the Graphviz via

brew install graphviz

if you're using Homebrew.
$brew install graphviz           

複制

然後,配置目錄 /usr/local/Cellar/graphviz/5.0.0/bin 到PATH環境變量。

$/usr/local/Cellar/graphviz/5.0.0/bin
acyclic      circo        dijkstra     dot_builtins gc           gv2gml       gvgen        gvpack       gxl2gv       nop          prune        tred
bcomps       cluster      dot          edgepaint    gml2gv       gv2gxl       gvmap        gvpr         mm2gv        osage        sccmap       twopi
ccomps       diffimg      dot2gxl      fdp          graphml2gv   gvcolor      gvmap.sh     gxl2dot      neato        patchwork    sfdp         unflatten           

複制

# using pip (pip3)
$ pip3 install diagrams           

複制

Or, using pipenv/poetry install:

# using pipenv
$ pipenv install diagrams

# using poetry
$ poetry add diagrams           

複制

You can start with quick start. Check out guides for more details, and you can find all available nodes list in here.

Quick Start

Example 1

# diagram1.py
from diagrams import Diagram
from diagrams.aws.compute import EC2
from diagrams.aws.database import RDS
from diagrams.aws.network import ELB

with Diagram("diagram1", show=False):
    ELB("lb") >> EC2("web") >> RDS("userdb")           

複制

This code generates below diagram.

$ python diagram1.py           

複制

Diagrams 系統架構圖繪制工具: 使用 Python 繪制漂亮的系統架構圖Diagrams

It will be saved as

diagram1.png

on your working directory.

Example 2

from diagrams import Cluster, Diagram
from diagrams.aws.compute import ECS
from diagrams.aws.database import ElastiCache, RDS
from diagrams.aws.network import ELB
from diagrams.aws.network import Route53

with Diagram("Clustered Web Services", show=False):
    dns = Route53("dns")
    lb = ELB("lb")

    with Cluster("Services"):
        svc_group = [ECS("web1"),
                     ECS("web2"),
                     ECS("web3")]

    with Cluster("DB Cluster"):
        db_primary = RDS("userdb")
        db_primary - [RDS("userdb ro")]

    memcached = ElastiCache("memcached")

    dns >> lb >> svc_group
    svc_group >> db_primary
    svc_group >> memcached           

複制

Diagrams 系統架構圖繪制工具: 使用 Python 繪制漂亮的系統架構圖Diagrams

Example 3

from diagrams import Cluster, Diagram
from diagrams.gcp.analytics import BigQuery, Dataflow, PubSub
from diagrams.gcp.compute import AppEngine, Functions
from diagrams.gcp.database import BigTable
from diagrams.gcp.iot import IotCore
from diagrams.gcp.storage import GCS

with Diagram("Message Collecting", show=False):
    pubsub = PubSub("pubsub")

    with Cluster("Source of Data"):
        [IotCore("core1"),
         IotCore("core2"),
         IotCore("core3")] >> pubsub

    with Cluster("Targets"):
        with Cluster("Data Flow"):
            flow = Dataflow("data flow")

        with Cluster("Data Lake"):
            flow >> [BigQuery("bq"),
                     GCS("storage")]

        with Cluster("Event Driven"):
            with Cluster("Processing"):
                flow >> AppEngine("engine") >> BigTable("bigtable")

            with Cluster("Serverless"):
                flow >> Functions("func") >> AppEngine("appengine")

    pubsub >> flow           

複制

Diagrams 系統架構圖繪制工具: 使用 Python 繪制漂亮的系統架構圖Diagrams

Example 4

from diagrams import Cluster, Diagram
from diagrams.onprem.analytics import Spark
from diagrams.onprem.compute import Server
from diagrams.onprem.database import PostgreSQL
from diagrams.onprem.inmemory import Redis
from diagrams.onprem.aggregator import Fluentd
from diagrams.onprem.monitoring import Grafana, Prometheus
from diagrams.onprem.network import Nginx
from diagrams.onprem.queue import Kafka

with Diagram("Advanced Web Service with On-Premise", show=False):
    ingress = Nginx("ingress")

    metrics = Prometheus("metric")
    metrics << Grafana("monitoring")

    with Cluster("Service Cluster"):
        grpcsvc = [
            Server("grpc1"),
            Server("grpc2"),
            Server("grpc3")]

    with Cluster("Sessions HA"):
        primary = Redis("session")
        primary - Redis("replica") << metrics
        grpcsvc >> primary

    with Cluster("Database HA"):
        primary = PostgreSQL("users")
        primary - PostgreSQL("replica") << metrics
        grpcsvc >> primary

    aggregator = Fluentd("logging")
    aggregator >> Kafka("stream") >> Spark("analytics")

    ingress >> grpcsvc >> aggregator           

複制

Diagrams 系統架構圖繪制工具: 使用 Python 繪制漂亮的系統架構圖Diagrams

Contributing

To contribute to diagram, check out contribution guidelines.

Let me know if you are using diagrams! I'll add you in showcase page. (I'm working on it!) :)

Who uses it?

GitPitch is the perfect slide deck solution for Tech Conferences, Training, Developer Advocates, and Educators. Diagrams is now available as a dedicated Cloud Diagram Markdown Widget so you can use Diagrams directly on any slide for conferences, meetups, and training.

Cloudiscovery helps you to analyze resources in your cloud (AWS/GCP/Azure/Alibaba/IBM) account. It allows you to create a diagram of analyzed cloud resource map based on this Diagrams library, so you can draw your existing cloud infrastructure with Cloudiscovery.

Airflow Diagrams is an Airflow plugin that aims to easily visualise your Airflow DAGs on service level from providers like AWS, GCP, Azure, etc. via diagrams.

Other languages

  • If you are familiar to Go, you can use go-diagrams as well.

License

MIT