【复刻版】ubuntu22.04安装kubernetes1.25 k8s1.25

1.条件

  1. 2G内存
  2. 2 CPU
  3. 网络连接(内网公网都可以)
  4. 唯一的主机名,mac地址
  5. 6443,10650等端口
  6. 禁用Swap
  7. 20 GB /var目录磁盘空间
  8. Sudo 用户权限

2.主机准备

master: ip-1 k8s-master
worker-1: ip-2 k8s-worker-1
worker-2: ip-2 k8s-worker-2

设置hostname,和/etc/hosts

master node

sudo hostnamectl --static set-hostname k8s-master

worker node

sudo hostnamectl --static set-hostname k8s-worker-1
sudo hostnamectl --static set-hostname k8s-worker-2

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 install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates

​​sudo apt-get update​​

安装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"

​​sudo apt-get upgrade​

安装containnerd:

​​sudo apt-get update​​
sudo apt install -y containerd.io

配置 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

重启和启用:

sudo systemctl restart containerd
sudo systemctl enable containerd

7.添加 k8s仓库(阿里云)

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

Note: At time of writing this guide, Xenial is the latest Kubernetes repository but when repository is available for Ubuntu 22.04 (Jammy Jellyfish) then you need replace xenial word with ‘jammy’ in ‘apt-add-repository’ command.

8.安装kubernetes组件Kubectl, kubeadm & kubelet

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

复制出来自节点worker1,worker2,
这样可以无需重复在worker机器上重复上面的步骤。

9.初始化kubernetes集群

在master上运行

sudo kubeadm init \
     --control-plane-endpoint=k8s-master \
     --kubernetes-version=v1.25.0 \
     --image-repository registry.aliyuncs.com/google_containers \
    --pod-network-cidr=10.24.0.0/16 \
    --ignore-preflight-errors=Swap

在kubeadm init后的打印信息中 找到 kubernetes的配置信息提示
master中直接执行,
worker中不能直接执行
(需要将master中的etc/kubernetes/admin.conf 复制到worker的/etc/kubernetes路径下 才能执行)

$ mkdir -p $HOME/.kube
$ cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ chown $(id -u):$(id -g) $HOME/.kube/config

在kubeadm init后的打印信息中 找到对应的 worker节点的join信息 复制出在各个worker中执行

#例子 如下
kubeadm join k8s-master:6443 --token qankob.c47c3h9otd5mo9m0   --discovery-token-ca-cert-hash sha256:9a8e7922f82452f3add16a12ebb49d8a1c51b6b778c8f5ad4979c0ecf6cf6c1c

【复刻版】ubuntu22.04安装kubernetes1.25 k8s1.25_第1张图片
join信息执行后 在master 中可以 查看k8s集群信息

kubectl cluster-info
kubectl get nodes

10.安装calico 支持 CNI (Container Network Interface)

curl https://projectcalico.docs.tigera.io/manifests/calico.yaml -O
sed -i "s#192\.168\.0\.0/16#10\.244\.0\.0/16#" calico.yaml

kubectl apply -f calico.yaml

11.安装完毕 :

#查看k8s组件pod的状态
kubectl get pods -n kube-system
#查看nodes 状态
kubectl get nodes

参考资料

https://blog.csdn.net/shelutai/article/details/126604857
https://blog.csdn.net/weixin_56752399/article/details/123217472
https://www.bilibili.com/video/BV1gV4y1p7CL?spm_id_from=333.337.search-card.all.click&vd_source=9d3f2a31f918888addf6f0d78cae7372
https://feisky.gitbooks.io/kubernetes/content/network/calico/calico.html
https://kubernetes.io/zh-cn/docs/concepts/extend-kubernetes/compute-storage-net/network-plugins/
https://kuboard.cn/learning/k8s-intermediate/service/cni.html

你可能感兴趣的:(k8s,kubernetes,docker,容器)