乾貨!OpenShift離線環境OperatorHub和ImageStream配置實戰技巧

OpenShift4.5.x離線環境OperatorHub和ImageStream配置實戰技巧linux

 

圖片


1 . 前言

在離線環境使用UPI(UserProvisioned Infrastructure)方式安裝OpenShift,相對於在聯網環境採用IPI(Installer Provisioned Infrastructure)徹底自動安裝方式OpenShift,的確要相對複雜的多,好比要設置離線鏡像倉庫、將須要的鏡像上傳到離線鏡像倉庫、定製CoreOS的點火文件等。web

本文的目的不是描述OpenShift 4.5.x的完整離線安裝過程,若是要了解OpenShift 4.5.x的完整離線安裝過程,能夠參考OpenShift的官方安裝手冊,或者本文附錄中的參考文檔。shell

在標準離線環境的OpenShift安裝完成後,系統是沒有OperatorHub的,同時,系統自帶的ImageStream也下載不了公網環境的鏡像。這個時候,你既不能建立Operator,也不能採用服務模板(Catalog)方式部署應用。所以,但願可以經過本文對離線Operator安裝配置和ImageSteam的配置工做的描述,可以幫助咱們解決上面提到的兩個問題。json


2    咱們從哪兒開始api

咱們的後安裝配置工做起始於一個安裝已經成功的OpenShift的集羣。若是你在運行命令openshift-install wait-for install-complete 看到以下輸出,恭喜你!您的OpenShift集羣已經安裝完畢,咱們能夠開始咱們下一步的工做了。安全

[root@helper install]# openshift-install wait-forinstall-completebash

INFO Waiting up to 30m0s for the cluster athttps://api.ocp4.example.com:6443 to initialize...服務器

INFO Waiting up to 10m0s for the openshift-consoleroute to be created...網絡

INFO Install complete!app

INFO To access the cluster as the system:admin userwhen using 'oc', run 'export KUBECONFIG=/root/ocp4/install/auth/kubeconfig'

INFO Access the OpenShift web-console here:https://console-openshift-console.apps.ocp4.example.com

INFO Login to the console with user:"kubeadmin", and password: "xMS9Y-KLmda-nxa6i-IniEB"


固然,爲了確保一切正常,你可使用oc命令或者web控制檯查看該集羣是否一切正常。下面是用oc 命令查詢集羣的一個操做:

在正式配置OperatorHub和服務模板須要的ImageStream以前,咱們首先作一些基本的設置。

2.1 建立用戶

安裝程序默認建立了一個kubeadmin的用戶,這個用戶的密碼是超長。通常狀況下,從安全考慮出發,咱們建議建立新的管理員用戶,而且在新的管理員用戶建立後,將kubeadmin刪除。

除了支持本地認證,OpenShift也能夠支持跟企業的LDAP服務器對接,具體用戶相關的認證操做過程請參見產品文檔瞭解OpenShift認證配置。

下面的腳本咱們使用HTPasswd方式建立了2個用戶,其中用戶admin是系統管理用戶,能夠管理整個集羣;而用戶user1只是一個普通用戶。

## 使用命令行工具htpasswd在本地文件users.htpasswd建立兩個用戶

htpasswd -c -B -b users.htpasswd admin passw0rd

htpasswd -b users.htpasswd user1 passw0rd

 

## users.htpasswd的內容導入一個secrt

oc create secret generic htpass-secret--from-file=htpasswd=./users.htpasswd -n openshift-config

 

## 建立一個認證提供器,其認證內容指向剛剛建立的secret

cat << EOF > htpass.yaml

apiVersion: config.openshift.io/v1

kind: OAuth

metadata:

  name:cluster

spec:

 identityProviders:

  - name: my_htpasswd_provider

   mappingMethod: claim

    type:HTPasswd

    htpasswd:

     fileData:

        name:htpass-secret

EOF

oc apply -f htpass.yaml

 

## 給用戶admin賦予集羣管理員權限

oc adm policy add-cluster-role-to-usercluster-admin admin


