1.检查centos发行版
查看centos版本
cat /etc/redhad-release
如果不是7.6,7.7,7.8直接升级centos
yum update -y kernel
rpm -q kernel
yum update
2.安装kubernetes准备工作
- 安装docker
# 添加repo
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
#安装
sudo yum update -y
sudo yum install docker-ce docker-ce-cli containerd.io -y
修改/etc/docker/daemon.json ---- 先配置好国内镜像,不然后面加入节点image pull不下来
{
"debug": true,
"experimental": false,
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"https://registry.docker-cn.com"
]
}
添加到开机启动
sudo systemctl start docker
sudo systemctl enable docker
- 安装kubernetes
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
#将 SELinux 设置为 permissive 模式(相当于将其禁用)
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable --now kubelet
起动kubelet 失败是正常的,因为还没有对它init。
- 关swap
swapoff -a && sed -i '/swap/d' /etc/fstab
- 设置iptable
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
3.使用kubeadm安装单机集群
- 配置使用参数
#本机ip
export MASTER_IP=192.168.88.201
#hostname
export APISERVER_NAME=apiserver.sh
export POD_SUBNET=10.100.0.1/16
#配置本地hosts,意思是:APISERVER_NAME与MASTER_IP相同
echo "${MASTER_IP} ${APISERVER_NAME}" >> /etc/hosts
- 执行安装
curl -sSL https://kuboard.cn/install-script/v1.19.x/init_master.sh | sh -s 1.19.2
卸载与重装
- 如果安装失败需要重置:
kubeadm reset -f
- 删除k8s
yum remove kubelet-1.19.2 kubeadm-1.19.2 kubectl-1.19.2
yum remove kubelet kubeadm kubectl
#删除所有docker images文件
docker ps -q | xargs docker rm -f
docker images -q |xargs docker rmi -f
rm -rf /var/lib/docker
#处理minikube遗留
find /etc/ -type f |xargs grep 'minikube'
rm -rf /etc/systemd/system/kubelet.service.d
4.提升效率
命令自动补全
linux bash
yum install bash-completion -y
kubectl completion bash >/etc/bash_completion.d/kubectl
echo "source <(kubectl completion bash)" >> ~/.bashrc
source ~/.bashrc
#别名,可选
echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -F __start_kubectl k' >>~/.bashrc
mac zsh
brew install bash-completion
echo "source <(kubectl completion zsh)" >> ~/.zshrc
source ~/.zshrc
#别名,可选
echo 'alias k=kubectl' >>~/.zshrc
echo 'complete -F __start_kubectl k' >>~/.zshrc
设置参考:Install and Set Up kubectl