[K8s] EP.1 ตั้ง Server ฟรีที่บ้านด้วย Ubuntu + Kubernetes + Cloudflare zero trust

--

ตังก็ไม่มีอยากได้ server ไว้ใช้ทำยังไงดีนะ ?

## โจทย์ของเราในวันนี้

  • มีจด domain อยู่ 1 domain ว่างๆ (https://www.theduckdose.com)
  • มีคอมเก่าๆอยู่เครื่องนึง อยากได้ server ไว้ใช้งาน
  • อยากสัมผัส Kubenetes
  • ไม่อยากลำบากถึงขนาด command line ทุกสิ่งอย่าง
  • Network ไม่แม่นเท่าไหร่ แต่ก็ยังทำได้
  • ไม่อยากเสียเงินค่า cloud
  • วันนี้เราจะจบที่ Hello world

## ผมจึงเลือก

  • Ubuntu Desktop — ต่อ wifi สะดวก ลง editor อย่าง visual studio code ไว้แก้ไขอะไรจุกจิกได้ด้วย
  • MicroK8s — มี addons ช่วยระดับนึง ไม่ต้องขึ้นใหม่ทั้งหมด
  • Cloudflare zero trust — ไม่อยากวุ่นวายกับ network มาก

## Install Ubuntu Desktop

  1. Download Ubuntu 22.04.2 LTS (https://ubuntu.com/download/desktop)
  2. Flash ISO file ใส่ Flash drive ด้วย https://etcher.balena.io/
MacOS: brew install — cask balenaetcher

3. เปิด Notebook สมัยพันปีที่แล้วขึ้นมา เสียบ Flash drive กด F12

4. Nextๆๆๆ ตามขั้นตอนไปเลยจ้า ไม่มีอะไร (https://ubuntu.com/tutorials/install-ubuntu-desktop#1-overview)

5. ท๊า ด๊ามมมมม สำเร็จไป 1 กรุ๊บ

## Install SSH (https://ubuntu.com/server/docs/service-openssh)

เพื่อให้เราสามารถ ssh ไปยังเครื่อง server ของเราได้จากเครื่องอื่น และย้าย Server ของเราไปวางไว้ในที่ๆจะเปิดได้ทั้งวันทั้งคืน

  1. เปิด Terminal ขึ้นมา
  2. sudo apt-get install openssh-server
  3. sudo systemctl enable ssh
  4. sudo systemctl start ssh
  5. สำเร็จแล้ว เย้ ลองซะหน่อย
  6. เปิดเครื่องอื่นขึ้นมา ssh {{user}}@{{server_ip}} เช่น ssh user@192.168.1.100

## Install MicroK8s (https://ubuntu.com/kubernetes/install)

ติดตั้ง kubernetes ใช้งาน

  1. เปิด Terminal ขึ้นมา
  2. Install microk8s ด้วยคำสั่ง
    sudo snap install microk8s — classic
  3. เช็คซะหน่อย สำเร็จรึยัง
    microk8s status — wait-ready
  4. Microk8s มี Dashboard ให้ใช้ด้วยนะ ด้วยคำสั่ง
    microk8s dashboard-proxy
dashboard-proxy

เข้า Link https://127.0.0.1:10443 จากนั้น Copy tokenใส่ลงไปในช่อง Enter token

https://127.0.0.1:10443

ท๊า ด๊ามมมมมม ได้แล้ว command ไม่ตอบโจทย์ GUI เพื่อการศึกษาสิคุ้นเคย~

dashboard

## Install cloudflare zero trust

เพื่อ Routing domain ของเราไปยัง kubernetes ที่ติดตั้งไว้

  1. เข้า https://www.cloudflare.com/
  2. เลือก Zero Trust
  3. เลือก Create a tunnel ตามรูปเลยจ้า

4. ตั้งชื่อซะหน่อย ผมตั้งว่า hello-world

5. Install connector โดย copy คำสั่งที่ cloudflare ให้มา run บน server สุดที่รักของเรา

6. Route tunnel ของผมจะให้ https://hello-world.theduckdose.com ชี้ไปหา localhost:80 ในเครื่อง

7. ดูผลลัพธ์กันหน่อย ถูกต้องเลยทีเดียว ส่วน cloudflare สำเร็จแล้ว

## Deploy Hello World Application

  1. ผมทำ API Server ง่ายๆที่ Return “Hello World” ออกมา Build ขึ้น Docker Hub > https://hub.docker.com/r/doctoraod/hello-world
  2. เราจะทำการ Routing Application ของเรา ไปยัง localhost:80 เพื่อให้เข้าผ่าน hello-world.theduckdose.com ได้
  3. มี 3 ส่วนเริ่มต้นที่เราต้องทำบน Kube คือ Deployment, Service, Ingress

สร้างไฟล์ deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
labels:
app: hello-world
spec:
replicas: 1
selector:
matchLabels:
app: hello-world
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: doctoraod/hello-world:0.0.2
ports:
- containerPort: 3000

สร้างไฟล์ service.yaml

apiVersion: v1
kind: Service
metadata:
name: hello-world-service
spec:
selector:
app: hello-world
ports:
- protocol: TCP
port: 80
targetPort: 3000

สร้างไฟล์ ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: core-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: "hello-world.theduckdose.com"
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: hello-world-service
port:
number: 80

4. Apply ทั้ง 3 ไฟล์ ด้วย 3 คำสั่งนี้

microk8s kubectl apply -f deployment.yaml
microk8s kubectl apply -f service.yaml
microk8s kubectl apply -f ingress.yaml

5. Check ผลลัพธ์ กันหน่อย https://hello-world.theduckdose.com/

hello world

ก็จบแล้วนะครับเจอกันใหม่ EP หน้า…ฝากเพจด้วยนะ
https://www.facebook.com/theduckdose

Ref:

https://ubuntu.com/
https://microk8s.io/
https://www.cloudflare.com/zero-trust/

--

--