环境:
本地Ubuntu20.04
在本地安装了qemu-kvm后,新建两台虚拟机作为node节点(详细操作可见另一篇文章:https://www.jianshu.com/p/443f3df08dbc)
k8s官网:【https://kubernetes.io/zh/docs/home/】
前期准备:
①安装docker,我这里安装的是Docker version 20.10.10(安装步骤不赘述,可参考官网:https://docs.docker.com/engine/install/ubuntu/)
修改默认的cgroupDriver,编辑/etc/docker/daemon.json,没有则创建,添加如下内容:
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
重启docker并查看
sudo systemctl daemon-reload
sudo systemctl restart docker.service
docker info
②本地配置代理,我使用的是clash,开放端口7890(clash for Linux的安装https://www.jianshu.com/p/260fbac1d4a2)
③docker代理设置(可先不设置,若要设置只需要在master上设置即可)
在此处踩了很大的坑,开了代理用的阿里云镜像加速,导致一直无法拉取镜像。可以先不设置,后续下载如果很慢在考虑
#创建配置文件夹
sudo mkdir -p /etc/systemd/system/docker.service.d/
#编辑配置文件
sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf
#输入以下内容,(183.131.227.249是阿里云的IP,因为我配了代理所以把阿里云设为不使用代理)
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:7890"
Environment="HTTPS_PROXY=http://127.0.0.1:7890"
Environment= "NO_PROXY=localhost,127.0.0.1,192.168.100.0/16,183.131.227.249]"
重启docker服务并验证加载
sudo systemctl daemon-reload
sudo systemctl restart docker.service
systemctl show --property=Environment docker
④关闭防火墙(可选)
sudo systemctl stop ufw
sudo systemctl status ufw
sudo systemctl enable ufw //开机不启动
安装k8s
①更新 apt 包索引并安装使用 Kubernetes apt 仓库所需要的包:
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl
②下载 Google Cloud 公开签名秘钥:
sudo curl -x 127.0.0.1:7890 -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
如果失败,可以通过wget命令来手动设置
wget https://packages.cloud.google.com/apt/doc/apt-key.gpg
sudo mv apt-key.gpg /usr/share/keyrings/kubernetes-archive-keyring.gpg
③添加 Kubernetes apt 仓库:
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
④更新 apt 包索引,安装 kubelet、kubeadm 和 kubectl,并锁定其版本:
sudo -E apt-get update
sudo -E apt-get install -y kubelet kubeadm kubectl
sudo -E apt-mark hold kubelet kubeadm kubectl
注:-E为使用系统代理设置,需要从Google下载东西所以必须使用代理
关闭交换
sudo swapoff -a && sudo sed -i '/swap/d' /etc/fstab //永久关闭
cat /proc/meminfo //查看分区大小
初始化集群(该步骤在node节点中不需要)
如果你后面想选择calico网络插件,注意下--pod-network-cidr这个参数,不要使用192.168.0.0/16(这是个巨坑!你会发现pod内始终是没有网络的!!)
calico官网:【https://docs.projectcalico.org/archive/v3.21/getting-started/kubernetes/quickstart#create-a-single-host-kubernetes-cluster】
①查看自己安装的kubelet版本
kubelet --version
#可能比较慢会超时
sudo kubeadm init --kubernetes-version v1.22.3 --pod-network-cidr=10.244.0.0/16
注:--pod-network-cidr使用局域网地址
或者使用阿里云加速(推荐)
$sudo kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.22.3 --pod-network-cidr=192.168.100.0/16
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
$mkdir -p $HOME/.kube
$sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.0.114:6443 --token btnrhw.fjx00zqm3lydlegm \
--discovery-token-ca-cert-hash sha256:b484b899674dcd76a420eb34bdcdfb2a4b267ba26b31cf9f5e1fdf0c4553dada
---注意保存此条返回信息
非root用户操作设置
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
查看k8s集群pod状态
kubectl get pods --all-namespaces
安装网络插件
calico(首选)
1.Download the Calico networking manifest for the Kubernetes API datastore.
curl https://docs.projectcalico.org/manifests/calico.yaml -O
2.If you are using pod CIDR 192.168.0.0/16, skip to the next step. If you are using a different pod CIDR with kubeadm, no changes are required - Calico will automatically detect the CIDR based on the running configuration. For other platforms, make sure you uncomment the CALICO_IPV4POOL_CIDR variable in the manifest and set it to the same value as your chosen pod CIDR.
3.Customize the manifest as necessary.
4.sudo vim calico.yaml
#修改CALICO_IPV4POOL_CIDR为前面--pod-network-cidr设置的值(line4222)
5.Apply the manifest using the following command.
kubectl apply -f calico.yaml
# 或者
kubectl apply -f /home/stevechan/hdd01/Container/calico.yaml
6.Delete
kubectl delete -f calico.yaml
token过期或者忘记可以通过一下命令查询或生成
kubeadm token list
kubeadm token create --print-join-command
子节点join到master
sudo kubeadm join 192.168.0.114:6443 --token btnrhw.fjx00zqm3lydlegm \
--discovery-token-ca-cert-hash sha256:b484b899674dcd76a420eb34bdcdfb2a4b267ba26b31cf9f5e1fdf0c4553dada
在master查看node信息
sudo kubectl get nodes
sudo kubectl get csr //join申请信息
如果node显示的是NotReady,检查下node的日志信息
journalctl -f -u kubelet.service
这是我因为代理设置不当导致pause:3.5镜像一直无法拉取的报错信息
如果还是不行,那就重头再来吧~
重置集群(谨慎操作)
sudo kubeadm reset
其他
安装面板
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml
查看面板
http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
删除面板
kubectl delete -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml
解除master节点隔离,使主节点也能部署工作任务
kubectl taint nodes --all node-role.kubernetes.io/master-
Kubernetes IDE
①这里使用的是Lens,下载适合你的版本安装
官网地址:https://docs.k8slens.dev/main/
②添加集群
将.kube/config文件内的内容直接拷到窗口
③没有prometheus,cluster无法监控数据,手动下载下,路径:集群右键-settings
④等待下载完成