k8s安装笔记(ubuntu)

1.说明

主要是根据k8s的官方文档关于安装k8s的步骤来写的脚本,以达到快速安装目的。脚本测试过多次,实现多次安装,均未出现问题。但是安装最新的版本1.24.0,距离写博客的时间,不到一周,可能是这个版本太新了,在ubuntu上不能很好的兼容,导致没法安装成功。
1.条件说明:本次安装操作系统版本用的是20.04(尝试过用22.04也行),docker版本用的20.10.12,kubeadm,kubectl,kubelet版本用的1.23.6.
ubuntu操作系统安装完成后,执行命令:apt-get update && apt upgrade -y && apt install net-tools && apt install openssh-server -y&& apt install docker.io -y
2.master节点,直接执行master节点的脚本,node节点,直接执行node节点的脚本,中间有需要参与的地方,都选择yes或者ok
ps:如果脚本在运行过程中,出现中断,可以再运行一遍

2.master节点脚本

master节点初始化环境的脚本
注意事项:
1.–apiserver-advertise-address=192.168.3.71这个地址需要根据实际情况来填写,不填写应该也没问题
2.中间有选择的地方,就选yes或者ok
3.执行脚本后,会生成一个加入的集群的token命令,导致填写到node节点的脚本里面
4.目前仅是针对对于k8s 1.23.6的版本,此脚本跑1.24.0会出现无法安装成功,估计是1.24.0暂时对于ubuntu的兼容性可能出现问题。(已经在ubuntu22.04和ubuntu20.03都尝试过,安装1.24.0无法安装成功)

#!/bin/bash
echo "--------------------------------------------------------------1.close firewall---------------------------------------------------------------------"
ufw disable

echo "--------------------------------------------------------------2.close swap-------------------------------------------------------------------------"
#修改swap可以参考链接:https://blog.csdn.net/weixin_42599091/article/details/107164366
#临时关闭
swapoff -a
#永久关闭,这个需要重启生效
sed -i 's#\/swap.img#\#\/swap.img#g' /etc/fstab

echo "--------------------------------------------------------------3.allow iptables bridge flow---------------------------------------------------------"
#参考kubadm官网:https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system


echo "--------------------------------------------------------------4.modify docker cgroup---------------------------------------------------------------"
#将docker的cgroup修改为systemd的参考链接:https://www.jianshu.com/p/8a62750c0eef
sudo mkdir /etc/docker
cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF
sudo systemctl enable docker
sudo systemctl daemon-reload
sudo systemctl restart docker


echo "--------------------------------------------------------------5.add k8s source list----------------------------------------------------------------"
#参考链接https://blog.csdn.net/uucckk/article/details/105193431
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add
echo "deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main" >>  /etc/apt/sources.list


echo "-----------------------------------------------------------6.install k8s apt packages------------------------------------------------------------"
#参考kubadm官网(同步骤3):https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y apt-transport-https ca-certificates curl

echo "-----------------------------------------------------------7.install kubelet kubeadm kubectl-----------------------------------------------------"
#参考kubadm官网(同步骤3):https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
sudo apt install kubeadm=1.23.6-00
sudo apt install kubectl=1.23.6-00
sudo apt install kubelet=1.23.6-00
sudo apt-mark hold kubelet kubeadm kubectl


echo "--------------------------------------------------------------8.download k8s images----------------------------------------------------------------"
#下载k8s相关的镜像,参考链接,和步骤5的链接一样,都是:https://blog.csdn.net/uucckk/article/details/105193431
**下载镜像前,需要执行kubeadm config images list,获取镜像列表,然后填写对应的版本号,如下的版本号可以进行修改**
images=(
    kube-apiserver:v1.23.6
    kube-controller-manager:v1.23.6
    kube-scheduler:v1.23.6
    kube-proxy:v1.23.6
    pause:3.6
    etcd:3.5.1-0
    coredns:v1.8.6
)
for imageName in ${images[@]} ; do
        docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/${imageName}
        if [ $(echo $imageName | awk -F [":"] '{print $1}') != "coredns" ]
        then
          #echo  "----------0-----------"$imageName
          docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/${imageName} k8s.gcr.io/${imageName}
        else
          #echo "-----------1-----------" $imageName
          docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/${imageName} k8s.gcr.io/coredns/${imageName}
        fi
        docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/${imageName}
done
#初始化集群
kubeadm init --apiserver-advertise-address=192.168.3.71 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version=v1.23.6 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=NumCPU

