在三台机器上都执行如下命令操作:
1、关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
2、关闭 selinux
sed -i 's/enforceing/disabled/' /etc/selinux/config # 永久关闭
setenforce 0 # 临时关闭
3、关闭 swap
swapoff -a # 临时关闭
vim /etc/fstab # 永久关闭
#注释掉swap这行
# /dev/mapper/centos‐swap swap swap defaults 0 0
(sed -ri 's/.*swap.*/#&/' /etc/fstab)
systemctl reboot #重启生效 可以不用重启
free ‐m #查看下swap交换区是否都为0,如果都为0则swap关闭成功
4、给三台机器分别设置主机名
hostnamectl set-hostname <hostname>
第一台:k8s‐master
第二台:k8s‐node1
第三台:k8s‐node2
5、在 k8s‐master机器添加hosts,执行如下命令,ip需要修改成你自己机器的ip
cat >> /etc/hosts << EOF
192.168.1.55 k8s‐master
192.168.1.50 k8s‐node1
192.168.1.60 k8s‐node2
EOF
6、将桥接的IPv4流量传递到iptables
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge‐nf‐call‐ip6tables = 1
net.bridge.bridge‐nf‐call‐iptables = 1
EOF
sysctl --system # 生效
7、设置时间同步
yum install ntpdate ‐y
ntpdate time.windows.com
8、添加k8s yum源
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF
配置docker yum源
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装docker 和k8s的版本对应
yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io-1.4.6
设置启动:systemctl enable docker --now
设置加速器:sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://8gh555t2.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
9、如果之前安装过k8s,先卸载旧版本
yum remove ‐y kubelet kubeadm kubectl
10、查看可以安装的版本
yum list kubelet --showduplicates | sort -r
11、安装kubelet、kubeadm、kubectl 指定版本,我们使用kubeadm方式安装k8s集群
sudo yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9 --disableexcludes=kubernetes
12、开机启动kubelet
sudo systemctl enable --now kubelet
systemctl enable kubelet
systemctl start kubelet
下载各个机器需要的镜像
sudo tee ./images.sh <<-'EOF'
#!/bin/bash
images=(
kube-apiserver:v1.20.9
kube-proxy:v1.20.9
kube-controller-manager:v1.20.9
kube-scheduler:v1.20.9
coredns:1.7.0
etcd:3.4.13-0
pause:3.2
)
for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName
done
EOF
chmod +x ./images.sh && ./images.sh
所有机器添加master域名映射,以下需要修改为自己的
echo "192.168.1.55 cluster-endpoint" >> /etc/hosts
主节点初始化
kubeadm init \
--apiserver-advertise-address=192.168.1.55 \
--control-plane-endpoint=cluster-endpoint \
--image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \
--kubernetes-version v1.20.9 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=192.166.0.0/16
拷贝安装成功后的信息
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/
You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:
kubeadm join cluster-endpoint:6443 --token v356cn.uooob6wel6064x16 \
--discovery-token-ca-cert-hash sha256:e887b8fb668a3bd5e548fb0c5c9f9ec73556e180c9989862b7c2df501ecb6227 \
--control-plane
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join cluster-endpoint:6443 --token v356cn.uooob6wel6064x16 \
--discovery-token-ca-cert-hash sha256:e887b8fb668a3bd5e548fb0c5c9f9ec73556e180c9989862b7c2df501ecb6227
如何是root用户需要执行否则 命令kubectl get pods 会报:The connection to the server localhost:8080 was refused - did you specify the right host or port?
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
或者创建非root用户,执行相应的
然后 #查看集群所有节点
kubectl get nodes
运行中的应用在docker里面叫容器,在k8s里面叫Pod
kubectl get pods -A
安装网络组件
curl https://docs.projectcalico.org/v3.20/manifests/calico.yaml -O
kubectl apply -f calico.yaml
加入Node节点:
kubeadm join cluster-endpoint:6443 --token v356cn.uooob6wel6064x16 \
--discovery-token-ca-cert-hash sha256:e887b8fb668a3bd5e548fb0c5c9f9ec73556e180c9989862b7c2df501ecb6227
● 验证集群节点状态
○ kubectl get nodes