如今你能夠用建立的用戶登陸Web控制檯或者oc命令行工具了。


2.2 加入離線鏡像倉庫的CA證書

咱們在安裝離線OpenShift的時候已經設置了一個離線的鏡像倉庫,所以,在安裝OperatorHub和服務模板的時候,咱們也但願把下載的鏡像上傳到該離線倉庫。若是該離線鏡像倉庫的證書是採用自簽名的證書,咱們須要讓OpenShift信任該證書。所以,咱們須要將該離線倉庫的CA證書加入OpenShift的信任證書列表裏面去。

OpenShift裏面存儲鏡像倉庫相關配置信息的是在一個叫作image.config.openshift.io/cluster的客戶定義資源(CustomerResource)裏面,咱們須要在這個CR裏面加載相關證書,具體的操做以下:


## 建立一個configmap以存放證書

## 下面的registry.ocp4.example.com是內部離線倉庫的域名,5443是端口後,須要根據狀況替換

## /etc/crts/ocp4.example.com.crt是證書文件名,須要根據實際狀況替換

oc create configmap registry-cas -nopenshift-config \

--from-file=registry.ocp4.example.com..5443=/etc/crts/ocp4.example.com.crt

 

## 修改image.config.openshift.io/cluster以引用證書

oc patch image.config.openshift.io/cluster--patch'{"spec":{"additionalTrustedCA":{"name":"registry-cas"}}}'--type=merge


到如今爲止,你已經能夠在OpenShift上用內部的鏡像倉庫裏面的鏡像來部署應用了。

注意:我在某一個4.5.x小版本中發現,儘快通過上述配置,但鏡像倉庫的證書仍是不能被OpenShift信任。若是你在配置過程當中發現上述問題,能夠簡單經過增長一個非安全鏡像倉庫方式繞過該問題,具體操做以下:

## 建立一個非安全鏡像倉庫

oc patch image.config.openshift.io/cluster -p'{"spec":{"registrySources":{"insecureRegistries":["registry.ocp4.example.com:5443"]}}}'  --type=merge


3. OperatorHub的安裝

咱們知道,OpenShift 4.5.x默認有4類的Operator:分別是:

類  別

描    述

Red Hat Operators

由紅帽公司分發並提供技術支持的Operator。

Certified Operators

由領先的獨立軟件開發商開發的並通過紅帽認證的Operator,技術支持由獨立開發商提供。

Community Operators

開源社區提供的Operator,沒有技術支持。

Marketplace Operators

能夠在紅帽軟件商店網站(Red Hat Marketplace)購買的Operator。

固然,若是客戶須要加入本身的Operator,那麼就會有第5個種類:客戶本身定製類。

爲了安裝OperatorHub,首先要把上面4類的目錄鏡像安裝上,而後再將每一類目錄鏡像中引用到的Operator中的鏡像同步進離線倉庫。

在剛安裝好的OpenShift集羣中,OpenShift默認是向一個公網的URL獲取OperatorHub信息,因爲咱們的集羣是在離線環境中,這個操做固然不能成功。咱們須要執行下面的命令來禁止這個默認行爲。

## Disable the default OperatorSources

oc patch OperatorHub cluster --type json \

-p '[{"op":"add", "path": "/spec/disableAllDefaultSources","value": true}]'

## Review OperatorHub configuration

oc get OperatorHub cluster -o yaml

爲了同步Operators的鏡像,咱們須要有一臺RHEL服務器可以鏈接到Internet,咱們將會在該服務器下載鏡像。

podman login registry.redhat.io

## <registry_host_name>is your hostname

podman login <registry_host_name>:<port>


        

3.1 紅帽提供Operator的安裝

3.1.1安裝Red Hat Operators目錄鏡像

爲了安裝Red Hat Operators目錄鏡像,咱們須要將該鏡像下載到本地的一個目錄並打包成一個tar文件。在外網服務器執行如下命令:

## create a catalog image for redhat operators

## To a local folder

