
容器運行時云原生網(wǎng)絡【免費下載鏈接】rkt[Project ended] rkt is a pod-native container engine for Linux. It is composable, secure, and built on standards.項目地址https://gitcode.com/gh_mirrors/rk/rkt點擊查看免費下載本指南基于 rkt 倉庫Documentation/examples/build-container目錄下的完整示例講解如何用 acbuild 從零構建兩個 ACI應用容器鏡像——一個基于 Alpine 的 PostgreSQL 數(shù)據(jù)庫鏡像與一個連接該數(shù)據(jù)庫的 Play Framework 示例應用并將二者放入同一個 rkt Pod 中協(xié)同運行。讀完本文你將掌握 ACI 鏡像的掛載點mount point設計、pre-start 初始化鉤子、--volume卷綁定與組合 Pod 啟動的完整實戰(zhàn)方法。示例全景兩個 ACI 組合成一個完整應用棧該示例目錄以構建容器為主題提供兩個可獨立構建、又可組合運行的 ACI 鏡像PostgreSQL 鏡像example.com/postgres提供數(shù)據(jù)庫服務并負責初始化數(shù)據(jù)Play Framework 示例應用example.com/play-example:17.10一個極簡 Web 應用每被訪問一次就把計數(shù)器寫入 PostgreSQL 并回顯該頁面已被檢索 N 次。Play 示例應用依賴 PostgreSQL 數(shù)據(jù)庫因此兩個鏡像被設計為可在同一個 Pod中運行rkt 的 Pod 天然共享網(wǎng)絡命名空間應用通過127.0.0.1即可訪問同 Pod 內(nèi)的數(shù)據(jù)庫無需暴露公網(wǎng)端口。這種一個 Pod 承載完整應用棧的組合方式正是 rkt Pod 模型的核心使用場景。構建工具鏈acbuild 與 ACI 鏡像示例中的兩個構建腳本都依賴 [containers/build] 項目提供的acbuild命令行工具需要獲取其最新 release。acbuild 的工作方式與 Dockerfile 類似但產(chǎn)物是符合 appc 規(guī)范的 ACI 鏡像它以acbuild begin開始一次構建、以acbuild write寫出.aci文件中間通過一系列acbuild --debug子命令聲明依賴、執(zhí)行命令、設置環(huán)境變量與掛載點。兩個腳本開頭都有相同的守護式寫法#!/usr/bin/env bash set -e if [ $EUID -ne 0 ]; then echo This script uses functionality which requires root privileges exit 1 fiset -e保證任何一步失敗即中止EUID檢查強制以 root 運行容器構建需要特權操作。同時使用trap注冊退出鉤子確保中途出錯時也能干凈地結束構建trap { export EXT$?; acbuild --debug end exit $EXT; } EXITPostgreSQL ACI 的構建與初始化邏輯構建腳本位于 Documentation/examples/build-container/postgres/build-postgres.sh逐段解讀如下?;?Alpine 起步并安裝軟件PGDATA/var/lib/postgresql/data # Start the build with an empty ACI acbuild --debug begin # Name the ACI acbuild --debug set-name example.com/postgres # Based on alpine acbuild --debug dep add quay.io/coreos/alpine-sh # Install postgres and bash acbuild --debug run -- apk update acbuild --debug run -- apk add postgresql bash腳本使用begin此處未指定來源從空鏡像起步、set-name命名 ACI、dep add添加 Alpine 基礎依賴、run在容器內(nèi)執(zhí)行apk安裝 PostgreSQL 與 bash。目錄、掛載點與用戶身份# Create postgres data directory acbuild --debug run -- mkdir -p $PGDATA acbuild --debug run -- chown -R postgres:postgres $PGDATA # Create postgres run directory acbuild --debug run -- mkdir -p /var/run/postgresql acbuild --debug run -- chown -R postgres:postgres /var/run/postgresql # Add a mount point for postgres data acbuild --debug mount add data $PGDATA # Add a mount point for custom initialization data acbuild --debug mount add custom-sql /customize.sql這里定義了鏡像的兩個關鍵掛載點掛載點名稱容器內(nèi)路徑用途data/var/lib/postgresql/data即$PGDATAPostgreSQL 數(shù)據(jù)目錄用于持久化數(shù)據(jù)庫文件custom-sql/customize.sql可選的自定義初始化 SQL 腳本掛載后由 pre-start 鉤子執(zhí)行隨后設置用戶與運行環(huán)境# Set PGDATA env variable acbuild --debug environment add PGDATA $PGDATA # Set user and group acbuild --debug set-user postgres acbuild --debug set-group postgres # Set postgres user, group, and test-db acbuild --debug environment add POSTGRES_USER rkt acbuild --debug environment add POSTGRES_PASSWORD rkt acbuild --debug environment add POSTGRES_DB rkt默認創(chuàng)建名為rkt的數(shù)據(jù)庫用戶名與密碼同樣為rkt。這些環(huán)境變量與 Docker 官方 postgres 鏡像的約定保持一致可被 pre-start 腳本消費也可在rkt run時通過--set-env覆蓋后文詳述。pre-start 初始化鉤子與端口# Add pre-start hook that will set up the database acbuild --debug copy postgres-prestart.sh /root/postgres-prestart.sh acbuild --debug set-event-handler pre-start /root/postgres-prestart.sh postgres # Add postgres port acbuild --debug port add postgres tcp 5432 # Run postgres server acbuild --debug set-exec postgres三個關鍵點pre-start 鉤子通過set-event-handler pre-start注冊Pod 啟動前會以postgres用戶執(zhí)行初始化腳本下文展開端口聲明port add postgres tcp 5432聲明容器監(jiān)聽 TCP 5432PostgreSQL 默認端口這是 ACI 清單中可選的端口聲明默認執(zhí)行命令set-exec postgres定義容器啟動后的主進程即直接運行postgres服務。最終write --overwrite postgres-latest-linux-amd64.aci寫出產(chǎn)物。pre-start 初始化腳本默認建庫與數(shù)據(jù)復用Documentation/examples/build-container/postgres/postgres-prestart.sh 改編自 Docker 官方 postgres 鏡像的入口腳本MIT 許可但刻意不在結尾使用exec $——因為它是 pre-start 鉤子而非主進程主進程由set-exec postgres另行啟動。腳本的核心邏輯如下用戶身份切換若以 root 身份啟動且主參數(shù)為postgres則創(chuàng)建/修復$PGDATA與/var/run/postgresql的屬主與權限chmod 700/775隨后以su postgres重新執(zhí)行自身數(shù)據(jù)目錄探測關鍵復用邏輯檢查$PGDATA/PG_VERSION是否存在# look specifically for PG_VERSION, as it is expected in the DB dir if [ ! -s $PGDATA/PG_VERSION ]; then若該文件不存在說明是全新數(shù)據(jù)目錄執(zhí)行initdb --usernamepostgres初始化并依據(jù)POSTGRES_PASSWORD決定認證方式有密碼用md5無密碼則輸出警告并回退到trust警告文本明確提示在 rkt 默認配置下可訪問該端口的基本是同系統(tǒng)上的任意其他容器應通過rkt run --set-env POSTGRES_PASSWORD...顯式設置密碼若PG_VERSION已存在則跳過全部初始化直接復用現(xiàn)有數(shù)據(jù)目錄——這正是 README 所述若掛載中存在 PostgreSQL 數(shù)據(jù)目錄則直接使用的實現(xiàn)依據(jù)建庫與授權按POSTGRES_DB默認rkt創(chuàng)建數(shù)據(jù)庫按POSTGRES_USER創(chuàng)建/修改用戶并授予超級權限自定義 SQL若容器內(nèi)存在/customize.sql即custom-sql掛載點的內(nèi)容則通過psql -f /customize.sql執(zhí)行if [ -f /customize.sql ]; then echo Using file /customize.sql to configure database ${psql[]} -f /customize.sql fi收尾以pg_ctl -m fast停止臨時實例并通過sed將postgresql.conf的listen_addresses改為*使服務監(jiān)聽所有接口。Play Framework 應用 ACI構建腳本位于 Documentation/examples/build-container/play-example/build-play-example.sh它從 Docker 鏡像ubuntu:17.10起步。構建過程# Start the build with an empty ACI acbuild --debug begin docker://ubuntu:17.10 ... # Name the ACI acbuild --debug set-name example.com/play-example # Install java, psql, and git acbuild --debug run -- apt-get update acbuild --debug run -- apt-get -y install openjdk-8-jre openjdk-8-jdk git postgresql # Clone play example repo acbuild --debug run -- git clone https://github.com/ics-software-engineering/play-example-postgresql.git /play-example acbuild --debug run -- apt-get -y autoremove --purge git acbuild --debug run -- chown -R postgres:postgres /play-example STAGE_DIR/play-example/target/universal/stage/ TARGET_BIN${STAGE_DIR}/bin/play-example-postgresql # Build example acbuild --debug run -- /bin/bash -c cd /play-example su postgres -c ./activator stage要點安裝openjdk-8運行/編譯 Java與postgresql提供psql客戶端克隆 Play 示例應用源碼后立即autoremove --purge git移除構建期工具避免把不必要的軟件帶入最終鏡像使用activator stage將應用打成可直接執(zhí)行的發(fā)布目錄target/universal/stage/與 PostgreSQL 鏡像一致set-user/set-group同樣設置為postgres。啟動編排等待數(shù)據(jù)庫就緒# Copy wait for postgres script acbuild --debug copy wait-for-postgres.sh /wait-for-postgres.sh # Run postgres server acbuild --debug set-exec /wait-for-postgres.sh 127.0.0.1 ${TARGET_BIN}由于兩個 App 位于同一 Pod、共享網(wǎng)絡棧Play 應用直接以127.0.0.1作為數(shù)據(jù)庫地址。但 Pod 內(nèi)兩個 App 的啟動并無嚴格順序保證因此主命令被替換為 Documentation/examples/build-container/play-example/wait-for-postgres.sh 這個先等待、再執(zhí)行的編排腳本host$1 shift cmd$ until psql -h $host -U postgres -c \q; do 2 echo Postgres is unavailable - sleeping sleep 10 done 2 echo Postgres is up - executing command exec $cmd腳本不斷用psql -h 127.0.0.1 -U postgres -c \q探測 PostgreSQL 是否已就緒每 10 秒重試一次一旦連通便exec正式的應用啟動命令。這種輪詢式健康檢查是組合 Pod 中解決依賴服務未啟動問題的常用手段。自定義 SQL為 Play 應用準備賬號與數(shù)據(jù)庫Documentation/examples/build-container/play-example/custom.sql 內(nèi)容如下CREATE ROLE root SUPERUSER LOGIN; CREATE DATABASE play_example_postgresql;它創(chuàng)建了 Play 應用所需的超級用戶root與數(shù)據(jù)庫play_example_postgresql。該文件在運行階段通過--volume custom-sql,kindhost,source$PWD/play-example/custom.sql以宿主卷形式掛載進 PostgreSQL 鏡像的/customize.sql由 pre-start 鉤子在數(shù)據(jù)庫初始化階段執(zhí)行——這就是 README 所說play 示例的 SQL 定制腳本的傳遞鏈路。構建鏡像命令與預期產(chǎn)物在Documentation/examples/build-container目錄下以特權 shell 依次執(zhí)行兩個構建腳本# (cd postgres ./build-postgres.sh) [...] # (cd play-example ./build-play-example.sh) [...]兩個腳本均以 root 運行內(nèi)部已做EUID校驗構建完成后每個目錄各產(chǎn)出對應的 ACI 文件# ls */*.aci play-example/play-latest-linux-amd64.aci postgres/postgres-latest-linux-amd64.aci在單個 Pod 中運行兩個應用啟動命令逐參數(shù)解析README 給出的啟動命令是# rkt --insecure-optionsimage \ run \ --volume custom-sql,kindhost,source$PWD/play-example/custom.sql \ postgres/postgres-latest-linux-amd64.aci \ play-example/play-latest-linux-amd64.aci各參數(shù)含義--insecure-optionsimage跳過對鏡像的簽名驗證允許運行未簽名的本地構建鏡像本地acbuild write產(chǎn)出的 ACI 沒有可信簽名默認校驗會失敗run創(chuàng)建并啟動一個 Pod兩個.aci作為兩個 App 放入同一 Pod共享網(wǎng)絡與資源--volume custom-sql,kindhost,source$PWD/play-example/custom.sql把宿主上的custom.sql以宿主卷形式提供給 Pod卷名custom-sql與 PostgreSQL ACI 清單中聲明的掛載點同名因此 rkt 會自動將其掛載到鏡像內(nèi)的/customize.sql。這里體現(xiàn)了 rkt 卷機制的一個關鍵約定--volume只聲明卷具體的掛載路徑由 ACI 清單中的掛載點mount point決定。詳見 Documentation/subcommands/run.md 與 rkt/run.go 中run子命令的語法說明--volume NAME,kindhost,sourceSOURCE_PATH,readOnlyBOOL,recursiveBOOL其中NAME與source宿主路徑必填readOnly默認falserecursive在 coreos 與 KVM stage1 下默認true、在 fly stage1 下默認false。若某個掛載點在鏡像清單中已聲明、但運行期未提供對應卷rkt 會自動為其創(chuàng)建隱式empty卷。若 ACI 未聲明掛載點還可使用--mount volumeNAME,targetPATH顯式將卷綁定到指定路徑--mount放在 App 名之前對所有 App 生效放在某個 App 之后僅對其生效。驗證運行結果Pod 啟動并完成數(shù)據(jù)庫初始化后先通過rkt list獲取 Pod 的 IP示例中為172.16.28.26# rkt list UUID APP IMAGE NAME STATE CREATED STARTED NETWORKS a94b94eb postgres example.com/postgres running 11 seconds ago 11 seconds ago default:ip4172.16.28.26 play-example example.com/play-example:17.10注意兩個 App 共享同一個 Pod UUIDa94b94eb且共用默認網(wǎng)絡分配的 IP172.16.28.26。由于同一 Pod 內(nèi)網(wǎng)絡共享Play 應用正是通過127.0.0.1訪問數(shù)據(jù)庫的。Play 框架應用默認監(jiān)聽 9000 端口直接對該 IP 發(fā)起 HTTP 請求即可驗證計數(shù)功能# curl 172.16.28.26:9000 [...] This page has been retrieved 0 times. [...] # curl 172.16.28.26:9000 [...] This page has been retrieved 1 times. [...]每次訪問頁面回顯的檢索次數(shù)都會 1且該計數(shù)值持久化在 PostgreSQL 中——完整驗證了PostgreSQL 初始化 → 掛載自定義 SQL 建庫 → Play 應用寫入并讀取計數(shù)的整條鏈路。小結與延伸閱讀這個示例濃縮了 rkt 日常使用的三塊核心能力ACI 構建acbuild begin/run/copy/environment/mount/set-event-handler/write一套命令即可產(chǎn)出可分發(fā)、可校驗的鏡像卷與持久化通過掛載點聲明 --volume宿主卷實現(xiàn)數(shù)據(jù)庫數(shù)據(jù)目錄的持久化與自定義初始化 SQL 的注入組合 Pod 編排多 App 共享 Pod 網(wǎng)絡棧配合 pre-start 鉤子與等待服務就緒腳本解決啟動順序問題。進一步閱讀可參考倉庫內(nèi)的相關資料卷掛載的完整語法與empty/host兩類卷 Documentation/subcommands/run.mdrun子命令的全部參數(shù)說明--volume、--mount、--set-env等 rkt/run.goPod 清單格式與掛載點、事件鉤子的規(guī)范定義 Documentation/pod-manifest.md 與 Documentation/aci-hosting.md鏡像獲取、校驗與存儲的通用行為 Documentation/image-fetching-behavior.md。贊分享容器運行時云原生網(wǎng)絡【免費下載鏈接】rkt[Project ended] rkt is a pod-native container engine for Linux. It is composable, secure, and built on standards.項目地址https://gitcode.com/gh_mirrors/rk/rkt點擊查看免費下載相關推薦rkt 快速上手指南構建 ACI 鏡像并運行你的第一個容器化應用rkt 快速上手指南構建 ACI 鏡像并運行你的第一個容器化應用 本篇指南以 rktApp Container 規(guī)范的參考實現(xiàn)為背景帶你從零開始體驗完整容器運行時云原生網(wǎng)絡理解 rkt 的 App Container 基礎ACI 鏡像、Pod 執(zhí)行單元與 appc 規(guī)范實現(xiàn)驗證理解 rkt 的 App Container 基礎ACI 鏡像、Pod 執(zhí)行單元與 appc 規(guī)范實現(xiàn)驗證 導讀 本文以 rkt 官方文檔 Documenta容器運行時云原生網(wǎng)絡rkt export 實戰(zhàn)指南將已退出 Pod 導出為 ACI 鏡像rkt export 實戰(zhàn)指南將已退出 Pod 導出為 ACI 鏡像 rkt export 子命令負責把一個 已經(jīng)退出exited的單應用 Pod 的根文容器運行時云原生網(wǎng)絡上一篇大麥自動搶票腳本上手指南配置說明與常見問題排查下一篇如何快速啟動eShopOnAbp項目5分鐘搭建完整電商微服務系統(tǒng)創(chuàng)作聲明:本文部分內(nèi)容由AI輔助生成(AIGC),僅供參考