第一次安装k8s是借鉴的网上教程视频教程安装的,是旧的版本。这次安装,将会把所有的组件安装成目前最新版,并且,只参考官方。
ps.k8s官网有中文文档,友好度+1
我们这里使用kubeadm安装k8s集群,所有节点都需要安装,kubeadm、kubectl、kubelet。
我这里参考的官方文档地址是:https://kubernetes.io/zh/docs...
虚拟机准备
ip | name |
---|---|
192.168.0.105 | k-master |
192.168.0.106 | k-node1 |
192.168.0.112 | k-node2 |
版本
组件 | 版本号 |
---|---|
kubernetes | 1.23.5 |
docker | 20.10.14 |
k8s版本注意:
1.24以上官方不再默认支持docker,推荐containerd。
https://github.com/kubernetes...
基础环境准备
机器命名:
hostnamectl set-hostname k-master
修改静态ip地址(我这是虚拟机所以要设置一下):
https://help.aliyun.com/docum...
vim /etc/sysconfig/network-scripts/ifcfg-enp0s3
BOOTPROTO="static"
IPADDR="192.168.0.105"
NETMASK="255.255.255.0"
GATEWAY="192.168.0.106"
DNS1="114.114.114.114"
systemctl restart network
写入host文件(只master):
cat >> /etc/hosts << EOF
192.168.0.105 k-master
192.168.0.106 k-node1
192.168.0.112 k-node2
EOF
关闭防火墙:
systemctl stop firewalld
systemctl disable firewalld
禁用selinux:
sed -i 's/enforcing/disabled/' /etc/selinux/config
禁用swap分区:
swapoff -a
vim /etc/fstab--->注释#/dev/mapper/centos-swap swap swap defaults 0 0
free -m
vim /etc/sysctl.conf--->添加vm.swappiness=0
sysctl -p
free -m检查swap是否为0
允许 iptables 检查桥接流量、加载br_netfilter模块:
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system
docker安装
https://developer.aliyun.com/...
配置docker国内仓库代理
我这里用的阿里云
登录阿里云。登录容器镜像服务控制台,在左侧导航栏选择镜像工具 > 镜像加速器,在镜像加速器页面获取镜像加速地址。
配置kubernetes阿里云代理
https://developer.aliyun.com/...
安装 kubeadm、kubelet 和 kubectl
yum install -y --nogpgcheck kubelet-1.23.5 kubeadm-1.23.5 kubectl-1.23.5
init
kubeadm init \
--control-plane-endpoint="192.168.0.105:6443" \
--apiserver-advertise-address=192.168.0.105 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.23.4 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
报错:
[root@k-master ~]# systemctl enable kubelet
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
[root@k-master ~]# kubeadm init \
> --apiserver-advertise-address=192.168.0.105 \
> --image-repository registry.aliyuncs.com/google_containers \
> --kubernetes-version v1.23.5 \
> --service-cidr=10.96.0.0/12 \
> --pod-network-cidr=10.244.0.0/16
[init] Using Kubernetes version: v1.23.5
[preflight] Running pre-flight checks
error execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR FileContent--proc-sys-net-ipv4-ip_forward]: /proc/sys/net/ipv4/ip_forward contents are not set to 1
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
To see the stack trace of this error execute with --v=5 or higher
解决:sysctl -w net.ipv4.ip_forward=1
报错2:
[kubelet-check] It seems like the kubelet isn't running or healthy.
[kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp [::1]:10248: connect: connection refused.
解决2:把docker的cgroup改成systemd
vim /etc/docker/daemon.json
"exec-opts": ["native.cgroupdriver=systemd"]
systemctl restart docker
报错3:
[root@k8s-1 ~]# kubeadm init --control-plane-endpoint="192.168.0.123:6443" --apiserver-advertise-address=192.168.0.123 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.26.0 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
[init] Using Kubernetes version: v1.26.0
[preflight] Running pre-flight checks
error execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR CRI]: container runtime is not running: output: E0111 22:16:28.313598 2755 remote_runtime.go:948] "Status from runtime service failed" err="rpc error: code = Unimplemented desc = unknown service runtime.v1alpha2.RuntimeService"
time="2023-01-11T22:16:28+08:00" level=fatal msg="getting status of runtime: rpc error: code = Unimplemented desc = unknown service runtime.v1alpha2.RuntimeService"
, error: exit status 1
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
To see the stack trace of this error execute with --v=5 or higher
解决3:
参考https://blog.csdn.net/weixin_38405770/article/details/127948286
mv /etc/containerd/config.toml /tmp/
systemctl restart containerd
安装完毕后,使用kubectl get nodes查看集群状态,发现所有节点均为not ready状态.
安装CNI插件:
可以借鉴我的另一篇文章:https://segmentfault.com/a/11...
为什么不安装cni网络插件,coredns服务不启用呢?
摘自官网:https://kubernetes.io/zh/docs...
你必须部署一个基于 Pod 网络插件的 容器网络接口 (CNI),以便你的 Pod 可以相互通信。 在安装网络之前,集群 DNS (CoreDNS) 将不会启动。
安装DashBoard
https://kubernetes.io/zh/docs...
遇到的问题及解决:https://blog.csdn.net/ninimin...
安装完成后,进入dashboard发现默认的token权限不足,这时,需要我们自行添加一个admin权限的账户。
kubectl create serviceaccount dashboard-admin -n kubernetes-dashboard
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:dashboard-admin
kubectl describe secrets -n kubernetes-dashboard $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
使用以上命令,获取到admin权限的账户token登录即可看到所有资源了。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。