Pernahkah anda berharap orkestrasi kontena boleh menjadi lebih fleksibel daripada rantai pergantungan statik tetapi lebih mudah daripada Kubernetes? Temui PnR (Prompt and Response) - pendekatan dipacu konfigurasi yang memanfaatkan keupayaan abstraksi platform Go yang berkuasa untuk mengatur kontena berdasarkan keadaan kesediaan sebenar dan bukannya kebergantungan mudah.
Sebelum terjun ke PnR, mari kita fahami sebab Go sangat sesuai untuk orkestrasi kontena merentas platform:
Antara Muka API Docker Bersatu: Pustaka klien Docker Go menyediakan antara muka yang konsisten merentas Windows, Linux dan macOS melalui sambungan soket khusus platform:
Sokongan Native Concurrency: goroutin dan saluran Go membolehkan pemantauan kontena yang cekap:
Pengendalian Rangkaian Merentas Platform: Pakej net Go merumuskan butiran rangkaian khusus platform:
PnR mengatur bekas melalui tiga komponen utama:
Mari kita lihat tindakan ini dengan tindanan web biasa: MongoDB, Pelayan API dan Pelanggan Web.
{ "name": "dev_stack", "cpuxs": { "stack_startup": { "design_chunks": [ { "name": "mongodb", "gatekeeper": { "system_ready": { "prompt": "Is system ready?", "response": ["yes"], "tv": "Y" } }, "flowout": { "mongodb_ready": { "prompt": "Is MongoDB ready?", "response": ["yes"], "tv": "Y" } }, "health_check": { "type": "tcp", "port_key": "27017", "timeout_seconds": 2, "status_mapping": { "success": { "key": "mongodb_ready", "response": ["yes"], "tv": "Y" }, "failure": { "key": "mongodb_ready", "response": ["no"], "tv": "N" } } }, "container": { "name": "pnr_mongodb", "image": "mongo:latest", "ports": { "27017": "27017" } } } ] } } }
Nadi PnR ialah pengurusan kontena agnostik platformnya. Begini caranya:
func (il *ContainerIntentionLoop) Execute() error { // Create platform-specific network _, err := il.dockerClient.NetworkCreate(il.ctx, "pnr_network", types.NetworkCreate{}) if err != nil { return fmt.Errorf("failed to create network: %v", err) } for { // Update runtime state if err := il.updateRTStateFromRuntime(); err != nil { return err } allCompleted := true anyExecuting := false // Process each container for i := range il.cpux.DesignChunks { chunk := &il.cpux.DesignChunks[i] // Container state machine switch chunk.Status { case "completed": continue case "executing": anyExecuting = true allCompleted = false if il.checkChunkCompletion(chunk) { chunk.Status = "completed" } case "", "ready": allCompleted = false if il.checkGatekeeper(chunk) { if err := il.startContainer(chunk); err != nil { return err } chunk.Status = "executing" anyExecuting = true } } } // Check termination conditions if allCompleted { return nil } if !anyExecuting && !allCompleted { return fmt.Errorf("no progress possible - execution stalled") } time.Sleep(5 * time.Second) } }
PnR melaksanakan pemeriksaan kesihatan bebas platform menggunakan perpustakaan standard Go:
{ "name": "dev_stack", "cpuxs": { "stack_startup": { "design_chunks": [ { "name": "mongodb", "gatekeeper": { "system_ready": { "prompt": "Is system ready?", "response": ["yes"], "tv": "Y" } }, "flowout": { "mongodb_ready": { "prompt": "Is MongoDB ready?", "response": ["yes"], "tv": "Y" } }, "health_check": { "type": "tcp", "port_key": "27017", "timeout_seconds": 2, "status_mapping": { "success": { "key": "mongodb_ready", "response": ["yes"], "tv": "Y" }, "failure": { "key": "mongodb_ready", "response": ["no"], "tv": "N" } } }, "container": { "name": "pnr_mongodb", "image": "mongo:latest", "ports": { "27017": "27017" } } } ] } } }
Pasang Go (1.19 atau lebih baru):
Pasang Docker
func (il *ContainerIntentionLoop) Execute() error { // Create platform-specific network _, err := il.dockerClient.NetworkCreate(il.ctx, "pnr_network", types.NetworkCreate{}) if err != nil { return fmt.Errorf("failed to create network: %v", err) } for { // Update runtime state if err := il.updateRTStateFromRuntime(); err != nil { return err } allCompleted := true anyExecuting := false // Process each container for i := range il.cpux.DesignChunks { chunk := &il.cpux.DesignChunks[i] // Container state machine switch chunk.Status { case "completed": continue case "executing": anyExecuting = true allCompleted = false if il.checkChunkCompletion(chunk) { chunk.Status = "completed" } case "", "ready": allCompleted = false if il.checkGatekeeper(chunk) { if err := il.startContainer(chunk); err != nil { return err } chunk.Status = "executing" anyExecuting = true } } } // Check termination conditions if allCompleted { return nil } if !anyExecuting && !allCompleted { return fmt.Errorf("no progress possible - execution stalled") } time.Sleep(5 * time.Second) } }
func (il *ContainerIntentionLoop) checkChunkCompletion(chunk *DesignChunk) bool { // Platform-agnostic container status check isRunning, err := il.isContainerRunning(chunk.Container.Name) if !isRunning { il.updateChunkStatus(chunk, false) return false } // Health check based on configuration status := false switch chunk.HealthCheck.Type { case "tcp": addr := fmt.Sprintf("localhost:%s", chunk.Container.Ports[chunk.HealthCheck.PortKey]) conn, err := net.DialTimeout("tcp", addr, timeout) if err == nil { conn.Close() status = true } case "http": url := fmt.Sprintf("http://localhost:%s%s", chunk.Container.Ports[chunk.HealthCheck.PortKey], chunk.HealthCheck.Path) resp, err := client.Get(url) if err == nil { status = (resp.StatusCode == chunk.HealthCheck.ExpectedCode) } } il.updateChunkStatus(chunk, status) return status }
pnr-orchestrator/ ├── main.go ├── containers.go ├── config/ │ └── domain.json └── runtime/ # Created automatically
Karang Docker Tradisional:
# Create project directory mkdir pnr-orchestrator cd pnr-orchestrator # Initialize Go module go mod init pnr-orchestrator # Install dependencies go get github.com/docker/docker/client go get github.com/docker/docker/api/types go get github.com/docker/go-connections/nat
Orkestrasi pintar PnR:
# Option 1: Direct run go run main.go containers.go # Option 2: Build and run separately go build ./pnr-orchestrator # Unix/Linux/Mac pnr-orchestrator.exe # Windows
Perbezaan utama? PnR memastikan kesediaan perkhidmatan sebenar merentas mana-mana platform, bukan hanya permulaan kontena.
PnR menunjukkan cara keupayaan abstraksi platform Go yang kukuh boleh mencipta alatan orkestrasi kontena merentas platform yang mantap tanpa mengorbankan kesederhanaan atau kuasa.
Beri tahu saya dalam ulasan jika anda ingin melihat lebih banyak contoh atau mempunyai soalan tentang pelaksanaan khusus platform!
Atas ialah kandungan terperinci PnR: Orkestrasi Kontena Didorong Niat Konfigurasi dengan Abstraksi Platform Go. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!