TypeScript को Kubernetes पर डिप्लॉय करना: शुरुआती लोगों के लिए बुनियादी गाइड
\n\nआधुनिक वेब डेवलपमेंट कंटेनरीकरण और ऑर्केस्ट्रेशन के बिना अधूरा है। यदि आप TypeScript में लिखते हैं और चाहते हैं कि आपका एप्लिकेशन स्थिर रूप से काम करे, स्वचालित रूप से स्केल हो और फॉल्ट-टॉलरेंट हो — तो Kubernetes (K8s) आपका सबसे अच्छा दोस्त बन जाएगा। इस गाइड में हम चरण-दर-चरण समझेंगे कि TypeScript एप्लिकेशन को Docker में कैसे पैकेज करें और इसे Kubernetes क्लस्टर में कैसे डिप्लॉय करें। यह सामग्री शुरुआती लोगों के लिए है: कोई जादू नहीं, केवल स्पष्ट उदाहरण और स्पष्टीकरण।
\n\n1. TypeScript एप्लिकेशन तैयार करना
\n\nKubernetes के बारे में सोचने से पहले, यह सुनिश्चित करना होगा कि आपका एप्लिकेशन कंटेनर में काम करने के लिए तैयार है। आइए Express के साथ TypeScript में एक सरल HTTP सर्वर बनाएं।
\n\n1.1. प्रोजेक्ट इनिशियलाइज़ेशन
\nप्रोजेक्ट फ़ोल्डर बनाएं और इनिशियलाइज़ेशन करें:
\nmkdir my-ts-app && cd my-ts-app\nnpm init -y\nnpm install express\nnpm install -D typescript @types/node @types/express ts-node nodemon\n\n1.2. TypeScript कॉन्फ़िगरेशन
\ntsconfig.json फ़ाइल बनाएं:
{\n "compilerOptions": {\n "target": "ES2020",\n "module": "commonjs",\n "outDir": "./dist",\n "rootDir": "./src",\n "strict": true,\n "esModuleInterop": true,\n "skipLibCheck": true\n },\n "include": ["src/**/*"],\n "exclude": ["node_modules", "dist"]\n}\n\n1.3. एप्लिकेशन कोड
\nsrc फ़ोल्डर और index.ts फ़ाइल बनाएं:
import express, { Request, Response } from 'express';\n\nconst app = express();\nconst PORT = process.env.PORT || 3000;\n\napp.get('/', (req: Request, res: Response) => {\n res.json({ message: 'Hello from TypeScript on Kubernetes!' });\n});\n\napp.listen(PORT, () => {\n console.log(`Server running on port ${PORT}`);\n});\n\n1.4. बिल्ड और रन
\npackage.json में स्क्रिप्ट जोड़ें:
"scripts": {\n "build": "tsc",\n "start": "node dist/index.js",\n "dev": "ts-node src/index.ts"\n}\nलोकल रन की जाँच करें: npm run dev और http://localhost:3000 खोलें।
2. Docker के साथ कंटेनरीकरण
\n\nKubernetes कंटेनरों के साथ काम करता है। हमें अपने TypeScript एप्लिकेशन का Docker इमेज बनाना होगा।
\n\n2.1. Dockerfile
\nप्रोजेक्ट की रूट में Dockerfile फ़ाइल बनाएं:
FROM node:18-alpine AS builder\nWORKDIR /app\nCOPY package*.json ./\nRUN npm ci\nCOPY . .\nRUN npm run build\n\nFROM node:18-alpine AS production\nWORKDIR /app\nCOPY --from=builder /app/dist ./dist\nCOPY --from=builder /app/package*.json ./\nRUN npm ci --only=production\nEXPOSE 3000\nCMD ["node", "dist/index.js"]\nहम multi-stage बिल्ड का उपयोग कर रहे हैं: पहले TypeScript कंपाइल करें, फिर एक हल्का फाइनल इमेज बनाएं।
\n\n2.2. इमेज बिल्ड करना
\ndocker build -t my-ts-app:latest .\nलोकल रूप से जाँच करें:
\ndocker run -p 3000:3000 my-ts-app:latest\n\n2.3. .dockerignore
\nअनावश्यक फ़ाइलों को कॉपी करने से बचने के लिए .dockerignore बनाएं:
node_modules\ndist\n.git\n*.md\n\n3. Kubernetes में डिप्लॉयमेंट
\n\nअब जब हमारे पास Docker इमेज है, तो Kubernetes पर आगे बढ़ सकते हैं। लोकल टेस्टिंग के लिए मैं Minikube या Kind सुझाता हूँ।
\n\n3.1. Minikube शुरू करना
\nminikube start --driver=docker\nminikube status\n\n3.2. Minikube में इमेज लोड करना
\nKubernetes हमारी इमेज ढूंढ सके, इसके लिए इसे Minikube एनवायरनमेंट में लोड करें:
\nminikube image load my-ts-app:latest\n\n3.3. Deployment बनाना
\ndeployment.yaml फ़ाइल बनाएं:
apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: my-ts-app-deployment\nspec:\n replicas: 2\n selector:\n matchLabels:\n app: my-ts