oc adm catalog build \

   --appregistry-org redhat-operators \

   --from=registry.redhat.io/openshift4/ose-operator-registry:v4.5 \

   --filter-by-os="linux/amd64" \

   --to=file://offline/redhat-operators:4.5 \

   --insecure

 

## Save the operator image

tar cvf rh-operator.tar v2


將該tar文件複製到能夠訪問內部離線鏡像倉庫的服務器上,解壓文件,採用下列命令同步進離線鏡像倉庫。其中,registry.ocp4.example.com:5443是內部離線鏡像倉庫的URL(下同,再也不重複說明)。

## 複製文件到內網

## Load the catalog image

##

oc image mirrorfile://offline/redhat-operators:4.5registry.ocp4.example.com:5443/offline/redhat-operators:4.5

 

## Verify whether theoperator catalog image is uploaded

curl -u dummy:dummy https://registry.ocp4.example.com:5443/v2/catalog/redhat-operators/tags/list

## sample successful result: {"name":"catalog/redhat-operators","tags":["4.5.2-20200726"]}


而後咱們建立一個CatalogSource:

cat <<EOF > redhat-operator-catalog.yaml

apiVersion: operators.coreos.com/v1alpha1

kind: CatalogSource

metadata:

  name:redhat-operator-catalog

  namespace:openshift-marketplace

spec:

 displayName: Red Hat Operators

  sourceType:grpc

  image:registry.ocp4.example.com:5443/offline/redhat-operators:4.5

  publisher:Red Hat

EOF

oc create -f redhat-operator-catalog.yaml

 

## check the result

oc get pods -n openshift-marketplace

oc get catalogsource -n openshift-marketplace

oc get packagemanifest -n openshift-marketplace

若是如今你登陸OperatorHub, 你應該能過看到Red Hat Operators已經成功顯示出來了,可是,到如今爲止,若是你嘗試安裝一個Operator, 安裝會失敗。那是由於咱們只安裝了一個Operator目錄鏡像,而這個目錄中引用的真正Operator進行尚未被導入,接下來咱們來作這一部分工做。


3.1.2 同步Red Hat 提供Operators中引用到的鏡像

在可以訪問內部離線倉庫的服務器上面,咱們經過執行下面的這個命令,來獲取須要同步的鏡像列表:

oc adm catalog mirror \

   registry.ocp4.example.com:5443/offline/redhat-operators:4.5 \

    registry.ocp4.example.com:5443 \

    --insecure \

    --filter-by-os="linux/amd64" \

   --manifests-only

執行完該命令後,當前目錄下會建立一個redhat-operators-manifests的子目錄,子目錄下面有2個文件:

  • mapping.txt – 用來同步鏡像的文件

  • imageContentSourcePolicy.yaml –用來進行鏡像重定向的文件

首先,咱們來看一下mapping.txt的文件內容(如下爲文件片斷):

registry.redhat.io/openshift-service-mesh/prometheus-rhel8@sha256:68d47c477bb9b1a4cae6432361326efd0f75146ecf104c84b9c23afb09e77f09

=registry.ocp4.example.com:5443/openshift-service-mesh/prometheus-rhel8:27f4a38e

registry.redhat.io/container-native-virtualization/kubevirt-kvm-info-nfd-plugin:v2.2.0-3=registry.ocp4.example.com:5443/container-

native-virtualization/kubevirt-kvm-info-nfd-plugin:v2.2.0-3


文件內容是以「SoureImage=DestinationImage」,表示將鏡像從前面的原地址同步到後面的目的地址。所以,咱們注意到,文件中的每一行內容都是能夠做爲參數直接傳遞給oc image mirror進行同步的。可是,在當前的網絡配置下,因爲Internet和內部的鏡像倉庫不聯通,咱們不能直接用oc image mirror進行同步,所以,須要引入一箇中轉站。鑑於如今已經有了一個公網的服務器,所以,咱們能夠考慮將該服務器的本地目錄做爲一箇中轉站。其思路是:咱們首先將鏡像下載到公網服務器的本地目錄;而後本地目錄打包經過U盤或移動硬盤複製到內部服務器,從內部服務器在同步到內部離線倉庫。