echo "--------------------------------------------------------------9.deploy flannel----------------------------------------------------------------"
#参考文章https://blog.csdn.net/weixin_43656190/article/details/117604612
#先将域名解析写到hosts文件里
echo 52.74.223.119 github.com >> /etc/hosts
echo 192.30.253.119 gist.github.com >> /etc/hosts
echo 54.169.195.247 api.github.com >> /etc/hosts
echo 185.199.111.153 assets-cdn.github.com >> /etc/hosts
echo 151.101.64.133 raw.githubusercontent.com >> /etc/hosts
echo 151.101.108.133 user-images.githubusercontent.com >> /etc/hosts
echo 151.101.76.133 gist.githubusercontent.com >> /etc/hosts
echo 151.101.76.133 cloud.githubusercontent.com >> /etc/hosts
echo 151.101.76.133 camo.githubusercontent.com >> /etc/hosts
echo 151.101.76.133 avatars0.githubusercontent.com >> /etc/hosts
echo 151.101.76.133 avatars1.githubusercontent.com >> /etc/hosts
echo 151.101.76.133 avatars2.githubusercontent.com >> /etc/hosts
echo 151.101.76.133 avatars3.githubusercontent.com >> /etc/hosts
echo 151.101.76.133 avatars4.githubusercontent.com >> /etc/hosts
echo 151.101.76.133 avatars5.githubusercontent.com >> /etc/hosts
echo 151.101.76.133 avatars6.githubusercontent.com >> /etc/hosts
echo 151.101.76.133 avatars7.githubusercontent.com >> /etc/hosts
echo 151.101.76.133 avatars8.githubusercontent.com >> /etc/hosts
wget --no-check-certificate https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml

3.node节点脚本

node节点初始化环境的脚本
注意事项:
1.中间有选择的地方,就选yes或者ok
2.对于master节点的脚本生成token命令,填写到node节点对应的位置

#!/bin/bash
echo "--------------------------------------------------------------1.close firewall---------------------------------------------------------------------"
ufw disable

echo "--------------------------------------------------------------2.close swap-------------------------------------------------------------------------"
#修改swap可以参考链接:https://blog.csdn.net/weixin_42599091/article/details/107164366
#临时关闭
swapoff -a
#永久关闭,这个需要重启生效
sed -i 's#\/swap.img#\#\/swap.img#g' /etc/fstab

echo "--------------------------------------------------------------3.allow iptables bridge flow---------------------------------------------------------"
#参考kubadm官网:https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system


echo "--------------------------------------------------------------4.modify docker cgroup---------------------------------------------------------------"
#将docker的cgroup修改为systemd的参考链接:https://www.jianshu.com/p/8a62750c0eef
sudo mkdir /etc/docker
cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF
sudo systemctl enable docker
sudo systemctl daemon-reload
sudo systemctl restart docker


echo "--------------------------------------------------------------5.add k8s source list----------------------------------------------------------------"
#参考链接https://blog.csdn.net/uucckk/article/details/105193431
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add
echo "deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main" >>  /etc/apt/sources.list


echo "-----------------------------------------------------------6.install k8s apt packages------------------------------------------------------------"
#参考kubadm官网(同步骤3):https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y apt-transport-https ca-certificates curl

sudo apt install kubeadm=1.23.6-00
sudo apt install kubectl=1.23.6-00
sudo apt install kubelet=1.23.6-00
sudo apt-mark hold kubelet kubeadm kubectl
kubeadm join 192.168.3.71:6443 --token e9llsa.clsbfv8l0ckemn82 \
        --discovery-token-ca-cert-hash sha256:442a9fb5bf80fc475c84a70520e8406be28d9fc048a1defb519350e1a7ff3006

4.参考资料和说明

1.安装ubuntu系统,对系统进行apt-get update && apt upgrade -y && apt install net-tools && apt install openssh-server -y&& apt install docker.io -y
2.关闭swapoff
https://blog.csdn.net/weixin_42599091/article/details/107164366
3.关闭防火墙
4.关于source源和apt的key问题
https://blog.csdn.net/uucckk/article/details/105193431

5.将docker的cgroup的cgroupfs修改成systemd
https://www.jianshu.com/p/8a62750c0eef

6.这篇文章的下载k8s的相关组件的地址
https://blog.csdn.net/uucckk/article/details/105193431
7.关于忽略cpu个数
https://blog.csdn.net/uucckk/article/details/105193431
8.flannel无法的yaml文件无法下载问题
https://blog.csdn.net/weixin_43656190/article/details/117604612
9.假如忘记join命令,可以参考下面的链接
https://www.csdn.net/tags/MtTaEg0sNjE4NDg1LWJsb2cO0O0O.html
10.设置免密码登录
https://www.cnblogs.com/huxinga/p/6418790.html
11.kubectl命令补全
https://blog.csdn.net/weixin_45552105/article/details/118111521
12.关于k8s的网络cni的理解
https://zhuanlan.zhihu.com/p/450140876
13.configmap的使用
https://blog.csdn.net/skh2015java/article/details/109228836
14.K8s之Ingress-nginx原理及配置,感觉写很好,清楚
https://blog.51cto.com/u_14306186/2523096

15.关于cgroup和namespace的问题
https://blog.csdn.net/qq_43337384/article/details/105921675
systemd与cgroupfs
https://www.jianshu.com/p/8a62750c0eef

16.将列出所有来源的版本。如下输出所示:
apt-cache madison <>

17.关于无法成功初始化
https://blog.csdn.net/grown_Liu/article/details/121619130

你可能感兴趣的:(Linux,k8s,ubuntu,docker,kubernetes)