01-利用vscode与kind搭建kubernetes开发环境

本文基于1.27.0版本

Kind 创建集群

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
cat << EOF > dev.yaml
kind: Cluster
apiVersion: "kind.x-k8s.io/v1alpha4"
kubeadmConfigPatches:
- |
  apiVersion: kubeadm.k8s.io/v1beta1
  kind: ClusterConfiguration
  metadata:
    name: dev
  imageRepository: registry.aliyuncs.com/google_containers
networking:
  podSubnet: "10.8.0.0/16"
  serviceSubnet: "10.9.0.0/16"
nodes:
  - role: control-plane
    image: kindest/node:v1.27.0@sha256:c6b22e613523b1af67d4bc8a0c38a4c3ea3a2b8fbc5b367ae36345c9cb844518
    extraPortMappings:
      - containerPort: 2379
        hostPort: 2379
        protocol: TCP
EOF

kind create cluster --name dev --config=dev.yaml

配置文件

将kind创建的kubernetes集群配置拷贝到本地

1
docker cp dev-control-plane:/etc/kubernetes/ ./config

Vscode 配置调试模式

验证 ApiServer

查看 API 接口

这是由于匿名用户 anonymous 没有权限

匿名用户绑定admin权限

1
kubectl create clusterrolebinding DevAdmin --user system:anonymous --clusterrole cluster-admin

再次查看 API 接口

Nocalhost

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
cat << EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /data/k8s/ssl
          name: ssl
          readOnly: true
        - mountPath: /data/yy/log/kube-controller-manager
          name: log
        - mountPath: /root/.kube
          name: kube
          readOnly: true
      volumes:
      - hostPath:
          path: /data/k8s/ssl
          type: DirectoryOrCreate
        name: ssl
      - hostPath:
          path: /data/yy/log/kube-controller-manager
          type: DirectoryOrCreate
        name: log
      - hostPath:
          path: /root/.kube
          type: DirectoryOrCreate
        name: kube
EOF

参考资料

01-Kubernetes-源码调试

0%