第一步:同步鏡像到公網服務器的本地目錄。

觀察mapping.txt,將目的地址改成本地目錄:

registry.redhat.io/openshift-service-mesh/prometheus-rhel8@sha256:68d47c477bb9b1a4cae6432361326efd0f75146ecf104c84b9c23afb09e77f09

=file://offline/openshift-service-mesh/prometheus-rhel8:27f4a38e

registry.redhat.io/container-native-virtualization/kubevirt-kvm-info-nfd-plugin:v2.2.0-3=file://offline/container-

native-virtualization/kubevirt-kvm-info-nfd-plugin:v2.2.0-3


咱們能夠經過執行如下命令進行同步:

## 建立downloading.txt

sed 's/registry.ocp4.example.com:5443/file:\/\/offline/g'mapping.txt > downloading.txt

 

## 建立下載的shell文件

cat << EOF >download-rh-op.sh

#!/usr/bin/env bash

 

STAGEING_FOLDER="localrep"

IMAGE_LIST="downloading.txt"

 

x=0

echo  `date` "- Starting to download image tolocal directory : " $STAGEING_FOLDER

echo  `date` "- The image list name is :" $IMAGE_LIST

cat $IMAGE_LIST | whileread line; do

  x=$(( x+1 ))

  echo"--------------------------------------------- No ${x} Started...---------------------------------------------"

  echo `date` $TIMESTAMP "- The image to be loaded is :"

  echo $line

  oc image mirror $line  --dir=$STAGEING_FOLDER --filter-by-os=.*

  echo `date` $TIMESTAMP "- The imageloading is completed."

  echo"--------------------------------------------- No ${x} Completed ---------------------------------------------"

  echo

done

echo  `date` "- All images are downloaded intothe local folder! Please check the local directory:" $STAGEING_FOLDER

EOF

chmod u+xdownload-rh-op.sh

nohup ./download-rh-op.sh  >> download-rh-operators.out2>&1 &

第二步:將目錄localrep打包壓縮複製到內網服務器

第三步:從本地目錄同步鏡像到內部離線鏡像倉庫。

再次觀察mapping.txt,將原地址改成本地目錄:

file://offline/openshift-service-mesh/prometheus-rhel8@sha256:68d47c477bb9b1a4cae6432361326efd0f75146ecf104c84b9c23afb09e77f09

=registry.ocp4.example.com:5443/openshift-service-mesh/prometheus-rhel8:27f4a38e

file://offline/container-native-virtualization/kubevirt-kvm-info-nfd-plugin:v2.2.0-3=registry.ocp4.example.com:5443/container-

native-virtualization/kubevirt-kvm-info-nfd-plugin:v2.2.0-3

咱們能夠經過執行如下命令進行同步:

## 建立uploading.txt

sed's/registry.redhat.io/file:\/\/offline/g' mapping.txt > uploading-all.txt

 

## 建立上傳的shell文件

cat << EOF >upload-rh-op.sh

#!/usr/bin/env bash

 

STAGEING_FOLDER="localrep"

IMAGE_LIST="uploading.txt"

 

x=0

echo  `date` "- Starting to mirror image tolocal image repository from local directory: " $STAGEING_FOLDER

echo  `date` "- The image list name is :" $IMAGE_LIST

cat $IMAGE_LIST | whileread line; do

  x=$(( x+1 ))

  echo"--------------------------------------------- No ${x} Started...---------------------------------------------"

  echo `date` $TIMESTAMP "- The image to be loaded is :"

  echo $line

  echo "oc image mirror" $line"--dir="$STAGEING_FOLDER "--filter-by-os=.*"

  oc image mirror $line  --dir=$STAGEING_FOLDER --filter-by-os=.*

  # sleep 1

  echo `date` $TIMESTAMP "- The imageloading is completed."

  echo "---------------------------------------------No ${x} Completed ---------------------------------------------"

  echo

done

echo  `date` "- All images are pushed to thelocal image repository! "

EOF

chmod u+xupload-rh-op.sh

