Kompose - 開始使用 - Kompose 繁體中文

開始使用

這是您開始使用 Kompose 的方法!

根據您的容器協調器和作業系統,有三個不同的指南。

對於初學者和最佳相容性,請遵循Minikube 和 Kompose指南。

Minikube 和 Kompose

在本指南中,我們將部署範例 compose.yaml 檔案到 Kubernetes 群集。

需求

啟動 minikube

如果您尚未執行 Kubernetes 群集,minikube 是開始使用的最佳方式。

$ minikube start
Starting local Kubernetes v1.7.5 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster

下載 範例 Docker Compose 檔案,或使用您自己的

wget https://raw.githubusercontent.com/kubernetes/kompose/main/examples/compose.yaml

將您的 Docker Compose 檔案轉換為 Kubernetes

在與您的 compose.yaml 檔案相同的目錄中執行 kompose convert

$ kompose convert
INFO Kubernetes file "frontend-service.yaml" created
INFO Kubernetes file "redis-leader-service.yaml" created
INFO Kubernetes file "redis-replica-service.yaml" created
INFO Kubernetes file "frontend-deployment.yaml" created
INFO Kubernetes file "redis-leader-deployment.yaml" created
INFO Kubernetes file "redis-replica-deployment.yaml" created

然後您可以使用 kubectl apply 在 Kubernetes 中建立這些資源。

存取新部署的服務

由於您的服務已部署,讓我們存取它。

如果您使用 minikube,您可以透過 minikube service 命令存取它。

$ minikube service frontend

否則,使用 kubectl 查看服務使用的 IP

$ kubectl describe svc frontend
Name:                   frontend
Namespace:              default
Labels:                 service=frontend
Selector:               service=frontend
Type:                   LoadBalancer
IP:                     10.0.0.183
LoadBalancer Ingress:   123.45.67.89
Port:                   80      80/TCP
NodePort:               80      31144/TCP
Endpoints:              172.17.0.4:80
Session Affinity:       None
No events.

注意:如果您使用的是雲端供應商,您的 IP 將會列在 Load Balancer Ingress 旁邊。

如果您尚未公開您的服務(例如,在 GCE 內),請使用命令

kubectl expose deployment frontend --type="LoadBalancer"

為檢查功能,您也可以 curl URL。

$ curl http://123.45.67.89

Minishift 和 Kompose

在本指南中,我們將部署範例 compose.yaml 檔案到 OpenShift 群集。

需求

注意:此服務將於您使用 oc expose 建立 OpenShift 路由之前,無法存取。您還必須有虛擬化環境設定。預設情況下,minishift 使用 KVM。

啟動 minishift

Minishift 是一個工具,可協助在 VM 內使用單節點群集於當地執行 OpenShift。類似於 minikube

$ minishift start
Starting local OpenShift cluster using 'kvm' hypervisor...
-- Checking OpenShift client ... OK
-- Checking Docker client ... OK
-- Checking Docker version ... OK
-- Checking for existing OpenShift container ... OK
...

下載 範例 Docker Compose 檔案,或使用您自己的

wget https://raw.githubusercontent.com/kubernetes/kompose/main/examples/compose.yaml

將您的 Docker Compose 檔案轉換為 OpenShift

在與您的 compose.yaml 檔案相同的目錄中執行 kompose convert --provider=openshift

$ kompose convert --provider=openshift
INFO OpenShift file "frontend-service.yaml" created
INFO OpenShift file "redis-leader-service.yaml" created
INFO OpenShift file "redis-replica-service.yaml" created
INFO OpenShift file "frontend-deploymentconfig.yaml" created
INFO OpenShift file "frontend-imagestream.yaml" created
INFO OpenShift file "redis-leader-deploymentconfig.yaml" created
INFO OpenShift file "redis-leader-imagestream.yaml" created
INFO OpenShift file "redis-replica-deploymentconfig.yaml" created
INFO OpenShift file "redis-replica-imagestream.yaml" created

然後您可以使用 kubectl apply 在 OpenShift 群集中建立這些資源。

