Windows系统搭建基于k8s开发所需本地集群环境

安装Docker Desktop

去官网下载,新版已经比前几年好用多了

安装前要去windows功能里边开启Hyper-V适用于Linux的Windows子系统

若安装完,启动显示乱码,打开管理员shell,执行完重启即可

netsh winsock reset 

新版Docker Desktop会自带kubectl

配置Docker Desktop镜像源

{
  "builder": {
    "gc": {
      "defaultKeepStorage": "20GB",
      "enabled": true
    }
  },
  "experimental": false,
  "features": {
    "buildkit": true
  },
  "registry-mirrors": [
    "http://hub-mirror.c.163.com",
    "https://docker.mirrors.ustc.edu.cn",
    "https://registry.docker-cn.com"
  ]
}

配置kind

下载 Kind,官方最新是v0.17.0,但是没有windows版本,故下载v0.16.0

curl.exe -Lo kind-windows-amd64.exe https://kind.sigs.k8s.io/dl/v0.16.0/kind-windows-amd64

下载后改名 kind.exe,放到 C:\Windows\目录下即可全局

如果前边Docker Desktop没有自动配置好kubectl

那么,下载 kubectl,下载后放到 C:\Windows\目录下即可全局。

curl.exe -LO "https://dl.k8s.io/release/v1.25.0/bin/windows/amd64/kubectl.exe"

使用kind创建集群

准备配置文件

创建单个节点的k8s集群的配置文件k8s-test.yaml,镜像使用v1.21.14

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: k8s-test
nodes:
- role: control-plane
  image: kindest/node:v1.21.14@sha256:9d9eb5fb26b4fbc0c6d95fa8c790414f9750dd583f5d7cee45d92e8c26670aa1
networking:
  apiServerAddress: "127.0.0.1"
  apiServerPort: 6443

创建五个节点(3个master,2个worker)的k8s集群的配置文件k8s32-config.yaml,镜像使用v1.21.14

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: k8s
nodes:
- role: control-plane
  image: kindest/node:v1.21.14@sha256:9d9eb5fb26b4fbc0c6d95fa8c790414f9750dd583f5d7cee45d92e8c26670aa1
- role: control-plane
  image: kindest/node:v1.21.14@sha256:9d9eb5fb26b4fbc0c6d95fa8c790414f9750dd583f5d7cee45d92e8c26670aa1
- role: control-plane
  image: kindest/node:v1.21.14@sha256:9d9eb5fb26b4fbc0c6d95fa8c790414f9750dd583f5d7cee45d92e8c26670aa1
- role: worker
  image: kindest/node:v1.21.14@sha256:9d9eb5fb26b4fbc0c6d95fa8c790414f9750dd583f5d7cee45d92e8c26670aa1
- role: worker
  image: kindest/node:v1.21.14@sha256:9d9eb5fb26b4fbc0c6d95fa8c790414f9750dd583f5d7cee45d92e8c26670aa1
networking:
  apiServerAddress: "127.0.0.1"
  apiServerPort: 6443

创建集群

使用配置文件k8s-test.yaml创建单个节点的k8s集群

C:\k8s-platform\kubeconfig>kind create cluster --config k8s-test.yaml
Creating cluster "k8s-test" ...
 • Ensuring node image (kindest/node:v1.21.14) 🖼  ...
 ✓ Ensuring node image (kindest/node:v1.21.14) 🖼
 • Preparing nodes 📦   ...
 ✓ Preparing nodes 📦
 • Writing configuration 📜  ...
 ✓ Writing configuration 📜
 • Starting control-plane 🕹️  ...
 ✓ Starting control-plane 🕹️
 • Installing CNI 🔌  ...
 ✓ Installing CNI 🔌
 • Installing StorageClass 💾  ...
 ✓ Installing StorageClass 💾
Set kubectl context to "kind-k8s-test"
You can now use your cluster with:

kubectl cluster-info --context kind-k8s-test

Have a nice day! 👋

查看节点

C:\k8s-platform\kubeconfig>kubectl get no
NAME                     STATUS   ROLES                  AGE   VERSION
k8s-test-control-plane   Ready    control-plane,master   11m   v1.21.14

运行容器

使用nginx.yaml,创建nginx的pod

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.19.3-alpine
          ports:
            - containerPort: 80

查看pod

C:\k8s-platform\kubeconfig>kubectl get po
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5bbffc7ddc-cbgrk   1/1     Running   0          2m6s
nginx-deployment-5bbffc7ddc-fck47   1/1     Running   0          2m6s
nginx-deployment-5bbffc7ddc-lw7f9   1/1     Running   0          2m6s

由于没有做服务暴露,所以是不能直接访问对应的服务的,所以用 kubectl 提供的端口转发功能来将流量从本地转发给 k8s 集群测试服务是否能正常访问

kubectl port-forward nginx-deployment-5bbffc7ddc-cbgrk 30080:80

浏览器访问Welcome to nginx!,可以正常访问

本地开发环境使用kubeconfig连接

在windows系统中,生成的kubeconfig的目录是

C:\Users\用户\.kube\config

kubeconfig复制到项目目录

使用goland获取pod

package main

import (
	"context"
	"fmt"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
)

func main() {
	conf, err := clientcmd.BuildConfigFromFlags("", "C:\\k8s-platform\\kubeconfig\\config")
	if err != nil {
		fmt.Println("error: ", err)
	}

	clientset, err := kubernetes.NewForConfig(conf)
	if err != nil {
		fmt.Println("error: ", err)
	}
	podList, err := clientset.CoreV1().Pods("default").List(context.TODO(), metav1.ListOptions{})
	if err != nil {
		fmt.Println("error: ", err)
	}
	for _, pod := range podList.Items {
		fmt.Println(pod.Name, pod.Namespace)
	}
}

输出

nginx-deployment-5bbffc7ddc-cbgrk default
nginx-deployment-5bbffc7ddc-fck47 default
nginx-deployment-5bbffc7ddc-lw7f9 default
posted @ 2022-10-31 13:14  请务必优秀  阅读(957)  评论(0编辑  收藏  举报