ubuntu安装k8s

发布时间 2023-11-10 15:47:44作者: MissSimple

1、查看Ubuntu系统版本
w@node1:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.6 LTS
Release:	18.04
Codename:	bionic

2、设置主机名

vim /etc/hostname 
w@node1:~$ cat /etc/hostname 
node1

 

3、配置节点静态IP地址(可选)。

使用ifconfig查询本机ip和掩码(没有ifconfig命令,可以apt-get install net-tools)

w@node1:~$ ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 02:42:53:5f:6a:cd  txqueuelen 0  (以太网)
        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

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.199.141  netmask 255.255.255.0  broadcast 192.168.199.255
        inet6 fe80::20c:29ff:fed8:ca5f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d8:ca:5f  txqueuelen 1000  (以太网)
        RX packets 7904  bytes 5375252 (5.3 MB)
        RX errors 1  dropped 0  overruns 0  frame 0
        TX packets 6380  bytes 772741 (772.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 19  base 0x2000  

 以太网卡为ens33,ip为192.168.199.141,掩码为255.255.255.0,

查询网关:

w@node1:/etc/netplan$ route -n
内核 IP 路由表
目标            网关            子网掩码        标志  跃点   引用  使用 接口
0.0.0.0         192.168.199.2   0.0.0.0         UG    100    0        0 ens33
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 ens33
192.168.199.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

备份 /etc/netplan/01-network-manager-all.yaml ,然后更改文件:

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager   #和原先的保持一致
  ethernets:
    ens33:
      dhcp4: no
      addresses: [192.168.199.141/24]  #静态ip/掩码
      gateway4: 192.168.199.2 #网关
      nameservers:
        addresses: [192.168.199.141,223.6.6.6]   #设置一个响应快的DNS,因后续会使用本机作为DNS,所以这里把本机加上

 

sudo netlan apply

重启机器

查看配置:

w@node1:~$ ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:18:e9:51:20  txqueuelen 0  (以太网)
        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

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.199.141  netmask 255.255.255.0  broadcast 192.168.199.255
        inet6 fe80::20c:29ff:fed8:ca5f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d8:ca:5f  txqueuelen 1000  (以太网)
        RX packets 5419  bytes 4116516 (4.1 MB)
        RX errors 2  dropped 2  overruns 0  frame 0
        TX packets 5133  bytes 709451 (709.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 19  base 0x2000  

 配置ip地址和主机映射

vim /etc/hosts
w@node1:~$ cat /etc/hosts
127.0.0.1	localhost
127.0.1.1	w-virtual-machine
192.168.199.141 node1

 

w@node1:~$ ping node1
PING node1 (192.168.199.141) 56(84) bytes of data.
64 bytes from node1 (192.168.199.141): icmp_seq=1 ttl=64 time=0.043 ms
64 bytes from node1 (192.168.199.141): icmp_seq=2 ttl=64 time=0.113 ms
64 bytes from node1 (192.168.199.141): icmp_seq=3 ttl=64 time=0.123 ms
64 bytes from node1 (192.168.199.141): icmp_seq=4 ttl=64 time=0.136 ms

 

4、配置Ubuntu软件源

vi /etc/apt/sources.list
在最后增加:
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
apt-key.gpg是k8s的deb源公钥,加载k8s的deb源公钥命令为: 首先su root再执行:
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - 

 然后apt-get update

apt-cache madison kubelet 查看可安装版本

5、关闭swap、防火墙
原因是swap会导致挂掉的程序靠swap续命,不能及时OOM,让k8s来接管重启容器,
关闭防火墙是因为防火墙可能阻碍集群内部组件通信。
root@node1:/home/w# ufw disable
防火墙在系统启动时自动禁用

 

root@node1:/home/w# swapoff -a ;sed -i '/swap/d' /etc/fstab
root@node1:/home/w# cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda1 during installation
UUID=2aa24dbd-cf22-4c5f-91d6-bc73544fd3c2 /               ext4    errors=remount-ro 0       1

 6、安装docker