一 基于kubeadm搭建Kubernetes集群
1 所有节点需要配置的操作
1.1 关闭防火墙
# systemctl stop firewalld
# systemctl disable firewalld
1.2 关闭selinux
# sed -i 's/enforcing/disabled/' /etc/selinux/config
1.3 禁用swap
# swapoff -a
# vim /etc/fstab
# 将与swap有关的配置注释,重启系统即可
1.3 修改主机名和/etc/hosts文件
# hostnamectl set-hostname {k8s-m|k8s-node1|k8s-node2}
# vim /etc/hosts
10.120.10.190 k8s-m
10.120.10.191 k8s-node1
10.120.10.192 k8s-node2
10.120.10.193 k8s-node3
1.4 重启主机
1.5 开启路由转发
# echo "net.ipv4.ip_forward = 1" >>/etc/sysctl.conf
# sysctl -p
1.6 设置仓库为阿里云仓库
# cd /etc/yum.repos.d/
# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
1.7 配置Docker的阿里云yum安装源,并安装
# cd /etc/yum.repos.d/
# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# yum clean all
# yum makecache
1.8 在每个节点上安装docker-ce-18.06.0并设置开机自启动
# yum install docker-ce-18.06.2.ce
# docker -v
# systemctl start docker
# systemctl enable docker
# systemctl status docker
1.9 启用bridge-nf-call-iptables和bridge-nf-call-ip6tables
# echo 'net.bridge.bridge-nf-call-iptables = 1' >>/etc/sysctl.conf
# echo 'net.bridge.bridge-nf-call-ip6tables = 1' >>/etc/sysctl.conf
# sysctl -p
1.10 配置kubernetes的阿里云apt安装源
[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
1.11 在每个节点上安装kubeadm,kubelet,kubectl并设置kubelet开机自启动
# yum install kubelet-1.12.1-0 kubeadm-1.12.1-0 kubectl-1.12.1-0
# kubeadm version
# systemctl enable kubelet
2 Master节点:
2.1 导入kube-apiserver,kube-proxy等kubernetes1.12.1版本的docker镜像
2.2 初始化master
Your Kubernetes master 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
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/
You can now join any number of machines by running the following on each node
as root:
kubeadm join 10.120.10.190:6443 --token d4557c.fh4vjxkiyoj2zt5y --discovery-token-ca-cert-hash sha256:b17d4618ab1149934f69582c727f58e53cd51f89dd9844fd3f7b776a0e3d389a
2.3 Master节点配置root用户管理集群:
2.3.1 使用root用户运行kubernetes
# mkdir -p $HOME /.kube
# sudo cp -i /etc/kubernetes/admin.conf $HOME /.kube/config
2.3.2 安装flannel
# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
2.3.3 查看集群状态
3 Node节点:
3.1 导入kube-proxy,pause和flannel,kubernetes1.12.1版本的docker镜像
#
# docker load < kube-proxy-amd64.tar
# docker tag 68987659c19a k8s.gcr.io/kube-proxy:v1.12.1
# docker load < pause.tar
# docker tag 97bc1dcec76a k8s.gcr.io/pause:3.1
# docker load < flannel.tar
# docker tag f0fad859c909 quay.io/coreos/flannel:v0.10.0-amd64
3.1 加入master节点:
kubeadm join 10 .120 .10 .190 :6443 --token s5gi75.o950l49ripas5e0y --discovery-token-ca-cert-hash sha256:b17d4618ab1149934f69582c727f58e53cd51f89dd9844fd3f7b776a0e3d389a
3.1 在Master上查看集群状态
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程