docker網(wǎng)絡(luò)通訊 & prometheus監(jiān)控

一、docker 基礎(chǔ)網(wǎng)絡(luò)模式

默認(rèn)daoker網(wǎng)絡(luò)模式是 bridge

網(wǎng)絡(luò)模式 含義
bridge 橋接網(wǎng)絡(luò)(默認(rèn))
host 與宿主機(jī)共用網(wǎng)絡(luò)
container 容器和另外一個(gè)容器共用網(wǎng)絡(luò)
none 沒有外部網(wǎng)絡(luò),只有本地的的回環(huán)地址
[root@k8s-node-2 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
79dcb24fd5e3        bridge              bridge              local
dda0d0ab86d2        host                host                local
46d21f5a5200        none                null                local

# 創(chuàng)建啟動(dòng)容器時(shí)指定網(wǎng)絡(luò)
docker run -d --network host alpine:latest
docker run -d -p 80:80 --network container:ID alpine:latest

二、自定義網(wǎng)絡(luò)

[root@k8s-node-2 ~]# docker network 
connect    create    disconnect   inspect     ls      prune      rm

[root@k8s-node-2 ~]# docker network create -d bridge --subnet 172.19.0.0/24 --gateway 172.19.0.1 cc
-d       指定網(wǎng)絡(luò)模式
--subnet     指定網(wǎng)段
--gateway    指定網(wǎng)關(guān)

三、跨宿主機(jī)容器間通訊

類似于橋接網(wǎng)絡(luò)

#創(chuàng)建
 docker network create -d macvlan --subnet 10.0.0.0/24 --gateway 10.0.0.1 -o parent=eth0 macvlan-1
-o parent=      指定使用的網(wǎng)絡(luò)接口

#應(yīng)用,需要指手動(dòng)分配 IP,并且ping 不通自己的宿主機(jī)
docker run -d --network macvlan-1 --ip 10.0.0.3 alpine:latest

四、跨宿主機(jī)容器間通訊之overlay

overlay是全局網(wǎng)絡(luò),在任意一個(gè)節(jié)點(diǎn)創(chuàng)建即可

1.在一臺(tái)主機(jī)上下載并導(dǎo)入 progrium/consul 鏡像
2.啟動(dòng) consul服務(wù) 容器

consul:kv類型的存儲(chǔ)數(shù)據(jù)庫(kù)(key:value)

docker run -d -p 8500:8500 -h consul --name consul progrium/consul -server -bootstrap

#通過(guò)http://IP:8500 訪問
3.所有docker 節(jié)點(diǎn)主機(jī) 配置docker.service 文件
#添加到 [Service] 后面
[root@docker01 ~]# vim /usr/lib/systemd/system/docker.service
[Service]
ExecStart=/usr/bin/dockerd --containerd=/run/containerd/containerd.sock

#重新加載啟動(dòng)
[root@docker01 ~]# systemctl daemon-reload
[root@docker01 ~]# systemctl restart docker.service 
4.所有docker節(jié)點(diǎn)主機(jī) 配置 daemon.json 文件
#添加
[root@docker01 ~]# vim /etc/docker/daemon.json
...
"cluster-store": "consul://10.0.0.13:8500",     #安裝consul的主機(jī)IP+port
"cluster-advertise": "10.0.0.11:2376"        #本地主機(jī)IP+port
...

#重啟docker
systemctl restart docker 
5.創(chuàng)建overlay 網(wǎng)絡(luò),并且使用它
docker network create -d overlay --subnet 172.16.0.0/16 --gateway 172.16.0.1 ol1

docker run -it --network ol1 --name one alpine:latest

真實(shí)原理:
每個(gè)容器有兩塊網(wǎng)卡,eth0實(shí)現(xiàn)容器間的通訊,eth1實(shí)現(xiàn)容器訪問外網(wǎng)

五、docker容器prometheus監(jiān)控

1.下載并解壓 prometheus

下載地址:https://prometheus.io/download/

[root@k8s-master ~]# tar xf prometheus-2.12.0.linux-amd64.tar.gz -C /usr/local/
[root@k8s-master ~]# cd /usr/local/
[root@k8s-master local]# mv prometheus-2.12.0.linux-amd64 prometheus
#創(chuàng)建用戶并授權(quán)
[root@k8s-master prometheus]# useradd prometheus
[root@k8s-master prometheus]# chown prometheus:prometheus /usr/local/prometheus
2.據(jù)情況修改配置文件 prometheus.yml
3.啟動(dòng)
[root@k8s-master prometheus]# ./prometheus --config.file=prometheus.yml &
#看服務(wù)是否啟動(dòng)
[root@k8s-master prometheus]# ps -ef | grep prometheus

也可以使用systemd方式啟動(dòng)服務(wù),需在 /usr/lib/systemd/system/ 目錄下,創(chuàng)建prometheus.service配置文件,然后啟動(dòng)服務(wù)。

4.訪問
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容