บทความนี้อธิบายวิธีเชื่อมต่อจากเครื่อง Windows Client
ไปยัง Oracle Cloud Infrastructure Container Engine for Kubernetes หรือ OCI OKE
ผ่าน Public Kubernetes API Endpoint โดยใช้ OCI CLI และ kubectl
สิ่งที่ต้องเตรียมก่อนเริ่ม
ก่อนเชื่อมต่อ OKE ควรมีข้อมูลและเครื่องมือต่อไปนี้:
- Windows client ที่สามารถออกอินเทอร์เน็ตได้
- สิทธิ์เข้าใช้งาน OCI Tenancy
- สิทธิ์เข้าถึง OKE Cluster
- Cluster OCID ของ OKE
- Region ของ Cluster เช่น
ap-singapore-1 - Public Endpoint ของ Kubernetes API เปิดใช้งานแล้ว
- ติดตั้ง
OCI CLI - ติดตั้ง
kubectl
1. ติดตั้ง OCI CLI และ kubectl บน Windows
เปิด PowerShell แล้วติดตั้งเครื่องมือที่จำเป็นด้วยคำสั่งนี้:
winget install Oracle.OCI-CLI
winget install Kubernetes.kubectl
หลังจากติดตั้งเสร็จ ให้ปิดและเปิด PowerShell ใหม่ แล้วตรวจสอบเวอร์ชัน:
oci --version
kubectl version --client
2. ตั้งค่า OCI CLI
รันคำสั่งต่อไปนี้เพื่อเริ่มตั้งค่า OCI CLI:
oci setup config
โดยปกติไฟล์ config จะถูกสร้างไว้ที่:
C:\Users\<your-user>\.oci\config
ระหว่างการตั้งค่า OCI CLI จะต้องใช้ข้อมูลต่อไปนี้:
Tenancy OCID
User OCID
Region
API Signing Key
Fingerprint
ตัวอย่าง Region สำหรับ Singapore:
ap-singapore-1
3. ค้นหา OKE Cluster OCID
เข้าไปที่ OCI Console แล้วไปที่:
Developer Services
→ Kubernetes Clusters
→ เลือก Cluster ที่ต้องการ
→ Cluster Details
→ OCID
คัดลอกค่า Cluster OCID เก็บไว้สำหรับใช้ในขั้นตอนถัดไป
ตัวอย่างรูปแบบ Cluster OCID:
ocid1.cluster.oc1.ap-singapore-1.aaaa...
4. สร้าง kubeconfig สำหรับเชื่อมต่อ OKE ผ่าน Public Endpoint
สร้างโฟลเดอร์ .kube ก่อน:
mkdir $HOME\.kube -Force
จากนั้นรันคำสั่งนี้เพื่อสร้าง kubeconfig:
oci ce cluster create-kubeconfig `
--cluster-id <cluster_ocid> `
--file "$HOME\.kube\config" `
--region ap-singapore-1 `
--token-version 2.0.0 `
--kube-endpoint PUBLIC_ENDPOINT
ตัวอย่าง:
oci ce cluster create-kubeconfig `
--cluster-id ocid1.cluster.oc1.ap-singapore-1.aaaa... `
--file "$HOME\.kube\config" `
--region ap-singapore-1 `
--token-version 2.0.0 `
--kube-endpoint PUBLIC_ENDPOINT
ค่าที่สำคัญคือ:
--region ap-singapore-1
--kube-endpoint PUBLIC_ENDPOINT
โดย PUBLIC_ENDPOINT หมายถึงการเชื่อมต่อไปยัง Kubernetes API Server
ผ่าน Public Endpoint ของ OKE Cluster
5. ทดสอบการเชื่อมต่อด้วย kubectl
หลังจากสร้าง kubeconfig แล้ว ให้ทดสอบด้วยคำสั่ง:
kubectl get nodes
หากเชื่อมต่อสำเร็จ จะเห็นรายการ Worker Nodes ของ OKE Cluster
สามารถตรวจสอบ namespace ได้ด้วยคำสั่ง:
kubectl get namespaces
หรือตรวจสอบ pod ทั้งหมดใน cluster:
kubectl get pods -A
6. กรณีมีหลาย OCI Profile
หากในเครื่องมี OCI CLI หลาย profile สามารถระบุ profile ได้ตอนสร้าง kubeconfig:
oci ce cluster create-kubeconfig `
--cluster-id <cluster_ocid> `
--file "$HOME\.kube\config" `
--region ap-singapore-1 `
--token-version 2.0.0 `
--kube-endpoint PUBLIC_ENDPOINT `
--profile <profile_name>
หรือกำหนด environment variable ก่อนใช้งาน kubectl:
$env:OCI_CLI_PROFILE="<profile_name>"
kubectl get nodes
7. ตรวจสอบ Context ของ Kubernetes
ดู context ที่มีอยู่ใน kubeconfig:
kubectl config get-contexts
ดู context ที่กำลังใช้งานอยู่:
kubectl config current-context
หากต้องการเปลี่ยน context:
kubectl config use-context <context_name>
8. ปัญหาที่พบบ่อย
ปัญหา: Unauthorized
ตัวอย่าง error:
error: You must be logged in to the server
สาเหตุที่เป็นไปได้:
- OCI CLI config ไม่ถูกต้อง
- API Key หรือ Fingerprint ไม่ถูกต้อง
- ใช้ OCI Profile ผิด
- ผู้ใช้ไม่มี IAM Policy สำหรับเข้าถึง OKE
- Kubernetes RBAC ยังไม่อนุญาตให้ผู้ใช้นี้เข้าถึง resource ใน cluster
ปัญหา: Connection Timeout
ตัวอย่าง error:
Unable to connect to the server: i/o timeout
สาเหตุที่เป็นไปได้:
- OKE Cluster ไม่ได้เปิด Public Endpoint
- เครื่อง Windows Client ถูก firewall หรือ proxy block
- ระบุ region ผิด
- ระบุ cluster OCID ผิด
- Security List หรือ Network Security Group ของ Public Endpoint ไม่อนุญาต IP ของเครื่อง client
ปัญหา: kubectl ไม่เจอ kubeconfig
สามารถกำหนด path ของ kubeconfig ได้โดยตรง:
$env:KUBECONFIG="$HOME\.kube\config"
kubectl get nodes
ปัญหา: kubectl พยายามเชื่อมต่อไปที่ localhost:8080
หากพบ error ลักษณะนี้:
couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s":
dial tcp 127.0.0.1:8080: connectex: No connection could be made because the target machine actively refused it.
หมายความว่า kubectl ยังไม่ได้ใช้ kubeconfig ของ OKE
หรือ kubeconfig ไม่ถูกโหลดอย่างถูกต้อง
ให้ลองระบุ kubeconfig โดยตรง:
kubectl --kubeconfig "$HOME\.kube\config" get nodes
หรือกำหนด environment variable:
$env:KUBECONFIG="$HOME\.kube\config"
kubectl config get-contexts
kubectl get nodes
ตรวจสอบว่า kubeconfig ชี้ไปที่ OKE endpoint ไม่ใช่ localhost:
kubectl config view --minify
ค่า server: ควรเป็น endpoint ของ OKE เช่น https://...:6443
ไม่ใช่ http://localhost:8080
คำสั่งสรุปแบบสั้น
ใช้ชุดคำสั่งนี้เมื่อต้องการเชื่อมต่อจาก Windows Client ไปยัง OCI OKE ผ่าน Public Endpoint:
mkdir $HOME\.kube -Force
oci ce cluster create-kubeconfig `
--cluster-id <cluster_ocid> `
--file "$HOME\.kube\config" `
--region ap-singapore-1 `
--token-version 2.0.0 `
--kube-endpoint PUBLIC_ENDPOINT
kubectl get nodes
สรุป
การเชื่อมต่อ OCI OKE จาก Windows Client ผ่าน Public Endpoint มีขั้นตอนหลักคือ
ติดตั้ง OCI CLI และ kubectl, ตั้งค่า OCI CLI,
สร้าง kubeconfig ด้วยคำสั่ง oci ce cluster create-kubeconfig
และทดสอบการเชื่อมต่อด้วย kubectl
เมื่อเชื่อมต่อสำเร็จ ผู้ใช้งานสามารถบริหารจัดการ Kubernetes resource บน OKE
ได้จากเครื่อง Windows Client โดยตรงผ่านคำสั่ง kubectl
