link一下 就知道容器的ip地址css
每作一次link 就會hosts解析html
# 啓動第一個容器 [root@docker01 ~]# docker run -d xiaoniao:v v1 v3 v4 v5 v6 v7 v8 [root@docker01 ~]# docker run -d xiaoniao:v4 b8d7b1f7e1f9d69e76d9af4b4ef5a06902244770c1293ccab0c1a87591f18e6f # 在第一個容器的基礎上 啓動第二個容器 [root@docker01 ~]# docker run -it --link sweet_chandrasekhar:xiaoniao xiaoniao:v1 /bin/bash # 看看網站狀態 [root@a227b9c0f449 /]# curl -I http://xiaoniao HTTP/1.1 200 OK Server: nginx/1.10.3 Date: Tue, 31 Dec 2019 02:16:56 GMT Content-Type: text/html Content-Length: 3049 Last-Modified: Sat, 02 Aug 2014 07:55:12 GMT Connection: keep-alive ETag: "53dc9960-be9" Accept-Ranges: bytes # 查看hosts解析 [root@a227b9c0f449 /]# cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.2 xiaoniao b8d7b1f7e1f9 sweet_chandrasekhar 172.17.0.3 a227b9c0f449
# 啓動第一個容器 [root@docker01 ~]# docker run -d mysql mysql mysql:v1 mysql:v2 mysql:v3 [root@docker01 ~]# docker run -d mysql:v3 7566567f3b28cf28d75f656007efb2697d38d1788d2909a5378a681499ab9f86 # 改個名 [root@docker01 ~]# docker run --name mysql -d mysql:v3 0e780acbda7e2e596bc3d43d41eea9671fd6cf6afd43928b7f44a93eaea28385 # 在第一個容器的基礎上 啓動第二個容器 [root@docker01 ~]# docker run -it --link mysql:mysql xiaoniao:v1 /bin/bash [root@d42da268737b /]# ping mysql PING mysql (172.17.0.4) 56(84) bytes of data. 64 bytes from mysql (172.17.0.4): icmp_seq=1 ttl=64 time=0.065 ms 64 bytes from mysql (172.17.0.4): icmp_seq=2 ttl=64 time=0.041 ms # 查看hosts解析 [root@d42da268737b /]# cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.4 mysql 0e780acbda7e 172.17.0.5 d42da268737b
1)拖四個包java
[root@docker01 /srv]# ll -rw-r--r-- 1 root root 392823296 Aug 15 09:36 docker-mysql-5.7.tar.gz -rw-r--r-- 1 root root 153172992 Aug 15 09:36 zabbix-java-gateway.tar.gz -rw-r--r-- 1 root root 110936576 Aug 15 09:35 zabbix-server-mysql.tar.gz -rw-r--r-- 1 root root 179232768 Aug 15 09:35 zabbix-web-nginx-mysql.tar.gz
2)寫個循環 導入node
[root@docker01 /srv]# for n in `ls *.tar.gz`;do docker load -i $n;done
3)命令mysql
docker run --name mysql-server -t \ -e MYSQL_DATABASE="zabbix" \ -e MYSQL_USER="zabbix" \ -e MYSQL_PASSWORD="zabbix_pwd" \ -e MYSQL_ROOT_PASSWORD="root_pwd" \ -d mysql:5.7 \ --character-set-server=utf8 --collation-server=utf8_bin docker run --name zabbix-java-gateway -t \ -d zabbix/zabbix-java-gateway:latest docker run --name zabbix-server-mysql -t \ -e DB_SERVER_HOST="mysql-server" \ -e MYSQL_DATABASE="zabbix" \ -e MYSQL_USER="zabbix" \ -e MYSQL_PASSWORD="zabbix_pwd" \ -e MYSQL_ROOT_PASSWORD="root_pwd" \ -e ZBX_JAVAGATEWAY="zabbix-java-gateway" \ --link mysql-server:mysql \ --link zabbix-java-gateway:zabbix-java-gateway \ -p 10051:10051 \ -d zabbix/zabbix-server-mysql:latest docker run --name zabbix-web-nginx-mysql -t \ -e DB_SERVER_HOST="mysql-server" \ -e MYSQL_DATABASE="zabbix" \ -e MYSQL_USER="zabbix" \ -e MYSQL_PASSWORD="zabbix_pwd" \ -e MYSQL_ROOT_PASSWORD="root_pwd" \ --link mysql-server:mysql \ --link zabbix-server-mysql:zabbix-server \ -p 80:80 \ -d zabbix/zabbix-web-nginx-mysql:latest
4)訪問10.0.0.5nginx
宿主機太多 鏡像太多 本地沒有 去官方拉 可是一羣都拉 慢的很git
私有倉庫 只拉一次 每次都去倉庫拉 節省帶寬github
1)拖個包web
[root@docker01 /srv]# ll -rw-r--r-- 1 root root 35771392 Aug 15 09:35 registry.tar.gz
2)上傳鏡像sql
[root@docker01 /srv]# docker load -i registry.tar.gz
3)啓動私有倉庫
[root@docker01 /srv]# docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregistry:/var/lib/registry registry 03b40960c4226efc526c21d64f1e95a442c7daa6094ec39228805cbfabd68bfd --restart=always 當我docker服務起來時 自動把我這個容器起來
4)上傳鏡像到私有倉庫
# 給鏡像打標籤 [root@docker02 ~]# docker image tag test:v1 10.0.0.5:5000/test:v1 # 上傳鏡像 [root@docker02 ~]# docker push 10.0.0.5:5000/test:v1 # 查看私有倉庫有沒有鏡像 [root@docker01 ~]# ls /opt/myregistry/docker/registry/v2/repositories/ test
5)對上面命令的解釋
# 查看鏡像 [root@docker02 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test v1 0ffdc2348521 3 hours ago 1.89MB # 把鏡像上傳到私有倉庫 [root@docker02 ~]# docker image tag test:v1 10.0.0.5:5000/test:v1 # 查看鏡像 多了一個 [root@docker02 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 10.0.0.5:5000/test v1 0ffdc2348521 3 hours ago 1.89MB test v1 0ffdc2348521 3 hours ago 1.89MB # 刪掉鏡像 移除的是標籤 [root@docker02 ~]# docker rmi test:v1 # 查看鏡像 [root@docker02 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 10.0.0.5:5000/test v1 0ffdc2348521 3 hours ago 1.89
[root@docker02 ~]# docker push 10.0.0.5:5000/test:v1 The push refers to repository [10.0.0.5:5000/test] Get https://10.0.0.5:5000/v2/: http: server gave HTTP response to HTTPS client [root@docker02 ~]# cat /etc/docker/daemon.json { "insecure-registries": ["10.0.0.5:5000"], "registry-mirrors": ["https://registry.docker-cn.com"] } [root@docker02 ~]# systemctl restart docker [root@docker02 ~]# docker push 10.0.0.5:5000/test:v1 The push refers to repository [10.0.0.5:5000/test] 254fbd52eeb8: Pushed 195be5f8be1d: Pushed v1: digest: sha256:10e10960acc278f44fcad76299fa3c87071647086f4728cb674b68bba6fab6ee size: 737
全部人均可以在私有倉庫上傳鏡像 若是名字相同 以前的鏡像就會被覆蓋掉
官方倉庫帶認證 私有倉庫也能夠帶認證
[root@docker01 ~]# yum install httpd-tools -y [root@docker01 ~]# mkdir /opt/registry-var/auth/ -p [root@docker01 ~]# htpasswd -Bbn mst 123456 >> /opt/registry-var/auth/htpasswd [root@docker01 ~]# docker run -d -p 5000:5000 --restart=always -v /opt/registry-var/auth/:/auth/ -v /opt/myregistry:/var/lib/registry -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" registry
# 再推一次鏡像 被拒絕 [root@docker01 ~]# docker push 10.0.0.5:5000/xiaoniao:v1 The push refers to repository [10.0.0.5:5000/xiaoniao] 50c9181be7e8: Preparing dc3717969f3a: Preparing 7a11b788a6b7: Preparing c71feb1f04e6: Preparing f59aa8ef2875: Preparing b5e11aae8a8e: Preparing no basic auth credentials # 登陸docker 輸入帳號密碼 [root@docker01 ~]# docker login 10.0.0.5:5000 Username: mst Password: Login Succeeded # 再次推鏡像 成功了 [root@docker01 ~]# docker push 10.0.0.5:5000/xiaoniao:v1 The push refers to repository [10.0.0.5:5000/xiaoniao] 50c9181be7e8: Layer already exists dc3717969f3a: Layer already exists 7a11b788a6b7: Layer already exists c71feb1f04e6: Layer already exists f59aa8ef2875: Layer already exists b5e11aae8a8e: Layer already exists v1: digest: sha256:1c606a09ba312d5d43c53d083b1a2c2ebd719b95f076617df97538dc4aa95cac
上傳要登陸 下載也要登錄 刪除私有倉庫鏡像不容易 權限控制不太好
查看鏡像列表
使用瀏覽器訪問:
http://10.0.0.11:5000/v2/_catalog
查看鏡像的版本
http://10.0.0.11:5000/v2/nginx/tags/list
# 刪除鏡像 1)進入docker registry的容器中 docker exec -it registry /bin/sh 2) 刪除repo rm -fr /var/lib/registry/docker/registry/v2/repositories/nginx 3) 清理掉blobs registry garbage-collect /etc/docker/registry/config.yml
wordpress
以前都是一次啓動一個容器 藉助這個工具一次能夠啓多個容器
像ansible劇本
# 下載工具(須要epel源) [root@docker01 ~]# yum install -y docker-compose [root@docker01 ~]# docker-compose -v docker-compose version 1.18.0, build 8dd22a9 # 進目錄編輯文件 [root@docker01 ~]# mkdir /opt/docker-compose [root@docker01 ~]# cd /opt/docker-compose/ [root@docker01 /opt/docker-compose]# mkdir wordpress [root@docker01 /opt/docker-compose]# cd wordpress/
# 編寫配置文件 [root@docker01 /opt/docker-compose/wordpress]# vim docker-compose.yml[必須叫這個名] version: '3' services: db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: - db image: wordpress:latest volumes: - web_data:/var/www/html ports: - "80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress volumes: db_data: web_data:
# 拖個wordpress的包 由於你配置它 本地沒有會很慢 [root@docker01 /srv]# ll -rw-r--r-- 1 root root 417304064 Aug 15 09:36 wordpress-latest.tar.gz # 導入鏡像 [root@docker01 /srv]# docker load -i wordpress-latest.tar.gz # 啓動 [root@docker01 /opt/docker-compose/wordpress]# docker-compose up #後臺啓動 docker-compose up -d
# 方法一: docker run --restart=always # 方法二:[這個很坑 最好忘掉] docker server配置文件/etc/docker/daemon.json { "registry-mirrors": ["http://b7a9017d.m.daocloud.io"], "insecure-registries":["10.0.0.11:5000"], "live-restore": true }
基本上不用了
功能是幫你在宿主機上安裝docker
服務。
None:不爲容器配置任何網絡功能,--net=none Container:與另外一個運行中的容器共享Network Namespace,--net=container:containerID(K8S) Host:與宿主機共享Network Namespace,--network=host 性能最高 端口先到先得 Bridge:Docker設計的NAT網絡模型
[root@docker01 ~]# docker run -d --network=HOST -it 10.0.0.5:5000/test:v1 --network= 就是指定網絡類型的 不寫默認bridge
默認一個物理網卡,只有一個物理mac地址,但虛擬多個mac地址就等效多個物理網卡
# 查看網絡 [root@docker01 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 369f3f070370 bridge bridge local 5410a95eb8da host host local b17e3901fdcd none null local # 建立macvlan網絡 [root@docker01 ~]# docker network create --driver macvlan --subnet 10.0.0.0/24 --gateway 10.0.0.254 -o parent=eth0 macvlan_1 # 查看網絡 多了一個剛建立的 [root@docker01 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 369f3f070370 bridge bridge local 5410a95eb8da host host local 1ec91b78a1b7 macvlan_1 macvlan local b17e3901fdcd none null local
# 另外一臺服務器也建立 [root@docker02 ~]# docker network create --driver macvlan --subnet 10.0.0.0/24 --gateway 10.0.0.254 -o parent=eth0 macvlan_1 [root@docker02 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE d38e976c914c bridge bridge local 23918a601f04 host host local bc13a798a6a6 macvlan_1 macvlan local
# 啓動容器 [root@docker01 ~]# docker run -it --network macvlan_1 --ip 10.0.0.33 xiaoniao:v5 /bin/bash # 查看ip [root@3327f80d8e38 /]# ifconfig eth0 Link encap:Ethernet HWaddr 02:42:0A:00:00:21 inet addr:10.0.0.33 Bcast:10.0.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) # ping一下 [g:\~]$ ping 10.0.0.33 正在 Ping 10.0.0.33 具備 32 字節的數據: 來自 10.0.0.33 的回覆: 字節=32 時間<1ms TTL=64 來自 10.0.0.33 的回覆: 字節=32 時間<1ms TTL=64 # 在容器裏啓動nginx [root@3327f80d8e38 /]# service nginx start 直連 不須要端口映射
# 再啓動一個容器 [要注意私有倉庫容器開沒開] [root@docker02 ~]# docker run -it --network macvlan_1 --ip 10.0.0.34 10.0.0.5:5000/xiaoniao:v1 # 查看ip 10.0.0.34 # ping ping 10.0.0.33 互通
解決了ip地址衝突問題 若是用macvlan須要手動指定ip地址
用overlay不須要手動指定 他會把ip存在倉庫裏 每次建立時都會讀倉庫 若是有了 就自動給你日後拍ip
# 準備三臺服務器 # 能夠再第三臺服務器上面裝通訊容器 docker03: docker run -d -p 8500:8500 -h consul --name consul progrium/consul -server -bootstrap
這裏我選擇裝在docker01上
先修改配置文件 再啓動通訊容器 由於重啓服務會退出
# 修改配置文件 [root@docker01 ~]# vim /etc/docker/daemon.json { "registry-mirrors": ["https://registry.docker-cn.com"], "insecure-registries": ["10.0.0.5:5000"], "hosts":["tcp://0.0.0.0:2376","unix:///var/run/docker.sock"], "cluster-store": "consul://10.0.0.5:8500", "cluster-advertise": "10.0.0.5:2376" } 【重啓 會失敗 查看日誌是由於配置文件和system管理配置文件衝突 因此修改sys文件】 # 修改systemctl配置文件 [root@docker01 ~]# vim /usr/lib/systemd/system/docker.service 把 -H fd:// 參數刪掉 # 重載 [root@docker01 ~]# systemctl daemon-reload [root@docker01 ~]# systemctl restart docker
# docker02 作相同操做 [root@docker02 ~]# vim /etc/docker/daemon.json { "registry-mirrors": ["https://registry.docker-cn.com"], "insecure-registries": ["10.0.0.5:5000"], "hosts":["tcp://0.0.0.0:2376","unix:///var/run/docker.sock"], "cluster-store": "consul://10.0.0.5:8500", "cluster-advertise": "10.0.0.6:2376" } [root@docker02 ~]# vim /usr/lib/systemd/system/docker.service 把 -H fd:// 參數刪掉 [root@docker02 ~]# systemctl daemon-reload [root@docker02 ~]# systemctl restart docker
由於沒有鏡像要在線pu 浪費時間 因此我託個包上傳
root@docker01 ~]# ll -rw-r--r-- 1 root root 70327296 Nov 10 2018 docker_progrium_consul.tar.gz [root@docker01 ~]# docker load -i docker_progrium_consul.tar.gz
[root@docker01 ~]# docker run -d -p 8500:8500 -h consul --name consul progrium/consul -server -bootstrap
[root@docker01 ~]# docker network create -d overlay --subnet 172.16.2.0/24 --gateway 172.16.2.254 ol1 避免網段衝突 因此改爲.2了 # 查看網絡 出現global類型的 [root@docker01 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE b7b7a68209d0 bridge bridge local 5410a95eb8da host host local 1ec91b78a1b7 macvlan_1 macvlan local b17e3901fdcd none null local 9ff4a5d78a4a ol1 overlay global # 另外一臺服務器查看 也有 由於docker開了一個遠程控制的工具 [root@docker02 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 1569e3d54ae4 bridge bridge local 23918a601f04 host host local bc13a798a6a6 macvlan_1 macvlan local 05173a1abc09 none null local 9ff4a5d78a4a ol1 overlay global
# 遠程鏈接查看docker01的服務 [root@docker02 ~]# docker -H 10.0.0.5:2376 ps -a -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8d296b93e72b progrium/consul "/bin/start -server …" 7 minutes ago Up 7 minutes 53/tcp, 53/udp, 8300-8302/tcp, 8400/tcp, 8301-8302/udp, 0.0.0.0:8500->8500/tcp consul # docker02本身的服務 [root@docker02 ~]# docker ps -a -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 兩邊都準備好busybox鏡像 # 啓動容器 主機名oldboy01 [root@docker01 ~]# docker run -it --network ol1 --name oldboy01 busybox:latest /bin/sh / # ping oldboy02 PING oldboy02 (172.16.2.2): 56 data bytes 64 bytes from 172.16.2.2: seq=0 ttl=64 time=0.485 ms 64 bytes from 172.16.2.2: seq=1 ttl=64 time=0.320 ms 【ping對方主機名 就能知道對方的ip】 # 啓動容器 主機名oldboy02 [root@docker02 ~]# docker run -it --network ol1 --name oldboy02 busybox:latest /bin/sh / # ping oldboy01 PING oldboy01 (172.16.2.1): 56 data bytes 64 bytes from 172.16.2.1: seq=0 ttl=64 time=14.043 ms 64 bytes from 172.16.2.1: seq=1 ttl=64 time=0.664 ms 64 bytes from 172.16.2.1: seq=2 ttl=64 time=0.410 ms
ip地址不會衝突
[root@docker01 ~]# docker run --network ol1 --name web01 -p 80:80 xiaoniao:v8 /bin/bash [root@2cbd20d33f82 /]# ifconfig [root@2cbd20d33f82 /]# ping oldboy02 PING oldboy02 (172.16.2.2) 56(84) bytes of data. 64 bytes from oldboy02.ol1 (172.16.2.2): icmp_seq=1 ttl=64 time=8.70 ms 64 bytes from oldboy02.ol1 (172.16.2.2): icmp_seq=2 ttl=64 time=0.538 ms [root@2cbd20d33f82 /]# ping 223.5.5.5 PING 223.5.5.5 (223.5.5.5) 56(84) bytes of data. 64 bytes from 223.5.5.5: icmp_seq=1 ttl=127 time=9.25 ms 64 bytes from 223.5.5.5: icmp_seq=2 ttl=127 time=40.6 ms
docker registry(私有倉庫):功能弱 權限簡單 刪除鏡像麻煩
docker harbor:重量級
第一步:安裝docker和docker-compose 第二步:下載harbor-offline-installer-v1.x.0.tgz 第三步:上傳到/opt,並解壓 第四步:修改harbor.cfg配置文件 hostname = 10.0.0.5 harbor_admin_password = 123456 第五步:執行install.sh
# 下載docker-compose [root@docker01 ~]# yum install -y docker-compose # 拖個包 [root@docker01 /opt]# ll -rw-r--r-- 1 root root 864933610 Aug 15 09:36 harbor-offline-installer-v1.5.1.tgz # 解壓 [root@docker01 /opt]# tar xf harbor-offline-installer-v1.5.1.tgz # 進目錄修改文件 [root@docker01 /opt/harbor]# vim harbor.cfg hostname = 10.0.0.5 harbor_admin_password = 123456 # 執行腳本 [root@docker01 /opt/harbor]# ./install.sh
# 根據格式推鏡像到倉庫 [root@docker02 ~]# docker tag busybox:latest 10.0.0.5/library/busybox:latest [root@docker02 ~]# docker push 10.0.0.5/library/busybox:latest The push refers to repository [10.0.0.5/library/busybox] Get https://10.0.0.5/v2/: dial tcp 10.0.0.5:443: connect: connection refused 【報錯了 仍是以前http和https的緣由】 # 修改配置文件 以前有端口映射5000 如今沒有 [root@docker02 ~]# vim /etc/docker/daemon.json { "insecure-registries": ["10.0.0.5"], "registry-mirrors": ["https://registry.docker-cn.com"], "hosts":["tcp://0.0.0.0:2376","unix:///var/run/docker.sock"], "cluster-store": "consul://10.0.0.5:8500", "cluster-advertise": "10.0.0.6:2376" } [root@docker02 ~]# systemctl restart docker # 修改完配置文件與可能harbor起不來 [root@docker01 ~]# docker-compose restart
# 推鏡像到倉庫 自帶登陸 因此先登陸 [root@docker02 ~]# docker login 10.0.0.5 Username: admin Password: (123456) [root@docker02 ~]# docker push 10.0.0.5/library/busybox:latest
爲了解決docker stats的問題(存儲、展現),谷歌開源的cadvisor誕生了,cadvisor不只能夠蒐集一臺機器上全部運行的容器信息,還提供基礎查詢界面和http接口,方便其餘組件如Prometheus進行數據抓取,或者cadvisor + influxdb + grafna搭配使用。
cAdvisor能夠對節點機器上的資源及容器進行實時監控和性能數據採集,包括CPU使用狀況、內存使用狀況、網絡吞吐量及文件系統使用狀況。
Cadvisor使用Go語言開發,利用Linux的cgroups獲取容器的資源使用信息,在K8S中集成在Kubelet裏做爲默認啓動項,官方標配。
下載二進制:https://github.com/google/cadvisor/releases/latest 本地運行:./cadvisor -port=8080 &>>/var/log/cadvisor.log
docker run \ --volume=/:/rootfs:ro \ --volume=/var/run:/var/run:rw \ --volume=/sys:/sys:ro \ --volume=/var/lib/docker/:/var/lib/docker:ro \ --volume=/dev/disk/:/dev/disk:ro \ --publish=8080:8080 \ --detach=true \ --name=cadvisor \ google/cadvisor:latest
#注意: 在Ret Hat,CentOS, Fedora 等發行版上須要傳遞以下參數,由於 SELinux 增強了安全策略: --privileged=true (慎用 共用一個內核) #啓動後訪問:http://127.0.0.1:8080查看頁面,/metric查看指標
Daemonset部署: https://github.com/google/cadvisor/tree/master/deploy/kubernetes kubelet自帶cadvisor監控全部節點,能夠設置--cadvisor-port=8080指定端口(默認爲4194) kubernetes 在2015-03-10 這個提交(Run cAdvisor inside the Kubelet. Victor Marmol 2015/3/10 13:39)中cAdvisor開始集成在kubelet中,目前的1.6及之後均存在
#注意: 從 v1.7 開始,Kubelet metrics API 再也不包含 cadvisor metrics,而是提供了一個獨立的 API 接口: Kubelet metrics: http://127.0.0.1:8001/api/v1/proxy/nodes/<node-name>/metrics Cadvisor metrics: http://127.0.0.1:8001/api/v1/proxy/nodes/<node-name>/metrics/cadvisor cadvisor 監聽的端口將在 v1.12 中刪除,建議全部外部工具使用 Kubelet Metrics API 替代。
# 1.拖個包 [root@docker01 ~]# ll -rw-r--r-- 1 root root 628516864 Aug 16 12:34 docker_monitor.tar.gz # 2.導入鏡像 [root@docker01 ~]# docker load -i docker_monitor.tar.gz # 3.啓動第一個容器 [root@docker01 ~]# docker run -itd -p 8083:8083 -p 8086:8086 --name influxdb tutum/influxdb
在界面上建立庫 後面使用
# 啓動第二個容器 (docker 1.7以上版本) [root@docker01 ~]# docker run -itd --name cadvisor -p 8080:8080 --link influxdb:influxdb --mount type=bind,src=/,dst=/rootfs,ro --mount type=bind,src=/var/run,dst=/var/run --mount type=bind,src=/sys,dst=/sys,ro --mount type=bind,src=/var/lib/docker/,dst=/var/lib/docker,ro google/cadvisor -storage_driver=influxdb -storage_driver_db=cadvisor -storage_driver_user=root -storage_driver_password=root -storage_driver_host=influxdb:8086
# 啓動第三個容器 [root@docker01 ~]# docker run -itd --name grafana -p 3000:3000 grafana/grafana
登陸
添加數據源
建立監控
Prometheus 是一個很是優秀的監控工具。提供了監控數據蒐集,存儲,處理,可視化和告警一系列完 整的解決方案。
#包含組件: Node Exporter :負責收集 host 硬件和操做系統數據,以容器的形式運行在全部 host 上 cAdvisor :負責收集容器數據,以容器的形式運行在全部 host 上
#部署 #地址規劃: 10.0.0.5 cAdvisor+ Node Exporter +prometheus+ grafana 10.0.0.6 cAdvisor+ Node Exporter
# 拖安裝包到兩臺服務器 [root@docker01 ~]# ll -rw-r--r-- 1 root root 70389760 Jan 9 16:34 cadvisor.tar -rw-r--r-- 1 root root 898 Jan 9 16:34 docker-compose.yml -rw-r--r-- 1 root root 42315 Jan 9 16:34 docker-host-container-overview_rev1.json -rw-r--r-- 1 root root 232943616 Jan 9 16:34 grafana.tar -rw-r--r-- 1 root root 24351232 Jan 9 16:34 node-exporter.tar -rw-r--r-- 1 root root 134138368 Jan 9 16:34 prometheus.tar -rw-r--r-- 1 root root 354 Jan 9 16:34 prometheus.yml
# 導入鏡像 docker load -i cadvisor.tar docker load -i grafana.tar docker load -i prometheus.tar docker load -i node-exporter.tar
# 修改配置文件 #docker01的配置文件: cat >docker-compose.yml<<EOF version: '3.2' services: prometheus: image: prom/prometheus:latest container_name: prometheus ports: - 9090:9090 command: - --config.file=/etc/prometheus/prometheus.yml volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro depends_on: - cadvisor node-exporter: image: prom/node-exporter:latest container_name: node_exporter ports: - 9100:9100 cadvisor: image: google/cadvisor:latest container_name: cadvisor ports: - 8080:8080 volumes: - /:/rootfs:ro - /var/run:/var/run:rw - /sys:/sys:ro - /var/lib/docker/:/var/lib/docker:ro grafana: image: grafana/grafana:latest container_name: grafana ports: - 3000:3000 EOF #prometheus配置文件 cat >prometheus.yml <<EOF scrape_configs: - job_name: cadvisor scrape_interval: 5s static_configs: - targets: - 10.0.0.5:8080 - 10.0.0.6:8080 - job_name: prometheus scrape_interval: 5s static_configs: - targets: - 10.0.0.5:9090 - job_name: node_exporter scrape_interval: 5s static_configs: - targets: - 10.0.0.5:9100 - 10.0.0.6:9100 EOF
#docker02配置文件 cat >docker-compose.yml<<EOF version: '3.2' services: node-exporter: image: prom/node-exporter:latest container_name: node_exporter ports: - 9100:9100 cadvisor: image: google/cadvisor:latest container_name: cadvisor ports: - 8080:8080 volumes: - /:/rootfs:ro - /var/run:/var/run:rw - /sys:/sys:ro - /var/lib/docker/:/var/lib/docker:ro EOF
# 運行 yum install docker-compose -y ntpdate time1.aliyun.com docker-compose -f docker-compose.yml up -d
# 檢查 http://10.0.0.5:8080/metrics http://10.0.0.5:9100/metrics http://10.0.0.5:9090 http://10.0.0.5:3000
打開 grafana:
10.0.0.5:3000
admin admin
**添加數據源 **
下載並加載監控面板
https://grafana.com/api/dashboards/10619/revisions/1/download
#能夠拖個下載好的 docker-host-container-overview_rev1.json