《OpenShift / RHEL / DevSecOps 汇总目录》
说明:本文已经在 OpenShift 4.12 的 OpenShift 环境中验证
单节点 OpenShift 指的是 OpenShift 集群只有一个节点,Master 和 Worker 都运行在该节点上。单节点 OpenShift 的运行方式比较适合边缘计算中 Far Edge 环境,另外还可以在 Development 或 Testing 环境中使用。
我们可以在虚机或裸机上安装单节点 OpenShift,其中在 Far Edge 环境中运行更适合裸机部署单节点 OpenShift,而 Development 或 Testing 环境可以用虚机部署单节点 OpenShift。在虚机中安装好的单节点 OpenShift 同红帽的 OpenShift Local 环境非常相近,它们都是运行在虚机中,而且都是只有一个节点。
OpenShift 辅助安装器是一个可帮助用户简化在线安装 OpenShift 的工具。用户在 RedHat 网站 OpenShift 辅助安装器的向导中提供适当的集群信息后便可得到一个包含安装配置+RHCOS 的 ISO 文件。当我们使用这个 ISO 启动节点后,节点会自动连接到 RedHat 网站并完成后续安装步骤。这样就能比较方便快捷地安装好一个 OpenShift 环境。
OpenShift 辅助安装器既可用来安装单节点 OpenShift,也可用来安装标准的多节点 OpenShift 集群。
本文将使用 KVM 虚机部署单节点 OpenShift,以下是运行 KVM 的宿主机配置:
说明:如果要直接在裸机上运行 OpenShift,需在后续步骤中先使用下载的 ISO 文件制作启动盘,然后启动裸机完成安装。
在宿主机上执行命令即可安装 KVM 环境。
$ sudo yum install -y virt-* libvirt qemu-img
在宿主机上执行命令生成 SSH 证书密钥对,其中 id_rsa.pub 是公钥。该证书用来远程登陆访问运行 OpenShift 的节点。
$ ssh-keygen -N ''
$ more ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDKQh4w4FBEPs5wk/0tVZVZ/l8+AwJCWMWZOdp8geXRCaMztSpYNGrPZWLm/bAT7qUUlbLSohkY5ItpsC5Llh9goYv48Ii1cVYqsdnL5vrqZyuR4FkzCJl5gxuZpXHBIMNVfe6tLYFMV6gLQ5P28ESBRkW51plO8MDqybSt+Ny+ypViyGKuEr4gcl3nGwLGtElfeqd3tw6aG9ZBQi0EOrwXLNYDhlu/XbKXmXaQVEauN1nO3UBo5fnqMa7AJuUAh1XaJdqPxym7qwaEszob5zL35/2syQWfqjGD3Os3fi3AuXOGkpBoV+hmoTUDkxgRTm0CeQJvuL7Kr1gvFLKymfpj dawnsky@x1
$ virt-manager
$ ip a show type bridge
3: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 52:54:00:3f:46:8d brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
$ nmcli connection
NAME UUID TYPE DEVICE
ChinaNet-YSHQ-5G 688a8258-4742-447d-9931-771b35b5a7b1 wifi wlp0s20f3
virbr0 6d1b3b66-a015-4969-9747-c067b0a5de09 bridge virbr0
vnet0 4863eb9f-51e1-4b1e-8999-54cfbf51f627 tun vnet0
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
OCP_CLUSTER_NAME=ocp-sno
BASE_DOMAIN=ocp.com
CONNECTION_NAME=ChinaNet-YSHQ-5G
DNS_IP=192.168.1.41
OCP_IP=192.168.122.63
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
yum -y install bind bind-utils
systemctl enable named --now
cp /etc/named.rfc1912.zones /etc/named.rfc1912.zones.bak
cp /etc/named.conf /etc/named.conf.bak
sed -i -e "s/listen-on port.*/listen-on port 53 { any; };/" /etc/named.conf
sed -i -e "s/allow-query.*/allow-query { any; };/" /etc/named.conf
sed -i '/recursion yes;/a \
forward first; \
forwarders { 114.114.114.114; 8.8.8.8; };' /etc/named.conf
sed -i -e "s/dnssec-enable.*/dnssec-enable no;/" /etc/named.conf
sed -i -e "s/dnssec-validation.*/dnssec-validation no;/" /etc/named.conf
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
cat >> /etc/named.rfc1912.zones << EOF
zone "${OCP_CLUSTER_NAME}.${BASE_DOMAIN}" IN {
type master;
file "${OCP_CLUSTER_NAME}.${BASE_DOMAIN}.zone";
allow-transfer { any; };
};
EOF
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
cat > /var/named/${OCP_CLUSTER_NAME}.${BASE_DOMAIN}.zone << EOF
\$ORIGIN ${OCP_CLUSTER_NAME}.${BASE_DOMAIN}.
\$TTL 1D
@ IN SOA ${OCP_CLUSTER_NAME}.${BASE_DOMAIN}. admin.${OCP_CLUSTER_NAME}.${BASE_DOMAIN}. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
@ IN NS dns.${BASE_DOMAIN}.
api IN A ${OCP_IP}
api-int IN A ${OCP_IP}
*.apps IN A ${OCP_IP}
EOF
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
nmcli connection modify ${CONNECTION_NAME} ipv4.dns ${DNS_IP}
systemctl restart NetworkManager
systemctl restart named
rndc reload
$ ./dns-sno.sh
$ ping www.redhat.com
$ ping api.sno.ocp.com
$ ping oauth-openshift.apps.sno.ocp.com
$ ping console-openshift-console.apps.sno.ocp.com
$ ping grafana-openshift-monitoring.apps.sno.ocp.com
$ ping thanos-querier-openshift-monitoring.apps.sno.ocp.com
$ ping prometheus-k8s-openshift-monitoring.apps.sno.ocp.com
$ ping alertmanager-main-openshift-monitoring.apps.sno.ocp.com
说明:上一节为宿主机配置的 DNS 是将访问 OpenShift 的域名解析到该宿主机的 virbr0 网桥网段,进而从网桥进入到 VM 中。由于该 virbr0 网桥网段只在宿主机上效,因此只能从宿主机通过域名直接访问 OpenShift 控制台,而从宿主机以外无法通过域名直接访问到 OpenShift 控制台。
$ tar -xvf oc.tar
$ sudo mv oc /usr/bin/
$ oc get project
NAME DISPLAY NAME STATUS
assisted-installer Active
default Active
kube-node-lease Active
kube-public Active
kube-system Active
openshift Active
......
在宿主机上执行命令,可进入运行 OpenShift 的 VM 操作系统内部。
$ ssh -i ~/.ssh/id_rsa [email protected]
The authenticity of host '192.168.122.63 (192.168.122.63)' can't be established.
ECDSA key fingerprint is SHA256:uBLlWdC4pmdJ8jUs5ie9snpoXkhC7kzZ4CJoVKQzVfk.
ECDSA key fingerprint is MD5:d5:b8:c8:c9:48:22:d5:60:6a:1b:b0:a7:70:a3:43:a9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.122.63' (ECDSA) to the list of known hosts.
Red Hat Enterprise Linux CoreOS 412.86.202301311551-0
Part of OpenShift 4.12, RHCOS is a Kubernetes native operating system
managed by the Machine Config Operator (`clusteroperator/machine-config`).
WARNING: Direct SSH access to machines is not recommended; instead,
make configuration changes via `machineconfig` objects:
https://docs.openshift.com/container-platform/4.12/architecture/architecture-rhcos.html
---
启动 VM 就会自动运行 OpenShift。有些时候,在 VM 启动后 OpenShift 可能有新的 CSR(certificate signing requests) 需要处理,只有在处理完后 OpenShift 才能启动完成。
$ oc get csr
NAME AGE SIGNERNAME REQUESTOR REQUESTEDDURATION CONDITION
csr-2kzc6 9m34s kubernetes.io/kube-apiserver-client-kubelet system:serviceaccount:openshift-machine-config-operator:node-bootstrapper <none> Pending
csr-4n8vq 24m kubernetes.io/kube-apiserver-client-kubelet system:serviceaccount:openshift-machine-config-operator:node-bootstrapper <none> Pending
csr-jrrtd 27m kubernetes.io/kube-apiserver-client-kubelet system:serviceaccount:openshift-machine-config-operator:node-bootstrapper <none> Pending
$ oc get csr -o go-template='{{range .items}}{{if not .status}}{{.metadata.name}}{{"\n"}}{{end}}{{end}}' | xargs oc adm certificate approve
$ oc get co
NAME VERSION AVAILABLE PROGRESSING DEGRADED SINCE MESSAGE
authentication 4.12.2 True False False 3d3h
baremetal 4.12.2 True False False 3d3h
cloud-controller-manager 4.12.2 True False False 3d3h
cloud-credential 4.12.2 True False False 3d4h
cluster-autoscaler 4.12.2 True False False 3d3h
config-operator 4.12.2 True False False 3d4h
console 4.12.2 True False False 3d3h
control-plane-machine-set 4.12.2 True False False 3d3h
csi-snapshot-controller 4.12.2 True False False 3d4h
dns 4.12.2 True False False 3d3h
etcd 4.12.2 True False False 3d4h
image-registry 4.12.2 True False False 3d3h
ingress 4.12.2 True False False 3d3h
insights 4.12.2 True False False 3d3h
kube-apiserver 4.12.2 True False False 3d3h
kube-controller-manager 4.12.2 True False False 3d3h
kube-scheduler 4.12.2 True False False 3d3h
kube-storage-version-migrator 4.12.2 True False False 3d4h
machine-api 4.12.2 True False False 3d3h
machine-approver 4.12.2 True False False 3d3h
machine-config 4.12.2 True False False 3d3h
marketplace 4.12.2 True False False 3d4h
monitoring 4.12.2 True False False 3d3h
network 4.12.2 True False False 3d4h
node-tuning 4.12.2 True False False 3d3h
openshift-apiserver 4.12.2 True False False 3d3h
openshift-controller-manager 4.12.2 True False False 3d3h
openshift-samples 4.12.2 True False False 3d3h
operator-lifecycle-manager 4.12.2 True False False 3d3h
operator-lifecycle-manager-catalog 4.12.2 True False False 3d3h
operator-lifecycle-manager-packageserver 4.12.2 True False False 3d3h
service-ca 4.12.2 True False False 3d4h
storage 4.12.2 True False False 3d3h
在宿主机上执行以下两段命令均可关闭 OpenShift 集群运行。
方法 1:
$ ssh -i ~/.ssh/id_rsa [email protected]
$ sudo shutdown -h now
方法 2:
$ oc get node
$ oc debug node/XXX
sh-4.4# chroot /host
sh-4.4# shutdown -h now
视频
https://cloud.redhat.com/blog/how-to-use-the-openshift-assisted-installer
https://jameswilkins.co.uk/install-openshift/
https://cloud.redhat.com/blog/meet-the-new-agent-based-openshift-installer-1