WSL安装Kubernetes

  1. 配置文件修改
    修改.wslconfig将WSL设置为镜像网络模式
    1
    2
    3
    4
    5
    6
    7
    [wsl2]
    networkingMode=mirrored
    dnsTunneling=true
    autoProxy=true
    [experimental]
    sparseVhd=true
    hostAddressLoopback=true
    /etc/resolv.conf内不可包含本地nameserver
  2. 安装cri-dockerd, kubeadm, kubelet, kubectl
  3. 初始化Kubernetes
    1
    2
    3
    4
    5
    sudo swapoff -a
    sudo systemctl start kubelet
    sudo kubeadm init --control-plane-endpoint=main --pod-network-cidr=10.244.0.0/16 --cri-socket=unix:///var/run/cri-dockerd.sock
    # 如果出错使用下列命令重置
    sudo kubeadm reset --cri-socket=unix:///var/run/cri-dockerd.sock
  4. 安装flannel
    1
    2
    3
    curl -o kube-flannel.yml https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
    # WSL的VXLAN内核功能异常,因此切换其它Backend,需要修改kube-flannel.yml, Backend的type改为UDP, privileged改为true
    kubectl apply -f kube-flannel.yml
  5. 安装Dashboard
    1
    2
    helm repo add kubernetes-dashboard https://kubernetes.github.io/dashboard/
    helm upgrade --install kubernetes-dashboard kubernetes-dashboard/kubernetes-dashboard --create-namespace --namespace kubernetes-dashboard --set kong.admin.tls.enabled=false #关闭TLS
  6. 移除主节点的NoSchedule taint
    1
    kubectl taint nodes xwd node-role.kubernetes.io/control-plane:NoSchedule-
  7. 创建root用户并绑定最高权限
    1
    2
    kubectl create serviceaccount root
    kubectl create clusterrolebinding rootBind --clusterrole=cluster-admin --serviceaccount=default:root
  8. 生成root用户登录Dashboard的Token,并开放Dashboard本地访问
    1
    kubectl create token root && kubectl port-forward -n kubernetes-dashboard svc/kubernetes-dashboard-kong-proxy 8443:443

常用命令

1
2
3
4
kubectl get pods -A #获取所有pod运行情况
kubectl describe pod -n <namespace> <pod name> #查看pod的部署log
kubectl logs -n <namespace> <pod name> #查看pod内容器的log
sudo ethtool -K flannel.1 tx-checksum-ip-generic off #若在NAT网络中出错,可尝试该命令关闭校验