nohup ./upload-rh-op.sh  >> upload-rh-operators.out 2>&1&

到這一步爲止,Red Hat Operators目錄鏡像引用到的鏡像都已經成功同步進本地鏡像倉庫了,你能夠建立須要的operator了。好比,如今你就能夠利用Operator建立OpenShift的日誌組件了。


3.2      其餘類Operators的安裝

3.2.1安裝其餘類Operators目錄鏡像

安裝其餘Operators的目錄鏡像與3.1.1節很是相似,惟一的區別是Operator目錄鏡像名稱不一致。

如下是建立紅帽認證Operators的步驟:

## create a catalogimage for certificated operators

## To a local folder

oc adm catalog build \

    --appregistry-org certified-operators \

   --from=registry.redhat.io/openshift4/ose-operator-registry:v4.5 \

    --filter-by-os="linux/amd64" \

    --to=file://offline/certified-operators:4.5\

--insecure

# Save the operatorimage

tar cvf rh-operator.tarv2

複製文件到內網

# Load the catalog image

oc image mirrorfile://offline/certified-operators:4.5registry.ocp4.example.com:5443/offline/certified-operators:4.5

 

## create aCatalogSource

cat <<EOF >certified-operator-catalog.yaml

apiVersion:operators.coreos.com/v1alpha1

kind: CatalogSource

metadata:

  name: certified-operator-catalog

  namespace: openshift-marketplace

spec:

  displayName: Certified Operators

  sourceType: grpc

  image:registry.ocp4.example.com:5443/offline/certified-operators:4.5

  publisher: Red Hat

EOF

oc create -f certified-operator-catalog.yaml


如下是建立紅帽認證Operators的步驟:

## create a catalogimage for certificated operators

## To a local folder

oc adm catalog build \

    --appregistry-org certified-operators \

   --from=registry.redhat.io/openshift4/ose-operator-registry:v4.5 \

    --filter-by-os="linux/amd64" \

    --to=file://offline/certified-operators:4.5\

--insecure

# Save the operatorimage

tar cvfcertified-operator.tar v2

## 複製文件到內網

# Load the catalog image

oc image mirrorfile://offline/certified-operators:4.5registry.ocp4.example.com:5443/offline/certified-operators:4.5

 

## create aCatalogSource

cat <<EOF >certified-operator-catalog.yaml

apiVersion: operators.coreos.com/v1alpha1

kind: CatalogSource

metadata:

  name: certified-operator-catalog

  namespace: openshift-marketplace

spec:

  displayName: Certified Operators

  sourceType: grpc

  image:registry.ocp4.example.com:5443/offline/certified-operators:4.5

  publisher: Red Hat

EOF

oc create -f certified-operator-catalog.yaml

如下是建立社區Operators的步驟:

## create a catalogimage for community operators

## To a local folder

oc adm catalog build \

    --appregistry-org community-operators \

    --from=registry.redhat.io/openshift4/ose-operator-registry:v4.5\

    --filter-by-os="linux/amd64" \

    --to=file://offline/community-operators:4.5\

    --insecure

 

# Save the operatorimage

tar cvfcommunity-operator.tar v2

 

## 複製文件到內網

# Load the catalog image

oc image mirrorfile://offline/community-operators:4.5registry.ocp4.example.com:5443/offline/community-operators:4.5

 

## create aCatalogSource

cat <<EOF >community-operator-catalog.yaml

apiVersion:operators.coreos.com/v1alpha1

kind: CatalogSource

metadata:

  name: community-operator-catalog

  namespace: openshift-marketplace

spec:

  displayName: Community Operators

  sourceType: grpc

  image:registry.ocp4.example.com:5443/offline/community-operators:4.5

  publisher: Red Hat

EOF

oc create -f community-operator-catalog.yaml


如下是建立紅帽Market Place Operators的步驟:

## create a catalogimage for redhat-marketplace operators

## To a local folder

