k8s 实战 Centos8.4 版本
网络 前置操作
nmcli con mod "Wired connection 1" ipv4.addresses 192.168.0.201/24 # 设置IP地址
nmcli con mod "Wired connection 1" ipv4.gateway 192.168.0.1 # 设置网关
nmcli con mod "Wired connection 1" ipv4.method manual # 设置静态IP
nmcli con mod "Wired connection 1" ipv4.dns "8.8.8.8, 8.8.4.4" # 设置DNS
nmcli con down "Wired connection 1" # 关闭网络连接
nmcli con up "Wired connection 1" # 打开网络连接
nmcli con mod "Wired connection 1" connection.id ens160 # 给设备重新命名
nmcli con show # 查看网络连接
systemctl restart NetworkManager # 重启网络服务
代理配置
如果有图形化界面:
docker 前置操作
os version: CentOs 8.4
执行以下脚本前,默认已完成如下准备:
- 你的网络配置ok,可以访问外网
- 当前用户在wheel组中,拥有了sudo权限(如果没有则执行:
usermod -aG lgw wheel
orusermod -aG wheel lgw
,然后退出从新登陆)
docker_ce_v=docker-ce-20.10.8
docker_ce_cli_v=docker-ce-cli-20.10.8
kubeadm_v=kubeadm-1.22.0
kubelet_v=kubelet-1.22.0
kubectl_v=kubectl-1.22.0
IPADDR=192.168.56.101
# 删除掉可能影响安装的docker
sudo yum remove -y docker docker-client docker-client-latest dockercommon docker-latest docker-latest-logrotate docker-logrotate docker-engine buildah podman
# 设置网络环境
# vim /etc/environment
# http_proxy="xxx"
# https_proxy="xxx"
# no_proxy="xxx"
# 添加yum仓库
sudo wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
yum makecache # 更新到缓存
# 添加docker的repo源
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum install -y $docker_ce_v $docker_ce_cli_v containerd.io
# 设置代理
# vim /etc/systemd/system/docker.service.d
# ...
sudo mkdir /etc/docker
sudo touch /etc/docker/daemon.json
sudo chmod a+w /etc/docker/daemon.json # all user to write permision
sudo cat > /etc/docker/daemon.json<<EOF
{
"registry-mirrors": ["https://registry.docker-cn.com","http://hub-mirror.c.163.com","https://r1xsqnzk.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl enable docker # 设置开机自启动
sudo usermod -aG docker $USER # 将用户追加到docker附加组中(类似于 Django 的多对多关系设置)
# su - # 刷新当前环境
# sudo gpasswd -a lgw docker # 将用户切换到docker组
echo "Start install kubernetes"
sudo cat <<EOF |sudo tee /etc/yum.repos.d/kubernetes.repo
[k8s]
name=k8s
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
sudo yum install $kubeadm_v $kubectl_v $kubelet_v -y
sudo systemctl enable kubelet
# 关闭swapoff分区
sudo swapoff -a
sudo sysctl -w vm.swappiness=0
sudo sed -i 's/.*swap.*/#&/' /etc/fstab
sudo setenforce 0
sudo sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
sudo systemctl stop firewalld
sudo systemctl disable firewalld
# 如果出错,根据错误提示,匹配这里的方案:https://github.com/kubernetes/kubeadm/issues/1893
# apiserver-advertise-address 填写你的局域网ip
sudo kubeadm init --kubernetes-version=1.22.0 \
--apiserver-advertise-address=$IPADDR \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=10.10.0.0/16 --pod-network-cidr=10.122.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
echo "source < (kubectl completion bash)" >> ~/.bashrc
source ~/.bashrc & /dev/null
kubectl apply -f ./kube-flannel.yml
# 这里不要sudo,否则会报错:`The connection to the server localhost:8080 was refused - did you specify the right host or port?`
# [下载失败,用这里的内容]:(https://www.cnblogs.com/li-gao-wang/p/15450668.html)
sudo usermod -aG docker $USER # 给当前用于追加附加组, a 是追加,G 是修改用户所属的附加群组
other_sh
kubeadm token create --print-join-command # node节点join时的token值