swapoff -a #临时
sed -ri 's/.*swap.*/#&/' /etc/fstab #永久
free -g #验证,swap 必须为 0;
vim /etc/hosts
192.168.10.27 master
192.168.10.28 node01
192.168.10.29 node02
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
cd /etc/yum.repos.d/
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
systemctl start docker
systemctl enable docker
cat > /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 list|grep kube
yum install -y kubelet-1.21.3 kubeadm-1.21.3 kubectl-1.21.3
systemctl enable kubelet
systemctl start kubelet
kubeadm init \
--apiserver-advertise-address=192.168.10.27 \
--image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \
--kubernetes-version v1.21.3 \
--service-cidr=10.125.0.0/16 \
--pod-network-cidr=10.150.0.0/16
#如有问题:
【用这个】
kubeadm init --apiserver-advertise-address=192.168.10.27 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers --kubernetes-version v1.21.3 --service-cidr=10.125.0.0/16 --pod-network-cidr=10.150.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
##镜像批量导出,方便以后使用docker save docker images | grep -v TAG | awk '{print $1":"$2}'
-o name.tar.gz
kubeadm join 192.168.10.27:6443 --token d5vdqp.v3061iapuu2qyw03 \
--discovery-token-ca-cert-hash sha256:a873822aa2acddfdb6d39f4983fff83e695f66f3e37cd5bc3cc09d21765f69ca
#若token 过期或丢失,需要先申请新的token 令牌【令牌过期时间是:24小时,等过了24小时之后,令牌失效,使用下面这个命令进行刷新重新生成令牌!】
kubeadm token create
#列出token
kubeadm token list | awk -F" " '{print $1}' |tail -n 1
#然后获取CA公钥的的hash值
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^ .* //'
#替换join中token及sha256:
kubeadm join 192.168.10.27:6443 --token zwl2z0.arz2wvtrk8yptkyz \
--discovery-token-ca-cert-hash sha256:e211bc7af55310303fbc7126a1bc7289f16b046f8798008b68ee01051361cf02
kubectl apply -f \
https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
[root@master ~]# kubectl label node node01 node-role.kubernetes.io/node=node
node/node01 labeled
[root@master ~]# kubectl label node node02 node-role.kubernetes.io/node=node
node/node02 labeled
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane,master 7m11s v1.21.3
node01 Ready node 2m32s v1.21.3
node02 Ready node 2m32s v1.21.3
//如果 kubectl get cs 发现集群不健康,更改以下两个文件
vim /etc/kubernetes/manifests/kube-scheduler.yaml
vim /etc/kubernetes/manifests/kube-controller-manager.yaml
# 修改如下内容
把--bind-address=127.0.0.1变成--bind-address=192.168.10.27 #修改成k8s的控制节点master01的ip
把httpGet:字段下的hosts由127.0.0.1变成192.168.10.27(有两处)
#- --port=0 # 搜索port=0,把这一行注释掉
systemctl restart kubelet