使用kubeadm安装k8s 1.26版本
环境及版本信息
- kubernetes: 1.26.0
- 容器运行时:containerd 1.6.19
- 虚拟机系统:centos 7.9
- 虚拟机内核版本:5.4.238-1.el7.elrepo.x86_64
在所有节点进行服务器设置
- 设置主机名
# master:
hostnamectl set-hostname kube-master
# node1:
hostnamectl set-hostname kube-node1
# node2:
hostnamectl set-hostname kube-node2
- 同步时间
yum -y install ntp
systemctl start ntpd
systemctl enable ntpd
echo '* */6 * * * /usr/sbin/ntpdate time.windows.com > /dev/null 2>&1' >> /var/spool/cron/root
- 设置hosts
cat >> /etc/hosts <<EOF
192.168.76.130 kube-master
192.168.76.131 kube-node1
192.168.76.132 kube-node2
EOF
- 关闭firewalld, selinux, swap
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
- 启用ip_vs模块
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack
modprobe overlay
modprobe br_netfilter
cat > /etc/modules-load.d/ip_vs.conf << EOF
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack
overlay
br_netfilter
EOF
- 调整内核参数
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
在所有节点安装containerd
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install containerd -y
containerd config default > /etc/containerd/config.toml
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
sed -i 's#sandbox_image =.*#sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"#' /etc/containerd/config.toml
sed -i '/.*plugins."io.containerd.grpc.v1.cri".registry.mirrors.*/ a\ [plugins."io.containerd.grpc.v1.cri".registry.mirrors."registry.k8s.io"]\n endpoint = ["https://registry.aliyuncs.com/google_containers"]\n [plugins."io.containerd.grpc.v1.cri".registry.mirrors."k8s.gcr.io"]\n endpoint = ["https://registry.aliyuncs.com/google_containers"]\n [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]\n endpoint = ["https://usydjf4t.mirror.aliyuncs.com"]' /etc/containerd/config.toml
systemctl start containerd
systemctl enable containerd
systemctl status containerd
master节点操作
- 安装 kubeadm、kubelet 和 kubectl
cat <<EOF > /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
EOF
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable kubelet && systemctl start kubelet
# kubelet 现在每隔几秒就会重启,因为它陷入了一个等待 kubeadm 指令的死循环。
- 配置kubectl命令补全
yum install bash-completion -y
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
- 预下载
[root@kube-master ~]# kubeadm config images pull --image-repository=registry.aliyuncs.com/google_containers
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-apiserver:v1.26.0
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-controller-manager:v1.26.0
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-scheduler:v1.26.0
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-proxy:v1.26.0
[config/images] Pulled registry.aliyuncs.com/google_containers/pause:3.9
[config/images] Pulled registry.aliyuncs.com/google_containers/etcd:3.5.6-0
[config/images] Pulled registry.aliyuncs.com/google_containers/coredns:v1.9.3
kubeadm init --pod-network-cidr=10.244.0.0/16 --image-repository=registry.aliyuncs.com/google_containers
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get pods -n kube-system
- 安装网络组件,flannel和calico二选一
-
flannel (参考:https://kubernetes.io/zh-cn/docs/concepts/cluster-administration/addons/#networking-and-network-policy)
- 特点:安装速度快,网络性能一般,适合小规模k8s集群。
- 安装
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml kubectl get pods -n kube-flannel
-
calico (参考:https://docs.tigera.io/calico/latest/getting-started/kubernetes/quickstart)
- 特点:适合大规模集群,网络性能好,安装较慢(30min)
- 安装
# 1. download 2 yaml files wget https://raw.githubusercontent.com/projectcalico/calico/v3.27.2/manifests/tigera-operator.yaml -O tigera-operator.yaml wget https://raw.githubusercontent.com/projectcalico/calico/v3.27.2/manifests/custom-resources.yaml -O custom-resources.yaml # 2. you may need to change the default IP pool CIDR to match your pod network CIDR. sed -i 's#cidr: .*#cidr: 10.244.0.0/16#g' calico-custom-resources.yaml # 3. Due to the large size of the CRD bundle, kubectl apply might exceed request limits. Instead, use kubectl create or kubectl replace. kubectl create -f calico-tigera-operator.yaml kubectl create -f calico-custom-resources.yaml # 4. keep watching pods in ns calico-system, till all pods ready watch kubectl get pods -n calico-system 5. check all pods in others namespace, especially coredns kubectl get pods -A
-
node节点操作
- 确保containerd已安装并启动
- 安装kubeadm、kubelet
cat <<EOF > /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
EOF
yum install -y kubelet kubeadm --disableexcludes=kubernetes
systemctl enable kubelet && systemctl start kubelet
- 将node加入集群
kubeadm join 192.168.76.130:6443 --token f0ey0v.mb4t62ldjj74pp9s \
--discovery-token-ca-cert-hash sha256:85004aa7ab8ba53992df1a228a45ed166f50ef0b355f2835c004a714acb4e991
测试k8s集群
[root@kube-master ~]# cat pod-myapp.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: myapp
name: myapp
spec:
containers:
- image: nginx
name: myapp
ports:
- containerPort: 80
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
[root@kube-master ~]# kubectl apply -f pod-myapp.yaml
pod/myapp created
[root@kube-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp 0/1 ContainerCreating 0 8s
[root@kube-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp 1/1 Running 0 53s
[root@kube-master ~]# kubectl exec -it pods/myapp -- curl localhost/a
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)