今天,來說一個在service中非常高級的知識點,configs.
然後,通過一些示例,來一步一步的演示,如何在service中使用,有什麼關鍵的注意事項。
什麼是configs?
configs的準確說法應該是,docker swar service configs.
通過configs這個名字可以看出,一般是存配置數據的。是的,沒錯,在configs中可以存儲非敏感的信息,比如配置文件。
這個配置文件可以是獨立於鏡像和容器之外的。
也就是說,在容器運行的時候,將配置掛載到容器中。這樣,同樣的鏡像,就可以在不同的環境中運行,僅僅是更新下不同的配置文件就可以了。
同時呢,configs以配置文件掛載容器中,容器不需要以bind掛載的方式,將宿主機中的配置掛載到容器了。
簡單來說,configs的好處:
- configs以文件掛載到容器,容器無需bind掛載
- 鏡像通用即可,不受環境限制
- configs是獨立鏡像之外的獨立資源對象
注意:docker configs只適用於集羣service,不適用於獨立的容器。
示例
創建configs
創建配置文件 index.html.tmpl
<html lang="en"> <head><title>Hello Docker</title></head> <body> <p>Hello Docker! You have deployed a HTML page.</p> </body> </html>
創建configs
docker config create homepage index.html.tmpl
查看創建configs
[root@nccztsjb-node-01 ~]# docker config ls ID NAME CREATED UPDATED pj31j03jkm31l6azeen39ibgv homepage 3 seconds ago 3 seconds ago [root@nccztsjb-node-01 ~]#
# 查看詳細的config信息 # 發現數據都是加密的
[root@nccztsjb-node-01 ~]# docker config inspect homepage [ { "ID": "pj31j03jkm31l6azeen39ibgv", "Version": { "Index": 3333 }, "CreatedAt": "2022-09-28T02:49:35.559023328Z", "UpdatedAt": "2022-09-28T02:49:35.559023328Z", "Spec": { "Name": "homepage", "Labels": {}, "Data": "PGh0bWwgbGFuZz0iZW4iPgogIDxoZWFkPjx0aXRsZT5IZWxsbyBEb2NrZXI8L3RpdGxlPjwvaGVhZD4KICA8Ym9keT4KICAgIDxwPkhlbGxvIERvY2tlciEgWW91IGhhdmUgZGVwbG95ZWQgYSBIVE1MIHBhZ2UuPC9wPgogIDwvYm9keT4KPC9odG1sPgo=" } } ] [root@nccztsjb-node-01 ~]#
使用config
有了config之後,接下來就是要將這個config掛載到容器的文件系統中
創建service使用這個config,使用--config參數
source : configs的名字
target : 掛載到容器中的位置及名字,注意:名字可以是自定義的
docker service create \ --with-registry-auth \ --name=nginx \ --replicas=4 \ --config source=homepage,target=/usr/share/nginx/html/index.html \ --publish published=8080,target=80 \ 172.20.58.152/middleware/nginx:1.21.4
查看service狀態
[root@nccztsjb-node-01 ~]# docker service ps nginx ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS tkntzl1iqppt nginx.1 172.20.58.152/middleware/nginx:1.21.4 nccztsjb-node-05 Running Running 14 seconds ago t40w6uia7u18 nginx.2 172.20.58.152/middleware/nginx:1.21.4 nccztsjb-node-04 Running Running 14 seconds ago psk6hhwuktvy nginx.3 172.20.58.152/middleware/nginx:1.21.4 nccztsjb-node-02 Running Running 14 seconds ago hlvggbg4ym24 nginx.4 172.20.58.152/middleware/nginx:1.21.4 nccztsjb-node-01 Running Running 14 seconds ago [root@nccztsjb-node-01 ~]#
查看容器中的配置文件
文件以及經掛載到/usr/share/nginx/html/index.html (把鏡像中的文件給覆蓋掉了)
[root@nccztsjb-node-01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 48deef4bbd6f 172.20.58.152/middleware/nginx:1.21.4 "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp nginx.4.hlvggbg4ym241lewhtk8q2h40 [root@nccztsjb-node-01 ~]# docker exec -it 48deef4bbd6f bash root@48deef4bbd6f:/# df -h Filesystem Size Used Avail Use% Mounted on overlay 49G 20G 27G 43% / tmpfs 64M 0 64M 0% /dev tmpfs 7.6G 0 7.6G 0% /sys/fs/cgroup shm 64M 0 64M 0% /dev/shm /dev/vda2 49G 20G 27G 43% /etc/hosts tmpfs 7.6G 4.0K 7.6G 1% /usr/share/nginx/html/index.html tmpfs 7.6G 0 7.6G 0% /proc/acpi tmpfs 7.6G 0 7.6G 0% /proc/scsi tmpfs 7.6G 0 7.6G 0% /sys/firmware root@48deef4bbd6f:/# cat /usr/share/nginx/html/index.html <html lang="en"> <head><title>Hello Docker</title></head> <body> <p>Hello Docker! You have deployed a HTML page.</p> </body> </html> root@48deef4bbd6f:/#
瀏覽器訪問
管理config
刪除config
通過下面的命令刪除config
[root@nccztsjb-node-01 ~]# docker config rm homepage Error response from daemon: rpc error: code = InvalidArgument desc = config 'homepage' is in use by the following service: nginx [root@nccztsjb-node-01 ~]#
如果這個config正在被service使用是不能進行刪除的
將config從service刪除後,進行config的刪除
[root@nccztsjb-node-01 ~]# docker config rm homepage homepage [root@nccztsjb-node-01 ~]#
將config從service移除
[root@nccztsjb-node-01 ~]# docker service update --config-rm homepage nginx nginx overall progress: 4 out of 4 tasks 1/4: running [==================================================>] 2/4: running [==================================================>] 3/4: running [==================================================>] 4/4: running [==================================================>] verify: Service converged [root@nccztsjb-node-01 ~]# docker service ps nginx ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS 98yv6zykhi0n nginx.1 172.20.58.152/middleware/nginx:1.21.4 nccztsjb-node-05 Running Running 26 seconds ago tkntzl1iqppt \_ nginx.1 172.20.58.152/middleware/nginx:1.21.4 nccztsjb-node-05 Shutdown Shutdown 27 seconds ago shk5gyh2fgu0 nginx.2 172.20.58.152/middleware/nginx:1.21.4 nccztsjb-node-04 Running Running 11 seconds ago t40w6uia7u18 \_ nginx.2 172.20.58.152/middleware/nginx:1.21.4 nccztsjb-node-04 Shutdown Shutdown 13 seconds ago 5r5uxqzc7zzy nginx.3 172.20.58.152/middleware/nginx:1.21.4 nccztsjb-node-02 Running Running 16 seconds ago psk6hhwuktvy \_ nginx.3 172.20.58.152/middleware/nginx:1.21.4 nccztsjb-node-02 Shutdown Shutdown 17 seconds ago ttzbp0q8z9ty nginx.4 172.20.58.152/middleware/nginx:1.21.4 nccztsjb-node-01 Running Running 21 seconds ago hlvggbg4ym24 \_ nginx.4 172.20.58.152/middleware/nginx:1.21.4 nccztsjb-node-01 Shutdown Shutdown 23 seconds ago [root@nccztsjb-node-01 ~]#
移除就相當於是從新發布了service
現有service中增加config
docker service update --config-add source=homepage,target=/usr/share/nginx/html/index.html nginx
[root@nccztsjb-node-01 ~]# docker service update --config-add source=homepage,target=/usr/share/nginx/html/index.html nginx nginx overall progress: 4 out of 4 tasks 1/4: running [==================================================>] 2/4: running [==================================================>] 3/4: running [==================================================>] 4/4: running [==================================================>] verify: Service converged [root@nccztsjb-node-01 ~]#
多個service共享一個config
再創建一個service用這個config
docker service create \ --with-registry-auth \ --name=nginx-new \ --replicas=4 \ --config source=homepage,target=/usr/share/nginx/html/index.html \ --publish published=8082,target=80 \ 172.20.58.152/middleware/nginx:1.21.4
[root@nccztsjb-node-01 ~]# docker service inspect nginx-new --pretty ID: 7ilcep8nbxuujbfyaft9gbp4e Name: nginx-new Service Mode: Replicated Replicas: 4 Placement: UpdateConfig: Parallelism: 1 On failure: pause Monitoring Period: 5s Max failure ratio: 0 Update order: stop-first RollbackConfig: Parallelism: 1 On failure: pause Monitoring Period: 5s Max failure ratio: 0 Rollback order: stop-first ContainerSpec: Image: 172.20.58.152/middleware/nginx:1.21.4@sha256:2f14a471f2c2819a3faf88b72f56a0372ff5af4cb42ec45aab00c03ca5c9989f Init: false Configs: Target: /usr/share/nginx/html/index.html Source: homepage Resources: Endpoint Mode: vip Ports: PublishedPort = 8082 Protocol = tcp TargetPort = 80 PublishMode = ingress [root@nccztsjb-node-01 ~]#
那麼,這個service也使用了這個config了。
--config未加target參數默認的掛載位置
如果指定target,默認的位置/config_name
使用模板config
在config中可以使用go 模板,放置一些佔位符,然後在創建service,啓動容器的時候會自動的獲取對應的變量值
使用下面的文件來創建config
<html lang="en"> <head><title>Hello Docker</title></head> <body> <p>Hello {{ env "HELLO" }}! I'm on node {{ env "NODENAME" }}.</p> </body> </html>
其中下面2個,以變量獲取的方式,獲取對應的值
{{ env "HELLO" }} {{ env "NODENAME" }}
創建config,使用模板驅動--template-driver golang
docker config create --template-driver golang homepage index.html.tmpl
變量在哪裏呢?
創建service的時候,通過--env來設置
--env HELLO="Docker" --env NODENAME={{.Node.Hostname}}
docker service create \ --with-registry-auth \ --name=nginx \ --replicas=4 \ --env HELLO="Docker" \ --env NODENAME={{.Node.Hostname}} \ --config source=homepage,target=/usr/share/nginx/html/index.html \ --publish published=8080,target=80 \ 172.20.58.152/middleware/nginx:1.21.4
[root@nccztsjb-node-01 ~]# docker service create \ > --with-registry-auth \ > --name=nginx \ > --replicas=4 \ > --env HELLO="Docker" \ > --env NODENAME={{.Node.Hostname}} \ > --config source=homepage,target=/usr/share/nginx/html/index.html \ > --publish published=8080,target=80 \ > 172.20.58.152/middleware/nginx:1.21.4 sywtvyc8ipg6zxxfegzlfmy1i overall progress: 4 out of 4 tasks 1/4: running [==================================================>] 2/4: running [==================================================>] 3/4: running [==================================================>] 4/4: running [==================================================>] verify: Service converged [root@nccztsjb-node-01 ~]#
查看容器中的掛載
文件已經在掛載到容器的時候,將變量給替換了
[root@nccztsjb-node-01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1c3358559e56 172.20.58.152/middleware/nginx:1.21.4 "/docker-entrypoint.…" 19 seconds ago Up 18 seconds 80/tcp nginx.3.txrhegcjr0dskys9h19eydgax [root@nccztsjb-node-01 ~]# docker exec -it 1c3358559e56 bash root@1c3358559e56:/# df -h Filesystem Size Used Avail Use% Mounted on overlay 49G 20G 27G 43% / tmpfs 64M 0 64M 0% /dev tmpfs 7.6G 0 7.6G 0% /sys/fs/cgroup shm 64M 0 64M 0% /dev/shm /dev/vda2 49G 20G 27G 43% /etc/hosts tmpfs 7.6G 4.0K 7.6G 1% /usr/share/nginx/html/index.html tmpfs 7.6G 0 7.6G 0% /proc/acpi tmpfs 7.6G 0 7.6G 0% /proc/scsi tmpfs 7.6G 0 7.6G 0% /sys/firmware root@1c3358559e56:/# cat /usr/share/nginx/html/index.html <html lang="en"> <head><title>Hello Docker</title></head> <body> <p>Hello Docker! I'm on node nccztsjb-node-01.</p> </body> </html> root@1c3358559e56:/#
瀏覽器訪問nginx服務
清瀏覽器緩存之後,再訪問
多個實例,是可以通過路由網格,也就是ingress網絡,訪問到不同的節點上的task的。
注意:是同一個入口!
這樣,就實現了將一些信息,動態的加載容器裏面!
OK,關於configs就介紹到這裏~