第四章 Ubuntu基于kubeadm安装Kubernetes

kubernetes1.25部署在ubuntu22.04上

参考链接

1、主机准备

为各主机设置静态IP,":w !sudo tee %"当忘记输入sudo可以帮助你将内容保存

#vim /etc/netplan/00-installer-config.yaml
network:
  ethernets:
    ens33:
      dhcp4: false
      dhcp6: false
      addresses:
        - 192.168.1.5/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses: [192.168.1.1]
  version: 2

使配置生效

# sudo netplan apply

2、配置主机hostname和hosts表

设置hostname

# sudo hostnamectl set-hostname "k8smaster.example.net"
# exec bash

设置host表

# vim /etc/hosts
192.168.1.5 k8smaster.example.net
192.168.1.8 k8sworker1.example.net
192.168.1.7 k8sworker2.example.net

3、升级系统

sudo apt update
sudo apt -y full-upgrade
[ -f /var/run/reboot-required ] && sudo reboot -f

4、禁用swap

master和所有worker都需要

sudo swapoff -a
sudo sed -i '/swap/ s/^\(.*\)$/#\1/g' /etc/fstab
# -i 插入修改模式,g 标识符表示全局查找替换,表示注释掉swap的那一行。
free -h

5、打开一些内核功能: [主机:master 和所有worker]

# Enable kernel modules
sudo tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter



# Add some settings to sysctl
sudo tee /etc/sysctl.d/kubernetes.conf<<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

# 重新加载 sysctl
sudo sysctl --system

6、安装 containerd run time

[主机:master 和所有worker]

自 Kubernetes v1.24 起,Dockershim 已被删除

配置ubuntu系统国内源:

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak​

vi /etc/apt/sources.list 替换成如下:

deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

sudo apt-get update
sudo apt-get upgrade

7、安装关联软件

sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates

8、安装docker仓库,使用阿里云的仓库

#  安装GPG证书
sudo curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

9、安装containnerd

sudo apt update sudo apt install -y containerd.io

10、配置 containerd 用systemdcgroup启动.

containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
sudo sed -i 's/k8s.gcr.io/registry.aliyuncs.com\/google_containers/g' /etc/containerd/config.toml

11、重启和启用containerd

sudo systemctl restart containerd
sudo systemctl enable containerd
sudo systemctl is-active containerd
activ

12、使用阿里云添加apt repository for Kubernetes

curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add - 
sudo apt-add-repository "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main"

apt-get update

13、安装kubernetes组件Kubectl, kubeadm & kubelet

sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
#apt-mark 用于将软件包标记/取消标记为自动安装。 hold 选项用于将软件包标记为保留,以防止软件包被自动安装、升级或删除。

14、复制出来自节点worker1,worker2

这样可以无需重复在worker机器上重复上面的步骤。

初始化kubernetes集群: 在master上运行

sudo kubeadm init --control-plane-endpoint=k8smaster.example.net \
   --image-repository registry.aliyuncs.com/google_containers \
   --kubernetes-version v1.25.0

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

posted @ 2022-11-20 10:44  摩天居士-谢烟客  阅读(183)  评论(0编辑  收藏  举报