存取新部署的服務

部署後,您必須建立 OpenShift 路由,才能存取該服務。

如果你正在使用 minishift,你將使用 ocminishift 命令之組合來存取此服務。

使用 ocfrontend 服務建立路由

$ oc expose service/frontend
route "frontend" exposed

使用 minishift 存取 frontend 服務

$ minishift openshift service frontend --namespace=myproject
Opening the service myproject/frontend in the default browser...

你也可以存取 OpenShift 的 GUI 介面來取得已部署容器的概觀

$ minishift console
Opening the OpenShift Web console in the default browser...

RHEL 和 Kompose

在此指南中,我們將使用 RHEL (Red Hat Enterprise Linux) 和 OpenShift 部署範例 compose.yaml 檔案。

需求

注意: 必須設定 KVM 虛擬化監視程式才能在 RHEL 正確使用 minishift。你可以使用「設定虛擬化環境」下的 CDK 文件 來設定 KVM。

安裝 Red Hat Development Suite

在我們能同時使用 minishiftkompose 之前,必須先安裝 DevSuite。即可取得更精簡的 安裝文件

切換至 root。

$ su -

啟用 Red Hat Developer Tools 軟體儲存庫。

$ subscription-manager repos --enable rhel-7-server-devtools-rpms
$ subscription-manager repos --enable rhel-server-rhscl-7-rpms

將 Red Hat Developer Tools 金鑰新增至你的系統。

$ cd /etc/pki/rpm-gpg
$ wget -O RPM-GPG-KEY-redhat-devel https://#/security/data/a5787476.txt
$ rpm --import RPM-GPG-KEY-redhat-devel

安裝 Red Hat Development Suite 和 Kompose。

$ yum install rh-devsuite kompose -y

啟動 minishift

在我們開始之前,我們必須先執行幾個設定 minishift 的初步步驟。

$ su -
$ ln -s /var/lib/cdk-minishift-3.0.0/minishift /usr/bin/minishift
$ minishift setup-cdk --force --default-vm-driver="kvm"
$ ln -s /home/$(whoami)/.minishift/cache/oc/v3.5.5.8/oc /usr/bin/oc

現在我們可以開始 minishift

$ minishift start
Starting local OpenShift cluster using 'kvm' hypervisor...
-- Checking OpenShift client ... OK
-- Checking Docker client ... OK
-- Checking Docker version ... OK
-- Checking for existing OpenShift container ... OK
...

下載 範例 Docker Compose 檔案,或使用您自己的

wget https://raw.githubusercontent.com/kubernetes/kompose/main/examples/compose.yaml

將您的 Docker Compose 檔案轉換為 OpenShift

在與您的 compose.yaml 檔案相同的目錄中執行 kompose convert --provider=openshift

$ kompose convert --provider=openshift
INFO OpenShift file "frontend-service.yaml" created
INFO OpenShift file "redis-leader-service.yaml" created
INFO OpenShift file "redis-replica-service.yaml" created
INFO OpenShift file "frontend-deploymentconfig.yaml" created
INFO OpenShift file "frontend-imagestream.yaml" created
INFO OpenShift file "redis-leader-deploymentconfig.yaml" created
INFO OpenShift file "redis-leader-imagestream.yaml" created
INFO OpenShift file "redis-replica-deploymentconfig.yaml" created
INFO OpenShift file "redis-replica-imagestream.yaml" created

然後你可以使用 kubectl apply 在 OpenShift 中建立這些資源。

存取新部署的服務

部署後,您必須建立 OpenShift 路由,才能存取該服務。

如果你正在使用 minishift,你將使用 ocminishift 命令之組合來存取此服務。

使用 ocfrontend 服務建立路由

$ oc expose service/frontend
route "frontend" exposed

使用 minishift 存取 frontend 服務

$ minishift openshift service frontend --namespace=myproject
Opening the service myproject/frontend in the default browser...

你也可以存取 OpenShift 的 GUI 介面來取得已部署容器的概觀

$ minishift console
Opening the OpenShift Web console in the default browser...