เมื่อใช้ Kubernetes ในสภาพแวดล้อมองค์กร คุณมักต้องการที่จะเก็ฉรักษา Docker images ของคุณเองแทนที่จะพึ่งพิง Docker Hub หรือ registry สาธารณะอื่นๆ ในบทความนี้ เราจะอธิบายวิธีการสร้าง Private Container Registry บน Cloud VPS ของ ผู้ให้บริการโฮสติ้ง และวิธีการใช้งานกับ Kubernetes อย่างมีประสิทธิภาพ
ทำไมต้อง Private Container Registry?
Private Container Registry มีข้อดีมากมาย:
- ความเป็นส่วนตัว: คุณสามารถเก็ฉรักษา Docker images ของคุณแบบส่วนตัว ไม่ต้องกังวลเรื่องการรั่วไหลของโคด
- ความปลอดภัย: ควบคุมการเข้าถึง images ด้วย authentication และ authorization
- ประสิทธิภาพ: ลดเวลา pull image เพราะ registry อยู่บน VPS ของคุณเองในเครือข่าย
- ประหยัดค่าใช้จ่าย: ไม่มีค่าใช้จ่ายเกี่ยวกับ bandwidth ในการ pull images จากที่อื่น
- ควบคุมเวอร์ชัน: ง่ายต่อการจัดการ images versions และ cleanup
ตัวเลือก: Docker Registry vs Harbor
มีหลายตัวเลือกสำหรับการสร้าง Private Container Registry:
Docker Registry
Docker Registry เป็นตัวเลือกที่ง่ายที่สุด สามารถรันเป็น Docker container ได้ทันทีและใช้พื้นที่ต่ำ เหมาะสมงานรเลิกหลังสำหรับงานปํ์มกลาง
Harbor
Harbor เป็น open-source registry ที่มีฟีเจอร์มากมายเช่น role-based access control (RBAC), vulnerability scanning, replication policy และ webhook integration เหมาะสุมใหมรเข้นที่ใหญ่
ติดตั้ง Docker Registry แบบง่าย
วิธีที่ง่ายที่สุดคือใช้ Docker Registry official image บน Cloud VPS ของคุณ
ขั้นตอนการติดตั้ง:
ขั้นแรก สร้าง directory สำหรับเก็บ registry data:
mkdir -p /opt/docker-registry
cd /opt/docker-registry
สร้างไฟล์ docker-compose.yml:
version: '3'
services:
registry:
image: registry:2
ports:
- "5000:5000"
environment:
REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY: /var/lib/registry
volumes:
- registry-data:/var/lib/registry
restart: always
volumes:
registry-data:
รัน registry ด้วย docker-compose:
docker-compose up -d
ตรวจสอบว่า registry กำลังทำงาน:
curl http://localhost:5000/v2/
ติดตั้ง Harbor ด้วย Docker Compose
สำหรับองค์กรที่ต้องการฟีเจอร์มากขึ้น Harbor เป็นตัวเลือกที่ดี
ขั้นตอนการติดตั้ง Harbor:
ดาวน์โหลด Harbor:
cd /opt
wget https://github.com/goharbor/harbor/releases/download/v2.8.0/harbor-offline-installer-v2.8.0.tgz
tar xzf harbor-offline-installer-v2.8.0.tgz
cd harbor
สำหรับ Cloud VPS ของ ผู้ให้บริการโฮสติ้ง คุณสามารถปรับแต่ง harbor.yml ตามความต้องการ:
cp harbor.yml.tmpl harbor.yml
# แก้ไข harbor.yml - เปลี่ยน hostname และตั้งค่า password
รัน Harbor:
./prepare
docker-compose up -d
ตั้งค่า TLS/HTTPS
เพื่อความปลอดภัย แนะนำให้ตั้งค่า TLS/HTTPS สำหรับ registry
สำหรับ Docker Registry:
สร้างใบรับรอง SSL (สามารถใช้ self-signed หรือ Let’s Encrypt):
mkdir -p /opt/docker-registry/certs
cd /opt/docker-registry/certs
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout registry.key -out registry.crt
อัปเดท docker-compose.yml:
version: '3'
services:
registry:
image: registry:2
ports:
- "443:5000"
environment:
REGISTRY_HTTP_TLS_CERTIFICATE: /certs/registry.crt
REGISTRY_HTTP_TLS_KEY: /certs/registry.key
volumes:
- registry-data:/var/lib/registry
- ./certs:/certs
restart: always
volumes:
registry-data:
สำหรับ Harbor:
Harbor รองรับ HTTPS ผ่านทาง nginx proxy ที่บรรจุอยู่ แพ้คเจช Harbor จะตั้งค่า TLS โดยอัตโนมัติหากคุณระบุใบรับรองใน harbor.yml
Push/Pull Image กับ Private Registry
หลังจากตั้งค่า registry แล้ว คุณสามารถ push และ pull images ได้
Login เข้า Registry:
docker login registry.yourdomain.com
# หรือ
docker login localhost:5000
Tag Image:
docker tag myapp:latest localhost:5000/myapp:latest
Push Image:
docker push localhost:5000/myapp:latest
Pull Image:
docker pull localhost:5000/myapp:latest
ตั้งค่า imagePullSecrets ใน Kubernetes
เพื่อให้ Kubernetes pods สามารถ pull images จาก private registry ได้ คุณต้องสร้าง secret สำหรับ authentication
สร้าง Docker Registry Secret:
kubectl create secret docker-registry regcred \
--docker-server=registry.yourdomain.com \
--docker-username=yourusername \
--docker-password=yourpassword \
[email protected]
ตรวจสอบ Secret:
kubectl get secret regcred
kubectl describe secret regcred
ใช้งานกับ Deployment
ในการสร้าง Kubernetes Deployment ที่ใช้ images จาก private registry ให้ระบุ imagePullSecrets:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
imagePullSecrets:
- name: regcred
containers:
- name: myapp
image: registry.yourdomain.com/myapp:latest
ports:
- containerPort: 8080
ในตัวอย่างนี้:
imagePullSecretsระบุ secret ที่ใช้สำหรับ authenticationimageใช้ fully qualified image name รวมถึง registry server
สรุป
การสร้าง Private Container Registry บน Cloud VPS ของ ผู้ให้บริการโฮสติ้ง นำเสนอการควบคุมและความเป็นส่วนตัวที่ดีสำหรับ Docker images ของคุณ ไม่ว่าคุณจะเลือก Docker Registry แบบง่ายหรือ Harbor ที่มีฟีเจอร์ครบครัน ทั้งสองตัวเลือกสามารถรองรับ Kubernetes clusters ได้อย่างดี
หลักการสำคัญคือ:
- เลือก registry ที่เหมาะสมกับความต้องการของคุณ
- ตั้งค่า TLS/HTTPS เสมอ
- ใช้ imagePullSecrets ใน Kubernetes เพื่อให้ pods สามารถเข้าถึง registry ได้
- จัดการ image versions อย่างเหมาะสม
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับบริการ Cloud VPS ของ ผู้ให้บริการโฮสติ้ง ที่สามารถใช้เป็นฐานสำหรับ Private Container Registry โปรดเยี่ยมชม https://de.co.th/cloud-vps
