Portainer實戰

    Portainer是一個輕量級的Docker環境管理UI,可以管理docker host和docker swarm(我主要看中了能管理swarm這個,畢竟市面上能管理swarm的平臺不多)。之所以說是輕量級的,是因爲部署只有一個container,也可以使用二進制程序直接部署,不像rancher的部署,部署了一大堆container,而且portainer是跨平臺的,windows和linux都可以部署,廢話不多說,直接開幹....

環境

ubuntu16.04-1 swarm manager portainer
ubuntu16.04-2 swarm works portainer agent
ubuntu16.04-3 swarm works portainer agent

    ps:環境還是之前文章的swarm集羣環境

部署

    獨立容器啓動

docker run -d -p 9000:9000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v /path/on/host/data:/data portainer/portainer

        ps:portainer的數據存儲在容器內部的/data目錄,這樣容器重啓的時候數據會丟失,所以要確保數據持久化

docker run -d -p 9000:9000 --name portainer --restart always -v portainer_data:/data portainer/portainer -H tcp://<REMOTE_HOST>:<REMOTE_PORT>

        ps:-H 後面的remote是你想用portainert管理的docker

    stack方式啓動

        下載啓動stack文件

curl -L https://portainer.io/download/portainer-agent-stack.yml -o portainer-agent-stack.yml

        以stack方式啓動

docker stack deploy --compose-file=portainer-agent-stack.yml portainer

    swarm service啓動

docker service create \
    --name portainer \
    --publish 9000:9000 \
    --replicas=1 \
    --constraint 'node.role == manager' \
    --mount type=bind,src=//path/on/host/data,dst=/data \
    portainer/portainer

    二進制啓動

wget https://github.com/portainer/portainer/releases/download/1.19.2/portainer-1.19.2-linux-amd64.tar.gz
tar xvpfz portainer-1.19.2-linux-amd64.tar.gz
cd /opt/portainer
./portainer --template-file "${PWD}/templates.json"

        ps:模板文件的內容下面會詳細解釋

[
 {
    "type": 1,
    "title": "Registry",
    "description": "Docker image registry",
    "categories": ["docker"],
    "platform": "linux",
    "logo": "https://portainer.io/images/logos/registry.png",
    "image": "registry:latest",
    "ports": [
      "5000/tcp"
    ],
    "volumes": [{ "container": "/var/lib/registry"}]
  },
]

驗證

    訪問

http://172.31.68.241:9000/

    登錄

    查看

    容器信息

配置

    之前說過portainer可以以二進制程序直接部署,也可以在啓動容器的時候直接在CLI設置參數,來修改portainer的默認行爲,可使用的參數主要有如下:

      --help                     幫助
      --version                  查看版本信息
  -p, --bind=":9000"             指定portainer監聽的地址和端口
  -a, --assets="./"              Path to the assets
  -d, --data="/data"             指定data數據目錄
  -H, --host=HOST                Endpoint URL
      --external-endpoints=EXTERNAL-ENDPOINTS  
                                 定義外部的endpoints
      --no-auth                  禁止portainer的認證
      --no-analytics             Disable Analytics in app
      --tlsverify                TLS support
      --tlsskipverify            Disable TLS server verification
      --tlscacert="/certs/ca.pem"  
                                 Path to the CA
      --tlscert="/certs/cert.pem"  
                                 Path to the TLS certificate file
      --tlskey="/certs/key.pem"  Path to the TLS key
      --ssl                      Secure Portainer instance using SSL
      --sslcert="/certs/portainer.crt"  
                                 Path to the SSL certificate used to secure the Portainer instance
      --sslkey="/certs/portainer.key"  
                                 Path to the SSL key used to secure the Portainer instance
      --sync-interval="60s"      Duration between each synchronization via the external endpoints source
      --snapshot                 Start a background job to create endpoint snapshots
      --snapshot-interval="5m"   Duration between each endpoint snapshot job
      --admin-password=ADMIN-PASSWORD  
                                 指定admin的password,是加密過後的,可以由htpasswd產生
      --admin-password-file=ADMIN-PASSWORD-FILE  
                                 如果怕泄密,可以把密碼寫在一個文件中,這個裏面是原始密碼
  -l, --hide-label=HIDE-LABEL ...  
                                 隱藏具有某些標籤的容器,-l可以多次使用
      --logo=LOGO                使用你自己的logo
  -t, --templates=TEMPLATES      或者直接指定一個url
      --template-file="/templates.json"  
                                 使用你自己的模板文件,默認的莫辦文件位置在/templates.json

    ps:關於admin密碼,有的同學覺得cli和文件都不保險,則可以把密碼設置在docker的secret中,如下:

