flannel
etcd数据库
1)做缓存 2)做配置中心(key:vlue dir:/etc/nginx/conf.d)
环境准备
主机 | IP | 角色 |
docker01 | 10.0.0.101 | docker |
docker02 | 10.0.0.102 | docker |
web | 10.0.0.100 | etcd |
(etcd要单独装一台机器)
安装etcd
# 安装etcd
[root@web ~]# yum install -y etcd
# 修改etcd配置文件
# [member]
ETCD_NAME=default
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_CLIENT_URLS="http://10.0.0.100:2379,http://127.0.0.1:2379"
# [cluster]
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://10.0.0.100:2379"
# 开启etcd(并检查端口起没起2379和2380)
[root@web ~]# systemctl start etcd
[root@web ~]# netstat -lntup
# 查看健康状态
[root@web ~]# etcdctl -C http://10.0.0.100:2379 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://10.0.0.100:2379
cluster is healthy
---测试-----
## 写入数据
[root@docker01 ~]# etcdctl -C http://10.0.0.100:2379 set /testdir/testkey "hello world"
hello world
## 查看数据
[root@docker01 ~]# etcdctl -C http://10.0.0.100:2379 get /testdir/testkey
hello world
安装flannel
# 主机docker01和主机docker02要安装docker
# 安装flannel(需要互相通信的机器都安装)
[root@docker01 ~]# yum install -y flannel
[root@docker02 ~]# yum install -y flannel
# 修改flannel配置文件
[root@docker01 ~]# vim /etc/sysconfig/flanneld
FLANNEL_ETCD_ENDPOINTS="http://10.0.0.100:2379"
[root@docker02 ~]# vim /etc/sysconfig/flanneld
FLANNEL_ETCD_ENDPOINTS="http://10.0.0.100:2379"
# 配置etcd数据库(到安装etcd的机器配置)
[root@web ~]# etcdctl mk /atomic.io/network/config '{"Network":"192.168.0.0/16"}'
或
[root@web ~]# etcdctl -C http://10.0.0.100:2379 set /atomic.io/network/config '{"Network":"192.168.0.0/16"}'
# 查看数据(到安装etcd的机器查看)
[root@web ~]# etcdctl -C http://10.0.0.100:2379 get /atomic.io/network/config
{"Network":"192.168.0.0/16"}
# 启动flannel(flannel启动后会把IP自动注册到etcd,图形化可查看)
[root@docker01 ~]# systemctl start flanneld
[root@docker02 ~]# systemctl start flanneld
# 查看一下网卡信息(查看有没有flannel0)
[root@docker01 ~]# ifconfig flannel0
flannel0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1472
inet 192.168.92.0 netmask 255.255.0.0 destination 192.168.92.0
inet6 fe80::89fc:e991:daf0:1aba prefixlen 64 scopeid 0x20<link>
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 500 (UNSPEC)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3 bytes 144 (144.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Docker关联flannel
# 修改docker启动脚本
[root@docker02 cicd]# vim /usr/lib/systemd/system/docker.service
EnviromentFile=/run/flannel/docker
ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_NETWORK_OPTIONS
# 重启docker
[root@docker01 ~]# systemctl daemon-reload
[root@docker01 ~]# systemctl start docker
etcd图形化
# 下载图形化安装包
# 解压
[root@harbor ~]# unzip etcdkeeper-v0.7.6-linux_x86_64.zip
# 进入etcdkeeper/目录给etcdkeeper文件执行权限
[root@harbor ~]# cd etcdkeeper/
[root@harbor etcdkeeper]# chmod +x etcdkeeper
# 启动 (-h 后面的ip按需求写)(启动后终端不能退出,否则页面会出问题)
[root@harbor etcdkeeper]# ./etcdkeeper -h 10.0.0.100
2023-09-15 10:43:48.746653 I | listening on 10.0.0.100:8080
2023-09-15 10:43:57.223658 I | POST v3 connect success.
2023-09-15 10:43:57.233196 I | GET v3 /
2023-09-15 10:44:01.804296 I | POST v2 connect success.
2023-09-15 10:44:01.809163 I | GET v2 /
2023-09-15 10:44:04.775646 I | GET v3 /
2023-09-15 10:44:20.277622 I | GET v3 /
2023-09-15 10:44:36.726657 I | GET v2 /
# 访问
10.0.0.100:8080
flannel启动后会把IP自动注册到etcd
两边启动容器测试
# 内核转发
[root@docker01 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
[root@docker02 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
# 防火墙加载(开启防火墙加载完后就关闭)
[root@docker01 ~]# systemctl start firewalld
[root@docker01 ~]# systemctl stop firewalld
[root@docker02 ~]# docker run -it busybox /bin/sh
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:C0:A8:51:02
inet addr:192.168.81.2 Bcast:192.168.81.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1472 Metric:1
RX packets:8 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:648 (648.0 B) TX bytes:0 (0.0 B)
[root@docker01 ~]# docker run -it busybox /bin/sh
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:C0:A8:0B:03
inet addr:192.168.11.3 Bcast:192.168.11.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1472 Metric:1
RX packets:6 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:508 (508.0 B) TX bytes:0 (0.0 B)
/ # ping 192.168.11.3 (做完内核转发两台机器可以ping通)
PING 192.168.11.3 (192.168.11.3): 56 data bytes
64 bytes from 192.168.11.3: seq=0 ttl=60 time=1.359 ms
64 bytes from 192.168.11.3: seq=1 ttl=60 time=4.459 ms
64 bytes from 192.168.11.3: seq=2 ttl=60 time=0.461 ms
64 bytes from 192.168.11.3: seq=3 ttl=60 time=6.416 ms