03.Ubuntu22.04 安装 K8S v1.26.1

一、环境配置

1.K8S官网

https://kubernetes.io/

2.关闭swap分区

# 查看swap是否关闭(全是0为关闭)
free
# 临时关闭;关闭swap主要是为了性能考虑
swapoff -a
# 永久关闭
sed -ri 's/.*swap.*/#&/' /etc/fstab

3.关闭UFW服务

# 禁用并关闭 ufw 这个服务
sudo systemctl disable ufw.service && \
sudo systemctl stop ufw.service

# 卸载 ufw
sudo apt remove ufw -y

4.将桥接的ipv4流量传递到iptables的链

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

详细参数

net.ipv4.tcp_keepalive_time=600 #此参数表示TCP发送keepalive探测消息的间隔时间(秒)
net.ipv4.tcp_keepalive_intvl=30 #tcp检查间隔时间(keepalive探测包的发送间隔)
net.ipv4.tcp_keepalive_probes=10  #tcp检查次数(如果对方不予应答,探测包的发送次数)
net.ipv6.conf.all.disable_ipv6=1 #禁用IPv6,修为0为启用IPv6
net.ipv6.conf.default.disable_ipv6=1 #禁用IPv6,修为0为启用IPv6
net.ipv6.conf.lo.disable_ipv6=1 #禁用IPv6,修为0为启用IPv6
net.ipv4.neigh.default.gc_stale_time=120 #ARP缓存条目超时
net.ipv4.conf.all.rp_filter=0  #默认为1,系统会严格校验数据包的反向路径,可能导致丢包
net.ipv4.conf.default.rp_filter=0 #不开启源地址校验
net.ipv4.conf.default.arp_announce=2 #始终使用与目的IP地址对应的最佳本地IP地址作为ARP请求的源IP地址
net.ipv4.conf.lo.arp_announce=2 #始终使用与目的IP地址对应的最佳本地IP地址作为ARP请求的源IP地址
net.ipv4.conf.all.arp_announce=2 #始终使用与目的IP地址对应的最佳本地IP地址作为ARP请求的源IP地址
net.ipv4.ip_local_port_range= 45001 65000 # 定义网络连接可用作其源(本地)端口的最小和最大端口的限制,同时适用于TCP和UDP连接。
net.ipv4.ip_forward=1 # 其值为0,说明禁止进行IP转发;如果是1,则说明IP转发功能已经打开。
net.ipv4.tcp_max_tw_buckets=6000 #配置服务器 TIME_WAIT 数量
net.ipv4.tcp_syncookies=1 #此参数应该设置为1,防止SYN Flood
net.ipv4.tcp_synack_retries=2 #表示回应第二个握手包(SYN+ACK包)给客户端IP后,如果收不到第三次握手包(ACK包),进行重试的次数(默认为5)
net.bridge.bridge-nf-call-ip6tables=1 # 是否在ip6tables链中过滤IPv6包
net.bridge.bridge-nf-call-iptables=1 # 二层的网桥在转发包时也会被iptables的FORWARD规则所过滤,这样有时会出现L3层的iptables rules去过滤L2的帧的问题
net.netfilter.nf_conntrack_max=2310720 #连接跟踪表的大小,建议根据内存计算该值CONNTRACK_MAX = RAMSIZE (in bytes) / 16384 / (x / 32),并满足nf_conntrack_max=4*nf_conntrack_buckets,默认262144

net.ipv6.neigh.default.gc_thresh1=8192
net.ipv6.neigh.default.gc_thresh2=32768
net.ipv6.neigh.default.gc_thresh3=65536

#gc_thresh3 是表大小的绝对限制
#gc_thresh2 设置为等于系统的最大预期邻居条目数的值
#在这种情况下,gc_thresh3 应该设置为一个比 gc_thresh2 值高的值,例如,比 gc_thresh2 高 25%-50%,将其视为浪涌容量。
#gc_thresh1 提高到较大的值;此设置的作用是,如果表包含的条目少于 gc_thresh1,内核将永远不会删除(超时)过时的条目。

net.core.netdev_max_backlog=16384 # 每CPU网络设备积压队列长度
net.core.rmem_max = 16777216 # 所有协议类型读写的缓存区大小
net.core.wmem_max = 16777216 # 最大的TCP数据发送窗口大小
net.ipv4.tcp_max_syn_backlog = 8096 # 第一个积压队列长度
net.core.somaxconn = 32768 # 第二个积压队列长度
fs.inotify.max_user_instances=8192 # 表示每一个real user ID可创建的inotify instatnces的数量上限,默认128.
fs.inotify.max_user_watches=524288 # 同一用户同时可以添加的watch数目,默认8192。
fs.file-max=52706963 # 文件描述符的最大值
fs.nr_open=52706963 #设置最大微博号打开数
kernel.pid_max = 4194303 #最大进程数
net.bridge.bridge-nf-call-arptables=1 #是否在arptables的FORWARD中过滤网桥的ARP包
vm.swappiness=0 # 禁止使用 swap 空间,只有当系统 OOM 时才允许使用它
vm.overcommit_memory=1 # 不检查物理内存是否够用
vm.panic_on_oom=0 # 开启 OOM
vm.max_map_count = 262144

5.config.toml文件配置

SystemdCgroup = true

sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.6"

0027

