docker实战(一):centos7 yum安装docker

最小化安装ifconfig命令无:(网络排查)

初装centos 7时,运行config报 command not found 错误

环境变量里没有ifconfig命令的路径,因为ifconfig是在/sbin路径下的,以root用户登录才可以运行

解决办法是:yum安装ifconfig这个命令包

yum search ifconfig

通过yum search 这个命令我们发现ifconfig这个命令是在net-tools.x86_64这个包里,接下来我们安装这个包就行了

yum install net-tools.x86_64

ifconfig   看到对应输出

ifconfig命令来源于net-tools,这个包里有ifconfig, netstat, whois等命令

  • 查看磁盘空间相关命令

df  -hl 查看磁盘的剩余空间

df --h   查看某个根路径的分区大小

du -sh [目录名] 返回该目录的大小

du -sm [文件夹] 返回该文件夹总M数

du -h [目录名] 查看指定文件夹下的所有文件大小(包含子文件夹)

查看硬盘的分区 #sudo fdisk -l

查看IDE硬盘信息 #sudo hdparm -i /dev/hda

查看STAT硬盘信息 #sudo hdparm -I /dev/sda 或 #sudo apt-get install blktool #sudo blktool /dev/sda id

查看硬盘剩余空间 #df -h #df -H

查看目录占用空间 #du -hs 目录名

优盘没法卸载 #sync fuser -km /media/usbdisk

 

  • 磁盘空间不足排查(拍错:write error in swap file)

查看了一下磁盘空间,原因在于磁盘空间不够,于是执行命令:

df  -h

查看该目录下的文件占用的磁盘空间,并排序

cd  /

du -sh * | sort -n

====================================结果如下================================

1.4G tools

4.0K dump6379.rdb

4.0K dump6380.rdb

4.0K dump.rdb

4.0K server

8.7M run

16G usr    ---- 这个目录下的文件占了16G

35M etc

36K zookeeper.out

92K home

96K data

100M boot

244K root

404K tmp

481M var

 

查看网卡的配置情况

vi  /etc/sysconfig/network-scripts/ifcfg-eno16777728 

TYPE="Ethernet"

BOOTPROTO="dhcp"  # nat会自动根据宿主主机进行获取ip,更网关密切相关192.168.227.2

DEFROUTE="yes"

PEERDNS="yes"

PEERROUTES="yes"

IPV4_FAILURE_FATAL="no"

IPV6INIT="yes"

IPV6_AUTOCONF="yes"

IPV6_DEFROUTE="yes"

IPV6_PEERDNS="yes"

IPV6_PEERROUTES="yes"

IPV6_FAILURE_FATAL="no"

NAME="eno16777728"

UUID="9d23808e-b03e-48cd-a6d1-67e7e2165971"

DEVICE="eno16777728"

ONBOOT="yes"

配置静态IP

TYPE="Ethernet"
BOOTPROTO="Static"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="eno16777728"
IPADDR=192.168.227.131
NETMASK=255.255.255.0
GATEWAY=192.168.227.2
DNS1=192.168.227.2  ----宿主虚拟机网关
UUID="e7b1f4e9-d4d5-4f63-a898-2f85f778c797"
DEVICE="eno16777728"
ONBOOT="yes"

配置dns

vi  /etc/resolv.conf

search localdomain

nameserver 192.168.227.2     # 在虚拟机查找,该配置为虚拟机的网关

配置如下图:

systemctl status network  查看当前网卡状态(发现下面的错误信息)

network[25416]: Bringing up loopback interface:  Could not load file '/etc/sysconfig/network-scripts/ifcfg-lo'

Nov 12 12:35:41 node-1 network[25416]: Could not load file '/etc/sysconfig/network-scripts/ifcfg-lo'

Nov 12 12:35:41 node-1 network[25416]: Could not load file '/etc/sysconfig/network-scripts/ifcfg-lo'

Nov 12 12:35:41 node-1 network[25416]: Could not load file '/etc/sysconfig/network-scripts/ifcfg-lo

 

解决方案:

发现无法加载/etc/sysconfig/network-scripts/ifcfg-lo文件

给NetworkManager-wait-online服务设置开机自启动

systemctl enable NetworkManager-wait-online.service

systemctl stop NetworkManager

systemctl disable NetworkManager

systemctl restart network  

systemctl status network

 

网卡信息说明:

ifconfig

  1. eno16777728: flags=4163  mtu 1500

2.        inet 192.168.227.136  netmask 255.255.255.0  broadcast 192.168.227.255

3.        inet6 fe80::20c:29ff:fed6:eb4d  prefixlen 64  scopeid 0x20

4.        ether 00:0c:29:d6:eb:4d  txqueuelen 1000  (Ethernet)

5.        RX packets 120  bytes 11277 (11.0 KiB)

6.        RX errors 0  dropped 0  overruns 0  frame 0

7.        TX packets 77  bytes 12429 (12.1 KiB)

8.        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

  1. lo: flags=73  mtu 65536

