使用 kind 低成本练习 kubernetes

kind 是一个使用 Docker 容器节点运行本地 Kubernetes 集群的工具(CLI)。最近在开发一些周边组件,急需一个集群进行验证,使用kind非常方便。这篇文章记录我使用的步骤。

一、安装

kind 就是一个二进制包,Linux mac Windows都可以快速安装:

Linux

curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.8.1/kind-linux-amd64 chmod +x ./kind mv ./kind /some-dir-in-your-PATH/kind

Mac

brew install kind

或者

curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.8.1/kind-darwin-amd64

Windows

curl.exe -Lo kind-windows-amd64.exe https://kind.sigs.k8s.io/dl/v0.8.1/kind-windows-amd64 Move-Item .\kind-windows-amd64.exe c:\some-dir-in-your-PATH\kind.exe

或者使用 Chocolatey (https://chocolatey.org/packages/kind)

choco install kind

二、使用

使用如下配置文件 kind.yaml

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
  # https://kind.sigs.k8s.io/docs/user/configuration/
  apiServerAddress: "10.19.0.54"
  apiServerPort: 888
nodes:
  - role: control-plane
    image: kindest/node:v1.17.5@sha256:ab3f9e6ec5ad8840eeb1f76c89bb7948c77bbf76bcebe1a8b59790b8ae9a283a
#featureGates:
#  FeatureGateName: true

这个配置文件中,我配置了单节点集群的 IP、apiserver 对外暴露的端口,k8s的版本 v1.17.

image相关信息来这里找:https://github.com/kubernetes-sigs/kind/releases

创建集群:

kind create cluster --config kind.yaml

> kind create cluster
Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.17.5) 🖼
 ✓ Preparing nodes 📦
 ✓ Writing configuration 📜
 ✓ Starting control-plane 🕹️
 ✓ Installing CNI 🔌
 ✓ Installing StorageClass 💾
Set kubectl context to "kind-kind"
You can now use your cluster with:kubectl cluster-info --context kind-kind Have a nice day! 👋

如果你集群恰好使用了 同版本的 kubectl,可以使用下面命令行在宿主机快速操作k8s:

kubectl cluster-info --context kind-kind 

不同版本的kubectl 会导致意外的。当然,你还有其他方式进行操作:

命令行使用

进入主容器:

docker exec -it kind-control-plane /bin/bash

配置kubectl:

export KUBECONFIG=/etc/kubernetes/admin.conf

可以直接操作了!

ui界面使用(lens)

查看kind的相关命令:

kind -h

kind获得配置:

kind get kubeconfig --name xxx # 如果是默认名字kind,不需要--name

将配置写入lens配置界面配置:

1598253646399

完成!界面如下所示。

1598253691029

获得一个简单容器的 yaml:

kubectl run busybox --image=busybox  --restart=Never --dry-run -o yaml  -- sleep 1000000 > pod.yaml

得到 pod.yaml :


apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: busybox
  name: busybox
spec:
  containers:
  - args:
    - sleep
    - "1000"
    image: busybox
    imagePullPolicy: IfNotPresent
    name: busybox
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

运行:

kubectl apply -f pod.yaml

几个常用的yaml备忘

busybox deployment

kind: Deployment
apiVersion: apps/v1
metadata:
  name: busybox
spec:
  selector:
    matchLabels:
      app: busybox
  replicas: 1
  template:
    metadata:
      labels:
        app: busybox  
    spec:
      containers:
        - name: busybox
          image: busybox
          args:
            - sleep
            - '1000'
          resources:
            limits:
              ephemeral-storage: 40Gi
            requests:
              ephemeral-storage: 100Mi
      nodeName: rqytest1
      tolerations:
        - key: env
          operator: Equal
          value: dev
          effect: NoSchedule

nginx deployment

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

nginx service

kind: Service
apiVersion: v1
metadata:
  name: nginx-svc
spec:
  ports:
    - name: '80'
      protocol: TCP
      port: 80
      targetPort: 80
  selector:
    app: nginx
  type: NodePort

浅谈人工智能:现状、任务、构架与统一 - 朱松纯 kubernetes 准入策略 —— Kyverno