wsl Ubuntu环境 创建 k8s集群

时间:2024-01-23 15:19:47


1.关闭swap

swapoff  -a

 wsl这个命令没法关闭swap,需要用如下方法

wsl Ubuntu环境 创建 k8s集群_linux

#先cd到c盘,并创建.wslconfig文件
root@timer:/mnt/c/Users/xiaoqi# cat .wslconfig 
[wsl2]
swap=0 # 关闭swap

[network]
generateResolvConf = false # 解决域名解析失败的问题

cmd进入命令行

wsl --shutdown  关闭所有的虚拟机

再bash进入 wsl,swap已关闭

wsl Ubuntu环境 创建 k8s集群_docker_02

2.service更换成systemd

Ubuntu默认是service启动服务,需要改成systemd方式

首先安装 Systemd 相关的依赖

apt install -yqq fontconfig daemonize

创建一个如下所示的脚本文件

# Create the starting script for SystemD
vi /etc/profile.d/00-wsl2-systemd.sh
SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')

if [ -z "$SYSTEMD_PID" ]; then
   sudo /usr/sbin/daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
   SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')
fi

if [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" != "1" ]; then
    exec sudo /usr/bin/nsenter -t $SYSTEMD_PID -a su - $LOGNAME
fi

上面的脚本放置在 /etc/profile.d 目录下面,所以要让脚本生效,我们需要退出当前 session,重新进入即可,再次登录session,我们会发现系统PID为1的进程已经变成systemd。

 

wsl Ubuntu环境 创建 k8s集群_运维开发_03

 这样就成功了

3.安装docker

apt-get install docker.io

如果需要配置为开机启动,可执行以下命令

systemcd enable docker

systemcd start docker

root@timer:~# docker --version
Docker version 20.10.14, build a224086

wsl Ubuntu环境 创建 k8s集群_docker_04

 这样子的docker就成功了

注意:如果没有源,记得更换国内的源,然后运行下面的命令

apt-get update

apt-get upgrade

4.安装kubelet组件

使得 apt 支持 ssl 传输

apt-get update && apt-get install -y apt-transport-https

下载 gpg 密钥

curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -

添加 k8s 镜像源

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF

更新源列表

apt-get update

安装 kubectl,kubeadm以及 kubelet

apt-get install -y kubelet kubeadm kubectl  --allow-unauthenticated

注意:这时候运行k8s 会运行不起来,如下状态说明已经安装成功了

 

wsl Ubuntu环境 创建 k8s集群_docker_05

5.使用kind安装k8s集群

下载kind二进制文件

curl -Lo ./kind https://github.com/kubernetes-sigs/kind/releases/download/v0.8.1/kind-$(uname)-amd64
# 标记为可执行文件
chmod +x ./kind
# 移动到 PATH 目录下去
mv ./kind /usr/bin/

创建单节点集群

root@timer:~# kind create cluster --name wsl2-k8s
Creating cluster "wsl2-k8s" ...
 ✓ Ensuring node image (kindest/node:v1.18.2) ???? 
 ✓ Preparing nodes ????  
 ✓ Writing configuration ???? 
 ✓ Starting control-plane ????️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️ 
 ✓ Installing CNI ???? 
 ✓ Installing StorageClass ???? 
Set kubectl context to "kind-wsl2-k8s"
You can now use your cluster with:

kubectl cluster-info --context kind-wsl2-k8s

Have a question, bug, or feature request? Let us know! https://kind.sigs.k8s.io/#community ????
root@timer:~# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS                       NAMES
29aa95a61d87   kindest/node:v1.18.2   "/usr/local/bin/entr…"   3 minutes ago   Up 3 minutes   127.0.0.1:44671->6443/tcp   wsl2-k8s-control-plane
root@timer:~# docker ps |grep "kindest\/node"|awk '{print $1}'
29aa95a61d87
root@timer:~# docker cp 29aa95a61d87:/kind/bin/kubectl /usr/bin/
root@timer:~# kubectl get node
NAME                     STATUS   ROLES    AGE     VERSION
wsl2-k8s-control-plane   Ready    master   4m46s   v1.18.2

kube-apiserver的6443端口暴露到docker宿主机的44671端口

进入到node节点的容器内

wsl Ubuntu环境 创建 k8s集群_多节点_06

 如上所示,一个单节点的kubernetes集群就创建好了,那如何创建多节点kubernetes集群呢

创建多节点kubernetes集群

删除现有集群

# 获取创建的集群名字
kind get clusters
# 删除集群
kind delete cluster --name wsl2-k8s

创建一个3节点集群的配置文件

root@timer:~# cat kind-3nodes.yaml 
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker

创建集群

root@timer:~# kind create cluster --name wsl2-3nodes --config ./kind-3nodes.yaml
Creating cluster "wsl2-3nodes" ...
 ✓ Ensuring node image (kindest/node:v1.18.2) ????
 ✓ Preparing nodes ???? ???? ????  
 ✓ Writing configuration ???? 
 ✓ Starting control-plane ????️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️ 
 ✓ Installing CNI ???? 
 ✓ Installing StorageClass ???? 
 ✓ Joining worker nodes ???? 
Set kubectl context to "kind-wsl2-3nodes"
You can now use your cluster with:

kubectl cluster-info --context kind-wsl2-3nodes

Have a nice day! ????

root@timer:~# kubectl get nodes
NAME                        STATUS   ROLES    AGE   VERSION
wsl2-3nodes-control-plane   Ready    master   93s   v1.18.2
wsl2-3nodes-worker          Ready    <none>   51s   v1.18.2
wsl2-3nodes-worker2         Ready    <none>   51s   v1.18.2

root@timer:~# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS                       NAMES
67bcbe67747d   kindest/node:v1.18.2   "/usr/local/bin/entr…"   2 minutes ago   Up 2 minutes                               wsl2-3nodes-worker
b5d9f5a30a98   kindest/node:v1.18.2   "/usr/local/bin/entr…"   2 minutes ago   Up 2 minutes                               wsl2-3nodes-worker2
74c504c4a04b   kindest/node:v1.18.2   "/usr/local/bin/entr…"   2 minutes ago   Up 2 minutes   127.0.0.1:39877->6443/tcp   wsl2-3nodes-control-plane

以上。