10.        inet 127.0.0.1  netmask 255.0.0.0

11.        inet6 ::1  prefixlen 128  scopeid 0x10

12.        loop  txqueuelen 0  (Local Loopback)

13.        RX packets 0  bytes 0 (0.0 B)

14.        RX errors 0  dropped 0  overruns 0  frame 0

15.        TX packets 0  bytes 0 (0.0 B)

16.        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno16777728 是网卡接口,address是分配给指定该接口的主机名或者IP地址

Linux系统中的网卡Io为本地环回接口,IP地址固定为127.0.0.1,子网掩码8位,表示本机

 

ifconfig  eno16777728  

第1行: UP 表示此网络接口为启用状态,RUNNING表示网卡设备已连接,MULTICAST 表示支持组播,MTU为数据包最大传输单元

第2行:依次为网卡IP,子网掩码,广播地址

第3行: ipv6地址

第4行:Ethernet(以太网),表示连接类型,ether为网卡的MAC地址

第5行:接收数据包个数,大小统计信息

 

route -n  显示所有的路由表

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

0.0.0.0         192.168.227.2   0.0.0.0         UG    0      0        0 eno16777728

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eno16777728

192.168.227.0   0.0.0.0         255.255.255.0   U     0      0        0 eno16777728

检查kernel版本,确定overlay的内核模块是否加载
[root@docker-02 ~]# uname -r
3.10.0-327.el7.x86_64
安装Docker 最新版本,执行下面的命令

==========================yum安装最新docker(一)==========================

[root@docker-02 ~]# yum install docker bridge-utils -y

启动docker

[root@docker-02 ~]# systemctl start docker

启动的过程发生了一点下错误

● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 六 2020-04-25 18:51:13 CST; 30s ago
     Docs: http://docs.docker.com
 Main PID: 25435 (code=exited, status=1/FAILURE)

解决方案:

  意思是此linux的内核中的SELinux不支持 overlay2 graph driver

  重新编辑docker配置文件:

解决方案:
vi  /etc/sysconfig/docker

OPTIONS='--selinux-enabled  --log-driver=journald --signature-verification=false'
改为下面这一行

OPTIONS='--selinux-enabled=false --log-driver=journald --signature-verification=false'
 

设置开机启动

[root@docker-02 ~]#  systemctl enable docker

查看当前 docker0 ip

[root@docker-02 ~]# ifconfig docker0
docker0: flags=4099  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        ether 02:42:4e:9b:b8:52  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 

[root@docker-02 ~]# ip addr
1: lo: mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777728: mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:78:21:c4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.227.132/24 brd 192.168.227.255 scope global dynamic eno16777728
       valid_lft 1541sec preferred_lft 1541sec
    inet6 fe80::20c:29ff:fe78:21c4/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:4e:9b:b8:52 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever

拉取镜像

[root@docker-02 ~]# docker pull centos

Using default tag: latest

Trying to pull repository docker.io/library/centos ...

latest: Pulling from docker.io/library/centos

8a29a15cefae: Pull complete

Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700

Status: Downloaded newer image for docker.io/centos:latest

查看已经拉取到的镜像

[root@docker-02 ~]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

docker.io/centos    latest              470671670cac        3 months ago        237 MB

运行镜像centos 并指定名称为my-test,后台运行

[root@docker-02 ~]# docker run -itd --name my-test centos /bin/bash

5c7b37d5c51f2d3133d47e3955834c4f84114589b3a22403b178c5cfb45bd731

查看当前正在运行的容器

[root@docker-02 ~]# docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

5c7b37d5c51f        centos              "/bin/bash"         28 seconds ago      Up 27 seconds                           my-test

查看容器的默认分配的IP地址

[root@docker-02 ~]# docker inspect 5c7b37d5c51f |grep -i ipaddr

            "SecondaryIPAddresses": null,

            "IPAddress": "172.17.0.2",

                    "IPAddress": "172.17.0.2",

[root@docker-02 ~]# cat /proc/sys/net/ipv4/ip_forward

1

进入容器my-test

[root@docker-02 ~]# docker exec -it my-test /bin/bash

ping 外网是否通

[root@5c7b37d5c51f /]# ping www.baidu.com

PING www.a.shifen.com (220.181.38.149) 56(84) bytes of data.

64 bytes from www.baidu.com (220.181.38.149): icmp_seq=1 ttl=127 time=9.65 ms

64 bytes from www.baidu.com (220.181.38.149): icmp_seq=2 ttl=127 time=9.13 ms

64 bytes from www.baidu.com (220.181.38.149): icmp_seq=3 ttl=127 time=6.70 ms

64 bytes from www.baidu.com (220.181.38.149): icmp_seq=4 ttl=127 time=6.79 ms

64 bytes from www.baidu.com (220.181.38.149): icmp_seq=5 ttl=127 time=8.17 ms

 

到此docker拉取的镜像能够连接外网了

 

你可能感兴趣的:(K8持续运维实战,Docker实战,yum安装docker)