oc adm catalog build \

    --appregistry-org redhat-marketplace \

   --from=registry.redhat.io/openshift4/ose-operator-registry:v4.5 \

    --filter-by-os="linux/amd64" \

    --to=file://offline/redhat-marketplace:4.5\

    --insecure

 

#

# Save the operatorimage

tar cvfredhat-marketplace.tar v2

 

## 複製文件到內網

 

# Load the catalog image

oc image mirrorfile://offline/redhat-marketplace:4.5registry.ocp4.example.com:5443/offline/redhat-marketplace:4.5

 

## create aCatalogSource

cat <<EOF >redhat-marketplace-catalog.yaml

apiVersion: operators.coreos.com/v1alpha1

kind: CatalogSource

metadata:

  name: redhat-marketplace-catalog

  namespace: openshift-marketplace

spec:

  displayName: Marketplace

  sourceType: grpc

  image:registry.ocp4.example.com:5443/offline/community-operators:4.5

  publisher: Red Hat

EOF

oc create -f redhat-marketplace-catalog.yaml


4.導入ImageStram

OpenShift 4.5.x中的絕大多數ImageSteam都是經過Sample Operator管理的,所以,咱們須要經過管理SampleOperator來導入ImageStream

4.1 同步鏡像

首先,咱們須要知道ImageStream到底使用了哪些鏡像。咱們能夠經過執行下列腳本獲取:

oc get is -o json -nopenshift | jq '.items[].spec.tags[] |select(.from.kind=="DockerImage").from.name'  | sed 's/\"//g' | grepregistry.redhat.io  > is-images.list

## 若是鏡像不包含tag,加上一個默認的latest tag

sed  '/:/! s/$/:latest/g' is-images.list >my-is-images.list

## 構建SourceImage=DestinationImage格式的文件

cat my-is-images.list  | awk -F '=' '{print$0"=file://offline/TAG"$1}' | sed 's/TAGregistry.redhat.io\///g' >download-sample-images.list

到這一步,你若是看文件download-sample-images.list,格式以下:

$ catdownload-sample-images.list

registry.redhat.io/3scale-amp21/apicast-gateway:1.4-2=file://offline/3scale-amp21/apicast-gateway:1.4-2

registry.redhat.io/3scale-amp22/apicast-gateway:1.8=file://offline/3scale-amp22/apicast-gateway:1.8

registry.redhat.io/3scale-amp23/apicast-gateway:latest=file://offline/3scale-amp23/apicast-gateway:latest

registry.redhat.io/3scale-amp24/apicast-gateway:latest=file://offline/3scale-amp24/apicast-gateway:latest

registry.redhat.io/3scale-amp25/apicast-gateway:latest=file://offline/3scale-amp25/apicast-gateway:latest

registry.redhat.io/3scale-amp26/apicast-gateway:latest=file://offline/3scale-amp26/apicast-gateway:latest

registry.redhat.io/3scale-amp2/apicast-gateway-rhel7:3scale2.7=file://offline/3scale-amp2/apicast-gateway-rhel7:3scale2.7

registry.redhat.io/fuse7/fuse-apicurito:1.2=file://offline/fuse7/fuse-apicurito:1.2

看起來是否是很熟悉?的確,這個文件格式就是剛纔咱們導入離線Operator鏡像的格式,採用相似的方法,咱們能夠把這些鏡像下載到公網服務器的本地目錄;而後打包複製到內網服務器的本地目錄,最後再用相似的方法上傳到內部的離線倉庫,這裏面就不重複描述


4.2修改Sample Operator指向內部倉庫

修改Sample Operator的配置資源,將鏡像倉庫指向內部的離線鏡像倉庫

ocpatch configs.samples.operator.openshift.io/cluster -p'{"spec":{"samplesRegistry":"registry.ocp4.example.com:5443"}}'--type=merge

在修改以後,Sample Operator會修改ImageStream的定義,ImageStream將會自動從內部離線倉庫下載鏡像。若是此時再管理界面切換成開發人員視圖,就能夠發現Catalog服務目錄如今已經能夠顯示了,咱們如今能夠利用服務模板建立應用。

相關文章
相關標籤/搜索