一、环境
主机 | 用途 |
---|---|
192.168.76.140 | k8s-master1 |
192.168.76.141 | k8s-node1 |
二、设置yum源
由于系统已经关闭,可以用centos9尝试
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
vi /etc/yum.repos.d/CentOS-Base.repo# 使用阿里云的yum源
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7sudo yum clean all
sudo yum makecache
yum install wget
三、系统操作
1.升级内核
wget https://linux.cc.iitk.ac.in/mirror/centos/elrepo/kernel/el7/x86_64/RPMS/kernel-lt-5.4.278-1.el7.elrepo.x86_64.rpm
yum localinstall kernel-lt-5.4.278-1.el7.elrepo.x86_64.rpmawk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
grub2-set-default 0 #根据自己实际情况定
2.关闭swap,设置网络等
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
swapoff -a ##交换分区必须要关闭
sed -ri 's/.*swap.*/#&/' /etc/fstab ##永久关闭swap分区,&符号在sed命令中代表上次匹配的结果
for i in $(ls /usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done ##加载模块cat >> /etc/sysctl.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness = 0
EOFsysctl --system
四、安装docker 和 k8s
sudo yum remove docker*
sudo yum install -y yum-utils
#配置docker的yum地址
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#安装指定版本
sudo yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io-1.4.6
#启动&开机启动docker
systemctl enable docker --now
# docker加速配置
sudo mkdir -p /etc/docker
cat >> /etc/docker/daemon.json << EOF
{"registry-mirrors" : [ "https:\/\/jockerhub.com" ]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart dockercat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
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-1.20.11 kubeadm-1.20.11 kubectl-1.20.11
systemctl enable kubelet.service
kubeadm config images list
ip addr
cat /etc/hosts
echo "192.168.76.140 k8s-master1" >> /etc/hosts
echo "192.168.76.141 k8s-node1" >> /etc/hosts
五、复制机器
修改IP
修改hostname
hostnamectl set-hostname k8s-node1
reboot
七、master运行
kubeadm init \
--apiserver-advertise-address=192.168.76.140 #本机ip \
--control-plane-endpoint=k8s-master1 #控制平面主机 \
--image-repository registry.aliyuncs.com/google_containers #镜像地址 \
--kubernetes-version v1.20.11 #版本 \
--service-cidr=10.96.0.0/16 #集群内部虚拟网络 \
--pod-network-cidr=10.0.0.0/16 #pod网络,注意不要和自己的ip重了
mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/configexport KUBECONFIG=/etc/kubernetes/admin.conf
八、node运行
kubeadm join k8s-master1:6443 --token 7olu40.s70rr3hwfp2ewubc \--discovery-token-ca-cert-hash sha256:3fbcf8208047b94499b34fce241ce4dffff075763e7c2dffde1765b3d3d0616c
主节点运行
九、安装calico
在k8s github上可以找到对应版本的calico,其他东西都可以在里面找
wget https://calico-v3-15.netlify.app/archive/v3.15/manifests/calico.yaml
kubectl apply -f calico.yaml
最后
vi /etc/kubernetes/manifests/kube-controller-manager.yaml
vi /etc/kubernetes/manifests/kube-scheduler.yaml
注释port = 0
到这里就搭建完成了,后面可以搭建dashboard
十、测试
vi nginx-deployment.yamlapiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deployment
spec:selector:matchLabels:app: nginxreplicas: 1 # 指定Pod副本数template:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:latest # 使用最新的Nginx镜像ports:- containerPort: 80 # Nginx监听的端口vi nginx-service.yaml
apiVersion: v1
kind: Service
metadata:name: nginx-service
spec:selector:app: nginxports:- protocol: TCPport: 80targetPort: 80nodePort: 30080type: NodePort # 外部访问的类型,指定主机端口kubectl apply -f nginx-deployment.yaml
kubectl apply -f nginx-service.yaml
访问