# 导出默认配置,config.toml这个文件默认是不存在的
containerd config default > /etc/containerd/config.toml
grep sandbox_image  /etc/containerd/config.toml
sed -i "s#registry.k8s.io/pause#registry.aliyuncs.com/google_containers/pause#g"       /etc/containerd/config.toml
grep SystemdCgroup  /etc/containerd/config.toml
sed -i 's#SystemdCgroup = false#SystemdCgroup = true#g' /etc/containerd/config.toml

重启容器

systemctl restart containerd

6.crictl.yaml文件配置

cat <<EOF> /etc/crictl.yaml 
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
EOF


二、K8S安装

1.安装kubeadm,kubelet,kubectl

网址:链接

使得 apt 支持 ssl 传输:

sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl

下载gpg密钥:

curl -sS https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/apt-key.gpg

添加K8S镜像源:

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF

更新 apt 包索引,安装 kubelet、kubeadm 和 kubectl,并锁定其版本:

搜索可用版本

apt-cache madison kubelet

安装

sudo apt-get update
sudo apt-get install -y kubelet=1.26.1-00 kubeadm=1.26.1-00 kubectl=1.26.1-00
#锁定内核升级不升级
sudo apt-mark hold kubelet=1.26.1-00 kubeadm=1.26.1-00 kubectl=1.26.1-00

其他apt-mark命令

解除锁定

sudo apt-mark unhold [software-name]

查看锁定

sudo apt-mark showhold

2.hosts记录添加

master内网IP


masert和所有node都在hosts文件里面添加一条master的IP记录

vim /etc/hosts


3.修改profile文件

vim /etc/profile

export KUBECONFIG=/etc/kubernetes/admin.conf

4.Masert主机执行init命令

kubeadm init \
--image-repository=registry.aliyuncs.com/google_containers \
--kubernetes-version=v1.26.1 \
--pod-network-cidr=10.244.0.0/16 \
--control-plane-endpoint=cluster-endpoint \
--cri-socket=unix:///var/run/cri-dockerd.sock

--control-plane-endpoint=cluster-endpoint(master中hosts设置的)

注:init失败后解决完问题需要重新设置命令

kubeadm reset --cri-socket=unix:///var/run/cri-dockerd.sock

5.错误解决

查看日志


 failed pulling image 错误解决


下载:对应镜像,然后改名

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6 registry.k8s.io/pause:3.6

6.执行下面三条命令



7.令牌过期重新获取令牌命令

kubeadm token create --print-join-command


8.子节点加入Master主机

加入节点前最好先重名命名主机

hostnamectl set-hostname Node-01

需要加--cri-socket参数

kubeadm join cluster-endpoint:6443 --token=pdnzoz.zpjamsqsmo6b7089 \
--discovery-token-ca-cert-hash=sha256:5863e8d61107808c5ebb233caf61239b4fccce54536b2bb30ae753e944161fff \
--cri-socket=unix:///var/run/cri-dockerd.sock

加入失败重置命令

kubeadm reset --cri-socket=unix:///var/run/cri-dockerd.sock


三、Master主机添加Calico网络插件

官网:https://docs.tigera.io/calico/3.25/getting-started/kubernetes/quickstart#install-calico


1.执行第一步中的命令

kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/tigera-operator.yaml


2.下载第二步中的custom-resources.yaml

curl https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/custom-resources.yaml > custom-resources.yaml


3.修改cidr跟--pod-network-cidr=10.244.0.0/16 对应

4.执行第二步命令

kubectl create -f custom-resources.yaml


5.查看进度

kubectl get pods -n calico-system


6.失败后删除重新下载

kubectl delete  pods calico-kube-controllers-6b7b9c649d-j9dp5 -n calico-system
watch kubectl get pods -n calico-system


7.查看节点状态

kubectl get nodes



四、可视化界面dashboard安装

网址:https://github.com/kubernetes/dashboard#install

1.安装命令

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml


2.暴露端口

kubectl edit svc kubernetes-dashboard -n kubernetes-dashboard


type:ClusterIP 改为 type:NodePort

3.查看暴露的端口

kubectl get svc -A | grep kubernetes-dashboard


4.根据官网介绍创建用户和绑定角色



dashboard-adminuser.yaml内容

apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kubernetes-dashboard

---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kubernetes-dashboard
kubectl apply -f dashboard-adminuser.yaml

5.生成token

kubectl -n kubernetes-dashboard create token admin-user



五、扩展内容

1.查看安装需要的镜像

kubeadm config images list

2.批量下载镜像脚本

downImages.sh内容(images里面的内容为要下载的镜像)

#!/bin/bash
images=(
kube-apiserver:v1.26.1
kube-controller-manager:v1.26.1
kube-scheduler:v1.26.1
kube-proxy:v1.26.1
pause:3.9
etcd:3.5.6-0
coredns:v1.9.3
)
for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
done

启用脚本

vim downImages.sh
chmod +x ./downImages.sh 
./downImages.sh 

3.批量重命名镜像

docker images | grep registry.cn-hangzhou.aliyuncs.com/google_containers | sed 's/registry.cn-hangzhou.aliyuncs.com\/google_containers/registry.k8s.io/g' | awk '{print "docker tag"" " $3" "$1":"$2}'|sh

4.批量删除镜像

docker images | grep registry.cn-hangzhou.aliyuncs.com/google_containers | awk '{print "docker rmi -f " $1":"$2}' | sh



(1)