通过k3d安装k3s

k3d是什么

k3d 是一个轻量级包装器,用于在 docker 中运行k3s(Rancher Lab 的最小 Kubernetes 发行版)。

k3d 使得在 docker 中创建单节点和多节点k3s集群变得非常容易,例如用于 Kubernetes 上的本地开发。

注意: k3d 是一个社区驱动的项目,但它不是官方 Rancher (SUSE) 产品。

k3d安装

通过脚本安装:

wget -q -O - https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash

macos 上通过homebrew安装:

brew install k3d

查看帮助文档:

$ k3d -h
https://k3d.io/
k3d is a wrapper CLI that helps you to easily create k3s clusters inside docker.
Nodes of a k3d cluster are docker containers running a k3s image.
All Nodes of a k3d cluster are part of the same docker network.

Usage:
  k3d [flags]
  k3d [command]

Available Commands:
  cluster      Manage cluster(s)
  completion   Generate completion scripts for [bash, zsh, fish, powershell | psh]
  config       Work with config file(s)
  help         Help about any command
  image        Handle container images.
  kubeconfig   Manage kubeconfig(s)
  node         Manage node(s)
  registry     Manage registry/registries
  version      Show k3d and default k3s version

Flags:
  -h, --help         help for k3d
      --timestamps   Enable Log timestamps
      --trace        Enable super verbose output (trace logging)
      --verbose      Enable verbose output (debug logging)
      --version      Show k3d and default k3s version

Use "k3d [command] --help" for more information about a command.

查看版本:

$ k3d --version
k3d version v5.6.3
k3s version v1.28.8-k3s1 (default)

创建k3s集群

快速创建一个集群:

$ k3d cluster create mycluster

INFO[0000] Prep: Network
INFO[0000] Created network 'k3d-mycluster'
INFO[0000] Created image volume k3d-mycluster-images
INFO[0000] Starting new tools node...
INFO[0001] Creating node 'k3d-mycluster-server-0'
INFO[0005] Pulling image 'ghcr.io/k3d-io/k3d-tools:5.6.3'
INFO[0005] Pulling image 'docker.io/rancher/k3s:v1.28.8-k3s1'
INFO[0009] Starting node 'k3d-mycluster-tools'
INFO[0017] Creating LoadBalancer 'k3d-mycluster-serverlb'
INFO[0021] Pulling image 'ghcr.io/k3d-io/k3d-proxy:5.6.3'
INFO[0031] Using the k3d-tools node to gather environment information
INFO[0031] HostIP: using network gateway 192.168.237.1 address
INFO[0031] Starting cluster 'mycluster'
INFO[0031] Starting servers...
INFO[0031] Starting node 'k3d-mycluster-server-0'
INFO[0034] All agents already running.
INFO[0034] Starting helpers...
INFO[0035] Starting node 'k3d-mycluster-serverlb'
INFO[0041] Injecting records for hostAliases (incl. host.k3d.internal) and for 2 network members into CoreDNS configmap...
INFO[0043] Cluster 'mycluster' created successfully!
INFO[0043] You can now use it like this:
kubectl cluster-info

查看集群节点:

$ kubectl get nodes

NAME                     STATUS   ROLES                  AGE    VERSION
k3d-mycluster-server-0   Ready    control-plane,master   117s   v1.28.8+k3s1

查看创建的集群:

$ k3d cluster list
NAME        SERVERS   AGENTS   LOADBALANCER
mycluster   1/1       0/0      true

创建多服务器集群

使用 k3s 的嵌入式 etcd 数据库创建一个具有 3 个服务器节点的集群。要创建的第一个服务器将使用该--cluster-init标志,k3d 将等待它启动并运行,然后再创建(和连接)其他服务器节点。

k3d cluster create multiserver --servers 3

将服务器节点添加到正在运行的集群:

k3d node create newserver --cluster multiserver --role server

查看节点:

$ kubectl get nodes
NAME                       STATUS   ROLES                       AGE     VERSION
k3d-multiserver-server-0   Ready    control-plane,etcd,master   2m55s   v1.28.8+k3s1
k3d-multiserver-server-1   Ready    control-plane,etcd,master   2m37s   v1.28.8+k3s1
k3d-multiserver-server-2   Ready    control-plane,etcd,master   2m22s   v1.28.8+k3s1
k3d-n

暴露服务

通过Ingress(推荐)

部署一个简单的 nginx Web 服务器部署并使其可通过 ingress 进行访问。因此,我们必须以某种方式创建集群,以便traefik在主机系统上公开内部端口 80(入口控制器正在侦听的端口)。

1、创建集群,将入口端口 80 映射到 localhost:8081

k3d cluster create --api-port 6550 -p "8081:80@loadbalancer" --agents 2

再次查看集群:

$ k3d cluster list
NAME          SERVERS   AGENTS   LOADBALANCER
k3s-default   1/1       2/2      true
mycluster     1/1       0/0      true

2、创建nginx deployment:

kubectl create deployment nginx --image=nginx

3、为其创建ClusterIP服务:

kubectl create service clusterip nginx --tcp=80:80

4、创建一个Ingress,注意:k3s默认安装了treafik ingress

cat << EOF | kubectl apply -f -
# apiVersion: networking.k8s.io/v1beta1 # for k3s < v1.19
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx
  annotations:
    ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 80
EOF

查看状态:

NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-7854ff8877-k4ss2   1/1     Running   0          30s

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.43.0.1       <none>        443/TCP   69s
service/nginx        ClusterIP   10.43.187.137   <none>        80/TCP    16s

NAME                              CLASS     HOSTS   ADDRESS                                     PORTS   AGE
ingress.networking.k8s.io/nginx   traefik   *       192.168.148.2,192.168.148.3,192.168.148.4   80      7s

5、本地访问:

curl localhost:8081/

通过 NodePort 暴露

1、创建集群,将端口30080从映射agent-0localhost:8082

k3d cluster create mycluster -p "8082:30080@agent:0" --agents 2
  • 注1:Kubernetes的默认NodePort范围是30000-32767
  • 注 2:您也可以从一开始就公开整个 NodePort 范围,例如通过k3d cluster create mycluster --agents 3 -p "30000-32767:30000-32767@server:0"

2、3步骤和上面一样

4、创建Service

cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  ports:
  - name: 80-80
    nodePort: 30080
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort
EOF

5、本地访问:

curl localhost:8082/

相关项目

  • vscode-k3d:VSCode 扩展,用于从 VSCode 内处理 k3d 集群
  • k3x:k3d 的图形接口(适用于 Linux)。
  • AbsaOSS/k3d-action:完全可定制的 GitHub Action 以运行轻量级 Kubernetes 集群。
  • AutoK3s:一个轻量级工具,可帮助在任何地方运行 K3s,包括 k3d 提供商。
  • nolar/setup-k3d-k3s:为 GitHub Actions 设置 K3d/K3s。
Share this post:

Related content