OpenShift4.5.x离线环境OperatorHub和ImageStream配置实战技巧linux
在离线环境使用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以前,咱们首先作一些基本的设置。
安装程序默认建立了一个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命令行工具了。
咱们在安装离线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
咱们知道,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>
为了安装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进行尚未被导入,接下来咱们来作这一部分工做。
在可以访问内部离线仓库的服务器上面,咱们经过执行下面的这个命令,来获取须要同步的镜像列表:
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的日志组件了。
安装其余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
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镜像的格式,采用相似的方法,咱们能够把这些镜像下载到公网服务器的本地目录;而后打包复制到内网服务器的本地目录,最后再用相似的方法上传到内部的离线仓库,这里面就不重复描述
修改Sample Operator的配置资源,将镜像仓库指向内部的离线镜像仓库
ocpatch configs.samples.operator.openshift.io/cluster -p'{"spec":{"samplesRegistry":"registry.ocp4.example.com:5443"}}'--type=merge
在修改以后,Sample Operator会修改ImageStream的定义,ImageStream将会自动从内部离线仓库下载镜像。若是此时再管理界面切换成开发人员视图,就能够发现Catalog服务目录如今已经能够显示了,咱们如今能够利用服务模板建立应用。