一、系统环境准备(所有节点)
1.关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
2.关闭 selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
setenforce 0 # 临时
3.关闭 swap
swapoff -a # 临时
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
4.修改主机名
hostnamectl set-hostname <hostname>
5.修改hosts
cat >> /etc/hosts << EOF
192.168.133.61 k8s-master
192.168.133.62 k8s-node1
192.168.133.63 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 -y ntpdate
timedatectl set-timezone Asia/Shanghai
二、安装 Docker/kubeadm/kubelet(所有节点)
1.安装docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum-config-manager --enable docker-ce-edge
yum install -y docker-ce-19.03.9-3.el7 docker-ce-cli-19.03.9-3.el7 containerd.io docker-compose
systemctl enable docker
systemctl start docker
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl daemon-reload
systemctl restart docker
2.安装kubeadm和kubelet
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 install -y kubeadm-1.19.4 kubelet-1.19.4
systemctl enable kubelet
#在主节点上(ip自行修改)
kubeadm init --kubernetes-version=1.19.4 --apiserver-advertise-address=192.168.133.61 --image-repository registry.aliyuncs.com/google_containers --service-cidr=10.64.0.0/24 --pod-network-cidr=10.244.0.0/16
#每个节点使用下面命令,否则kubectl命令无法使用
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
#查看node信息
kubectl get nodes
3.加入Kubernetes Node
#在从节点上执行上面kubeadm init生成的命令,添加到Kubernetes Node
kubeadm join 192.168.133.61:6443 --token xxxxx \
--discovery-token-ca-cert-hash xxxxxx
#默认token有效期为24小时,当过期之后,该token就不可用了。这时就需要重新创建token,操作如下
kubeadm token create --print-join-command
4.安装 Pod 网络插件( CNI)
#安装 Pod 网络插件(CNI),在主节点上
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
#查看pods信息
kubectl get pods -n kube-system
5.测试 kubernetes 集群
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
6.重置kubernetes集群
如果觉得版本或者是配置有问题,可以重置集群,在每个节点都执行下面命令。
kubeadm reset
rm -rf .kube/*
7.卸载kubernetes集群
每个节点上使用命令即可卸载。
kubeadm reset -f
modprobe -r ipip
lsmod
rm -rf ~/.kube/
rm -rf /etc/kubernetes/
rm -rf /etc/systemd/system/kubelet.service.d
rm -rf /etc/systemd/system/kubelet.service
rm -rf /usr/bin/kube*
rm -rf /etc/cni
rm -rf /opt/cni
rm -rf /var/lib/etcd
rm -rf /var/etcd
yum clean all
yum remove kube*