# K3D (LV.3)

k3d ถูกสร้างขึ้นเพื่อเป็นเครื่องมือที่ช่วยให้สามารถรัน k3s ภายในคอนเทนเนอร์ Docker โดยมีวัตถุประสงค์หลักเพื่อทำให้การจัดการและการใช้งานคลัสเตอร์ของ K3s เป็นไปอย่างง่ายดายและรวดเร็ว โดยเฉพาะอย่างยิ่งสำหรับสภาพแวดล้อมการพัฒนาและการทดสอบในเครื่อง

k3d เป็นเครื่องมือที่จำลอง k3s cluster ภายใน Docker container มีวัตภุประสงค์เพื่อให้ง่ายต่อการสร้างและจัดการ Kubernetes cluster ซึ่งใช้ในการทดสอบภายในเครื่องของ Developer เอง

ประโยชน์หลักของ k3d มีดังนี้

1. **การพัฒนาในเครื่อง (Local Development)**: k3d ช่วยให้นักพัฒนาสามารถสร้างคลัสเตอร์ Kubernetes บนเครื่องของตัวเองได้ง่าย ๆ โดยใช้ Docker ทำให้สามารถพัฒนาและทดสอบแอปพลิเคชันที่ใช้ Kubernetes โดยไม่จำเป็นต้องใช้ Infrastructure บนคลาวด์หรือเซิร์ฟเวอร์จำนวนมาก
2. **ความเรียบง่าย (Simplicity)**: เนื่องจาก K3s เป็น Kubernetes ที่ออกแบบมาให้มีขนาดเล็กอยู่แล้ว k3d ช่วยทำให้กระบวนการสร้างและจัดการคลัสเตอร์ Kubernetes ภายใน Docker นั้นง่ายยิ่งขึ้น ลดความซับซ้อนและขั้นตอนที่เกี่ยวข้องกับการจัดการ Kubernetes สำหรับการพัฒนาและทดสอบ
3. **ความรวดเร็ว (Speed)**: การติดตั้งคลัสเตอร์ Kubernetes แบบเต็มรูปแบบอาจใช้เวลานานและต้องการทรัพยากร แต่ด้วย k3d คุณสามารถสร้างคลัสเตอร์ Kubernetes ที่พร้อมใช้งานได้ภายในไม่กี่วินาที ซึ่งเหมาะสำหรับการสร้างสภาพแวดล้อมการทดสอบหรือการทดลองฟีเจอร์ต่าง ๆ ของ Kubernetes อย่างรวดเร็ว
4. **คลัสเตอร์แบบหลายโหนด (Multi-Node Clusters)**: k3d ช่วยให้ผู้ใช้สามารถสร้างคลัสเตอร์ที่มีหลายโหนด (Node) ได้ ซึ่งเป็นการจำลองสภาพแวดล้อม Kubernetes ที่มีหลายโหนดเหมือนในโลกความเป็นจริง ทั้งหมดนี้รันอยู่ในคอนเทนเนอร์ Docker บนเครื่องเดียว
5. **ประหยัดค่าใช้จ่าย (Cost-Effective)**: การรัน Kubernetes ในคอนเทนเนอร์ Docker บนเครื่องช่วยหลีกเลี่ยงค่าใช้จ่ายที่เกี่ยวข้องกับการรันคลัสเตอร์ Kubernetes บนผู้ให้บริการคลาวด์ในระหว่างการพัฒนาและทดสอบ

สรุปแล้ว k3d ถูกใช้หลัก ๆ สำหรับการพัฒนาและทดสอบ Kubernetes ในเครื่อง โดยมอบสภาพแวดล้อมที่รวดเร็ว กิน resource น้อย และใช้งานง่าย ทำให้นักพัฒนาสามารถทดสอบและพัฒนาได้อย่างรวดเร็วโดยไม่ต้องใช้ Infrastructure ขนาดใหญ่

## Setup

```bash
curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash
```

## Usage

create cluster

```bash
k3d cluster create mycluster
```

หรือ

```bash
k3d cluster create --agents 3 mycluster
```

add agent node

```bash
k3d node create agent-node --cluster mycluster
```

list node

```bash
k3d node ls
```

remove node

```bash
kubectl drain k3d-agent-node-0 --ignore-daemonsets
kubectl delete node k3d-agent-node-0
k3d node delete k3d-agent-node-0
```

delete cluster

```bash
k3d cluster delete mycluster
```

***

Expose Loadbalancer port

```bash
k3d cluster edit --port-add 8080:80@loadbalancer mycluster
```

command นี้จะเปิด port 8080 บนเครื่อง host ไปยัง loadbalancer node ของ cluster `mycluster` port 80

***

## K3D with cilium

เตรียม config file ชื่อ `k3dconfig.yaml` ดังนี้

```yaml
apiVersion: k3d.io/v1alpha5
kind: Simple
metadata:
  name: cilium-test
servers: 1
agents: 2

ports:
  - port: 8080:80
    nodeFilters:
      - loadbalancer
  - port: 8443:443
    nodeFilters:
      - loadbalancer

options:
  k3s:
    extraArgs:
      - arg: --disable=traefik
        nodeFilters:
          - server:*
      - arg: --disable-network-policy
        nodeFilters:
          - server:*
      - arg: --flannel-backend=none
        nodeFilters:
          - server:*
```

สร้าง cluster ด้วยคำสั่งดังนี้

```bash
k3d cluster create --config k3dconfig.yml
```

custom k3d node เพื่อให้รองรับ cilium (\*ใช้กับกรณี k3s/k3d เท่านั้น)

```bash
docker exec -it k3d-cilium-test-server-0 mount bpffs /sys/fs/bpf -t bpf && \
    docker exec -it k3d-cilium-test-server-0 mount --make-shared /sys/fs/bpf
docker exec -it k3d-cilium-test-agent-0 mount bpffs /sys/fs/bpf -t bpf && \
    docker exec -it k3d-cilium-test-agent-0 mount --make-shared /sys/fs/bpf
docker exec -it k3d-cilium-test-agent-1 mount bpffs /sys/fs/bpf -t bpf && \
    docker exec -it k3d-cilium-test-agent-1 mount --make-shared /sys/fs/bpf
```

ติดตั้ง cilium

```bash
helm install cilium cilium/cilium \
	--namespace kube-system \
	--set hostServices.enabled=false \
	--set externalIPs.enabled=true \
	--set nodePort.enabled=true \
	--set hostPort.enabled=true \
	--set bpf.masquerade=false \
	--set image.pullPolicy=IfNotPresent \
	--set ipam.mode=kubernetes \
	--set kubeProxyReplacement=strict
```

### reference:

{% embed url="<https://github.com/cilium/cilium/issues/23237>" %}

{% embed url="<https://sandstorm.de/blog/posts/running-cilium-in-k3s-and-k3d-lightweight-kubernetes-on-mac-os-for-development>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://handbook.novice.solutions/tools/container/kubernetes/k3d-lv.3.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