$ echo -n mypassword | docker secret create portainer-pass -
$ docker service create \
  --name portainer \
  --secret portainer-pass \
  --publish 9000:9000 \
  --replicas=1 \
  --constraint 'node.role == manager' \
  --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
  portainer/portainer \
  --admin-password-file '/run/secrets/portainer-pass' \
  -H unix:///var/run/docker.sock

模板

    portainer中可以自定義一些模板,然後在portainer中創建容器的時候使用這些模塊快速的創建,模板是json格式的,包含一些必選項和可選項。

    Container template definition format

        樣例

{
  "type": 1,
  "title": "Nginx",
  "description": "High performance web server",
  "logo": "https://cloudinovasi.id/assets/img/logos/nginx.png",
  "image": "nginx:latest",
  "ports": [
    "8080:80/tcp",
    "443/tcp"
  ]
}

        解釋

type	必選項,1表示container,2表示swarm stack,3表示compose stack
title   必選項
description		必選項
image	必選項,該應用使用的docker鏡像
administrator_only	可選項,是否只有管理員可用模板
name	可選項,該模板在管理界面中顯示的名稱
log		可選項,自定義的logo
registry	可選項,docker鏡像的倉庫,默認是Dockerhub 
command		可選項,容器裏面運行的命令,如果沒有指定,則默認運行dockerfile的command
network		可選項,模板使用的網絡
volumes		可選項,模板使用的volume
ports    可選項,模板exposed的端口
labels    可選項,模板上定義的labels
privileged    可選項,容器是否能在超級權限啓動
interactive    可選項,容器啓動時是否是交互模式,即添加-i -t
restart_policy    可選項,容器的重啓策略
hostname    可選項,容器的主機名
note    可選項,關於模板的信息
platform    可選項,支持的平臺,一般是linux或者windows
categories    可選項,模板的類別,可以在UI上通過category來過濾


    Stack template definition format

        樣例

{
  "type": 2,
  "title": "CockroachDB",
  "description": "CockroachDB cluster",
  "note": "Deploys an insecure CockroachDB cluster, please refer to <a href=\"https://www.cockroachlabs.com/docs/stable/orchestrate-cockroachdb-with-docker-swarm.html\" target=\"_blank\">CockroachDB documentation</a> for production deployments.",
  "categories": ["database"],
  "platform": "linux",
  "logo": "https://cloudinovasi.id/assets/img/logos/cockroachdb.png",
  "repository": {
    "url": "https://github.com/portainer/templates",
    "stackfile": "stacks/cockroachdb/docker-stack.yml"
  }
}

        解釋

type	必選項,1表示container,2表示swarm stack,3表示compose stack
title   必選項
description		必選項
registry	可選項,docker鏡像的倉庫,默認是Dockerhub 
administrator_only	可選項,是否只有管理員可用模板
name	可選項,該模板在管理界面中顯示的名稱
logo		可選項,自定義的logo
env	     可選項,定義模板的變量
note    可選項,關於模板的信息
platform    可選項,支持的平臺,一般是linux或者windows
categories    可選項,模板的類別,可以在UI上通過category來過濾

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章