一、初始化服务器
1、关闭防防火墙
systemctl stop firewalld
systemctl disable firewalld
2、关闭setlinux
sed -i 's/enforcing/disabled/' /etc/selinux/config #永久关闭
setenforce 0 #临时
3、设置主机名
hostnamectl set-hostname
bash #让设置立即生效
master节点修该文件hosts:
cat >> /etc/hostname << EOF
192.168.159.200 k8smaster01
192.168.159.199 k8snode01
192.168.159.198 k8snode02
EOF
4、关闭swap
sed -ri 's/.*swap.*/#&/' /etc/fstab #永久关闭 注意*swap.*
swapoff -a #临时
5、将桥接的ipv4流量传递到iptable的
cat >> /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
执行生效
sysctl --system #生效
6、时间同步
yum install ntpdate -y
ntpdate time.windows.com
二、安装docker
1、wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-18.06.1.ce-3.el7
systemctl enable docker && systemctl start docker
docker --version
以上操作找不到docker版本,改操作如下:
yum update
yum list installed | grep docker #列出安装过的docker包
#卸载旧版本 旧版名称是docker , 最新社区版 docker-engine, 目前已改名为docker-ce
yum -y remove docker docker-common docker-selinux docker-engine
yum install -y yum-utils device-mapper-persistent-data lvm2 #设置Yum源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #使用阿里源访问
yum list docker-ce --showduplicates | sort -r #查看所有仓库中docker版本
#有文件写入,无文件新建
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
#添加kubernetes yum 源
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 docker-ce (这样写默认安装最新版本)
yum install docker-ce-
例: yum install docker-ce-18.03.1.ce
systemctl start docker 启动
systemctl restart docker 重启
systemctl enable docker 开机启动
docker run hello-world 验证是否安装成功
三、安装kubeadm/kubelet/kubectl
#指定版本安装
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
systemctl enable kubelet
注意:kubernetes 1.24+版本之后,docker必须要加装cir-docker
问题:Kubeadm初始化主节点遇到的坑:[ERROR CRI]: container runtime is not running:
解决:rm -rf /etc/containerd/config.toml
#执行 这个不生效,移到临时目录执行生效,暂时不理解: mv /etc/containerd/config.toml /tmp/
systemctl restart containerd
#执行systemctl restart containerd报错:
Failed to restart containerd.service: Unit not found [closed]无法重新启动 containerd.service:未找到单元 [关闭]
解决:
1.、从官方 Docker 存储库安装 containerd.io 包:
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y containerd.io
2、配置
containerdsudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml
3、重启 containerd
systemctl restart containerd
四、安装kubernetes master
kubeadm init --apiserver-advertise-address=192.168.159.200 --image-repository registry.aliyuncs.com/google_containers --ignore-preflight-errors=Swap --kubernetes-version v1.18.0 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
#初始化 kubeadm DNS镜像可能拉取失败,要单独拉取:
docker pull coredns/coredns:1.8.0
docker tag coredns/coredns:1.8.0 registry.aliyuncs.com/google_containers/coredns:v1.8.0
#安装完成后会输出如下操作指引,在master执行:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
#查看nodes节点
kubectl get nodes
#所有节点执行:
kubeadm join 192.168.159.200:6443 --token mtue5d.zjywgfag2clb7nzi \
--discovery-token-ca-cert-hash sha256:f180c1869ce1948d298062a938d0e1ef83dbda47605f0c3aef152b1352f0dc8e
五、安装CNI或calico网络插件
1、wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kubeflannel.yml #下载kubeflannel.yml文件
kubectl apply -f kube-flannel.yml
#查看k8s启动状态
kubectl get pods -n kube-system
2、kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
报错的话用这个:curl https://docs.projectcalico.org/v3.20/manifests/calico.yaml -O
六、测试k8s集群
#创建一个pod,验证是否正常运行
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
访问地址:http:NodeIP:Port
卸载docker:
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
卸载k8s:
yum remove -y kubelet kubeadm kubectl
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