Centos7 yum安装k8s 1.23.17

本次部署有3个节点,一个master,2个node。其中maser是192.168.18.11,node分别是192.168.18.12、192.168.18.13。

 

一、在master:192.168.18.11上

1、前提条件安装docker,并修改/etc/docker/daemon.json

{
  "registry-mirrors": ["https://sfpt7y1n.mirror.aliyuncs.com","https://docker-cf.registry.cyou","https://hub.xdark.top", "https://hub.littlediary.cn", "https://dockerpull.org", "https://hub.crdz.gq", "https://docker.1panel.live", "https://docker.unsee.tech"],
  "insecure-registries": [
    "http://192.168.18.11:88"
  ],
  "exec-opts": ["native.cgroupdriver=systemd"]
}

 

2、关闭selinux

setenforce 0
sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

3、配置内核参数

vim /etc/sysctl.d/k8s.conf

net.ipv4.ip_forward=1 # 其值为0,说明禁止进行IP转发;如果是1,则说明IP转发功能已经打开。 net.bridge.bridge
-nf-call-ip6tables = 1 # 是否在ip6tables链中过滤IPv6包   net.bridge.bridge-nf-call-iptables = 1     # 二层的网桥在转发包时也会被iptables的FORWARD规则所过滤,这样有时会出现L3层的iptables rules去过滤L2的帧的问题
sysctl -p # 重新加载配置(立即生效)

 

4、修改hostname,hosts文件。修改主机名

192.168.18.11 tjgjc-ai11
192.168.18.12 tjgjc-ai12
192.168.18.13 tjgjc-ai13
hostnamectl set-hostname tjgjc-ai11

5、禁用交换分区,否则会在k8s初始化时报错,[kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp [::1]:10248: connect: connection refused.

# 禁用交换分区
swapoff -a
# 永久禁用,打开/etc/fstab注释掉swap那一行。
sed -i 's/.*swap.*/#&/' /etc/fstab

 

6、配置k8s阿里云源

vim /etc/yum.repos.d/kubernetes.repo

[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

7、安装kubeadm、kubectl、kubelet

sudo yum install -y kubelet-1.23.17 kubeadm-1.23.17 kubectl-1.23.17 --disableexcludes=kubernetes

关键选项说明

--disableexcludes=kubernetes

作用
在 RHEL/CentOS 系统中,默认的 yum 配置可能会排除 Kubernetes 相关包的更新(通过 exclude=kube* 配置)。此选项强制 yum 忽略这些排除规则,确保 Kubernetes 组件能正常安装。

何时需要
当直接运行 yum install kubelet 出现类似 No package kubelet available 错误时,说明系统存在包排除策略,必须添加此选项。

 

8、设置kubelet服务开机启动

systemctl enable kubelet 

9、初始化k8s,这条命令执行时会卡在[preflight] You can also perform this action in beforehand using ''kubeadm config images pull,大概需要2分钟,请耐心等待。

提示:如果之前安装过k8s需要执行kubeadm reset

 

kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.23.17 --apiserver-advertise-address 192.168.18.11 --pod-network-cidr=10.244.0.0/16 --token-ttl 0 
  --image-repository registry.aliyuncs.com/google_containers \  # 指定镜像仓库
  --kubernetes-version v1.23.17 \                             # 指定 K8s 版本
  --apiserver-advertise-address 192.168.18.11 \               # 指定 API Server 通告地址
  --pod-network-cidr=10.244.0.0/16 \                         # 指定 Pod 网络 CIDR
  --token-ttl 0                                              # 设置 token 永不过期

 

 

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

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

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.18.11:6443 --token xybjgk.dnutwxyfpdxnratp \
    --discovery-token-ca-cert-hash sha256:21d14e3ebe35ca81caef9653270c5c5a76d841ed4ec18393b3fbdc4fc1deff09

 

-- 若初始化失败或者重新初始化,清理命令如下
kubeadm reset -f 
rm -rf /etc/cni/net.d
ipvsadm --clear
rm -rf ~/.kube

10、看到上面,初始化成功了,上面kubeadm init执行成功后会返回给你node节点加入集群的命令,等会要在node节点上执行,需要保存下来,如果忘记了,可以使用如下命令获取。

kubeadm token create --print-join-command

 11、根据提示:To start using your cluster, you need to run the following as a regular user:。运行以下命令

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

 12、配置免密拉取Docker Harbor镜像,所有节点都要登录过Harbor。

kubectl create secret docker-registry harbor-secret(别名) --namespace=项目所在命名空间 --docker-server=Harbor地址 --docker-username=账户 --docker-password=Harbor密码

 

二、在node节点192.168.18.12、192.168.18.13上

1、前提条件安装docker

2、做maser的2~8步骤的操作

3、加入集群,运行在master初始化k8s时获得的加入集群的命令

kubeadm join 192.168.18.11:6443 --token xybjgk.dnutwxyfpdxnratp \
    --discovery-token-ca-cert-hash sha256:21d14e3ebe35ca81caef9653270c5c5a76d841ed4ec18393b3fbdc4fc1deff09

 

-- 若初始化失败或者重新初始化,清理命令如下
kubeadm reset -f 
rm -rf /etc/cni/net.d
ipvsadm --clear
rm -rf ~/.kube

三、在master上安装flannel网络插件

1、从github找到flannel配置文件,注意配置文件中的"Network"字段要和初始化k8s命令的参数--pod-network-cidr=10.244.0.0/16指定的网段一致。

wget https://github.com/flannel-io/flannel/releases/download/v0.26.2/kube-flannel.yml

 

kubectl apply -f kube-flannel.yml

2、查看k8s集群的状态,Ready说明成功了

 

  

 

 

 

posted @ 2023-04-21 16:02  苦逼yw  阅读(417)  评论(0)    收藏  举报