附註:
- 本教學課程適用於 Oracle 提供的免費實驗室環境。
- 其使用 Oracle Cloud Infrastructure 證明資料、租用戶以及區間的範例值。完成實驗室時,請將這些值替代為雲端環境特定的值。
使用 Oracle Linux Automation Engine 設定 Podman Containers
簡介
Oracle Linux Automation Engine 可讓使用者建立用於安裝 Podman 的手冊,然後使用 containers.podman 集合執行與管理容器。
執行手冊時,Oracle Linux Automation Engine 會在電腦上執行與手冊中 hosts:
指令相符的任務。這些主機通常定義在產品目錄檔案中,而且可以是遠端或本機。在本教學課程中,我們將示範如何在本機執行手冊。
目標
在本教學課程中,您將學習到:
- 在本地端執行手冊
- 新增集合至播放手冊
- 安裝 Podman
- 提取並執行 oraclelinux:9 容器
必要條件
-
具有下列配置的 Oracle Linux 系統:
- 具備 sudo 權限的非 root 使用者
- 安裝 Oracle Linux Automation Engine
部署 Oracle Linux Automation Engine
注意:如果是在您自己的租用戶中執行,請先閱讀 linux-virt-labs
GitHub 專案 README.md 並完成先決條件,再部署實驗環境環境。
-
在 Luna Desktop 上開啟終端機。
-
複製
linux-virt-labs
GitHub 專案。git clone https://github.com/oracle-devrel/linux-virt-labs.git
-
變更至工作目錄。
cd linux-virt-labs/olam
-
安裝必要的集合。
ansible-galaxy collection install -r requirements.yml
-
更新 Oracle Linux 執行處理組態。
cat << EOF | tee instances.yml > /dev/null compute_instances: 1: instance_name: "ol-control-node" type: "control" olam_type: olae EOF
-
建立產品目錄檔案。
cat << EOF | tee hosts > /dev/null localhost ansible_connection=local ansible_connection=local ansible_python_interpreter=/usr/bin/python3.6 EOF
-
部署實驗環境環境。
ansible-playbook create_instance.yml -i hosts -e "@instances.yml"
免費實驗室環境需要額外的 localhost
ansible_python_interpreter
變數,因為它會為 Python 適用的 Oracle Cloud Infrastructure SDK 安裝 RPM 套件。安裝此套裝軟體的位置位於以您 Oracle Linux 版本為基礎的系統預設 Python 模組底下。使用產品目錄變數可避免影響 localhost 以外的主機上執行的播放。預設部署資源配置使用 AMD CPU。您可以在命令行中傳送新的資源配置變數定義,以變更執行處理的資源配置。
例如:
-e instance_shape="VM.Standard3.Flex"
同樣地,Oracle Linux 映像檔的預設版本會使用 `default_vars.yml 檔案中定義的變數
os_version
。您可以在命令行中傳送 Oracle Linux 主要版本,即可修改這個值。例如:
-e os_version="9"
重要事項: 請等待播放手冊順利執行,然後到達暫停工作。在手冊的這個階段,Oracle Linux 的安裝已完成,實例已就緒。請注意,上一個播放會列印其部署節點的公用和專用 IP 位址。
集合
集合是 Oracle Linux Automation Engine 內容的分送格式,可包含手冊、角色、模組及外掛程式。本教學課程將使用 requirements.yml
檔案安裝 containers.podman
集合。requirements.yml
檔案會根據檔案中定義的金鑰來安裝集合、角色或兩者。
建立需求檔案
-
開啟新的終端機,並透過 SSH 連線至 ol-control-node 系統。
ssh oracle@<ip_address_of_instance>
-
建立工作目錄。
mkdir -p ~/podman-project
-
建立需求檔案。
cat << 'EOF' | tee ~/podman-project/requirements.yml > /dev/null --- collections: - name: containers.podman EOF
-
安裝此收藏 。
ansible-galaxy collection install -r ~/podman-project/requirements.yml
輸出顯示從 Galaxy 網站擷取壓縮存檔檔案,然後將壓縮存檔檔案安裝到
.ansible/collections.
下的本位目錄的程序注意:如果輸出顯示
ERROR: Ansible requires the locale encoding to be UTF-8; Detected None.
,這表示ansible
的地區設定設定不正確。設定這兩個環境變數來修正問題:export LC_ALL="en_US.UTF-8" export LC_CTYPE="en_US.UTF-8"
安裝 Podman
Oracle Linux Automation Engine 手冊由播放組成,其中包含對應至特定主機的工作。這些工作主要執行冪等模組。無論是播放簿執行一次或多次,Idempotency 都能確保您獲得相同的結果。本教程將展示通過在階段中構建播放手冊並在每次改變之間運行。
執行 Podman 容器之前,我們需要先從 Oracle Linux AppStream 儲存區域安裝 Podman 套裝程式。
-
建立手冊檔 。
cat << 'EOF' | tee ~/podman-project/podman-playbook.yml > /dev/null --- - hosts: localhost connection: local tasks: - name: Install podman ansible.builtin.dnf: name: '@container-tools:ol8' state: present become: yes when: - ansible_distribution == 'OracleLinux' - ansible_facts['distribution_major_version'] == '8' - name: Install podman ansible.builtin.dnf: name: 'container-tools' state: present become: yes when: - ansible_distribution == 'OracleLinux' - ansible_facts['distribution_major_version'] == '9' EOF
一本手冊與任務的名稱,目的是要讓手冊自行記錄。以下資訊提供一些主要項目的進一步說明。
hosts: localhost
:指示要對 localhost 執行的工作。connection: local
:確保連結保持在本機,且不會透過 SSH 執行。become: yes
:依預設使用sudo
權限來提升此播放手冊區段中的工作。
-
執行手冊 。
由於我們針對本機主機執行此手冊,因此不需要明確地建立或參照產品目錄檔案。不過,如果想要對遠端主機執行此動作,則必須更新
hosts:
項目、移除手冊中的connection:
行,然後建立並參照產品目錄檔案。如果您不熟悉此過程,請參閱本自學課程結束時,在 Oracle Linux Training Station 中連結的一些其他自學課程。ansible-playbook ~/podman-project/podman-playbook.yml
指令的輸出應顯示該指令已針對 localhost 順利執行,並報告 Install podman 工作中的變更。此狀態表示我們已適當地完成容器工具模組的安裝。
-
顯示 Podman 套件的版本以驗證已安裝的 Podman 套件 。
podman --version
繪製圖片
安裝 Podman 之後,我們就可以從我們選擇的登錄中提取映像檔,並在本機暫存映像檔。在此步驟中,我們將從 GitHub Container Registry 提取 Oracle Linux 映像檔。
如需有關 Oracle Linux 開發人員映像檔的其他資訊,請參閱此處。
-
新增將容器映像檔提取至手冊的作業。
cat << EOF | tee -a ~/podman-project/podman-playbook.yml > /dev/null - name: Pull oraclelinux:9 from GitHub containers.podman.podman_image: name: ghcr.io/oracle/oraclelinux:9 EOF
-
執行手冊 。
ansible-playbook ~/podman-project/podman-playbook.yml
-
確認 Podman 已提取影像。
podman images
範例輸出:
REPOSITORY TAG IMAGE ID CREATED SIZE ghcr.io/oracle/oraclelinux 9 97e22ab49eea 20 hours ago 254 MB
執行容器影像
我們也可以在單一步驟中,根據影像提取和執行容器,而不只是提取影像。立即下載並執行 Oracle Linux NGINX 開發人員映像檔。
-
更新 playbook 檔案 。
cat << EOF | tee -a ~/podman-project/podman-playbook.yml > /dev/null - name: Run image containers.podman.podman_container: name: nginx image: ghcr.io/oracle/oraclelinux9-nginx:1.20 state: started detach: yes expose: - '80' - '443' publish: - '8080:80' EOF
其中:
name
:容器的名稱。image
:用來建立容器的儲存區域路徑 (或映像檔名稱) 和標記。state
:檢查符合名稱和組態的執行中容器。Podman 在找不到相符項目時,會建立並啟動新的容器。detach
:以分離模式執行容器。expose
:顯示連接埠或連接埠範圍。publish
:將容器的連接埠或一系列的連接埠發布至主機。
-
執行手冊 。
ansible-playbook ~/podman-project/podman-playbook.yml
-
請確認容器已在執行中。
podman ps
範例輸出:
[oracle@ol-server podman-project]$ podman ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f7a28cc4c6b ghcr.io/oracle/oraclelinux9-nginx:1.20 nginx -g daemon o... 2 minutes ago Up 2 minutes ago 0.0.0.0:8080->80/tcp nginx
輸出顯示容器已經啟動並在執行中 2 分鐘。
-
停止容器。
使用上述輸出的 CONTAINER ID ,執行:
podman stop $(podman ps -q -f name=nginx)
此命令使用 CONTAINER ID 作為停止 nginx 容器的參照。
使用磁碟區執行容器映像檔
Podman 會新增連結掛載來建立磁碟區,此掛載會將本機目錄對應至容器內的目錄。我們將透過執行相同的 NGINX 容器,並以自訂頁面取代預設的 index.html 頁面來示範此功能。
-
更新 playbook 檔案 。
建立本機目錄。雖然我們能夠從命令行有效率且手動執行此步驟,但還是可以將它自動化。自動化此步驟可確保每次執行播放簿時都會存在目錄。執行影像之前,請先新增建立目錄和 index.html 檔案的這些工作。
請確保在任務之間保留空白行以供閱讀,並遵循 YAML 語法規則和對齊準則。我們將使用
sed
來完成此任務,因為它可讓您在特定行中輕鬆將文字區塊插入檔案。sed -i -e '24 r '<(cat -<< EOF - name: Ensure the destination directory exists ansible.builtin.file: path: "/home/oracle/nginx/" state: directory - name: Create an empty file ansible.builtin.file: path: "/home/oracle/nginx/index.html" state: touch mode: '0755' - name: Create index.html ansible.builtin.copy: dest: "/home/oracle/nginx/index.html" content: | Hello! Welcome to Oracle Linux Containers. EOF ) ~/podman-project/podman-playbook.yml
-
然後在
Run image
工作的結尾新增下列選項。cat << EOF | tee -a ~/podman-project/podman-playbook.yml > /dev/null volume: "/home/oracle/nginx:/usr/share/nginx/html:Z" EOF
volume
選項會在source:destination
目錄之間建立連結掛載。:Z
選項可解決與連結掛載相關的任何 SELinux 權限問題。Podman 會重新標示磁碟區的內容,使其與容器內的標籤相符。此處是播放手冊的完整版本,以供參考。
-
執行手冊 。
ansible-playbook ~/podman-project/podman-playbook.yml
-
請確認容器已在執行中。
podman ps
範例輸出:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f74aa726d470 ghcr.io/oracle/oraclelinux9-nginx:1.20 nginx -g daemon o... 10 minutes ago Up 10 minutes ago 0.0.0.0:8080->80/tcp nginx
-
確認 index.html 檔案存在。
ls -l /home/oracle/nginx
範例輸出:
[oracle@ol-server podman-project]$ ls -l /home/oracle/nginx/ total 4 -rwxr-xr-x. 1 oracle oracle 41 Nov 5 16:46 index.html
-
驗證連結掛載。
您可以使用 cURL 來顯示容器的 index.html 頁面,以及歡迎訊息。
curl localhost:8080
其中 8080 是與容器中連接埠 80 的本機連接埠對應。
接下來的步驟
完成本教學課程後,您應該瞭解如何使用 Oracle Linux Automation Engine 將 Podman 和容器的工作自動化。探索 containers.podman 集合中的其他模組,以建立可自動執行基礎架構的其他手冊。
相關連結
其他學習資源
在 docs.oracle.com/learn 上探索其他實驗室,或在 Oracle Learning YouTube 頻道上存取更多免費學習內容。此外,請造訪 education.oracle.com/learning-explorer 以成為 Oracle Learning Explorer。
如需產品文件,請造訪 Oracle Help Center 。
Configure Podman Containers with Oracle Linux Automation Engine
F52953-04
Copyright ©2021, Oracle and/or its affiliates.