本文根据 DO280 学习笔记整理,保留概念说明、命令示例、实验步骤与自测内容。
实验版本:OpenShift 3.9 / RHEL 7 / Docker / Ansible 2.4。命令、主机名、账户、路径与软件依赖按课程环境记录;阅读其他版本时需核对对应配置。文中引用的课程镜像、脚本和课件不随本文提供。
相关阅读:CL110 · CL210 · Ceph 分布式存储管理
背景知识#
- 容器共享宿主机内核。Namespace 隔离进程、网络等视图,cgroup 约束资源用量,镜像提供程序及依赖。
- Kubernetes 负责声明式编排。Pod 是调度单位,容器是 Pod 内的运行单位。
- OpenShift Container Platform(OCP)在 Kubernetes 之上提供项目、认证、构建、镜像流、路由和安全约束。
- 平台版本:OpenShift 3.9、RHEL 7、Docker、Ansible 2.4。安装对象使用
DeploymentConfig,指标由 Heapster、Hawkular Metrics 和 Cassandra 提供。
OpenShift#
整体结构#
1
2
3
4
5
6
7
8
9
| 管理:用户 → oc / Web Console → API → 认证 → 授权 → 准入 → etcd
↓
控制器 + 调度器
↓
Node → Docker → Pod
访问:客户端 → DNS → Router → Route 对应的 Service 后端 → 就绪 Pod
构建:源码 + Builder Image → Build → Registry → ImageStreamTag → DeploymentConfig
存储:应用 → PVC → PV → NFS / 块存储后端
指标:节点 / Pod → Heapster → Hawkular Metrics → Cassandra → Console
|
Router 根据 Service、Endpoints 和 Route 配置代理到后端;业务流量不必先经过 Service 的虚拟 IP。
| 对象 | 职责 | 判断依据 |
|---|
| Master | API、控制器、调度与认证 | API 可用、控制循环正常 |
| etcd | 保存平台资源状态 | 成员健康、数据备份可用 |
| Node | 运行容器与节点网络 | Ready、磁盘和内存条件正常 |
| Project | 命名空间及项目管理边界 | 当前项目与 RoleBinding |
| DeploymentConfig / DC | Pod 模板、部署策略与触发器 | 最新部署版本及可用副本 |
| ReplicationController / RC | 维持某个版本的副本数 | 期望副本与实际副本 |
| Service / Endpoints | 稳定服务入口及可用后端 | 选择器、端口、就绪地址 |
| Route | HTTP(S) 外部入口 | 主机名、TLS、服务端口 |
| ImageStream / IS | 追踪镜像及标签变化 | Tag 与镜像摘要 |
| BuildConfig / BC | 源码、构建方式与输出目标 | Build 日志及输出镜像 |
| Security Context Constraints / SCC | 限制容器身份和能力 | 实际 ServiceAccount 与 SCC |
1.1 平台职责#
Master 接收 API 请求并协调资源;Node 执行 Pod;etcd 保存声明和状态,不保存数据库业务文件或 Registry 镜像层。
控制器持续协调期望状态。删除 replicas: 2 对应的一个 Pod,只会触发 RC 补建;将 DC 缩容到 0 才会停止该应用的所有副本。
1.2 三条生命周期#
1
2
3
| 构建:BuildConfig → Build → 镜像 → Registry
部署:ImageStreamTag → DeploymentConfig → ReplicationController → Pod
访问:Route → Service 的选择器 → Endpoints → 容器端口
|
构建成功只说明生成了镜像;部署完成还需要调度、镜像拉取、容器启动和就绪检查。Route 创建成功只说明配置被接受,还需要 DNS、Router 和后端工作。
1.3 对象范围#
| 集群范围 | 项目范围 |
|---|
| Node、PV、User、Group、ClusterRole、SCC | Pod、Service、Route、DC、BC、IS、PVC、Secret、RoleBinding |
同名对象位于不同项目时互不等价。oc get all 不包括所有对象类型,Secret、PVC、Quota 等需要单独查询。
1.4 构建方式与部署策略#
- Source-to-Image(S2I):Builder 镜像提供构建脚本,将源码和运行环境组合为应用镜像。
- Docker 构建:按照 Dockerfile 构建镜像。
- Pipeline:由流水线编排构建、测试和部署;其插件、执行环境需单独安装配置。
- Rolling:逐步增加新副本、减少旧副本,依赖就绪检查和兼容的共享状态。
- Recreate:先终止旧版本再启动新版本;单实例数据库实验采用该策略,避免两个进程同时写同一数据目录。
自测问题#
2.1 安装条件#
Ansible 通过 SSH 连接主机并使用 sudo。SSH 可达不等于 sudo 成功,ansible ... -m ping 与 ansible ... -b -m command -a id 分别验证两层。
RHEL 软件源、OCP 3.9 RPM、Docker 和平台镜像必须成套可用。离线仓库应包含 API、节点、Router、Registry、部署器、Pod 基础镜像、控制台和实验应用镜像。
SELinux 保持 Enforcing;节点禁用 swap;时间同步;Docker 数据目录预留独立容量。openshift_disable_check 跳过检查不能修复资源不足。
2.2 Inventory 分组#
OSEv3 汇总 masters、etcd、nodes。Master 同时属于 nodes 时仍需要节点配置。region=infra 决定 Router 等基础设施工作负载可选择的位置;项目默认 region=apps 限制普通应用进入应用节点。
安装配置、SSH 身份、节点标签、镜像前缀、API 端口和通配域名必须一致。API 和 Console 均可使用 443;不要将节点服务端口当成用户访问端口。
2.3 分层验收#
Ansible recap 中 failed=0、unreachable=0 是安装器验收。平台还要验证节点 Ready、系统 Pod 就绪、Registry 可写、普通用户可部署及 Route 可访问。
实验操作#
引导练习:准备安装环境#
在此练习中, 为 OpenShift Container Platform 安装的 master 和节点主机做准备。
文件:/home/student/DO280/labs/install-prepare
学习目标#
能够自定义并运行 Ansible Playbook 来准备 master 和 OpenShift Container Platform 安装的节点主机。
开始之前#
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab install-prepare setup
|
切换到实验工作目录 :
1
| [student@workstation ~]$ cd /home/student/DO280/labs/install-prepare
|
操作步骤#
1. 安装 Playbook 与 Ansible 2.4 兼容。确保安装流程能够使用 Ansible 2.4。
1.1. 安装 ansible 软件包 :
1
| [student@workstation install-prepare]$ sudo yum install ansible
|
1.2. 检查 ansible 命令是否正常。在同一终端窗口执行以下命令:
1
| [student@workstation install-prepare]$ ansible --version
|
输出表明 Ansible 2.4 已安装。
输出还表明,ansible 命令使用当前 /home/student/DO280/labs/install-prepare 目录中的 ansible.cfg 配置文件。
1.3. 简要查看 ansible.cfg 文件。
1
2
3
4
5
| [student@workstation install-prepare]$ cat ansible.cfg
- [defaults]
remote_user = student
inventory = ./inventory
log_path = ./ansible.1log
|
操作界面、表格与配置图
1
2
3
4
| [privilege_escalation]fa)
become = yes
become_user = root
become_method = sudo
|
默认情况下,Ansible 通过 SSH 以 student 用户连接远程主机,使用 ./inventory 作为清单文件,并将 Playbook 输出写入 ./ansible.log。连接建立后,Ansible 通过 sudo 切换为 root。
2. 清单文件包含 VMS 和变量以配置 OpenShift 安装。 简要审查清单文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| [student@workstation install-prepare]$ cat inventory
[workstations]
workstation.lab.example.com
{nfs]
services.lab.example.com
[masters]
master.lab.example.com
[etcd]
master.lab.example.com
[nodes]
master.lab.example.com
node1.lab.exampie.com
node2.1lab.example.com
[OSEv3: children]
masters
etcd
nodes
nfs
#Variables needed by the prepare_install.yml playbook.
[nodes: vars]
registry_local=registry.lab.example.com
use_overlay2_driver=true
insecure_registry=false
run_docker_offline=true
docker_storage_device=/dev/vdb
|
清单文件定义了六个主机组:
workstations:开发者工作站组,Playbook 从这台主机执行。OpenShift 高级安装 Playbook 不使用这个组。之所以保留该组,
操作界面、表格与配置图
是为了让 inventory 能完整表示课堂环境中的所有虚拟机。
nfs:在环境中为集群储存提供NFS服务的VMs。
masters:在 OpenShift 集群中承担 master 角色的虚拟机组。
etcd:OpenShift集群etcd服务所使用的VMs组。在课堂环境中,etcd服务设计与masterVMs合用同一地点。
nodes:在 OpenShift 集群中承担节点角色的虚拟机组。
OSEv3:组成 OpenShift 集群的虚拟机组。masters、etcd、nodes 或 nfs 组中的主机也属于 OSEv3 组。
默认情况下,Docker 从在线镜像仓库下载镜像。课堂环境不能访问互联网,因此配置为使用安全的私有仓库。课堂自定义 Playbook prepare_install.yml 使用 nodes:vars 中定义的变量配置 Docker。
此外,安装过程将各主机的 Docker daemon 配置为使用 overlay2 存储驱动保存镜像。Docker 支持多种存储驱动配置,具体优缺点可查阅相应文档。
3. 开始安装前,确认 Ansible 能连接 inventory 中的所有虚拟机。
3.1. 查看自定义 ping.yml Playbook,它用于验证 Ansible 能否连接清单中的所有主机。
1
2
3
4
5
6
7
8
| [student@workstation install-prepare]$ cat ping.yml
- name: Verify Connectivity
hosts: all
gather_facts: no
tasks:
- name: "Test connectivity to machines."
shell: "whoami"
changed_when: false
|
该 Playbook 检查 Ansible 能否通过 SSH 连接各主机,并在连接后切换为 root。
3.2. 运行 ping.yml,使用 -v 输出详细信息。
1
| [student@workstation install-prepare]$ ansible-playbook -v ping.yml
|
预期输出如下:
1
2
3
| Using /home/student/DO280/labs/install-prepare/ansible.cfg as config file
PLAY [verify Connectivity] KKK KKK KEEKEKEKKK EKER KKK KKKKEKEKEK ARK ERR KKK EEKEEKK KKK
TASK [Test connectivity to machines.] *** GOO error nOr ini R aE
|
操作界面、表格与配置图
.output omitted.
1
2
3
4
| ...OuTpUt omitted...
ok: [node2.lab.example.com] => {"changed": false, "cmd": "whoami", ...,
"stderr": "", "stderr_lines": [], "stdout": "root "@, "stdout_lines": ["root"]}
...output omitted...
|
.output omitted…
Whoami 命令的输出预计为 root。 这表明 Ansible 能够使用 sudo 命令提升权限。
4. 课堂提供了准备 OpenShift 安装环境的自定义 Playbook。查看 prepare_install.yml。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| [student@workstation install-prepare]$ cat prepare_install.yml
name: "Host Preparation: Docker tasks"
hosts: nodes
roles:
- docker-storage
- docker-registry-cert
- openshift-node
#Tasks below were not handled by the roles above.
tasks:
- name: Student Account - Docker Access
user:
name: student
groups: docker
append: yes
|
本Playbook对nodes组中的每个VM应用三个角色:docker-storage, docker-registry-cert,和openshift-node.
docker-storage 角色安装 docker 和配置 docker 守护进程存储器。 docker-registry-cert 角色确保每个节点上的 docker 守护程序信任私人 Docker 注册证书。 openshift-node 角色安装了先决条件的基本软件包。
要查看特定角色的任务列表, 请检查此角色的 tasks/main.yml 文件。
1
| [student@workstation install-prepare]$ cat roles/<role-name>/tasks/main.yml
|
应用角色后,各节点的 student 用户会加入 docker 组,因此可以不借助 sudo 执行 docker 命令。
5. 运行课堂自定义 Playbook,为虚拟机准备 OpenShift 安装环境。
从 workstation VM 运行以下命令 :
1
| [student@workstation install-prepare]$ ansible-playbook prepare_install.yml
|
您应该看到类似于以下输出的输出 :
.output omitted..
1
2
3
4
5
| : . -
...output omitted...
PLAY RECAP KKK KEKE EEE EEE EEE EEE ERENT EET EERE EHNA HERR ENE EERE
master. 1lab.example.com : ok=28 changed=24 unreachable=0 failed=0
node1.lab.example.com : ok=28 changed=24 unreachable=0 failed=0
|
操作界面、表格与配置图
1
| node2.iab.exampie.com : OK=2Z8 changead=24 £unreachnadle=0 Talie€a=-0
|
Playbook摘要中的计数在您的系统中可能有所不同。 请确定在执行Playbook过程中没有失败的任务。
6. Playbook确保:
。在每一个节点上安装并运行 Docker。
各节点信任私有 Docker 镜像仓库的自签名证书。
每个节点都安装基本包。
核查每个节点配置是否正确。
6.1. 验证每个节点是否启动并启用了 docker 服务。
1
2
3
4
| [student@workstation install-prepare]$ for vm in master node1 node2; do
echo -e "\n$vm"
ssh $vm sudo systemctl status docker | head -n3
done
|
您应该看到类似于以下输出的输出 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| master
* docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor
preset: disabled)
Active: active (running) since Mon 2018-08-13 04:39:29 PDT; 24min ago
/ nodet
* docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor
preset: disabled)
Active: active (running) since Mon 2018-08-13 04:39:29 PDT; 24min ago
node2
* docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor
preset: disabled)
Active: active (running) since Mon 2018-08-13 04:39:29 PDT; 24min ago
|
6.2. 验证 Docker 守护程序使用卷组 docker-vg 中名为 docker-pool 的逻辑卷。
使用 lvs 命令来列出每个主机的逻辑卷。 使用 df -h 命令来校验逻辑卷, 在 /var/lib/docker 目录上挂载。
1
2
| [student@workstation install-prepare]$ for vm in master node1 node2; do
echo -e "\n$vm : 1vs"
|
操作界面、表格与配置图
1
2
3
4
| ssn $vm sudo ivs
echo -e "\n$vm : df -h"
ssh $vm sudo df -h | grep vg-docker
done
|
您应该看到类似于以下输出的输出 :
VG Attr Lsize Pool …output omitted..
1
2
3
4
5
| 2 a ee nt la SO? Se eee
docker -poo1® docker-vg -wi-ao---- <20.00g
master : df -h
/dev/mapper /docker --vg-docker - -pool® 20G 1.66 19G 8% /var/lib/docker
node1 : lvs
|
LV VG Attr Lsize Pool …output omitted…
1
2
3
4
| docker-poo1® docker-vg -wi-ao---- <20.00g
node1 : df -h
/dev/mapper/docker --vg-docker --poo1® 206 1.8G 19G 9% /var/lib/docker
node2 : lvs
|
LV VG Attr LSize Pool …output omitted…
1
2
3
| docker -pool® docker-vg -wi-ao---- <20.00g
node2 : df -h
/dev/mapper/docker- -vg-docker - -pool1® 20G 1.86 19G 9% /var/lib/docker
|
逻辑卷 docker-pool 存在于每个节点的 docker-vg 卷组中。 @o /var/lib/docker 目录是逻辑卷的挂载点。
6.3. 使用 docker 拉取命令来验证镜像默认是从本地私有镜像仓库提取的。 这也测试每个节点是否信任私有镜像仓库证书。
1
2
3
4
| [student@workstation install-prepare]$ for vm in master node1 node2; do
echo -e "\n$vm"
ssh $vm docker pull rhel17:latest
done
|
您应该看到类似于以下输出的输出 :
1
2
3
4
5
6
7
8
9
| master
Trying to pull repository @-egistry.1ab.example.com/rhel7 4 "I
latest: Pulling from registry.lab.example.com/rhel7
845d34b6bc6a: Pulling fs layer
...output omitted...
Status: Downloaded newer image® for registry.lab.example.com/rhel7:latest
node1
Trying to puil repository Or egistry.1ab.example.com/rhel7 hs
latest: Pulling from registry.lab.example.com/rhel7
|
操作界面、表格与配置图
1
| 845d34b6bc6a: Pulling fs layer
|
…output omitted…
1
2
3
4
| node2
Trying to pull repository Oregistry.lab.example.com/rhel7 $7,
latest: Pulling from registry.lab.example.com/rhel7
845d34b6bc6a: Pulling fs layer
|
…output omitted…
rhel7:latest 镜像从 registry.lab.example.com 拉取,而非 registry.access.redhat.com。
由于容器镜像下载成功,说明私有镜像仓库
每个节点都信任证书。
6.4. 核查每个节点是否都存在安装所需的包件。
1
2
3
4
5
6
| [student@workstation install-prepare]$ for vm in master node1 node2; do
echo -e "\n$vm"
ssh $vm rpm -qa wget git net-tools bind-utils \
yum-utils iptables-services bridge-utils bash-completion \
kexec-tools sos psacct atomic-openshift-utils
done
|
您应该看到类似于以下输出的输出 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| master
kexec-tools-2.0.15-13.e17.x86_64
git-1.8.3.1-13.e17.x86_64
net-tools-2.0-0.22.20131004git.e17.x86_64
bridge-utils-1.5-9.e17.x86_64
bash-completion-2.1-6.e17.noarch
atomic-openshift-utils-3.9.14-1.git.3.c62bc34.e17.noarch
sos-3.5-6.e17.noarch
bind-utils-9.9.4-61.e17.x86_64
wget-1.14-15.e17_4.1.x86_64
iptables-services-1.4.21-24.e17.x86_64
yum-utils-1.1.31-45.e17.noarch
psacct-6.6.1-13.e17.x86_64
node1
|
.output omitted..
..output omitted.
本引导练习到此结束。
操作界面、表格与配置图
在此练习中, 安装 Red Hat OpenShift Container Platform 配置 master 和两个节点。
文件:/home/student/DO280/labs/install-run
学习目标#
能够:
。安装 OpenShift Container Platform。
:配置 master 和两个节点。
开始之前#
名为“ 指导操作: 安装准备” 的区域内的指导练习应该完成。 否则, 请重置 master 、 node1 和 node2 主机, 并在 workstation 主机上运行以下命令, 以确保环境设置正确 :
1
2
3
4
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ sudo yum install ansible
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ansible-playbook playbooks/prepare_install.yml
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
2
3
| [student@workstation ~]$ lab install-run setup
[student@workstation ~]$ cd /home/student/DO280/labs/install-run
[student@workstation install-run]$
|
操作步骤#
此制导练习模拟开发在断开的教室环境中安装 OpenShift Container Platform 所需的清册文件。 相关变量组被记录在单独的文件中以协助模拟。 然后将这些文件合并来创建高级安装的清册文件。
1. 安装 atomic-openshift-utils 软件包。
1
| [student@workstation install-run]$ sudo yum install atomic-openshift-utils
|
该软件包提供安装 OpenShift Container Platform 所需的 Ansible Playbook 与角色。运行高级安装 Playbook 还需要 inventory 清单文件。
2. 创建OpenShift高级安装方法所需的清单文件。
操作界面、表格与配置图
2.1. 提供的初始清单基于上一个练习。将 inventory.initial 复制为 inventory,以便后续出错时可以方便地重新开始。
1
| [student@workstation install-run]$ cp inventory.initial inventory
|
2.2. 在 inventory 末尾添加 OSEv3 组变量节。
在以下步骤中,在清单中添加变量以配置 :
普通集群特征
- .Networking
数据持久性
断开的安装变量
3. 配置 OpenShift 集群的以下总体特征:
- 使用 OpenShift Container Platform 的默认设置,而非 OpenShift Origin。
OpenShift 版本 3.9 已安装。
- 各容器化 OpenShift 服务使用标签为 v3.9.14 的镜像。
3.1. 使用文本编辑器打开 general_vars.txt 文件。
1
2
3
4
5
| #General Cluster Variables
openshift_deployment_type=
openshift_release=
openshift_image_tag=
openshift_disable_check=disk_availability, docker_storage, memory_availability
|
默认情况下,OpenShift Playbook 检查推荐的系统要求。该变量允许禁用个别检查;通常不应
操作界面、表格与配置图
禁用这些检查。但课堂环境需要关闭相应检查,安装才能继续。
3.2. 添加缺值的三个变量的适当值。
不要从教材直接复制解答到 *_vars.txt 文件,这可能引入空白字符差异,导致评分失败。需要复制时,应使用 /home/student/DO280/solutions 中的对应文件。
3.3. general_vars.txt现在包含:
1
2
3
4
5
| #General Cluster Variables
openshift_deployment_type=openshift-enterprise
openshift_release=v3.9
openshift_image_tag=v3.9.14
openshift_disable_check=disk_availability, docker_storage,memory_availability
|
保存 general_vars.txt 文件。
将集群配置为使用 htpasswd 认证。创建 admin 与 developer 两个用户,密码均为 redhat。
4.1. 使用文本编辑器审查 authentication_vars.txt 文件。
1
2
3
4
5
6
| #Cluster Authentication Variables
openshift_master_identity_providers=[{'name': 'htpasswd_auth', 'login': 'true',
'challenge': 'true', 'kind': 'HTPasswdPasswordIdentityProvider', 'filename': '/
etc/origin/master/htpasswd'}]
openshift_master_htpasswd_users={'user1':'htpasswd_hash_here', 'user2':
"htpasswd_hash_here'}
|
4.2. 编辑 openshift_master_htpasswd_users,将 user1 和 user2 分别替换为 admin 和 developer。
4.3. 密码文件不应暴露哪些账户使用相同密码,因为这类信息可能帮助攻击者或恶意用户。
为 redhat 生成两个不同的 MD5 密码散列。
1
2
3
4
| [student@workstation install-run]$ openssl passwd -apr1 redhat
S$aprigVd4/F6nNTS$xYB.UFGvVCZeWPdMOAXSZJ1
[student@workstation install-run]$ openssl passwd -apr1 redhat
$apr1$jhQYVWRa$AG6LOPTNOdKSYnsGEhahHr4.
|
4.4. 用生成的散列替换
openshift_master_htpasswd_users 变量中的各个密码散列占位值。
1
| #Cluster Authentication Variables
|
操作界面、表格与配置图
1
2
3
4
5
| opensnift_master_identity providers=|{ name : ntpasswd_autn, login': true,
"challenge': 'true', 'kind': 'HTPasswdPasswordIdentityProvider', 'filename': '/
etc/origin/master/htpasswd'}]
openshift_master_htpasswd_users={'admin':'$apri$vd4/F6nT
$xYB.UFGvCZeWPAMOAXSZJ1', 'developer': '$apri$jhQYVWRa$AGLOPTNOdkKSYnsGEhaHr4. |}
|
为同一密码生成多个 MD5 散列并不是功能上的必要条件,但使用不同散列能让他人更难识别哪些用户密码相同。
确保 openshift_master_htpasswd_users 的定义位于同一行。如果值被拆成两行,集群安装后 admin 和 developer 都可能无法登录。遇到这种情况,应修正 master 主机上的 /etc/origin/master/htpasswd。
4.5. 保存 authentication_vars.txt 的修改。
5. 配置 OpenShift 集群的网络属性:
:使用 firewalld 作为防火墙。
- API 与控制台均通过标准 HTTPS 端口 443 访问。
使用 apps.lab.example.com 对应的通配符 DNS 作为托管应用的子域。
5.1. 使用文本编辑器审查 networking_vars.txt 文件。
1
2
3
4
5
| #OpenShift Networking Variables
firewalld_var=true
API_port_var=443
console_port_var=443
app_subdomain_var=apps.lab.example.com
|
5.2. 将firewalld_var替换为相应的OpenShift变量。
5.3. 将 API_port_var 替换为适当的 OpenShift 变量。
5.4. 将console_port_var替换为相应的OpenShift变量。
5.5. 将app_subdomain_var替换为相应的OpenShift变量。
5.6. networking_vars.txt 文件现在包含 :
1
2
3
| #OpenShift Networking Variables
os_firewall_use_firewalld=true
openshift_master_api_port=443
|
操作界面、表格与配置图
1
2
| openshift_master_console_port=443
openshift_master_default_subdomain=apps.lab.example.com
|
保存 networking_vars.txt 文件。
6. 将 OpenShift Container Registry(OCR)和 OpenShift Ansible Broker(OAB)配置为使用 NFS 持久存储。
6.1. 用文本编辑器查看 persistence_vars.txt。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| #NFS is an unsupported configuration
allow_nfs_var=true
#0CR configuration variables
openshift_hosted_registry_storage_kind=nfs
openshift_hosted_registry_storageaccess_modes=[ 'ReadWriteMany']
openshift_hosted_registry_storage_nfs_directory=/exports
openshift_hosted_registry_storage_nfs_options='*(rw, root_squash) '
openshift_hosted_registry_storage_volume_name=registry
openshift_hosted_registry_storage_volumesize=40Gi
#0AB's etcd configuration variables
openshift_hosted_etcd_storage_kind=nfs
openshift_hosted_etcd_storage_nfs_options="*(rw,root_squash, sync,no_wdelay)"
openshift_hosted_etcd_storage_nfs_directory=/exports
openshift_hosted_etcd_storage_volume_name=etcd-vol2
openshift_hosted_etcd_storage_access_modes=["ReadWriteOnce"]
openshift_hosted_etcd_storage_volume_size=1G
openshift_hosted_etcd_storage_labels={'storage': 'etcd'}
|
6.2. 使用 NFS 进行持久性存储不是一个支持的配置。 将 allow_nfs_var 替换为允许不支持配置的 Openshift 变量。
6.3. persistence_vars.txt 文件现在包含 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| #NFS is an unsupported configuration
openshift_enable_unsupported_configurations=true
#0CR configuration variables
openshift_hosted_registry_storage_kind=nfs
openshift_hosted_registry_storage_access_modes=['ReadWriteMany' ]
openshift_hosted_registry_storage_nfs_directory=/exports
openshift_hosted_registry_storage_nfs_options='*(rw,root_squash) '
- openshift_hosted_registry_storage_volume_name=registry
openshift_hosted_registry_storage_volume_size=40Gi
#0AB's etcd configuration variables
openshift_hosted_etcd_storage_kind=nfs
- openshift_hosted_etcd_storagenfs_options="*(rw, root_squash, sync,no_wdelay)"
openshift_hosted_etcd_storage_nfs_directory=/exports
openshift_hosted_etcd_storage_volume_name=etcd-vol2
openshift_hosted_etcd_storage_access_modes=["ReadWriteOnce"]
openshift_hosted_etcd_storage_volume_size=1G
openshift_hosted_etcd_storage_labels={'storage': 'etcd'}
|
保存 persistence_vars.txt 文件。
操作界面、表格与配置图
7. 配置 OpenShift 离线安装。
7.1. 使用文本编辑器审查 disconnected_vars.txt 文件。
1
2
3
4
5
6
7
8
9
10
11
12
| #Modifications Needed for a Disconnected Installation
oreg_url=/openshift3/ose-${component}:${version}
modify_imagestreams_var=true
openshift_docker_additional_registries=
openshift_docker_blocked_registries=
#Image Prefixes
openshift_web_console_prefix=registry.lab.example.com/openshift3/ose-
openshift_cockpit_deployer_prefix='registry.lab.example.com/openshift3/'
openshift_service_catalog_image_prefix=registry.lab.example.com/openshift3/ose-
template_service_broker_prefix=registry.lab.example.com/openshift3/ose-
ansible_service_broker_image_prefix=registry.lab.example.com/openshift3/ase-
ansible_service_broker_etcd_image_prefix=registry.lab.example.com/rhel7/
|
7.2. 在 oreg_url 变量值的起始处添加教室镜像仓库。
7.3. 将 modify_imagestreams_var 替换为相应的 OpenShift 变量, 以修改镜像流资源。
7.4. 将课堂镜像仓库加入 openshift_docker_additional_registries 变量。
7.5. 将 registry.access.redhat.com 和 docker.io 加入禁止访问的镜像仓库列表,用逗号分隔。
7.6. The disconnected_vars.txt 文件现在包含 :
1
2
3
4
5
6
7
8
9
10
11
12
| #Modifications Needed for a Disconnected Install
oreg_url=registry.lab.example.com/openshift3/ose-${component}:${version}
openshift_examples_modify_imagestreams=true
openshift_docker_additional_registries=registry.lab.example.com
openshift_docker_blocked_registries=registry.access.redhat.com, docker.io
#Image Prefixes
openshift_web_console_prefix=registry.ilab.example.com/openshift3/ose-
openshift_cockpit_deployer_prefix='registry.lab.example.com/openshift3/'
openshift_service_catalog_image_prefix=registry.lab.example.com/openshift3/ose-
template_service_broker_prefix=registry.lab.example.com/openshift3/ose-
ansible_service_broker_image_prefix=registry.lab.example.com/openshift3/ose-
- ansible_service_broker_etcd_image_prefix=registry.lab.example.com/rhel7/
|
保存 disconnected_vars.txt 文件。
所需 OSEv3 组变量已定义,接下来定义主机变量。
8. 为充分利用节点,两台节点同时运行基础设施与应用 Pod。在 node1 和 node2 上都设置 region=infra 和 node-role.kubernetes.io/compute=true 标签。
8.1. 使用文本编辑器审查清单文件的 [nodes] 部分:
操作界面、表格与配置图
1
2
3
| master .lab.example.com
node1.lab.example.com
node2.iab.example.com
|
8.2. 为 node1 和 node2 添加 openshift_node_labels 主机变量,值为 “{‘region’:‘infra’, ’node-role.kubernetes.io/compute’:’true’}"。
完成后, 清册文件的 [nodes] 部分应该像 :
1
2
3
4
5
6
| [nodes]
master.lab.example.com
node1.lab.example.com openshift_node_labels="{'region':'infra', 'node-
role.kubernetes.io/compute':'true'}"
node2.lab.example.com openshift_node_labels="{'region':'infra', 'node-
role.kubernetes.io/compute':'true'}"
|
8.3. 保存 inventory 文件。
9. 将实验文本文件的所有内容添加到清单文件的末尾。
1
2
| [student@workstation install-run]$ cat general_vars.txt networking_vars.txt \
authentication _vars.txt persistence_vars.txt disconnected_vars.txt >> inventory
|
务必使用两个大于号(»)向 inventory 追加内容:
如果您使用单个 greater-than (>) 符号, 会覆盖现有文件 :
如果误覆盖了 inventory,请重复上述步骤;修改 *_vars.txt 的步骤可以跳过。
10. 检查 inventory 是否有错误。
OpenShift 高级安装耗时较长,清单错误可能导致必须重新安装。为避免因
操作界面、表格与配置图
清单错误而反复执行漫长的安装,课堂提供了检查器。在 workstation 虚拟机上运行以下评分脚本,确认清单无误:
1
| [student@workstation ~]$ lab install-run grade
|
如果您的清单文件有效, 评分脚本应该通过, 您应该看到类似以下输出的输出 :
1
| Ove raw RhVenco pyaniellCEChe Ck. 0, wate uencetie PASS
|
如果您的清单文件的变量值与参考解答清单文件的变量值不同, 期待输出类似于 :
1
2
| * Checking openshift_hosted_registry_storage_access_mode...... FAIL
_hw
|
1c1
1
2
3
4
| Se al eea ee eA et MM
['ReadWriteMany' ]
2] [ OSEv3:vars ] of
[ 'ReadWriteMany' ]
|
您的清单文件中的变量值。 参考解答清单文件中的变量值。
评分脚本对空白字符的差异敏感。 请检查您的变量是否与解决方案的变量完全匹配。
如果您的清册文件有拼错变量或变量缺失, 期望输出与以下相似 :
1
2
3
4
| * Checking openshift_hosted_registry_storage_access_mode...... FAIL
aos
< [ OSEv3:vars ] @openshift_hosted_registry_storage_access_modes =
['ReadWriteMany']
|
0
在此示例中, 您的清单文件中不存在 openshift_hosted_registry_storage_access_modes 变量。 纠正错误并重新运行评分脚本以确保它通过。 您可以将您的清单文件与位于 /home/student/DO280-ansible/inventory-run 的解决方案文件比较。
11. 运行 OpenShift Container Platform prerequisites.yml Playbook
操作界面、表格与配置图
/usr/share/ansible/openshift-ansible/playbooks/prerequisites.yml
ansible.cfg 将默认清单指定为 ./inventory。若要显式指定清单,使用 -i 加清单文件路径。
当Playbook完成时, 您应该在终端中看到以下输出 :
1
2
| PLAY RECAP *
localhost
|
: ok=12 changed=0 unreachable=0 failed=0
1
2
3
4
5
6
7
8
9
| PLAY RECAP KKRREKKKEKERKERKEKKKK KKKEEKKH EEE HERSEKKKKKKKKKAKKEKKKKKKKKKKKKERKKKK
localhost : ok=12 changed=0 unreachable=0 failed=0
master.lab.example.com : ok=68 changed=12 unreachable=0 failed=0
node1.lab.example.com : ok=62 changed=12 unreachable=0 failed=0
node2.lab.example.com : ok=61 changed=12 unreachable=0 failed=0
services.lab.example.com : ok=37 changed=4 unreachable=0 failed=0
workstation.lab.example.com : ok=2 changed=0 unreachable=0 failed=0
INSTALLER STATUS REAR EKEKEKKEKE EEE EEE REE KK KAK KEKETEE HEE EERE HEHEHE KK AKRKAKKK
Initialization : Complete (0:00:28)
|
12. 运行 OpenShift Container Platform deploy_cluster.yml Playbook
1
2
| [student@workstation install-run]$ ansible-playbook \
/usr/share/ansible/openshift-ansible/playbooks/deploy cluster.yml
|
安装可能需要较长时间。等待安装 Playbook 完成并返回命令提示符后,再继续下一步。
安装完成后,您应在终端中看到以下输出:
1
2
3
4
5
6
| PLAY RECAP *******t ttt erneeweeseneeree tere eee ereeweseens ees euesseeessssuess
localhost : ok=13 changed=0 unreachable=0 failed=0
master.lab.example.com : Ok=601 changed=251 unreachabie=0 failed=0
node1.lab.example.com : ok=134 changed=51 unreachable=0 failed=0
node2.lab.example.com : ok=134 changed=51 unreachable=0 failed=0
services.lab.example.com : ok=32 changed=8 unreachable=0 failed=0
|
: ok=32 changed=8 unreachable=o failed=o
1
2
3
4
5
6
7
8
9
10
11
12
| Sa ne ee ne ee >, 2 »«) "Sa eee. eeEe a ' ee
workstation.lab.example.com : ok=21 changed=0 unreachable=0 failed=0
INSTALLER STATUS KKKKKKIKKEKE EEE REE ERE KRKKK KKK KKK KEKKKK KKK KKK EKKK KEKKKEKEKRKEEKEEEE
Initialization : Complete (0:00:28)
Health Check : Complete (0:00:21)
etcd Install : Complete (0:01:16)
NFS Install : Complete (0:00:17)
Master Install : Complete (0:02:45)
Master Additional Install : Complete (0:01:23)
Node Install : Complete (0:03:15)
Hosted Install : Complete (0:03:00)
Web Console Install : Complete (0:00:44)
|
操作界面、表格与配置图
1
| service Catalog Install : Complete (0:02:44)
|
Ansible 会输出 Playbook 执行摘要。PLAY RECAP 中的计数可能与
系统上的不同。 请确定安装器执行过程中没有错误。
安装过程的详细信息记录在 install-run 实验目录的 ansible.log 中,可据此排查安装过程中的问题或失败。
操作界面、表格与配置图
13. 登录为 OpenShift 网络控制台的developer 用户,以验证安装。
13.1. 在浏览器输入 https://master.lab.example.com,信任课堂 OpenShift 生成的自签名证书;随后应出现控制台登录页面:
Login - OpenShift Co.-
- ; @@. | htps-//master lah example.com/lngin 2then=%2Fauth%7Fauto1 Q Search
REDHAT OPENSHIFT
1
2
3
| UFEWNoNIE I
ContainerPlatform
OPENSHIFT CONTAINER PLATFORM
|
Welcome to the OpenShift Container Platform. Usemame
Password
Log In
图 2.2:Web 控制台登录
13.2. 以 developer 为用户名、redhat 为密码登录。随后应显示 OpenShift 服务目录:
OpenShift Web Cons.
P/https://master.lab.example.com/console/catalog Q Search
OPENSHIFT CONTAINERPLATFORM
QSearch Catolog
Getting Started +Create Project
Browse Catalog Deploy Image ImportYAML/JSON Select from Project OTake Home Page Tour Databases
Middleware CIVCD All Languages Other
100Items Filte
.NET .NET .NET .NET
.NET Core Builder .NET Corc + .NET Core Example .NET Core Runtime jiistgreSQL
Images Example
(Persistent)
图 2.3:OpenShift Container Platform 服务目录
本引导练习到此结束。
操作界面、表格与配置图
引导练习:完成安装后任务#
在此练习中,你测试OpenShift容器平台集群装置的基本功能。
学习目标#
能够:
。在 OpenShift 上建立并部署一个简单的应用程序,测试该组的功能。
开始之前#
应先完成“准备安装”和“安装 Red Hat OpenShift Container Platform”两个引导练习。否则,重置 master、node1 和
node2 主机,然后在 workstation 上执行以下命令:
1
2
3
4
5
6
7
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ sudo yum install ansible atomic-openshift-utils
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ansible-playbook playbooks/prepare_install.yml
[student@workstation do280-ansible]$ ansible-playbook playbooks/install_ocp.yml
[student@workstation do280-ansible]$ cd
[student@workstation ~]$
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab install-post setup
|
操作步骤#
1. 在 workstation 上以 admin 用户登录 OpenShift 集群。
1.1. 在 workstation 上使用 oc,以 admin 用户登录集群。
1
2
3
4
5
6
| [student@workstation install-run]$ oc login -u admin -p redhat \
https://master.lab.example.com --insecure-skip-tls-verify=true
Login successful.
You don't have any projects. You can try to create a new project, by running
oc new-project <projectname>
Welcome! See 'oc help' to get started.
|
–insecure-skip-tls-verify=true 选项跳过 TLS 证书验证,使课堂安装生成的自签名证书可用于本次连接。
1.2. 尝试获取 OpenShift 节点的状态。
操作界面、表格与配置图
1
2
3
| [student@workstation ~-]$ oc get nodes
Error from server (Forbidden): nodes is forbidden: User "admin" cannot list
nodes at the cluster scope: User "admin" cannot list all nodes in the cluster
|
命令预计会失败, 因为admin 用户与 cluster-admin 角色没有关联。
2. 将 admin 用户绑定到 cluster-admin 角色。
2.1. 从 workstation , 使用 SSH 登录到 master 主机。 使用 sudo 成为 root 用户。
1
2
3
| [student@workstation ~]$ ssh master.lab.example.com
[student@master ~]$ sudo -i
[root@master ~]#
|
2.2. 确认当前已以 system:admin 用户通过 OpenShift API 认证。
1
2
| [root@master ~]# oc whoami
system:admin
|
2.3. 将 cluster-admin 角色授予 admin 用户。
1
2
| [root@master ~]# oc adm policy add-cluster-role-to-user cluster-admin admin
cluster role "cluster-admin" added: "admin"
|
2.4. 退出 master 主机。
1
2
3
4
5
6
| [root@master ~]# exit
logout
[student@master ~]$ exit
logout
Connection to master.lab.example.com closed.
[student@workstation ~]$
|
您现在可以使用admin 用户远程执行 workstation 机器的集群管理任务。
3. 使用 oc get命令对集群安装进行基本核查。
3.1. 从 workstation 登录到 OpenShift 集群作为admin 用户。 如果提示, 请输入 redhat 以获取密码。
1
2
3
4
5
6
7
| [student@workstation ~]$ oc login -u admin
Logged into ...output omitted... as "admin" using existing credentials. O
You have access to the following projects ...output omitted...:
* default
kube-public
kube-service-catalog
kube-system
|
操作界面、表格与配置图
1
2
3
4
5
6
7
8
9
| logging
Management -infra
openshift
openshift-ansible-service-broker
openshift-infra
openshift-node
openshift-template-service-broker
openshift-web-console
Using project "default".
|
oc 按用户和集群域名缓存凭据,并假定新用户仍要访问上一个用户所用的集群。
3.2. 使用 oc get nodes 命令来验证主机是否都返回 Ready 状态 :
1
2
3
4
5
| [root@master ~]# oc get nodes
NAME STATUS ROLES AGE VERSION
-master.lab.example.com Ready master 14m vi.9.1+a0ceibc657
node1.lab.example.com Ready compute 14m v1.9.1+a0ce1bc657
node2.lab.example.com Ready compute 14m v1.9.1+a0ce1bc657
|
node1 和 node2 都有计算作用。 这显示目录文件中的节点标签正确适用于每个节点。
3.3. 用 oc get pods 确认内部 Registry 和 Router 的 Pod 均为 Running:
1
2
3
4
5
6
7
| [root@master ~]# oc get pods
NAME READY STATUS RESTARTS AGE
docker -registry-1-9ckhk 1/1 Running iC} 14m
docker-registry-1-nccxn 1/1 Running 0 141m
registry-console-1-s2tkt 1/1 Running (c] om
router-1-hmtlx 1/1 Running (o) 12m
router -1-qxvx6 1/1 Running 0 12m
|
9m
1
2
3
| PEeytolt YOeVyvlisVset setne tft AUTTTLNY uy ait
router-1-hmtlx 1/1 Running (0) 12m
router-1-qxvx6 1/1 Running ic] 12m
|
4. 以 developer 用户执行 S2I 构建,测试集群功能。
4.1. 从 workstation VM 登录到 OpenShift 集群,作为developer 用户。
1
2
3
4
| [student@workstation ~]$ oc login -u developer
Authentication required for https://master.lab.example.com:443 (openshift)
Username: developer
Password:
|
为密码输入 redhat。
1
2
| Login successful.
You don't have any projects. You can try to create a new project, by running
|
操作界面、表格与配置图
oc new-project
4.2. 创建一个名为 smoke-test. 的新项目
1
2
3
4
5
| eciientonnaneeersen ~]$ oc new-project smoke-test
Now uSing project smoke-test on server "https://master.lab.example.com:443".
You can add applications ...output omitted... For example, try:
oc new-app centos/ruby-22-centos7~https://github.com/openshift/ruby-ex.git |
to build a new example application in Ruby.
|
4.3. 使用 PHP 5.6 执行 S2I 构建,在 OpenShift 部署 php-helloworld。源码位于 http://services.lab.example.com/php-helloworld。
1
2
| [student@workstation ~]$ oc new-app \
php:5.6~http://services.lab.example.com/php-helloworld --name hello
|
–name hello 将资源名从 php-helloworld 缩短为 hello,因此应用的 FQDN 也更短。
4.4. 使用 oc logs -f 监视构建过程:
1
| [student@workstation -]$ oc logs -f bc/hello
|
命令不立即完成, 因为构建过程需要几秒钟完成。 您应该看到类似以下输出的输出 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| Cloning "http://services.lab.example.com/php-helloworld" ...
Commit: ff80c967857225207864664a28fa078dbda31f33 (Establish remote repository)
Author: root <root@services.lab.example.com>
Date: Mon Aug 6 16:26:23 2018 -0700
---> Installing application source...
=> sourcing 20-copy-config.sh ...
---> 20:23:00 Processing additional arbitrary httpd configuration provided
PP IWF SZ BEG
=> sourcing @0-documentroot.conf ...
- => sourcing 50-mpm-tuning.conf ...
=> sourcing 40-ssl-certs.sh ...
Pushing image docker-registry.default.svc:5000/smoke-test/hello:latest ...
Pushed 0/6 layers, 1% complete
Pushed 1/6 layers, 23% complete
Pushed 2/6 layers, 38% complete
Pushed 3/6 layers, 53% complete
Pushed 4/6 layers, 78% complete
Pushed 5/6 layers, 100% complete
Pushed 6/6 layers, 100% complete
|
操作界面、表格与配置图
输出显示 OpenShift 能够复制源代码存储库, 建立镜像, 并将新镜像推进到内部镜像仓库。
4.5. 创建 Route,公开 hello 服务。
1
2
| [student@workstation ~]$ oc expose svc hello
route "hello" exposed
|
4.6. 查看 hello 服务的外部路由地址。
1
2
3
| [student@workstation ~]$ oc get routes
NAME HOST/PORT ...output omitted...
hello hello-smoke-test.apps.lab.example.com ...output omitted...
|
4.7. 用 curl 确认应用能够响应已发布路由上的请求:
1
2
| [student@workstation ~]$ curl hello-smoke-test.apps.lab.example.com
Hello, World! php version is 5.6.25
|
本引导练习到此结束。
操作界面、表格与配置图
自测问题#
C3 理解 OpenShift 网络#
3.1 网络地址#
| 地址 | 生命周期与用途 |
|---|
| Node IP | 宿主机网络地址,承载节点间通信 |
| Pod IP | Pod 生命周期内有效,重建可能改变 |
| ClusterIP | Service 存续期间的集群虚拟地址 |
| NodePort | 在节点端口上公开 Service,默认范围通常为 30000–32767 |
| Route 主机名 | Router 依据 HTTP Host 或 TLS SNI 选择后端 |
工作站通常不能直接路由到 Pod IP 或 ClusterIP;在节点或集群 Pod 内测试这些地址。外部访问使用 Route 或明确开放的 NodePort。
3.2 SDN 插件#
OpenShift SDN 使用 Open vSwitch 与 VXLAN 建立覆盖网络。ovs-subnet 提供扁平 Pod 网络;ovs-multitenant 通过项目 VNID 隔离;ovs-networkpolicy 使用 NetworkPolicy 表达隔离。先核对安装插件,再选择隔离命令,不能混用。
oc get hostsubnet 查看节点分配的子网;oc get netnamespace 查看项目网络标识。multitenant 中 oc adm pod-network join-projects --to=项目A 项目B 合并通信域,isolate-projects 项目B 恢复隔离;仅在该插件下执行。
3.3 Service 与 Route#
Service selector 匹配 Pod 标签,targetPort 指向容器端口。Pod 未就绪时不会进入正常的可用 Endpoints。Service 没有 Endpoints 时先检查标签和探针。
| TLS 终止方式 | TLS 终止位置 | 后端要求 |
|---|
| edge | Router | 后端 HTTP |
| passthrough | 应用 | 后端 HTTPS,按 SNI 转发 |
| reencrypt | Router 解密后再次加密 | 后端 HTTPS,Router 信任后端 CA |
Route 适用于 HTTP(S) 等可由 Router 处理的流量,不用于直接公开任意 MySQL TCP 端口。
实验操作#
引导练习:探索软件定义网络#
在本练习中,部署应用的多个 Pod,观察 OpenShift 软件定义网络功能。
文件:/home/student/DO280/labs/openshift-network/
学习目标#
能够使用多个应用Pod的副本并访问它们:
DirectlyviatheirpodIPaddressesfromwithinthecluster.
开始之前#
应先完成第 2 章的全部实验,并已有一个 master、两个节点组成的运行中集群。否则,重置 master、node1、node2,并在 workstation 执行以下命令,准备正确的环境:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab openshift-network setup
|
操作步骤#
1. 创建一个新项目。
1.1. 在 workstation 上使用 OpenShift 客户端连接 https://master.lab.example.com。
以 developer 用户登录,接受课堂证书。
1
2
3
4
| [student@workstation ~]$ oc login -u developer -p redhat \
https://master .lab.example.com
... Output omitted .,.,
Use insecure connections? (y/n): y
|
1.2. 创建 network-test 项目。
1
| [student@workstation ~]$ oc new-project network-test
|
操作界面、表格与配置图
2. 部署测试应用的多个 Pod。
2.1. 从私有镜像仓库部署 scaling 应用。
应用监听 8080 端口,显示主机 IP;在此环境中,该地址就是承载应用的 Pod IP。
1
2
| [student@workstation ~]$ oc new-app --name=hello -i php:7.0 \
http://registry.lab.example.com/scaling
|
2.2. 执行以下命令,确认应用 Pod 已就绪并运行。构建和部署需要一些时间。
1
| [student@workstation ~]$ oc get pods
|
命令的输出应类似如下:
1
2
3
| NAME READY STATUS RESTARTS AGE
hello-1-build 0/1 Completed 0 30s
hello-1-nvfgd 1/1 Running ic) 23s
|
2.3. 使用 oc scale 将应用扩展为两个 Pod。
1
2
| [student@workstation ~]$ oc scale --replicas=2 de hello
deploymentconfig "hello" scaled
|
2.4. 现在应有两个运行中的 Pod,通常每个节点各一个:
1
2
3
4
| [student@workstation ~]$ oc get pods -o wide
NAME pid STATUS IP NODE
hello-1-4bb1t ad Running 10.129.0.27 node1.lab.example.com
hello-1-nvfgd a Running 10.130.0.13 node2.lab.example.com
|
IP NODE
1
2
3
4
| [student@workstation ~]$ oc get pods -o wide
NAME pid STATUS IP NODE
hello-1-4bb1t ad Running 10.129.0.27 node1.lab.example.com
hello-1-nvfgd a Running 10.130.0.13 node2.lab.example.com
|
你的环境中的 Pod IP 地址可能不同。
3. 使用上一步列出的 IP,确认无法从 workstation 直接访问应用。
1
2
3
4
| [student@workstation ~]$ curl http://10.129.0.27:8080
curl: (7) Failed connect to 10.129.0.27:8080; Network is unreachable
[student@workstation ~]$ curl http://10.130.0.13:8080
curl: (7) Failed connect to 10.130.0.13:8080; Network is unreachable
|
Pod IP 无法从集群外直接访问。
操作界面、表格与配置图
4. 使用单个Pod IP 验证应用程序是否无障碍。
4.1. 在workstation上发射两个新终端,并使用Ssh与node1和node2连接:
1
2
| [student@workstation ~]$ ssh root@node1
[student@workstation ~]$ ssh root@node2
|
4.2. 使用上一个步骤中显示的各自iP地址,核实在node1和node2上是否可访问该应用程序。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| [root@node1 ~]# curl http://10.129.0.27:8080
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<br/> Server IP: 10.129.0.27
</body>
</html>
[root@node2 ~]# curl http://10.130.0.13:8080
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<br/> Server IP: 16.130.0.13
</body>
</html>
|
5. 使用 Service IP(ClusterIP)验证应用可访问性:
5.1. 在 workstation 使用 oc get svc 查看 Service IP。
1
2
3
| [student@workstation ~]$ oc get sve hello
NAME CLUSTER- IP EXTERNAL - IP PORT(S) AGE
hello 172.30.105.51 <none> 8080/TCP 25m
|
你的环境中的 ClusterIP 可能不同。
5.2. 使用集群 IP 地址核实无法从 workstation 访问该应用程序。
1
| [student@workstation ~]$ curl http://172.36.105.51:8080
|
操作界面、表格与配置图
curl: (7) 连接到 172.30.105.51:8080 失败; 网络无法访问
集群 IP 地址也无法从集群之外获取。
5.3. 使用 集群 IP 地址核实该应用程序可从master、node1或node2 访问。
1
2
| [student@workstation ~]$ ssh root@node1 curl http://172.30.105.51:8080
Server IP: 10.129.0.27
|
5.4. 向 ClusterIP URL 发送更多 HTTP 请求,观察请求如何通过轮询负载均衡发送到两个 Pod。
1
2
| [student@workstation ~]$ ssh root@node1 curl http://172.30.105.51:8080
<br/> Server IP: 10.130.0.13
|
5.5. 查看应用的 Service。
使用 oc describe svc 查看 hello 服务的详细信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
| [student@workstation ~]$ oc describe svc hello
Name: hello
Namespace: network-test
Labels: app=hello
Annotations: openshift.io/generated-by=OpenShiftNewApp
Selector: app=hello, deploymentconfig=hello
Type: ClusterIP
eR Bem Oma el55)
Porat: 8080-tcp 8080/TCP
TargetPort: 8080/TCP
! Endpoints: 10.128.0.24:8080,10.129.0.20:8080
- Session Affinity: None
- Events: <none>
|
端点属性显示请求所要遵循的 pod iP 地址列表。 当 pod 被杀死或创建新 pod 时, 这些端点会自动更新。
OpenShift 根据 Pod 的标签和 Service 选择器,将发往 ClusterIP 的请求分配到后端。本例服务选择带有 app=hello 和 deploymentconfig=hello 标签的 Pod。
查看其中一个 Pod,确认这些标签存在。
1
2
3
4
5
| [student@workstation ~]$ oc describe pod hello-1-4bbit
Labels: app=hello
deployment=hello-1
deploymentconfig=hello
_EOEee ee aS =-=-lh lrf
|
操作界面、表格与配置图
6. 允许从集群外访问应用程序。
编辑应用的 Service 配置,将 type 改为 NodePort。
6.1. 使用 oc edit svc 命令编辑应用程序的服务配置。
1
| [student@workstation ~]$ oc edit sve hello
|
该命令在 vi 中以 YAML 显示 Service 配置。将 type 改为 NodePort,在 ports 数组的对应条目中添加 nodePort,值为 30800。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| apiVersion: v1
kind: Service
metadata:
...
spec:
clusterIP: 172.30.105.51
ports:
- name: 8080-tcp
port: 8080
protocol: TCP
targetPort: 8080
nodePort: 30800
selector:
app: hello
deploymentconfig: hello
sessionAffinity: None
type: NodePort
status:
...
|
输入 :wq 保存并退出编辑器。
6.2. 通过再次运行 oc describe svc 命令来验证您的更改。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [student@workstation ~]$ oc describe svc hello
Name: hello
Namespace: network-test
Labels: app=hello
Annotations: openshift.io/generated-by=OpenShiftNewApp
Selector: app=hello, depioymentconfig=hello
Type: NodePort
PE 172.30.171.155
Pont: 8080-tcp 8080/TCP
TargetPort: 8080/TCP
NodePort: 8080-tcp 30800/TCP
Endpoints: 10.128.0.24:8080,10.129.0.20:8080
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
|
在 workstation 上通过节点 IP 和 NodePort 访问应用。
1
2
| [student@workstation ~]$ curl http://node1.lab.example.com:30800
Server IP: 10.129.0.27
|
操作界面、表格与配置图
1
2
| > curl http://node2.1lab.example.com:30800
|
|
现在可从集群外访问应用,请求仍在后端 Pod 之间进行负载均衡。
7. 上面观察了外部客户端到 Pod 的流量。要验证 Pod 到外部网络的出站流量,可使用 oc rsh 进入 Pod 内的 shell。
7.1. 使用 oc rsh 命令在Pod内访问 shell。
1
| [student@workstation ~]$ oc rsh hello-1-4bbit
|
7.2. 从 Pod 访问集群外主机,例如 services 虚拟机上的 Git 仓库,验证出站连接。
1
2
3
4
5
6
7
8
| sh-4.2$ curl http://services.lab.example.com
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.@ Strict//EN" "http: //www.w3.org/TR/
xhtm11/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<!-- git web interface version 1.8.3.1, (C) 2005-2006, Kay Sievers
<kay.sievers@vrfy.org>, Christian Gierke -->
<!-- git core binaries version 1.8.3.1 -->
|
7.3. 输入 exit 退出 Pod shell,返回 workstation 的命令提示符。
1
2
| sh-4.2$ exit ;
[student@workstation ~]$
|
清理
删除 network-test 项目。
1
2
3
| [student@workstation ~]$ oc delete project network-test :
project "network-test" deleted
StoeSA ee eee ee
|
本引导练习到此结束。
操作界面、表格与配置图
引导练习:创建路由#
在本练习中,为 OpenShift 上的应用创建采用边缘 TLS 终止的安全 Route。
文件:/home/student/DO280/labs/secure-route
应用地址:https://hello.apps.lab.example.com
学习目标#
能够为 OpenShift Container Platform 上部署的应用创建采用边缘 TLS 终止的安全 Route。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab secure-route setup
|
操作步骤#
1. 创建一个新项目。
1.1. 在 workstation 上使用 OpenShift 客户端连接 https://master.lab.example.com。
以 developer 用户登录;出现提示时接受课堂证书。
1
2
| [student@workstation ~]$ oc login -u developer -p redhat \
https://master .lab.example.com
|
1.2. 创建 secure-route 项目。
1
| [student@workstation ~]$ oc new-project secure-route
|
部署测试应用程序。
2.1. 从私有镜像仓库中部署 hello-openshift 应用程序。该应用程序在 8080 端口运行,并显示简单的短信。
操作界面、表格与配置图
命令可在 /home/student/DO280/labs/secure-route/commands.txt 上使用,以尽量减少打字错误。
1
2
3
| [student@workstation ~]$ oc new-app \
--docker-image=registry.lab.example.com/openshift/hello-openshift \
--name=hello
|
2.2. 运行以下命令,确认应用 Pod 已就绪并运行。部署需要一些时间。
1
2
3
| [student@workstation ~]$ oc get pods -o wide
NAME READY STATUS do 1rd NODE
hello-1-qmnbn 4a/1 Running .. 10.130.0.11 node1.lab.example.com
|
IP NODE
1
2
3
| [student@workstation ~]$ oc get pods -o wide
NAME READY STATUS do 1rd NODE
hello-1-qmnbn 4a/1 Running .. 10.130.0.11 node1.lab.example.com
|
记下 hello Pod 的 IP 及所在节点的 FQDN。你的 Pod 名称和 IP 可能不同;稍后测试应用时需要这个 IP。
3. 创建自签名 TLS 证书,保护路由连接。
3.1. 简要审查 /home/student/DO280/labs/secure-route 目录中 create-cert.sh 文件的命令。
1
2
3
4
5
6
7
8
9
10
11
12
13
| [student@workstation ~]$ cat \
/home/student /DO280/labs/secure-route/create-cert.sh
echo "Generating a private key..."
openssl genrsa -out hello.apps.lab.example.com.key 2048
echo "Generating a CSR..."
openssl req -new -key hello.apps.lab.example.com.key \
-out hello.apps.lab.example.com.csr \
-subj "/C=US/ST=NC/L=Raleigh/0=RedHat /OU=RHT/CN=hello.apps.lab.example.com"
echo "Generating a certificate..."
openssl x509 -req -days 366 -in \
hello.apps.lab.example.com.csr -signkey \
hello.apps.lab.example.com.key \
-out hello.apps.lab.example.com.crt
|
脚本创建了自签名 TLS 证书,该证书对 366 天有效。
3.2. 运行 create-cert.sh 脚本。
1
2
3
4
| [student@workstation ~]$ cd /home/student/DO280/labs/secure-route
[student@workstation secure-route]$ ./create-cert.sh
Generating a private key...
Generating RSA private key, 2048 bit long modulus
|
操作界面、表格与配置图
1
2
3
4
5
6
7
8
| Cia
e is 65537 (0x10001)
Generating a CSR...
Generating a certificate...
Signature ok
subject=/C=US/ST=NC/L=Raleigh/0=RedHat /OU=RHT/CN=hello.apps.lab. example.com
Getting Private key
DONE.
|
校验三个文件是否在同一文件夹中创建 :
.hello.apps.lab.example.com.crt
.hello.apps.lab.example.com.csr
.hello.apps.lab.example.com.key
4. 使用生成的 TLS 证书和密钥创建采用边缘 TLS 终止的安全 Route。
4.1. 使用上一步生成的文件创建安全的边缘终止 Route。
从终端窗口运行以下命令。 命令可以在 /home/student/DO280/labs/secure-route/commands.txt 文件上使用, 以最小化打字错误。
1
2
3
4
5
| [student@workstation secure-route]$ oc create route edge \
--service=hello --hostname=hello.apps.lab.example.com \
--key=hello.apps.lab.example.com.key \
--cert=hello.apps.lab.example.com.crt
route "hello" created
|
4.2. 确保创建该路由。
1
2
3
| [student@workstation secure-route]$ oc get routes
NAME HOST/PORT SERVICES PORT TERMINATION ..
hello hello.apps.lab.example.com hello 8080-tcp edge ..
|
4.3. 以 YAML 查看 Route 配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
| [student@workstation secure-route]$ oc get route/hello -o yaml
apiVersion: v1
kind: Route
metadata:
spec:
host: hello.apps.lab.example.com
port:
targetPort: 898@-tcp
tls:
certificate: |
-=== BEGIN) CERTIFICATE-----
MIIDZj...
-----END CERTIFICATE-----
|
操作界面、表格与配置图
1
2
3
4
5
6
7
8
9
10
11
| Key: |
-----BEGIN RSA PRIVATE KEY-----
MITEpQ...
-----END RSA PRIVATE KEY-----
termination: edge
to:
kind: Service
name: hello
weight: 100
wildcardPolicy: None
status:
|
测试路由
5.1. 确认通过该 Route 的 HTTP URL 无法访问 hello 服务。
1
2
3
4
| [student@workstation secure-route]$ curl http://hello.apps.lab.example.com
<hi>Application is not available</h1i>
<p>The application is currently not serving requests at this endpoint. It
may not have been started or is still starting.</p>
|
显示通用路由器主页,显示请求未转发给任何Pod。
5.2. 确认能够通过该 Route 的安全 URL 访问 hello 服务。
1
2
3
4
5
6
7
8
9
10
11
12
13
| [student@workstation secure-route]$ curl -k -vvv \
https://hello.apps.lab.example.com
* About to connect() to hello.apps.lab.example.com port 443 (#0)
bl Trying 172.25.250.11...
* Connected to hello.apps.lab.example.com (172.25.250.11) port 443 (#0)
* SSL connection using TLS_ECDHE RSA _WITH_AES_128 GCM_SHA256
* Server certificate:
™ subject: CN=hello.apps.lab.example.com,OU=RHT, O=RedHat,L=Raleigh,ST=NC,C=US |
* start date: Jun 29 07:02:24 2017 GMT
™" expire date: Jun 30 07:02:24 2018 GMT
* common name: hello.apps.lab.example.com
* issuer: CN=hello.apps.lab.example.com,OU=RHT, O=RedHat, L=Raleigh, ST=NC, C=US
Hello OpenShift!
|
5.3. 加密连接在 Router 处终止,Router 使用未加密的 HTTP 将请求转发到 Pod。因此可以使用 Pod IP 通过普通 HTTP 直接访问应用。通过 oc get pods -o
Pod 的 IP 地址。使用之前执行 oc get pods -o 时记录的地址,
wide 命令查看该 IP。
在 workstation 上打开新终端,执行以下命令。
1
2
3
4
5
| [student@workstation secure-route]$ ssh node1 curl -vvv http://10.130.0.11:8080
* About to connect() to 10.130.0.11 port 8080 (#0)
* Trying 10.130.0.11...
* Connected to 10.130.0.11 (10.130.0.11) port 8080 (#0)
> GET / HTTP/1.12
|
操作界面、表格与配置图
1
2
3
4
5
| > User-Agent: cCurl//.29.0
> Host: 10.130.0.11:8080
12 Accept: */*
>
Hello OpenShift!
|
清理
删除 secure-route 项目:
1
2
| [student@workstation secure-route]$ oc delete project secure-route
project "secure-route" deleted
|
本引导练习到此结束。
操作界面、表格与配置图
实验:探索 OpenShift 网络概念#
在这个实验里,用OpenShift路由 解决与访问应用程序有关的问题。
应用地址:http://hello.apps.lab.example.com:8o80
学习目标#
能够解决与使用路由访问应用程序有关的故障并修正错误。
在你开始之前
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab network-review setup
|
操作步骤#
1. 准备脚本以 developer 账户创建 network-review 项目,并部署 hello-openshift 应用。以 developer 登录,列出项目,确保当前使用 network-review。
2. 检查该项目的资源。
2.1. 检查此项目中的Pod, 并注意Pod IP 地址。
2.2. 检查该项目的服务,并注意IP集群地址。
2.3. 检查该项目的路由,并注明URL号路由。
3. 使用curl 命令访问应用程序以引用 URL 路由。 默认路由器主页显示的是默认路由器主页, 但不是实际应用程序输出。
4. 调查并排除故障 为何使用路由失败
4.1. 使用curl 命令直接引用 pod iP 地址。 校验您是否从应用程序中得到有效回应。
操作界面、表格与配置图
4.2. 使用curl 命令来调用集群 IP 地址。 应用程序不返回一个有效的响应。 这意味着服务配置有问题。
4.3. 在 workstation 上运行 oc describe svc 查看服务详情,检查为该服务注册的 Endpoints。
4.4. 查看应用Pod使用 oc describe pod 命令选择器标签的细节。 验证该 pod 和服务匹配的选择器标签, 以确保该 pod 注册为服务端点。
4.5. 编辑服务配置并修正错误。
将 app 值改为 hello-openshift,与 Pod 标签一致。保存后再次执行 oc describe svc hello-openshift,选择器和端点应如下所示:
1
2
3
4
5
6
7
8
| ... Output omitted ...
selector:
app: heilo-openshift
deploymentconfig: hello-openshift
sessionAffinity: None
type: ClusteriIP
status:
loadBalancer: {}
|
4.6. 在 master 虚拟机上访问 ClusterIP,确认应用能够返回有效结果。
4.7. 在 workstation 上再次测试 Route URL;此时仍然访问失败。
4.8. 查看使用 oc describe路由命令的路由详情。 请检查此路由的服务名称和注册端点。
4.9. 编辑路由配置并修正错误。
再次运行 oc describe route hello-openshift,服务名称和端点应如下所示。
4.10. 访问 Route URL,确认现在能够获得应用的有效输出。
5. 评价
运行以下命令来评分您的工作。
1
| [student@workstation ~]$ lab network-review grade
|
如果评分不是 PASS,请检查操作后重新运行评分命令。
操作界面、表格与配置图
清理
删除 network-review 项目。
实验到此结束
操作界面、表格与配置图
在这个实验里,用OpenShift路由 解决与访问应用程序有关的问题。
应用地址:http://hello.apps.lab.example.com:8o80
学习目标#
能够解决与使用路由访问应用程序有关的故障并修正错误。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab network-review setup
|
操作步骤#
1. 实验设置脚本创建了一个新项目,名为 network-review, 使用developer 用户账户, 并部署一个名为 hello-openshift 的应用程序, 以developer 用户身份登录到 OpenShift, 并列出项目。 确保 network-review 项目是用户的默认项目。
1
2
3
4
| eel
https://master.lab.example.com
[student@workstation ~]$ oc projects
[student@workstation ~]$ oc project network-review
|
2. 检查该项目的资源。
2.1. 检查此项目中的Pod, 并注意Pod IP 地址。
1
2
3
| [student@workstation ~]$ oc get pods -o wide
NAME oo fp NODE
hello-openshift-1-2lxcg .. 10.130.0.16 node1.lab.example.com
|
IP NODE
1
2
3
| [student@workstation ~]$ oc get pods -o wide
NAME oo fp NODE
hello-openshift-1-2lxcg .. 10.130.0.16 node1.lab.example.com
|
2.2. 检查该项目的服务,并注意IP集群地址。
1
| [student@workstation ~]$ oc get svc
|
操作界面、表格与配置图
1
2
| NAME CLUSTER-IP EXTERNAL- IP PORT(S) AGE
hello-openshift 172.30.162.216 <none> 8080/TCP, 8888/TCP 3m
|
3m
2.3. 检查该项目的路由,并注明URL号路由。
1
2
3
| [student@workstation ~]$ oc get routes
NAME HOST/PORT ee
hello-openshift hello.apps.lab.example.com ..
|
3. 使用curl 命令访问应用程序以引用 URL 路由。 默认路由器主页显示的是默认路由器主页, 但不是实际应用程序输出。
1
2
3
| [student@workstation ~]$ curl http://hello.apps.lab.example.com
<hi>Application is not available</h1>
<p>The application is currently not serving requests at this endpoint...
|
4. 调查并排除故障 为何使用路由失败
4.1. 使用curl 命令直接引用 pod IP 地址。 校验您是否从应用程序中得到有效回应。
打开一个新终端, 在运行curl 命令前打开 master VM 作为根用户的 SSH 会话。
1
2
3
| [student@workstation ~]$ ssh root@master
[root@master ~]# curl http://10.130.0.16:8080
Hello OpenShift!
|
4.2. 使用curl 命令来调用集群 IP 地址。 应用程序不返回一个有效的响应。 这意味着服务配置有问题。
1
2
3
| [root@master ~]# curl http://172.30.162.216:8080
Piece, output omitted ...
- Failed connect to 172.30.162.216:8080; Connection refused
|
4.3. 从 workstation VM 查看使用 oc describe svc 命令的服务详情。 审查注册的端点。
1
2
3
4
5
6
7
8
9
10
11
12
13
| [student@workstation ~]$ oc describe svc hello-openshift \
-n network-review
Name: hello-openshift
Namespace: network-review
Labels: app=hello-openshift
Annotations: openshift.io/generated -by=OpenShiftNewApp
Selector: app=hello_openshift, deploymentconfig=hello-openshift
Type: ClusterIP
IP: AL7/2 5 C19) LS ALG}
Port: 8080-tcp 8080/TCP
TargetPort: 8080/TCP
Endpoints: <none>
Port: 8888-tcp 8888/TCP
|
操作界面、表格与配置图
1
2
3
4
| TargetPort: 8668/ TCP
Endpoints: <none>
Session Affinity: None
Events: <none>
|
请注意, 此服务没有端点。 这是请求 服务 IP 返回一个拒绝的连接错误的原因。 记住端点是根据 pods 的 选择器 标签注册的。 请注意此服务的选择器标签。
4.4. 查看应用Pod使用 oc describe pod 命令选择器标签的细节。 验证该 pod 和服务匹配的选择器标签, 以确保该 pod 注册为服务端点。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| [student@workstation ~]$ oc describe pod hello-openshift-1-21xcg \
Name: hello-openshift-1-21xcg
Namespace: network-review
Node: node1.lab.example.com/172.25.250.11
Start Time: Wed, 18 Jul 2018 13:31:03 -0700
Labels: app=hello-openshift
deployment=hello-openshift-1
deploymentconfig=hello-openshift
Annotations: kubernetes.io/created-
by={"kind":"SerializedReference", "apiVersion":"v1i","reference":
{"kind":"ReplicationControliler","namespace": "network-review", "name":"hello-
openshift-1","uid":"5e91df96-57e1-11e7-9...
openshift.io/deployment -config.latest-version=1
openshift.io/deployment -config.name=hello-openshift
openshift.io/deployment .name=hello-openshift-1
openshift.io/generated-by=OpenShiftNewApp
openshift.io/scc=restricted
Status: Running
IP: 10.130.0.16
Containers:
helilo-openshift:
... output omitted ...
|
注意 Pod 标签是 app=hello-openshift,而 Service 选择器是 app=hello_openshift,需要编辑服务
配置,修改选择器,使其与 Pod 标签匹配。
4.5. 编辑服务配置并修正错误。
使用 workstation VM 的 oc edit svc 命令编辑服务配置。
1
| [student@workstation ~]$ oc edit svc hello-openshift
|
该命令在 vi 中显示 Service 配置。selector 下的 app 值误写为 hello_openshift。没有 Pod 带这个标签,所以应用不会响应通过该服务发出的请求。
将 app 改为 hello-openshift,与 Pod 标签一致。保存后再次运行 oc describe svc
操作界面、表格与配置图
hello-openshift。选择器和端点应如下所示:
1
2
3
4
5
6
7
8
| ... Output omitted ,,.
selector:
app: hello-openshift
deploymentconfig: hello-openshift
sessionAffinity: None
type: ClusterIP
status:
- loadBalancer: {}
|
4.6. 在 master 虚拟机上访问 ClusterIP,确认应用能够返回有效结果。
1
2
| [root@master ~]# curl http://172.30.162.216:8080
Hello OpenShift!
|
4.7. 在 workstation 上再次测试 Route URL;访问仍然失败。
1
2
3
4
| [student@workstation ~]$ curl http://hello.apps.lab.example.com
<hi>Application is not available</hi>
<p>The application is currently not serving requests at this endpoint...
le ee ee OE ON ae ll -™
|
4.8. 查看使用 oc describe路由命令的路由详情。 请检查此路由的服务名称和注册端点。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| [student@workstation ~]$ oc describe route hello-openshift
Name: hello-openshift
Namespace: network-review
Created: 19 minutes ago
Labels: app=hello-openshift
Annotations: <none>
Requested Host: hello.apps.lab.example.com
exposed on router router 19 minutes ago
Path: <none>
TLS Termination: <none>
Insecure Policy: <none>
Endpoint Port: 8080-tcp
Service: hello-opensift
"Weight: 100 (100%)
Endpoints: <error: endpoints ""hello-opensift" not found>
_ fee
|
注意此路由没有端点的错误。 这就是为什么请求访问 URL 路由时没有显示应用程序主页。 记住路由器询问端点服务并登记 load-balancing 的有效端点。 请注意
操作界面、表格与配置图
服务名存在拼写错误,应改为实际服务名称 hello-openshift。
4.9. 编辑路由配置并修正错误。
使用 workstation VM 上的 oc edit路由命令编辑路由配置。
1
| [student@workstation ~]$ oc edit route hello-openshift
|
该命令在 vi 中显示 Route 配置。spec.to 中的目标服务名误写为 hello-opensift,项目中不存在这个服务。将目标服务名改为 hello-openshift,然后保存。
再次运行 oc describe route hello-openshift,服务名称和端点应如下所示。
1
2
3
4
5
6
7
8
9
10
11
| ... Output omitted ...
spec:
host: hello.apps.lab.example.com
port:
targetPort: 8080-tcp
to:
kind: Service
name: hello-openshift
weight: 100
wildcardPolicy: None
... Output omitted ...
|
4.10. 访问 Route URL,确认现在能够获得应用的有效输出。
1
2
| [student@workstation ~]$ curl http://hello.apps.lab.example.com
Hello OpenShift!
|
5. 评价
运行以下命令来评分您的工作。
1
| [student@workstation ~]$ lab network-review grade
|
如果评分不是 PASS,请检查操作后重新运行评分命令。
清理
删除 network-review 项目。
1
2
| [student@workstation ~]$ oc delete project network-review
project "network-review" deleted
|
实验到此结束
操作界面、表格与配置图
自测问题#
C4 使用命令行管理与排错#
4.1 命令分类#
| 目标 | 命令 |
|---|
| 确认上下文 | oc whoami、oc project、oc config view --minify |
| 观察对象 | oc get、oc describe、oc get events |
| 读取日志 | oc logs POD -c 容器、oc logs POD --previous |
| 容器内操作 | oc exec POD -- 命令、oc rsh POD |
| 文件传输 | oc rsync 本地目录/ POD:/目标目录/,两端需相应工具 |
| 临时访问 | oc port-forward POD 18080:8080 |
| 更新与回退 | oc apply -f、oc patch、oc rollout undo dc/名称 |
4.2 状态与证据#
| 现象 | 检查点 | 常见修复 |
|---|
| Pending | Events、PVC、nodeSelector、资源请求 | 满足绑定和调度条件 |
| ImagePullBackOff | Events、镜像名、仓库证书及凭据 | 修正镜像或 pull-secret |
| CrashLoopBackOff | previous 日志、退出码、环境变量 | 修复启动配置 |
| Running 但 0/1 Ready | readiness 路径、端口、依赖服务 | 修正就绪条件 |
| Route 返回 503 | Endpoints、Router、后端端口 | 修复后端或路由映射 |
| Forbidden | 当前用户、角色、作用范围 | 最小范围授权 |
| OOMKilled | 上次终止原因、内存限制和指标 | 修复内存用量或调整额度 |
节点层读取 journalctl -u atomic-openshift-node、journalctl -u docker;Master 层读取实际启用的 atomic-openshift-master 服务日志。先用 systemctl list-units 'atomic-openshift*' 确认服务名称。
实验操作#
引导练习:使用 oc 管理 OpenShift 实例#
在此练习中, 使用 oc 命令管理 OpenShift Container Platform 实例。
应用地址:https://master.lab.example.com
学习目标#
能够登录到 OpenShift master, 并使用 oc 命令行工具管理集群。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do28@-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab manage-oc setup
|
操作步骤#
1. 获取 OpenShift 集群的当前状态。
1.1. 在 workstation 打开终端,以 admin 用户和 redhat 密码登录 OpenShift。出现提示时接受课堂证书。
1
2
| [student@workstation ~]$ oc login -u admin -p redhat \
https://master.lab.example.com
|
1.2. 确保您正在使用默认项目 :
1
2
| [student@workstation ~]$ oc project default
Already on project "default" on server "https://master.lab.example.com:443".
|
1.3. 列出属于集群一部分的节点及其状态:
1
| [student@workstation ~]$ oc get nodes
|
操作界面、表格与配置图
此命令产生一个列表式的节点列表, 类似以下的节点。 注意已准备的节点, 作为其状态描述的一部分。 应用程序( pods) 将安装在这样的节点上。
1
2
3
4
| NAME STATUS ROLES AGE VERSION
master.lab.example.com Ready master 8m vi.9.1+a0ce1bc6é57
node1.lab.example.com Ready compute 8m vi.9.1+a0ce1bc657
node2.lab.example.com Ready compute 8m v1.9.1+a0ce1bc657
|
8m v1.9.1+a0ce1bc657
1
2
3
4
| NAME STATUS ROLES AGE VERSION
master.lab.example.com Ready master 8m vi.9.1+a0ce1bc6é57
node1.lab.example.com Ready compute 8m vi.9.1+a0ce1bc657
node2.lab.example.com Ready compute 8m v1.9.1+a0ce1bc657
|
8m compute v1.9.1+a0ce1bc657
1.4. 使用 oc describe 命令 显示关于 OpenShift master 节点的更详细信息 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| [student@workstation ~]$ oc describe node master.lab.example.com
Name: master.lab.example.com
Role:
Labels: beta. kubernetes.io/arch=amd64
beta. kubernetes.io/os=linux
kubernetes. io/hostname=master.lab.example.com
node-role.kubernetes.io/master=true
openshift-infra=apiserver
region=master
Taints: <none>
... output omitted ...
System Info:
... output omitted ...
Kernel Version: 3.10.0-862.e17.x86_64
OS Image: Red Hat Enterprise Linux Server 7.5 (Maipo)
Operating System: linux
Architecture: amd64
Container Runtime Version: docker://1.13.1
Kubelet Version: v1.9.1+a0ce1lbc657
Kube-Proxy Version: vi.9.1+a0ce1bc657
ExternalID: master.lab.example.com
... Output omitted ...
Events:
... output omitted ...
Normal Starting Starting kubelet.
4... Output omitted ...
- Normal NodeReady Node master.lab.example.com status is now: NodeReady
|
Events 部分显示集群启动以来 master 节点上发生的重要生命周期事件,对排查 master 问题很有帮助。
1.5. 同样,检查OpenShift节点之一的描述:
1
2
3
4
5
6
7
8
9
10
11
| [student@workstation ~]$ oc describe node node1.lab.example.com
Name: node1.lab.example.com
Roles: compute
Labels: beta. kubernetes.io/arch=amd64
beta.kubernetes.io0/os=linux
kubernetes.io/hostname=node1.lab.example.com
node-role.kubernetes.io/compute=true
region=infra
Annotations: volumes. kubernetes.io/controller -managed-attach-detach=true
Taints: <none>
CreationTimestamp: Wed, 25 Jul 2018 14:18:36 -0700
|
操作界面、表格与配置图
1
2
| ».. OUTpuUt omitted ...
Node node1.lab.example.com status is now: NodeReady
|
1.6. 使用 oc get pods 命令检查项目中的现有 pods 列表。
1
2
3
4
5
6
7
| [student@workstation ~]$ oc get pods -o wide
NAME READY STATUS i[?) NODE
docker-registry-1-pnt4r 1/1 Running 10.128.0.12 node2.lab.example.com
docker-registry-1-q8hrl 1/1 Running 10.129.0.7 node1.lab.example.com
registry-console-1-ch4gp 1/1 Running 10.128.0.11 node2.lab.example.com
router -1-9dq65 1/1 Running 172.25.250.11 node1.lab.example.com
router-1-vsnb9 1/1 Running 172.25.250.12 node2.lab.example.com
|
NODE 列显示 Pod 所在的节点。
1.7. 使用 oc describe命令查看一个Pod的详细信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| [student@workstation ~]$ oc describe pod docker-registry-1-pnt4r
Name: docker-registry-1-pnt4r
Namespace: default
Node: node2.lab.example.com/172.25.250.12
Start Time: Wed, 25 Jul 2018 14:21:13 -9700
Labels: deployment=docker-registry-1
deploymentconfig=docker-registry
docker-registry=default
Annotations: openshift .io/deployment -config.latest-version=1
openshift.io/deployment -config.name=docker-registry
openshift.io/deployment .name=docker-registry-1
openshift.io/scc=restricted
Status: Running
... Output omitted ...
Events:
Type Reason Age From
Message
|
9m default-scheduler Normal Scheduled
1
2
3
4
| Successfully assigned docker-registry-1-pnt4r to node2.lab.example.com
... output omitted ,.,
Normal Created 5m (x3 over 9m) kubelet,
node2.lab.example.com Created container
|
仔细查看 Events 部分,其中记录 Pod 的重要生命周期事件,有助于排查 Pod 和节点问题。
2. 查看 Pod。
2.1. oc exec 是管理员常用命令,可在 Pod 中远程执行命令。对 Registry Pod 执行 hostname。
1
| [student@workstation ~]$ oc exec docker-registry-i-pnt4r hostname
|
操作界面、表格与配置图
1
2
| agocker -reqistry-1-pnt4r
a eee ee aS ==
|
在其中一个 Router Pod 中执行 ls。
1
2
3
4
5
6
7
| [student@workstation ~]$ oc exec router-1-9dq65 ls /
bin
boot
dev
etc
exports
~ hame
|
2.2. 任意命令可以执行, 只要在您执行命令的Pod内可以使用。 这种能力可用于从容器内读取文件、内容和进程。 请检查 /etc/resolv.conf 文件。
1
2
3
4
5
| [student@workstation ~]$ oc exec docker-registry-1-pnt4r cat /etc/resolv.conf
nameserver 172.25.250.12
search default.svc.cluster.local svc.cluster.local cluster.local lab.example.com
example.com
options ndots:5
|
2.3. 使用 oc rsh 连接 Router Pod 的远程 shell,以便深入排错。在 master 节点上启动 Pod 内的远程 shell:
1
2
| [student@workstation ~]$ oc rsh docker-registry-1-pnt4r
bash-4.2$
|
2.4. 再次执行先前通过非交互方式运行过的 ls 命令:
1
2
3
| bash-4.2$ ls /
bin config.yml etc lib lost+found mnt proc root sbin sys usr
boot dev home 1ib64 media opt registry run srv_ tmp var
|
2.5. 退出远程shell :
也可以使用 oc rsh 进入运行中的 Pod shell。
操作界面、表格与配置图
3. 查看项目状态与集群事件。
3.1. 使用 oc status命令获取当前项目的 high-level 状态 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| [student@workstation ~]$ oc status -v
In project default on server https://master.lab.example.com:443
https://docker-registry-default.apps.lab.example.com (passthrough) (svc/docker-
registry)
dc/docker-registry deploys registry.lab.example.com/openshift3/ose-docker-
registry:v3.9.14
deployment #1 deployed 15 minutes ago - 2 pods
svc/kubernetes - 172.30.0.1 ports 443, 53->8053, 53->8053
https://registry-console-default.apps.lab.example.com (passthrough) (svc/
registry-console)
dc/registry-console deploys docker.io/openshift3/registry-console:v3.9
deployment #1 deployed 14 minutes ago - 1 pod
svc/router - 172.30.149.232 ports 80, 443, 1936
dc/router deploys registry.lab.example.com/openshift3/ose-haproxy-
router:v3.9.14
deployment #1 deployed 16 minutes ago - 2 pods
View details with 'oc describe <resource>/<name>' or list everything with 'oc
get all'.
|
您主节点上的输出可能与上面显示的有差异。
3.2. 使用 oc get事件命令查看 OpenShift 集群中的 生命周期 事件 :
1
| [student@workstation ~]$ oc get events
|
信息按事件发生的先后顺序以表格形式列报。
4. 导入与导出资源。
4.1. 使用 oc get all命令来获取项目资源列表 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [student@workstation ~]$ oc get all
NAME REVISION DESIRED CURRENT TRIGGERED BY
deploymentconfigs/docker-registry 1 2 2) config
deploymentconfigs/registry-console 1 4 sit config
deploymentconfigs/router 4 2 2 config
NAME DOCKER REPO TAGS UPDATED
imagestreams/registry-console docker-registry.default.svc:
5000/default/\registry-console v3.9
... output omitted ...
NAME READY STATUS RESTARTS AGE
po/docker-registry-1-pnt4r 1/1 Running 2 16m
po/docker -registry-1-q8hrl 1/1 Running 1 16m
po/registry-console-1-ch4gp 1/1 Running 2 15m
po/router-1-9dq65 1/1 Running 1 16m
po/router-1-vsnb9 1/1 Running 2 16m
|
操作界面、表格与配置图
1
2
3
4
5
| NAME DESIRED CURRENT READY AGE
rc/docker-registry-1 2 2 2 17m
rc/registry-console-i 1 1 1 15m
rc/router-1 2 2 2 17m
... Output omitted ...
|
您系统中的输出可能与上面显示的有差异。
4.2. oc export 导出现有资源,将其转换为 YAML 或 JSON 配置文件,用于备份或在集群其他位置重新创建资源。
以默认的 YAML 格式导出 docker-registry-1-pnt4r pod 资源。 将pod 名称替换为您集群中可用的一个注册pod。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| [student@workstation ~]$ oc export pod docker-registry-1-pnt4r
apiversion: v1
kind: Pod
metadata:
annotations:
openshift.io/deployment-config.latest-version: "1"
openshift.io/deployment-config.name: docker-registry
openshift.io/deployment.name: docker-registry-1
openshift.io/scc: restricted
creationTimestamp: null
generateName: docker-registry-1-
labels:
deployment: docker-registry-1
deploymentconfig: docker-registry
docker-registry: default
ownerReferences:
= apiVersion: v1
blockOwnerDeletion: true
controller: true
kind: ReplicationController
name: docker-registry-1
... Output omitted ...
|
给 oc export 传入 -o json,可以用 JSON 格式导出 Pod 定义。
4.3. 使用 oc export 的 –as-template 选项,可将多个资源同时导出为 OpenShift 模板。
通过运行以下命令将服务和部署配置定义导出为单个 OpenShift 模板:
1
2
3
4
5
6
| [student@workstation ~]$ oc export svc,dc docker-registry --as-template=docker-
registry
apiVersion: v1
kind: Template
metadata:
creationTimestamp: null
|
操作界面、表格与配置图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| name: agocker-registry
objects:
- apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
docker-registry: default
name: docker-registry
spec:
ports:
- name: 5000-tcp
port: 5000
protocol: TCP
targetPort: 5000
selector:
docker-registry: default
sessionAffinity: ClientIP
sessionAffinityConfig:
clientIP:
timeoutSeconds: 10800
type: ClusterIP
status:
loadBalancer: {}
... output omitted ...
|
上一个命令将服务定义和部署配置导出为模板。 此命令的输出可以作为输入发送到 oc 创建命令以在组内重建资源。
运行 oc export –help 查看可用选项的详细列表。
1
2
3
4
5
6
7
8
9
10
11
| [student@workstation ~]$ oc export --help
... Output omitted ...
Examples:
# export the services and deployment configurations labeled name=test
oc export svc,dc -1 name=test
# export all services to a template
oc export service --as-template=test
# export to JSON
oc export service -o json
... output omitted ...
a as esa ee OS Sle
|
您可以使用标准的 UNiX 重定向符号 > 将输出从 oc 导出命令转向文件。 例如 :
1
| oc export svc,dc docker-registry > docker-registry.yaml
|
本引导练习到此结束。
操作界面、表格与配置图
引导练习:排查常见问题#
在本练习中,排查并修复 OpenShift 上失败的应用部署。
S21 Application: http://services.lab.example.com/php-helloworld
学习目标#
能够解答OpenShift上一个失败的应用程序, 并解决问题。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab common-troubleshoot setup
|
操作步骤#
1. 创建一个新项目。
1.1. 在 workstation 上使用 OpenShift 客户端访问 https://master.lab.example.com。
以 developer 用户登录,接受课堂证书。
1
2
| [student@workstation ~]$ oc login -u developer -p redhat \
https://master .lab.example.com
|
1.2. 创建 common-troubleshoot 项目 :
1
2
3
| [student@workstation ~]$ oc new-project common-troubleshoot
Now using project "common-troubleshoot" on server "https://
master.lab.example.com: 443".
|
2. 部署 Source-to-Image(S2I)应用。
操作界面、表格与配置图
2.1. 使用 services 虚拟机上 Git 仓库中的 php-helloworld 源码,在 OpenShift 创建应用。
1
2
3
4
5
| [student@workstation ~]$ oc new-app --name=hello -i php:5.4 \
http://services.lab.example .com/php-helloworld
error: multiple images or templates matched "php:5.4": 2
The argument "php:5.4" could apply to the following Docker imag
image streams, or templates:
|
观察错误信息,其中指出镜像流标签不正确。
2.2. 使用 oc describe命令列出php 镜像流中的有效标记。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| [student@workstation ~]$ oc describe is php -n openshift
Name: php
Namespace: openshift
Created: About an hour ago
Labels: <none>
Annotations: openshift.io/display-name=PHP
openshift.io/image.dockerRepositor yCheck=2018-07-257T21:16:14Z
Docker Pull Spec: docker-registry.default.svc:5000/openshift/php
Image Lookup: local=false
Unique Images: 2
Tags: iS}
7.1 (latest)
tagged from registry.lab.example.com/rhscl/php-71-rhel7:latest
Build and run PHP 7.1 applications on RHEL 7. For more information about using
this builder image, including OpenShift considerations, see https://github.com/
sclorg/s2i-php-container/blob/master/7.1/README.md.
Tags: builder, php
Supports: php:7.1, php
Example Repo: https://github.com/openshift/cakephp-ex.git
! error: Import failed (NotFound): dockerimage.image.openshift.io
"registry.lab.example.com/rhscl/php-71-rhel7:latest" not found
7.0
tagged from registry.lab.example.com/rhscl/php-70-rhel7:latest
Build and run PHP 7.0 applications on RHEL 7. For more information about using
this builder image, including OpenShift considerations, see https://github.com/
sclorg/s2i-php-container/blob/master/7.O/README.md.
Tags: builder, php
Supports: php:7.0, php
Example Repo: https://github.com/openshift/cakephp-ex.git
* registry.lab.example.com/rhscl/php-70-
rhel7@sha256 : 23765e00df8d0a934ce4f2e22802bc0211a6d 450bfbb69144b18cbob51008cdd
5 days ago
ae
|
操作界面、表格与配置图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| tagged trom registry.lab.example.com/rnscl/pnp-56-rhei7:latest
Build and run PHP 5.6 applications on RHEL 7. For more information about using
this builder image, including OpenShift considerations, see https://github.com/
sclorg/s2i-php-container/blob/master/5.6/README.md.
Tags: builder, php
Supports: php:5.6, php
Example Repo: https://github.com/openshift/cakephp-ex.git
* registry.lab.example.com/rhsel/php-56-
rhel7@sha256: 920c2cf85b5da5d0701898f0ec9ee567473fa4b9af6f3ac5b2b3F863796bbd68 |
io.)
tagged from registry.lab.example.com/openshift3/php-55-rhel7:latest
Build and run PHP 5.5 applications on RHEL 7. For more information about using
this builder image, including OpenShift considerations, see https://github.com/
sclorg/s2i-php-container/biob/master/5.5/README.md.
Tags: hidden, builder, php
Supports: php:5.5, php
Example Repo: https://github.com/openshift/cakephp-ex.git
! error: Import failed (NotFound): dockerimage.image.openshift.io
"registry.lab.example.com/openshift3/php-55-rhel7:latest" not found
About an hour ago
|
输出表明 php:7.0 和 php:5.6 是有效标签,而 php:7.1 和 php:5.5 无效,因为对应镜像不存在。
2.3. 使用正确的镜像流标记部署应用程序 :
1
2
3
4
5
6
7
8
9
10
11
12
| [student@workstation ~]$ oc new-app --name=hello -i php:7.0 \
http://services.lab.example.com/php-helloworld
--> Found image c101534 (10 months old) in image stream "openshift/php" under
tag "7.0" for ""php:7.0"
... Output omitted ...
--> Success
Build scheduled, use 'oc logs -f bc/hello' to track its progress.
Application is not exposed. You can expose services to the outside world by
executing one or more of the commands below:
'ac expose svc/hello'
Run 'oc status' to view your app.
ee ee ae ee 5 i: aad: =|Gkdim Sh epoom]
|
Oc new-app 命令现在应该成功。
2.4. 验证应用是否成功构建并部署:
1
2
3
| [student@workstation ~]$ oc get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
hello-1-builid 0/1 Pending (0) 41s <none> <none>
|
IP NODE STATUS RESTARTS
1
2
3
| [student@workstation ~]$ oc get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
hello-1-builid 0/1 Pending (0) 41s <none> <none>
|
hello-1-build Pod在等待状态, 申请Pod没有启动。 调查为何部署在等待状态, 并解决问题。
操作界面、表格与配置图
3. 使用 oc 日志命令检查构建Pod的日志。
1
| [student@workstation ~]$ oc logs hello-1-build
|
此命令不产生任何输出。 日志显示没有有用的信息可以帮助您解答问题。
4. 查看项目事件日志,可用两种方式。第一种是运行以下 oc get events 命令:
1
2
| [student@workstation ~]$ oc get events
LAST SEEN FIRST SEEN COUNT NAME KIND
|
5m 5m hello-1-build.1544bd6f20c095e9 Pod
5m 5m he11o-1-bui1d.1544bd76256c9383 Pod
9s 2m 14 hello-1-build.1544bd9af1aac5o6 Pod
1
2
3
4
5
6
7
8
| TMPE REASON SOURCE
Warning FailedScheduling default-scheduler
Warning FailedScheduling default-scheduler
Warning FailedScheduling default-scheduler
MESSAGE
0/3 nodes are available: 1 MatchNodeSelector, 2 NodeNotReady.
skip schedule deleting pod: common-troubleshoot/hello-1-build
0/3 nodes are available: 1 MatchNodeSelector, 2 NodeNotReady.
|
使用 oc describe命令来查看输出是否给出了某些提示, 说明为什么Pod失败 :
1
2
3
4
5
6
7
8
9
10
11
12
13
| [student@workstation ~]$ oc describe pod hello-1-build
Name: hello-1-build
Namespace: common-troubleshoot
Node: <none>
Labels: openshift.io/build.name=hello-1
Annotations: openshift.io/build.name=hello-i
openshift.io/scc=privileged
- Status: Pending
P=. output omitted ...
Events:
Type Reason Age From Message
Warning FailedScheduling 2s (x18 over 4m) default-scheduler 6/3 nodes are
available: 1 MatchNodeSelector, 2 NodeNotReady.
|
此命令还在事件一节中报告同样的失败调度警告。
事件日志表明,没有可用于调度 Pod 的节点。
5. 调查警告原因,查看集群节点状态。注意,此命令应在 master 上以 root 用户执行。
1
2
3
4
| [student@workstation ~]$ ssh root@master oc get nodes
NAME STATUS ROLES AGE VERSION
Master.lab.example.com Ready master ah vi1.9.1+a0ce1bc657
node1.lab.example.com NotReady compute th v1.9.1+a0ce1bc657
|
1h v1.9.1+a0ce1bc657
1
2
3
4
| [student@workstation ~]$ ssh root@master oc get nodes
NAME STATUS ROLES AGE VERSION
Master.lab.example.com Ready master ah vi1.9.1+a0ce1bc657
node1.lab.example.com NotReady compute th v1.9.1+a0ce1bc657
|
操作界面、表格与配置图
1
| nodez.labd.example.com NotReady compute in vi.9.1+a0celbc657
|
STATus 列中显示 node1 和 node2 都处于未准备状态。 Kubernetes 不能在标记为未准备状态的节点上排队运行。
如果在这一步骤中出现错误,例如:
1
2
| error: You must be logged in to the server (the server has asked for the
client to provide credentials)
|
登录到 master 主机为 root 并运行以下命令。 然后继续运行 oc get节点命令。
1
| [root@master ~]# oc login -u system:admin
|
6. 调查节点为何不是 Ready。OpenShift 节点必须运行 atomic-openshift-node 服务,该服务与 master 通信,并按 master 的调度运行 Pod。
在 workstation 上打开两个新终端, 并使用 ssh 命令登录到 node1 和 node2 主机为 root :
1
2
| [student@workstation ~]$ ssh root@node1
[student@workstation ~]$ ssh root@node2
|
检查两个节点上的 atomic-openshift-node 服务状态 :
1
2
| [root@node1 ~]# systemctl status atomic-openshift-node.service -1
[root@node2 ~]# systemctl status atomic-openshift-node.service -1
|
虽然两个节点都报告该服务正在运行中。 服务报告, 节点上的 docker 守护程序有问题 :
1
2
3
4
5
6
7
| ... Output omitted ...
Jul 25 15:46:08 node2.lab.example.com atomic-openshift-node[23635]: E0725
15:46:08. 480373 23635 generic.go:197] GenericPLEG: Unable to retrieve pods: rpc
error: code = Unknown desc = Cannot connect to the Docker daemon at unix:///var/
run/docker.sock. Is the docker daemon running?
... Output omitted ...
ee ie = a ee, eee
|
7. 检查两个节点上的 docker 服务状态 :
1
| [root@node1 ~]# systemctl status docker.service -1
|
操作界面、表格与配置图
1
| Lroot@node2 ~|# systemctl status docker.service -1
|
此服务在两个节点上都不活动 :
1
2
3
4
5
6
7
| ... output omitted .,.,
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset:
disabled)
Active: inactive (dead) since Wed 2018-07-25 15:32:35 PDT; 14min ago
... Output omitted ...
Jul 25 15:32:35 node2.lab.example.com systemd[1]: Stopped Docker Application
Container Engine.
|
8. 在两个节点上启动 docker 服务 :
1
2
3
| - [root@node1 ~]# systemctl start docker.service
- [root@node2 ~]# systemctl start docker.service
|
|
9. 在 workstation 上运行 oc get nodes,确认两个节点均变为 Ready:
1
2
3
4
5
| [student@workstation ~]$ ssh root@master oc get nodes
NAME STATUS ROLES AGE VERSION
- master.lab.example.com Ready master ah vi.9.1+a0ce1bc6é57
node1.lab.example.com Ready compute th vi.9.1+a@ce1bc657
node2.lab.example.com Ready compute ih vi.9.1+a0ce1bc657
|
1h v1.9.1+a0ce1bc657
1
2
3
4
5
| [student@workstation ~]$ ssh root@master oc get nodes
NAME STATUS ROLES AGE VERSION
- master.lab.example.com Ready master ah vi.9.1+a0ce1bc6é57
node1.lab.example.com Ready compute th vi.9.1+a@ce1bc657
node2.lab.example.com Ready compute ih vi.9.1+a0ce1bc657
|
1h v1.9.1+a0ce1bc657
命令报告节点在准备状态前可能需要几分钟时间。
在 workstation 上确认 Pod 现在为 Running:
1
2
| [student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
|
操作界面、表格与配置图
Running hello-1-build 1/1 11m
你该看看运行状态中的应用Pod
在集群上建立和部署该应用程序可能需要一些时间。运行以上命令直到您看到运行状态中的应用程序Pod。
当组节点登记 docker 重新启动事件时, 如果您检查应用程序构建日志, 您有时会看到失败信息 :
1
2
3
4
5
6
7
8
9
10
11
| Cloning "http://services.lab.example.com/php-helloworld" .,,
Commit: e9f757edc9ab596aea20e1f1a44df739005b1453 (Establish remote
repository)
Author: root <root@services.lab.example.com>
Date: Tue Jul 17 18:52:26 2018 -0700
---> Installing application source...
Pushing image docker-registry.default.svc:5000/common-troubleshoot/
hello:latest ......
Warning: Push failed, retrying in 5s ...
Warning: Push failed, retrying in 5s ...
Warning: Push failed, retrying in 5s ,.,
|
可以忽略此错误,前提是应用 Pod 处于 Running
state.
运行 oc describe is,验证应用已经构建,并推送到 OpenShift 内部镜像仓库:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [student@workstation ~]$ oc describe is
Name: hello
Namespace: common-troubleshoot
- Created: 13 minutes ago
Labels: app=hello
Annotations: openshift .io/generated-by=OpenShiftNewApp
Docker Pull Spec: docker-registry.default .svc:5000/common-troubleshoot/hello
Image Lookup: local=false
Unique Images: 1
Tags: al
latest
no spec tag
* docker-registry.default.svc:5000/common-troubleshoot/
hello@sha256 : 1aadOdf1a216b6b070ea3ecfd8cadfdee6dd10b45ib8e252dbc835148fc9fafo
About a minute ago
|
清理
删除 common-troubleshoot 项目。
1
| [student@workstation ~]$ oc delete project common-troubleshoot
|
操作界面、表格与配置图
1
| project "common-troubiesnoot' aeieted
|
本引导练习到此结束。
操作界面、表格与配置图
实验:执行管理命令#
在这个实验中,使用 oc 命令行工具 解决与 OpenShift 应用程序有关的问题。
Docker image URL: http://services.lab.example.com/node-hello Application URL:
http://hello.apps.lab.example.com
学习目标#
您可以解决与自定义应用程序有关的错误, 解决故障。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab execute-review setup
|
操作步骤#
1. 准备脚本为 developer 创建 execute-review 项目。在 workstation 进入 /home/student/DO280/labs/execute-review,使用 git clone 获取 http://services.lab.example.com/node-hello 中的应用。
用 Docker 构建名为 node-hello、标签为 latest 的镜像。在 workstation 列出镜像,为其添加 registry.lab.example.com/node-hello:latest 引用,然后推送到 https://registry.lab.example.com 私有仓库。
在推动镜像后, 转到学生用户的主目录。
2. 以 developer 登录 OpenShift 并列出项目,确保当前项目为 execute-review。
执行 oc new-app,使用最新的 node-hello 镜像创建应用。
execute-review 项目中的镜像。
检查并列出execute-review项目的资源。
操作界面、表格与配置图
3. 检查部署Pod的日志并确定是否为解决问题提供了有用的信息。
检查项目的事件日志并找到显示错误的条目。
4. 调查位于前一步的错误原因。 检查应用程序的部署配置, 并审查镜像设置。 应用程序使用运行在services VM 上的教室镜像仓库上的 registry.lab.example.com/node-hello 镜像。
识别Pod计划所在的节点, 并尝试手动从节点上拉取应用程序的 Docker 镜像。 如果拉取失败, 请审查两个节点上的 Docker 设置, 以识别和解决问题。
5. 修复问题后,连接 workstation,执行以下命令部署 DeploymentConfig 的新版本:
1
| [student@workstation ~]$ oc rollout latest hello
|
审查应用Pod和访问Pod日志的状况。
创建 Route 公开 hello 服务,主机名使用 hello.apps.lab.example.com。
在 workstation 使用 curl 或浏览器访问 hello.apps.lab.example.com,应显示“Hi! I am running on host -> hello-2-25bts”一类输出。
7. 评价
运行以下命令来评分您的工作 :
1
| [student@workstation ~]$ lab execute-review grade
|
如果您没有得到 PAS 等级,请审查您的工作并再次运行评分命令。
8. 清理
删除 execute-review 项目。
实验到此结束
操作界面、表格与配置图
在这个实验中, 使用 occommand linetooi 解决与 OpenShift 应用程序有关的问题并修补问题。
Docker image URL: http://services.lab.example.com/node-hello Application URL:
http://hello.apps.lab.example.com
学习目标#
能够解决与自定义应用程序有关的故障并修正错误。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do28Q-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab execute-review setup
|
操作步骤#
1. 准备脚本为 developer 创建 execute-review 项目。在 workstation 进入 /home/student/DO280/labs/execute-review,使用 git clone 获取 http://services.lab.example.com/node-hello 中的应用。
用 Docker 构建名为 node-hello、标签为 latest 的镜像。列出 workstation 上的镜像,为 node-hello 添加 registry.lab.example.com/node-hello:latest 引用,然后推送到
位于 https://registry.lab.example.com 的私有 Docker 镜像仓库。
在推动镜像后, 转到学生用户的主目录。
1.1. 从 workstation VM 中打开一个新的终端, 并转到 /home/student/DO280/labs/execute-review 目录。 运行 git 克隆命令以获取自定义的 node-hello 应用程序。
1
2
3
4
5
6
7
8
| a a a a a eee
- [student@workstation ~]$ cd /home/student/DO280/labs/execute-review
[student@workstation execute-review]$ git clone http://services.lab.example.com/
node -hello
Cloning into 'node-hello'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 0), reused © (delta @)
|
操作界面、表格与配置图
1
| Unpacking opjects: 100% (5/5), done.
|
1.2. 进入 node-hello 目录,执行 docker build,构建名为 node-hello、标签为 latest 的镜像。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| [student@workstation execute-review]$ cd node-hello
[student@workstation node-hello]$ docker build -t node-hello:latest .
Sending build context to Docker daemon 54.27 kB
Step 1/6 : FROM registry.lab.example.com/rhscl/nodejs-6-rhel7:latest
---> fba56b5381b7
Step 2/6 : MAINTAINER username "username@example.com"
---> Using cache
---> 594c1b383445
Step 3/6 : EXPOSE 3000
---> Running in abbbee82f23f
---> 9697c5a46218
Removing intermediate container abbbee82f23f
Step 4/6 : COPY . /opt/app-root/src
---> a6da56146c15
Removing intermediate container bfa6669d33bd
Step 5/6 : RUN source scl_source enable rh-nodejs6é && npm install \
--registry=http://services.lab.example.com:8081/nexus/content/groups/nodejs/
---> Running in 8403f9741cbe
... Output omitted ...
Removing intermediate container c5a774d306fe
Successfully built a9861ee36be4
|
1.3. 在 workstation VM 上列出 Docker 镜像, 以确认镜像已创建。
1
2
3
4
5
| [student@workstation node-hello]$ docker images
REPOSITORY TAG IMAGE ID es
node-hello latest a986iee36be4 ...
registry.lab.example.com/openshift/hello-openshift latest 7af3297a3fb4 ...
registry.lab.example.com/rhscl/nodejs-6-rhel7 latest fba56b5381b7 ...
|
1.4. 使用 docker tag 添加镜像引用,仓库为 registry.lab.example.com,标签为 latest;命令中使用 node-hello 的镜像 ID。
重新运行 docker 镜像命令以确保标签被正确应用。
1
2
3
4
5
6
7
8
| [student@workstation node-hello]$ docker tag a9861ee36be4 \
registry.lab.example.com/node-hello:latest
[student@workstation node-hello]$ docker images
REPOSITORY TAG IMAGE ID aoc
node-hello latest a9861ee36be4 ...
registry.lab.example.com/node-hello latest 1362bf635aa4...
registry.lab.example.com/openshift/hello-openshift latest 7af3297a3fb4 ...
registry.lab.example.com/rhscl/nodejs-6-rhel7 latest fba56b5381b7 ...
|
1.5. 将 node-hello 镜像推进到私有 Docker 镜像仓库。
1
2
| [student@workstation node-hello]$ docker push \
registry.lab. example.com/node-hello: latest
|
操作界面、表格与配置图
1
2
3
4
5
6
7
8
9
10
| me push rerers to a repository |[registry.lao.example.com/node-hello]
29edi6eb8Ge5: Pushed
82dfac496b77: Mounted from rhscl/nodejs-6-rhel7
aa29c7023a3c: Mounted from rhscl/nodejs-6-rhel7
45f0d85c3257: Mounted from rhscl/nodejs-6-rhel7
5444fe2e6b50: Mounted from rhscl/nodejs-6-rhel7
d4d408077555: Mounted from rhscl/nodejs-6-rhel7
latest: digest:
sha256:d1a9ee136434268efc3029a43fa91e576fcOe3dd89b72784bb6cf16808be7d91 size: |
1579
|
1.6. 去主目录
1
2
| [student@workstation node-hello]$ cd
[student@workstation ~]$
|
2. 以 developer 登录 OpenShift 并列出项目,确保当前项目为 execute-review。
运行 oc new-app,在 execute-review 中基于 node-hello 的 latest 镜像创建应用。
检查并列出execute-review项目的资源。
2.1. 以developer 用户身份登录到集群并列出项目。
1
2
3
4
5
6
| [student@workstation ~]$ oc login -u developer -p redhat \
https://master.lab.example.com
[student@workstation ~]$ oc projects
[student@workstation ~]$ oc project execute-review
Now using project "execute-review" on server "https://
master.lab.example.com:443".
|
2.2. 执行 oc new-app,在该项目中创建名为 hello 的新应用,
execute-review项目。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| [student@workstation ~]$ oc new-app \
registry.lab.example.com/node-hello \
~-name hello
--> Found Docker image 94ca56d (9 minutes old) from registry.lab.example.com for
"registry.lab.example.com/node-hello"
4... output omitted ...
_--> Creating resources ...
imagestream "hello" created
deploymentconfig "hello" created
service "hello" created
--> Success
Application is not exposed. You can expose services to the outside world by
executing one or more of the commands below:
'oc expose svc/hello'
|
操作界面、表格与配置图
1
| Run oc status to view your app.
|
检查并列出execute-review项目的资源。
2.3. 检查并列出execute-review项目的资源。
1
2
3
4
5
6
7
8
| [student@workstation ~]$ oc get all
NAME REVISION DESIRED CURRENT TRIGGERED BY
deploymentconfigs/hello 1 1 1 config, image(hello: latest)
NAME DOCKER REPO
imagestreams/hello docker-registry.default.svc:5000/execute-review/hello
NAME READY STATUS RESTARTS AGE
po/hello-1-deploy 1/1 Running Q 36s
po/hello-1-jq41f 0/1 Terminating 2 2m
|
2m
1
2
3
4
5
6
| Peseta S ST oiled ee a a ee Ll Ce
po/hello-1-qz7v2 0/1 ImagePullBackOff (c] 33s
NAME DESIRED CURRENT READY AGE
rc/hello-t 1 1 ic) 36s
NAME TYPE CLUSTER-IP EXTERNAL - IP PORT(S) AGE
svc/hello ClusterIP 172.30.40.167 <none> 3000/TCP, 8080/TCP 36s
|
hello-1-qz7v2 Pod 停留在 ImagePullBackOff,应用未启动。调查它为何未进入 Running,并修复问题。
3. 检查部署Pod的日志并确定是否为解决问题提供了有用的信息。
检查项目的事件日志并找到显示错误的条目。
3.1. 执行 oc logs hello-1-deploy,查看负责部署应用的 Pod 日志;此处日志没有提供有用的排错信息。
1
2
3
| [student@workstation ~]$ oc logs hello-1-qz7v2
Error from server (BadRequest): container "hello" in pod "hello-1-qz7v2" is
waiting to start: trying and failing to pull image
|
3.2. 使用对部署Pod指令的 oc describe来查看它是否给出了应用Pod失败的原因的提示。 不幸的是, 输出没有提供任何有用的信息。
1
2
3
4
5
6
7
8
9
10
| [student@workstation ~]$ oc describe pod hello-i-deploy
Name: hello-1-deploy
Namespace: execute-review
Node: node2.lab.example.com/172.25.250.12
Start Time: Thu, 26 Jul 2018 11:13:16 -90700
Labels: openshift.io/deployer -pod-for.name=hello-1
Annotations: openshift.io/deployment-config.name=hello
openshift.io/deployment .name=hello-1
openshift.io/scc=restricted
Status: Running
|
操作界面、表格与配置图
IP: 10.128.1.119
3.3. 检查项目的事件日志并确定显示错误的条目的位置。
事件日志显示, OpenShift 无法拉取应用程序部署配置中定义的镜像。 定位显示端点被屏蔽的线条。
1
2
3
4
5
6
7
| [student@workstation ~]$ oc get events \
--sort-by='.metadata.creationTimestamp'
... Output omitted ...
g Failed kubelet, node1.lab.example.com
Failed to pull image "registry.lab.example.com/node-hello@\
sha256:02071b47f0385e8e94893fcfa74943ec6ec96fce5eb53eac6691dea57ae18a98" :
rpe error: code = Unknown desc = All endpoints blocked.
|
1m 1m he1lo-1-qz7v2.154724ea481d96b3
1
2
3
4
5
| ANT ae _< PettET MEEVL ATE ETCANOLUIU
Pod spec.containers{hello} Normal
Pulling kubelet, node1.lab.example.com
pulling image "registry.lab.example.com/node-hello@\
sha256:02071b47f0385e8e94893fcfa74943ec6ec96fced5eb53eac6691dea57ae18a98"
|
1m 1m he1lo-1-qz7v2.154724ea6c66941e
1
2
3
4
5
| An AM cs MELLOWRWULEVL, LOST ECHOAVULUUISTILS
Pod Normal
SandboxChanged kubelet, node1.lab.example.com
Pod sandbox changed, it will be killed and re-created.
Ae 4m rf a.a.er 2 enee eeee ae re|a er eT
|
1m 1m he1lo-1-qz7v2.154724eaedb1ee14
1
2
3
4
5
| ee AT ad PIS RAT AOE FVEe A ECACUMNLe aT
Pod spec.containers{hello} Normal
Backoff kubelet, node1.lab.example.com
Back-off pulling image "registry.lab.example.com/node-hello@\
sha256 :02071b47f0385e8e94893fcfa74943ec6ec96fce5eb53eac6691dea57ae18a98"
|
1m 1m he11o-1-qz7v2.154724eaedb20987
1
2
3
4
| ant An UU MELLOULIVL. LOS!ZACACUULUOLS
Pod spec.containers{hello} Warning
Failed kubelet, node1.lab.example.com
Error: ImagePullBackoff
|
4. investigate 位于前一步的错误原因。 请检查应用程序的部署配置, 并审查镜像设置。 应用程序使用运行在 VM 服务上的教室注册处的 registry.lab.example.com/node-hello 镜像。
确定Pod计划所在的节点, 并尝试手动从节点上拉取应用程序的 Docker 镜像。 如果拉取失败, 请审查两个节点上的 Docker 设置, 以查明并解决问题。
4.1. 使用 oc get dc 命令获取部署配置。 定位确定 Docker 镜像位置的区段。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| [student@workstation ~]$ oc get dc hello -o yaml
... Output omitted ..
spec:
containers:
- image: registry.lab.example.com/node-
hello@sha256 : d1a9ee136434268efc3029a43fa91e576fCO0e3dd89b72784bb6cf16808be7d91
imagePullPolicy: Always
name: hello
ports:
- containerPort: 8080
protocol: TCP
- containerPort: 3000
protocol: TCP
resources: {}
|
操作界面、表格与配置图
1
2
3
| terminationMessageratn: /aev/termination-log
terminationMessagePolicy: File
... output omitted ...
|
部署配置应指向 registry.lab.example.com /node-hello 镜像,并应有一个 sha256 散列后缀。
4.2. 标明该Pod预定所在的节点, 并尝试手动将应用程序的 docker 镜像( registry.lab.example.com/node-hello) 从节点上拖动。
1
2
| [student@workstation ~]$ oc get pods -o wide
NAME READY STATUS RESTARTS AGE
|
IP
1
2
3
4
5
6
7
| [student@workstation ~]$ oc get pods -o wide
NAME READY STATUS RESTARTS AGE IP
NODE
hello-1-deploy al//al Running (0) 33s 10.128.1.139
node1.lab.example.com
hello-1-qz7v2 O/1 ImagePullBackoff 0 29s <none>
node1.lab.example.com
|
在 workstation 上打开一个新的终端, 并登录到使用 ssh 命令预定的Pod点, 然后拉取 node-hello docker 镜像。
1
2
3
4
5
| [student@workstation ~]$ ssh root@node1i
[root@node1 ~]# docker pull registry.lab.example.com/node-hello
Using default tag: latest
Trying to pull repository registry.lab.example.com/node-hello ...
All endpoints blocked.
|
您应该看到一条消息, 表示所有端点都被屏蔽。 这种类型的错误通常发生在 OpenShift 中, 原因是部署配置不正确或 Docker 设置无效。
4.3. 检查并修正两个节点的 Docker 设置,确保配置文件中的 ADD_REGISTRY 和 BLOCK_REGISTRY 正确。
在 workstation 上使用 SSH 登录 node1 和 node2:
1
2
| [student@workstation ~]$ ssh root@node1
[student@workstation ~]$ ssh root@node2
|
用文本编辑器打开 /etc/sysconfig/docker,查看文件底部的 BLOCK_REGISTRY 行。
1
2
3
4
| ... Output omitted ...
# Added by the 'lab execute-review setup' script
BLOCK_REGISTRY='--block-registry registry.access.redhat.com \
--block-registry docker.io --block-registry registry.lab.example.com'
|
课堂私有镜像仓库被误加入 BLOCK_REGISTRY,因此出现端点被阻止的错误。从 BLOCK_REGISTRY 中移除该仓库。修改后应如下所示:
1
| # Added by the 'lab execute-review setup' script
|
操作界面、表格与配置图
1
2
| BLUCK_REGISIRY= --blocKk-registry registry.access.rednat.com \
--block-registry docker.io'
|
重复步骤并纠正其他节点 VM 上的 Docker 配置。
4.4. 重新启动两个节点上的 docker 服务。
1
2
| [root@node1 ~]# systemctl restart docker.service
[root@node2 ~]# systemctl restart docker.service
|
5. 修复问题后,连接 workstation,执行以下命令部署 DeploymentConfig 的新版本:
1
| [student@workstation ~]$ oc rollout latest hello
|
审查应用Pod和访问Pod日志的状况。
5.1. 运行 oc rollout latest hello,部署应用的新版本。
1
2
| [student@workstation ~]$ oc rollout latest hello
deploymentconfig "hello" rolled out
|
5.2. 运行 oc get pods 命令以获取运行 pod 列表。 应该有一个运行状态的 pod
1
2
3
4
5
6
| [student@workstation ~]$ oc get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
hello-1-deploy 0/1 Error (c} 1im 10.128.1.139
node2.lab.example.com
hello-2-xk4nn a/1 Running 0 2m 10.128.1.146
node2.lab.example.com
|
2m 10.128.1.146
1
2
| PEN NT NN ETT Afa
node2.lab.example.com
|
5.3. 使用 oc logs 确认 Pod 正常运行。
1
2
| [student@workstation ~]$ oc logs hello-2-xk4nn
nodejs server running on http://0.0.0.0:3000
|
6. 创建 Route 公开 hello 服务,路由主机名为 hello.apps.lab.example.com。
在 workstation 使用 curl 或浏览器访问 hello.apps.lab.example.com,应显示“Hi! I am running on host -> hello-2-25bts”一类输出。
6.1. 创建 Route 公开 hello 服务,主机名使用 hello.apps.lab.example.com。
1
2
| [student@workstation ~]$ oc expose svc/hello \
--hostname=hello.apps.lab.example.com
|
操作界面、表格与配置图
1
| route node-nelio" exposea
|
6.2. 在 workstation 用 curl 或浏览器访问 hello.apps.lab.example.com,应显示应用所在 Pod 的主机名。
1
2
| [student@workstation ~]$ curl http://hello.apps.lab.example.com
Hi! I am running on host -> hello-2-25bts
|
7. 评价
运行以下命令来给您的工作评分 :
1
| [student@workstation ~]$ lab execute-review grade
|
如果您没有得到 PAS 等级,请审查您的工作并再次运行评分命令。
8. 清理
删除 execute-review 项目。
1
2
| [student@workstation ~]$ oc delete project execute-review
project "execute-review" deleted
|
实验到此结束
操作界面、表格与配置图
自测问题#
C5 控制资源访问#
5.1 认证、授权与准入#
身份提供方确认登录用户;RoleBinding 决定其对资源的操作;SCC 等准入控制决定 Pod 请求能否被接受。能登录、能创建 DC、DC 能创建 Pod 是三个不同的条件。
view 提供项目资源只读能力,但不等价于读取所有敏感数据;edit 允许应用管理,并能接触较多项目内容;admin 可管理项目授权。cluster-admin 是集群权限,不应用于日常应用用户。
ServiceAccount(SA)身份格式为 system:serviceaccount:项目:名称。未指定时 Pod 使用项目的 default SA。给登录用户授权不会自动改变 Pod SA 的权限。
5.2 Secret 与配置#
Secret 的 base64 是编码,不是加密。能读取 Secret 的主体可以恢复其值。环境变量注入的值在容器启动时确定;更新 Secret 后需要重新部署。卷挂载的内容更新与应用重新读取是两个独立过程。
修改 MySQL Secret 不会自动修改已经初始化的数据库账户密码;必须同时执行数据库账户变更,再更新 Secret 并重新部署依赖应用。
5.3 SCC#
默认 restricted 约束通常为容器分配项目范围 UID。镜像需要支持任意 UID 和可写目录权限。anyuid 允许镜像指定 UID,但不等于 privileged。确需 root 的实验只给专用 SA 绑定 anyuid,不修改 default SA 或全局 restricted。
实验操作#
引导练习:管理项目与账户#
在本练习中,禁止普通用户自行创建项目、隔离项目访问,并为特定项目启用服务账户。
文件:/home/student/DO280/labs/secure-resources Application URL:
http://nginx-project-
user1.apps.lab.example.com
学习目标#
能够禁止用户创建项目、授予个人项目访问权限,以及创建服务账户来调整特定项目的安全约束。
在你开始之前
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab secure-resources setup
|
操作步骤#
1. 创建本实验用户:user1 为项目管理员,user2 为项目开发者,密码都为 redhat。
教室设置使用 htpasswd 工具在 master VM 上创建用户。
1.1. 使用SSH从workstationVM进入masterVM。
打开 workstation VM 上的终端并运行以下命令:
1
2
| [student@workstation ~]$ ssh root@master
[root@master ~]#
|
1.2. 创建项目管理员 user1。
在现有的终端窗口中,运行以下命令:
操作界面、表格与配置图
1.3. 创建developer 用户 user2
在终端窗口中,运行以下命令:
1
| [root@master ~]# htpasswd -b /etc/origin/master/htpasswd user2 redhat
|
1.4. 从 master VM 中登录出。
在终端窗口中,运行以下命令:
禁用所有常规用户的项目创建能力。
2.1. 在 workstation 上以 admin 登录。
从终端窗口运行以下命令 :
1
2
| [student@workstation ~]$ oc login -u admin -p redhat \
https://master.lab.example.com
|
承认你接受不安全的连接
2.2. 禁止所有普通用户创建项目。
此步骤的命令可以从 the/home/student/DO280/labs/secure-resources 文件夹中的 configure-policy.sh 脚本运行或复制。
从终端窗口运行以下命令 :
1
2
3
| [student@workstation ~]$ oc adm policy remove-cluster-role-from-group \
self-provisioner system: authenticated: oauth
cluster role "self-provisioner" removed: "system:authenticated:oauth"
|
3. 验证常规用户无法在 OpenShift 中创建项目。
3.1. 登录到OpenShift作为user1。
从终端窗口运行以下命令 :
1
2
3
4
5
| [student@workstation ~]$ oc login -u user1 -p redhat \
https://master.lab.example.com
Login successful.
You don't have any projects. Contact your system administrator to request a |
project.
|
3.2. 尝试创建一个新项目。
运行以下命令 :
1
| [student@workstation ~]$ oc new-project test
|
操作界面、表格与配置图
1
| Error Trom server (Forbiaqgen): You may not request a new project via this Arl. |
|
安全策略修改后,普通用户不能创建新项目,这项任务由 OpenShift 集群管理员承担。
4. 创建两个项目作为集群管理器。
4.1. 登录到 OpenShift 作为admin 用户。
从终端窗口运行以下命令 :
1
2
| [student@workstation ~]$ oc login -u admin -p redhat \
https://master.lab.example.com
|
4.2. 创建一个新项目。
运行以下命令 :
1
| [student@workstation ~]$ oc new-project project-user1
|
因为您是集群管理者,所以创建了一个新项目。
4.3. 创建另一个项目。
运行以下命令 :
1
| [student@workstation ~]$ oc new-project project-user2
|
5. user1与project-user1和user2与project-user1和project-user2的联系。
5.1. 添加 user1 作为 project-user1 项目的管理者。
从终端窗口运行以下命令 :
1
2
3
4
5
| [student@workstation ~]$ oc project project-user1
Now using project "project-useri1" on server "https://
master.lab.example.com: 443".
[student@workstation ~]$ oc policy add-role-to-user admin user1
role "admin" added: ""user1"
|
5.2. 添加user2作为project-user1项目的开发者。
从终端窗口运行以下命令 :
1
2
| [student@workstation ~]$ oc policy add-role-to-user edit user2
role "edit" added: ""user2"
|
5.3. 添加user2作为project-user2项目的开发者。
从终端窗口运行以下命令 :
1
| [student@workstation ~]$ oc project project-user2
|
操作界面、表格与配置图
1
2
3
4
| NOW USing project "project-user2 on server "nttps://
master.lab.example.com: 443".
[student@workstation ~]$ oc policy add-role-to-user edit user2
role "edit" added: "user2"
|
6. 测试各用户对项目的访问权限。
6.1. 验证 user1 只能访问 project-user1 项目。
登录到 OpenShift 时为 user1。
从终端窗口运行以下命令 :
1
2
| [student@workstation ~]$ oc login -u user1 -p redhat \
https://master.lab.example.com
|
6.2. 输入 project-user1 项目。
从终端窗口运行以下命令。 它应该工作 :
1
| [student@workstation ~]$ oc project project-user1i
|
6.3. 输入 project-user2 项目。
从终端窗口运行以下命令。 它应该失败 :
1
2
3
4
| [student@workstation ~]$ oc project project-user2
error: You are not a member of project "project-user2".
You have one project on this server: project-user1
To see projects on another server, pass '--server=<server>'.
|
6.4. 核实user2 能够访问project-user1和project-user2项目。
登录到 OpenShift 时为 user2。
从终端窗口运行以下命令 :
1
2
3
4
5
6
7
| [student@workstation ~]$ oc login -u user2 -p redhat
Login successful.
You have access to the following projects and can switch between them with 'oc
project <projectname>':
* project-user1
project-user2
Using project "project-user1".
|
6.5. 输入 project-user1 项目。
从终端窗口运行以下命令。 它应该工作 :
1
| [student@workstation ~]$ oc project project-user1
|
操作界面、表格与配置图
1
2
| Already on project "project-userl on server "nttps://
master.lab.example.com:443".
|
6.6. 输入 project-user2 项目
在终端执行以下命令,预期成功:
1
2
3
| [student@workstation ~]$ oc project project-user2
Now using project "project-user2" on server "https://
master.lab.example.com:443".
|
7. 以开发者身份在 project-user1 中部署一个需要更高权限的应用。
7.1. 登录为 user2 并输入 project-user1 项目:
1
2
| [student@workstation ~]$ oc login -u user2 -p redhat
[student@workstation ~]$ oc project project-user1
|
7.2. 运行以下命令 :
1
2
3
4
5
6
7
| [student@workstation ~]$ oc new-app \
--name=nginx \
--docker-image=registry.lab.example.com/nginx: latest
... output omitted ...
* WARNING: Image "registry.lab.example.com/nginx:latest" runs as the 'root'
user which may not be permitted by your cluster administrator
... output omitted ...
|
命令提醒注意 nginx 容器镜像使用 root 用户。 默认情况下, OpenShift 将容器作为随机操作系统用户运行。
请注意,user2作为该项目的开发者,可创造部署配置和服务等资源。
7.3. 核查部署情况
从命令行执行以下命令:
1
| [student@workstation ~]$ oc get pods
|
如前所述,镜像要求 root 用户,因此部署失败。等待 Pod 进入 CrashLoopBackOff 或 Error 状态:
1
2
| NAME READY STATUS RESTARTS AGE
nginx-2-fd68t 0/1 Error 0 im
|
1m
操作界面、表格与配置图
减少对具体项目的安全限制。
要使用特许通道管理容器,应创建一个服务账户,让Pod号使用任何操作系统用户运行。
其中一些行动需要由拥有项目管理员特权的用户进行,有些行动需要由拥有集群管理员特权的用户进行。
此步骤中的所有命令都可以从 /home/student/DO280/labs/secure-resources 文件夹中的 configure-sc.sh 脚本运行或复制。
8.1. 以 user1 用户身份登录并选择 project-user1 项目。
从终端窗口运行以下命令 :
1
2
3
4
| [student@workstation ~]$ oc login -u user1 -p redhat
Login successful.
You have one project on this server: "project-user1i"
Using project "project-user1".
|
8.2. 创建服务账户。
此项行动需要一个项目管理员用户。
从现有终端窗口运行以下命令 :
1
2
| [student@workstation ~]$ oc create serviceaccount useroot
serviceaccount "useroot" created
|
8.3. 以admin 用户身份登录并选择 project-user1 项目。
从终端窗口运行以下命令 :
1
2
| [student@workstation ~]$ oc login -u admin -p redhat
[student@workstation ~]$ oc project project-user1
|
8.4. 将新服务账户关联到 anyuid 安全上下文约束。
此项行动需要一个集群管理员用户。
运行以下命令 :
1
2
| [student@workstation ~]$ oc adm policy add-scc-to-user anyuid -z useroot
sce "anyuid" added to: ["system:serviceaccount:project-~user1:useroot" |
|
8.5. 以 user2 用户身份登录并选择 project-user1 项目。
从终端窗口运行以下命令 :
1
| [student@workstation ~]$ oc login -u user2 -p redhat
|
操作界面、表格与配置图
1
| Lstudent@workstation ~]% oc project project-user1
|
8.6. 更新负责管理Nginx部署的部署配置资源。
此项操作可由任何developer 用户进行。
运行以下命令。 它可以从 /home/student/DO280/labs/secure-resources 文件夹中的 configure-sc.sh 脚本复制。
1
2
3
| [student@workstation ~]$ oc patch dc/nginx --patch \
'{"spec":{"template":{"spec":{"serviceAccountName": ""useroot"}}}}'
deploymentconfig "nginx" patched
|
在部署配置资源补齐后,部署将成功。
也可以用 oc edit 修改 DeploymentConfig 的 serviceAccountName。oc patch 便于脚本化,oc edit 更适合交互修改。
8.7. 校验使用更新部署配置创建的新Pod。
等到运行中的新Pod状态 :
1
2
3
| [student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
nginx-2-fd68t 1/1 Running 0} 8m
|
8m
9. 测试容器
9.1. 展示让外部访问 nginx Pod的服务。
运行以下命令 :
1
2
| [student@workstation ~]$ oc expose svc nginx
route "nginx" exposed
|
9.2. 连接到 workstation VM
在终端窗口执行以下命令:
1
2
3
4
5
6
7
8
| [student@workstation ~]$ curl -s \
http://nginx-project-user1.apps.lab.example.com
<!DOCTYPE html>
<html>
<head>
<title>welcome to nginx!</title>
<style>
body {
|
操作界面、表格与配置图
1
2
3
4
5
6
7
8
9
10
| widtn: 35em;
margin: © auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<hi>welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
|
预计输出为NGinx欢迎页面。
10. 清理干净
10.1. 重新允许所有普通用户创建项目。
此步骤的命令可以从 the/home/student/DO280/labs/secure-resources 文件夹中的 restore-policy.sh 脚本运行或复制。
从终端窗口运行以下命令 :
1
2
3
4
5
| [student@workstation ~]$ oc login -u admin -p redhat
[student@workstation ~]$ oc adm policy add-cluster-role-to-group \
self-provisioner system:authenticated system: authenticated:oauth
cluster role "self-provisioner" added: ["system:authenticated"
"system: authenticated:oauth"]
|
10.2. 删除项目。
运行以下命令 :
1
2
| [student@workstation ~]$ oc delete project project-user1
[student@workstation ~]$ oc delete project project-user2
|
10.3. 删除用户。
运行以下命令 :
1
2
3
4
| [student@workstation ~]$ ssh root@master
[root@master ~]$ htpasswd -D /etc/origin/master/htpasswd user1
[root@master ~]$ htpasswd -D /etc/origin/master/htpasswd user2
[root@master ~]$ exit
|
本引导练习到此结束。
操作界面、表格与配置图
引导练习:保护数据库密码#
在本练习中,使用 Secret 对访问数据库容器的凭据进行“加密”。
文件:/home/student/DO280/labs/secure-secrets/ Application URL:
NA
学习目标#
能够创建使用 OpenShift Secret 资源保存数据库认证凭据的 MySQL 容器。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab secure-secrets setup
|
操作步骤#
1. 创建一个新项目。
1.1. 在 workstation 上使用 OpenShift 客户端连接 https://master.lab.example.com。
以 developer 用户登录,并确认接受课堂的不安全连接提示:
1
2
| [student@workstation ~]$ oc login -u developer -p redhat \
https://master .lab.example.com
|
1.2. 创建 secure-secrets 项目 :
1
| [student@workstation ~]$ oc new-project secure-secrets
|
1.3. 在此练习实验文件夹中检查 mysql-ephemeral 模板的副本。
从终端窗口运行以下命令 :
1
| [student@workstation ~]$ cd ~/DO280/labs/secure-secrets
|
操作界面、表格与配置图
1
| LStuagent@workstation secure-secrets|s less mysql-ephemeral.yml
|
查看 mysql-ephemeral.yml,它来自 openshift 项目的 mysql-ephemeral 模板,但移除了 Secret 定义。本练习将手动创建模板需要的 Secret。
1.4. 模板中的 MySQL Pod 定义从 Secret 读取值,初始化环境变量:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| spec:
containers:
- capabilities: {}
env:
- name: MYSQL_USER
valueFrom:
secretKeyRef:
key: database-user
name: ${DATABASE_SERVICE_NAME}
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
key: database-password
name: ${DATABASE_SERVICE_NAME}
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
key: database-root-password
name: ${DATABASE_SERVICE_NAME}
|
1.5. 该Pod所需的其他环境变量是从模板参数初始化的,并具有默认值。
注意文件末尾附近 DATABASE_SERVICE_NAME 参数的默认值 mysql;上一步用该参数确定初始化环境变量时引用的服务名称。
1
2
3
4
5
| - description: The name of the OpenShift Service exposed for the database.
displayName: Database Service Name
name: DATABASE_SERVICE_NAME
required: true
value: mysql
|
操作界面、表格与配置图
2. 按模板要求创建名为 mysql 的 Secret,保存 MySQL 镜像所需的认证凭据。
数据库应用用户名称由 database-user 键定义。
数据库用户密码由 database-password 键定义。
数据库管理员密码由 database-root-password 键定义。
2.1. 创建 Secret。
从终端窗口运行以下命令 :
1
2
3
4
5
| [student@workstation secure-secrets]$ oc create secret generic mysql \
--from-literal='database-user'='mysql' \
--from-literal='database-password'='redhat' \
--from-literal='database-root -password'='do280-admin'
secret "mysql" created
|
2.2. 查看新建 Secret,确认数据库用户和管理员密码不以明文形式显示。
从终端窗口运行以下命令 :
1
2
3
4
5
6
7
| [student@workstation secure-secrets]$ oc get secret mysql -o yaml
apiVersion: v1
data:
database-password: cmVkaGF0
database-root-password: ZG8yODAtYWRtaW4=
database-user: bXlzcWw=
kind: Secret
|
3. 使用YAML 文件中存储的模板创建 MySQL 数据库容器。
从终端窗口运行以下命令 :
4. 等待 MySQL Pod已准备好并运行。
从终端窗口运行以下命令 :
1
2
3
| [student@workstation secure-secrets]$ oc get pods
NAME READY STATUS RESTARTS AGE
mysql-1-clizq 1/1 Running 0} im
|
1m
5. 使用上一步的 Pod 名称,创建通往 MySQL Pod 的端口转发隧道。
从终点窗口运行以下命令 :
1
2
| [student@workstation secure-secrets]$ cd ~
[student@workstation ~]$ oc port-forward mysql-1-clizq 3306: 3306
|
操作界面、表格与配置图
Forwardingfrom127.0.@.1:3306->3306
不要杀死 oc port-forward 命令。 保持运行, 因为下一步需要这样做。
6. 连接数据库,使用 Secret 对象中定义的凭据验证访问。
打开另一个终端窗口运行以下命令:
1
2
3
4
5
6
7
8
9
10
11
| [student@workstation ~]$ mysql -uroot -pdo280-admin -h127.0.0.1
MySQL [(none)]> show databases;
+---------55---------4+
Database
+--------------------+
information_schema |
mysql
performance_schema |
sampledb
sys
(pega695655008S5 555505 55
|
MySQL 容器使用 Secret 提供的凭据验证 mysql 客户端的登录信息。因为创建并启动容器时传入了 Secret,使用 DO280-admin 密码可以访问 root 账户。Secret 中的值采用 base64 编码;编码不等于加密。
7. 退出 MySQL 客户端并终止隧道。
在运行 MySQL 客户端的终端窗口上,运行以下命令:
切换到运行 oc port-forward 的终端,按 Ctrl+C 结束端口转发。
8. 清理。
运行以下命令。
1
| [student@workstation ~]$ oc delete project secure-secrets
|
本引导练习到此结束。
操作界面、表格与配置图
实验:控制 OpenShift 资源访问#
在本实验中,授权用户访问项目,并使用 Secret 部署 MySQL 数据库容器。
文件:/home/student/DO280/labs/secure-review
应用地址:http://phpmyadmin.apps.lab.example.com
学习目标#
能够创建一个配置 MySQL 数据库容器的项目,该容器使用Secret对象类型存储数据库认证凭据。您还可以让用户创建和访问项目。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab secure-review setup
|
操作步骤#
1. 创建一个用户账户,其详细内容如下:
:login:user-review
密码: redhat
回顾以前的实验,教室是用 OpenShift HTPasswd 身份提供者模块配置的。
1.1. 使用SSH从workstationVM进入masterVM。
1.2. 在 OpenShift 中创建 user-review 用户。
1.3. 退出 master 虚拟机。
2. 禁用所有常规用户的项目创建能力。
2.1. 在 workstation 上以 admin 用户登录 OpenShift,密码为 redhat。
操作界面、表格与配置图
2.2. 禁止所有普通用户创建项目,自助创建项目所用的集群角色为 self-provisioner。
3. 验证常规用户无法在 OpenShift 中创建项目。
3.1. 作为 user-review 用户登录到 OpenShift。
3.2. 尝试创建一个新项目。 它应该失败。
4. 创建一个名为 secure-review 的项目。
4.1. 登录到 OpenShift 作为admin 用户。
4.2. 创建一个名为 secure-review 的新项目。
5. 将user-review用户与secure-review项目联系。
5.1. 添加 user-review 用户作为 secure-review 项目的developer 用户。
5.2. 测试访问权限
6. 课堂提供了部署 phpmyadmin 所用数据库的模板。查看 mysql-ephemeral.yml,确定需要创建的 Secret 名称及其键。
7. 以 user-review 开发者身份创建名为 mysql 的 Secret,保存用户名 mysql、密码 redhat,以及数据库管理员密码 do280-admin。
数据库用户名由 database-user 密钥定义。此用户的密码由 database-password 密钥定义。
数据库管理员密码由 database-root-password 密钥定义。
7.1. 创造Secret
7.2. 验证密钥已创建。
8. 使用模板创建 MySQL 数据库容器。
8.1. 使用 YAML 文件中的模板部署 MySQL 数据库服务器。
8.2. 等待 MySQL 服务器 Pod 就绪并运行。
9. 使用 Mysql 数据库用户对数据库服务器进行测试访问。
9.1. 创建一个 port-forwarding 隧道以访问数据库。
9.2. 使用 Mysql 命令作为 Mysql 用户使用 redhat 密码访问容器并列出数据库。
9.3. 使用 exit 退出 MySQL 客户端,再用 Ctrl+C 结束端口转发隧道。
操作界面、表格与配置图
10. 部署phpmyadmin:4.7容器。该容器存放在registry.lab.example.com镜像仓库,该镜像仓库是一个安全的镜像仓库。
phpmyadmin:4.7 通过 PMA_HOST 环境变量获取 MySQL 地址。使用模板所建 MySQL 服务的 FQDN:mysql.secure-review.svc.cluster.local。
10.1. 从容器镜像中部署phpmyadmin应用。
10.2. 确认部署因 OpenShift 默认安全策略而失败。
11. 减少项目的安全限制。
使容器能够使用根权限运行, 创建一个带有根支持的服务账户。
11.1. 登录为admin 用户, 即管理组管理员。
11.2. 创建一个名为 phpmyadmin-account 的服务账户。
11.3. 将新服务账户关联到 anyuid 安全上下文约束。
11.4. 更新负责管理 phpmyadmin 部署的部署配置资源, 以便使用新建的服务账户。 您可以使用 oc patch或 oc edit命令。
您可以在 /home/student/DO280/labs/secure-review 文件夹中从 patch-dc.sh 脚本复制 oc patch命令。
11.5. 返回为此项目创建的developer 用户。
11.6. 等待新的phpmyadmin pod准备好并运行。
12. 使用 Web 浏览器测试应用程序。
12.1. 为phpmyadmin 服务创建一个路由。 使用以下主机名: phpmyadmin.apps.lab.example.com。
12.2. 用浏览器打开 phpmyadmin,用户名为 mysql,密码为 redhat。
来自phpmyadmin的设置页面是预期输出。
13. 运行评分脚本以核实所有任务已经完成。
运行 lab secure-review grade,确认所有任务已完成。
1
| [student@workstation ~]$ lab secure-review grade
|
操作界面、表格与配置图
14. 清理干净
14.1. 恢复所有普通用户创建项目的角色。
14.2. 删除项目。
14.3. 从 HTPasswd 文件移除 user-review,并用 oc delete user 从 OpenShift 删除该用户。
实验到此结束
操作界面、表格与配置图
在本实验中,授权用户访问项目,并使用 Secret 部署 MySQL 数据库容器。
文件:/home/student/DO280/labs/secure-review
应用地址:http://phpmyadmin.apps.lab.example.com
学习目标#
能够创建一个配置 MySQL 数据库容器的项目,该容器使用Secret对象类型存储数据库认证凭据。您还可以让用户创建和访问项目。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab secure-review setup
|
操作步骤#
1. 创建一个用户账户,其详细内容如下:
登录名:user-review
.password:redhat
回顾以前的实验,教室是用 OpenShift HTPasswd 身份提供者模块配置的。
1.1. 使用 SSH从 workstation VM 访问 master VM。
在 workstation 上打开终端,执行以下命令:
1
2
| [student@workstation ~]$ ssh root@master
[root@master ~]#
|
1.2. 在 OpenShift 中创建 user-review 用户。
在现有的终端窗口中,运行以下命令:
1
| [root@master ~]# htpasswd /etc/origin/master/htpasswd user-review
|
操作界面、表格与配置图
当提示时, 请输入 redhat 作为密码。
1.3. 退出 master 虚拟机。
在终端窗口中,运行以下命令:
2. 禁用所有常规用户的项目创建能力。
2.1. 在 workstation 上以 admin 用户登录 OpenShift,密码为 redhat。
从终端窗口运行以下命令 :
1
2
| [student@workstation ~]$ oc login -u admin -p redhat \
https://master .lab.example.com
|
如果受到干扰,请接受不安全的连接。
2.2. 禁止所有普通用户创建项目,自助创建项目所用的集群角色为 self-provisioner。
从终端窗口运行以下命令 :
1
2
| [student@workstation ~]$ oc adm policy remove-cluster-role-from-group \
self-provisioner system:authenticated system: authenticated: oauth
|
3. 验证常规用户无法在 OpenShift 中创建项目。
3.1. 作为 user-review 用户登录到 OpenShift。
从终端窗口运行以下命令 :
1
| [student@workstation ~]$ oc login -u user-review -p redhat
|
3.2. 尝试创建一个新项目。 它应该失败。
运行以下命令 :
1
2
| [student@workstation ~]$ oc new-project test
Error from server (Forbidden): You may not request a new project via this API.
|
由于安全策略已更改,该用户不能创建新项目。此任务
委托给 OpenShift 集群管理器。
操作界面、表格与配置图
4. 创建一个名为 secure-review 的项目。
4.1. 登录到 OpenShift 作为admin 用户。
从终端窗口运行以下命令 :
1
| [student@workstation ~]$ oc login -u admin -p redhat
|
4.2. 创建一个名为 secure-review 的新项目。
运行以下命令 :
1
| [student@workstation ~]$ oc new-project secure-review
|
5. 将user-review用户与secure-review项目联系。
5.1. 将 user-review 用户添加为 secure-review 项目的开发者。
从终端窗口运行以下命令 :
1
2
3
| [student@workstation ~]$ oc project secure-review
[student@workstation ~]$ oc policy add-role-to-user edit user-review
role "edit" added: "user-review"
|
5.2. 测试访问权限
登录为 user-review 用户, 并验证该用户是否可访问 secure-review 项目。
以 user-review 用户身份登录到 OpenShift。
从终端窗口运行以下命令 :
1
2
| [student@workstation ~]$ oc login -u user-review -p redhat
[student@workstation ~]$ oc project secure-review
|
6. 查看 mysql-ephemeral.yml 中的数据库模板,确定 phpmyadmin 所需 Secret 的名称和键。
从终端窗口运行以下命令 :
1
2
| : [student@workstation ~]$ cd ~/DO280/labs/secure-review/
[student@workstation secure-review]$ less mysql-ephemeral.yml
|
注意从DATABASE_SERVICE_NAME 参数中命名的Secret初始化了几个环境变量。参数的默认值是 Mysql。
1
2
3
| spec:
containers:
- Capabilities: {}
|
操作界面、表格与配置图
信封 :
1
2
3
4
5
6
7
8
9
10
11
| env:
- name: MYSQL_USER
valueFrom:
secretKeyRef:
key: database-user
name: ${DATABASE_SERVICE_NAME}
- description: The name of the OpenShift Service exposed for the database.
displayName: Database Service Name
name: DATABASE_SERVICE_NAME
required: true
value: mysql
|
初始化环境变量需要 Secret 中的 database-password、database-root-password 和 database-user 键。
7. 以 user-review 创建名为 mysql 的 Secret,保存用户名 mysql、密码 redhat,以及数据库管理员密码 DO280-admin。
数据库用户名由 database-user 密钥定义。此用户的密码由 database-password 密钥定义。
数据库管理员密码由 database-root-password 键定义。
7.1. 创造Secret
从终端窗口运行以下命令 :
1
2
3
4
5
| [student@workstation secure-review]$ oc create secret generic mysql \
--from-literal='database-user'='mysql' \
--from-literal='database-password'='redhat' \
--from-literal='database-root -password'='do280-admin'
secret "mysql" created
|
7.2. 验证密钥已创建。
从终端窗口运行以下命令 :
1
| [student@workstation secure-review]$ oc get secret mysql -o yaml
|
8. 使用模板创建 MySQL 数据库容器。
8.1. 使用 YAML 文件中的模板部署 MySQL 数据库服务器。
从终端窗口运行以下命令 :
1
| [student@workstation ~]$ oc new-app --file=mysql-ephemeral.yml
|
8.2. 等待 MySQL 服务器 Pod 就绪并运行。
从终端窗口运行以下命令 :
1
| [student@workstation secure-secret]$ oc get pods
|
操作界面、表格与配置图
1
2
| NAME READY STATUS RESTARTS AGE
mysql-1-s6gxf 1/1 Running (o] 1m
|
1m
9. 使用 mysql 数据库用户测试访问。
9.1. 创建一个 port-forwarding 隧道以访问数据库。
从终端窗口运行以下命令 :
1
2
3
| [student@workstation secure-secret]$ cd ~
[student@workstation ~]$ oc port-forward mysql-1-s6gxf 3306:3306
Forwarding from 127.0.0.1:3306 -> 3306
|
9.2. 使用 Mysql 命令作为 Mysql 用户使用 redhat 密码访问容器并列出数据库。
打开另一个命令行窗口执行以下命令,该命令连接到数据库并列出数据库:
1
2
3
4
5
6
7
8
| [student@workstation ~]$ mysql -umysql -predhat -h127.0.0.1
MySQL [(none)]> show databases;
+-------------ee--- +
{| Database
+-----------------t+
information_schema |
sampledb
+--------------------4+
|
9.3. 使用 exit 退出 MySQL 客户端,再用 Ctrl+C 结束端口转发隧道。
10. 部署phpmyadmin:4.7容器。该容器可在registry.lab.example.com登记簿上查阅,该登记簿是安全的登记簿。
phpmyadmin:4.7 使用 PMA_HOST 环境变量指定 MySQL 服务器地址。本例使用 mysql.secure-review.svc.cluster.local 服务域名。
10.1. 从容器镜像中部署phpmyadmin应用。
运行以下命令 :
1
2
| n ~]$ oc new-app --name=phpmyadmin \
stry.lab.example.com/phpmyadmin/phpmyadmin:4.7 \
|
操作界面、表格与配置图
1
| -e€@ PMA_HOST=mysqli.secure-review.svc.ciuster.local
|
命令提醒它需要 root 权限。 默认情况下, OpenShift 不支持将容器运行为 root 操作系统用户。
10.2. 确认部署因 OpenShift 默认安全策略而失败。
从命令行执行以下命令:
1
| [student@workstation ~]$ oc get pods
|
没有所需 root 权限时,部署失败。预期看到 phpmyadmin 的状态为 Error 或 CrashLoopBackOff:
1
2
3
| NAME READY STATUS RESTARTS AGE
mysql-1-s6gxf a/1 Running (c) 23m
phpmyadmin-1-ttgp5 0/1 CrashLoopBackOff 5 5m
|
5m
11. 减少项目的安全限制。
使容器能够使用根权限运行, 创建一个带有根支持的服务账户。
11.1. 登录为admin 用户, 即管理组管理员。
在终端窗口执行以下命令:
11.2. 创建一个名为 phpmyadmin-account 的服务账户。
从现有终端窗口运行以下命令 :
1
2
| [student@workstation ~]$ oc create serviceaccount phpmyadmin-account
serviceaccount "phpmyadmin-account" created
|
11.3. 将新服务账户关联到 anyuid 安全上下文约束。
运行以下命令 :
1
2
| [student@workstation ~]$ oc adm policy add-scc-to-user anyuid \
-Z phpmyadmin-account
|
操作界面、表格与配置图
1
2
| scc "anyulag"= aaaed to: | system: serviceaccount: secure-review: pnpmyadmin -
account"]
|
11.4. 更新负责管理 phpmyadmin 部署的部署配置资源, 以便使用新建的服务账户。 您可以使用 oc patch或 oc edit命令。
您可以在 /home/student/DO280/labs/secure-review 文件夹中从 patch-dc.sh 脚本复制 oc patch命令。
运行以下命令 :
1
2
3
| [student@workstation ~]$ oc patch dc/phpmyadmin --patch \
'{"spec":{"template":{"spec":{"serviceAccountName": "phpmyadmin-account"}}}}'
deploymentconfig "phpmyadmin" patched
|
更改后, 新的部署自动执行。
11.5. 返回为此项目创建的developer 用户。
从终端窗口运行以下命令 :
1
| [student@workstation ~]$ oc login -u user-review -p redhat
|
11.6. 等待新的phpmyadmin pod准备好并运行。
1
2
3
4
| [student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
mysql-1-s6gxf e/a Running 90 25m
phpmyadmin -2-g4wb4 /al Running ic) 46s
|
12. 使用 Web 浏览器测试应用程序。
12.1. 为phpmyadmin 服务创建一个路由。 使用以下主机名: phpmyadmin.apps.lab.example.com。
从终端窗口运行以下命令 :
1
2
3
| [student@workstation ~]$ oc expose svc/phpmyadmin \
--hostname=phpmyadmin. apps.lab.example.com
route "phpmyadmin" exposed
|
12.2. 用浏览器打开 phpmyadmin,用户名为 mysql,密码为 redhat。
在 workstation VM, 打开网络浏览器并浏览到 URL :
http://phpmyadmin.apps.lab.example.com
来自phpmyadmin的设置页面是预期输出。
操作界面、表格与配置图
13. 运行评分脚本,确认所有任务已完成。
运行 lab secure-review grade,验证所有任务已完成。
1
| [student@workstation ~]$ lab secure-review grade
|
14. 清理干净
14.1. 恢复所有普通用户创建项目的角色。
从终端窗口运行以下命令 :
1
2
3
4
| [student@workstation ~]$ oc login -u admin -p redhat
[student@workstation ~]$ oc adm policy add-cluster-role-to-group \
self-provisioner system:authenticated system:authenticated:oauth
_- a ee
|
14.2. 删除项目。
运行以下命令 :
1
| [student@workstation ~]$ oc delete project secure-review
|
14.3. 从 HTPasswd 文件移除 user-review,并用 oc delete user 从 OpenShift 删除该用户。
运行以下命令 :
1
2
3
| [student@workstation ~]$ ssh root@master htpasswd -D \
/etc/origin/master/htpasswd user-review
[student@workstation ~]$ oc delete user user-review
|
实验到此结束
操作界面、表格与配置图
自测问题#
C6 分配持久存储#
6.1 PV、PVC 与后端#
PersistentVolume(PV)是集群存储资源;PersistentVolumeClaim(PVC)是项目申请。容量、访问模式、StorageClass、selector 和显式卷名共同影响绑定。
| 访问模式 | 含义 |
|---|
| ReadWriteOnce / RWO | 可由单个节点读写;不等于只允许一个 Pod |
| ReadOnlyMany / ROX | 可由多个节点只读挂载 |
| ReadWriteMany / RWX | 可由多个节点读写挂载 |
存储提供 RWX 不意味着数据库支持多个实例同时写同一个数据目录。
6.2 回收策略#
Retain 保留后端数据。删除 PVC 后 PV 通常进入 Released,旧 claimRef 不会自动清除。重新绑定前确认旧写入者退出、数据归属和清理要求。
Delete 由支持该能力的存储插件删除后端卷;手工 NFS PV 不能假定具有动态删除能力。Recycle 是旧式回收方式,不适合作为可靠的数据销毁策略。
6.3 文件访问与持久性#
PVC Bound 只证明绑定。实际写入还取决于 NFS 导出、节点可达性、目录属组、supplementalGroups、SELinux。root_squash 将远端 root 映射为匿名身份,不应靠关闭它掩盖目录权限问题。
持久性验证:写入确定内容 → 记录 PVC/PV → 删除 Pod → 等待新 Pod → 读取原内容。仅检查文件夹或 Running 状态不够。
NFS 示例用于实验。Registry 和 Cassandra 的生产后端要按组件支持的存储要求设计,不能从实验可用推出生产适用。
实验操作#
引导练习:实施数据库持久存储#
数据库存储#
在本练习中,为 MySQL 数据库 Pod 配置持久存储。
文件:/root/DO280/labs/deploy-volume/(services)
/home/student/DO280/labs/deploy-volume/(workstation)
学习目标#
能够在 services 虚拟机上配置 NFS 共享,供 OpenShift 节点访问,并把对应的持久卷绑定给数据库 Pod。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| fstudent@workstation ~]$ lab deploy-volume setup
|
操作步骤#
1. 在 services 虚拟机上配置 NFS 共享,用作 OpenShift Container Platform 的持久卷。
1.1. 以 root 登录 services 虚拟机:
1
| [student@workstation ~]$ ssh root@services
|
1.2. /root/DO280/labs/deploy-volume/config-nfs.sh 可以自动配置持久卷所用 NFS 共享。先查看脚本,熟悉配置流程:
1
2
| [root@services ~]$ less -FiX /root/DO280/labs/deploy-volume/config-nfs.sh
... output omitted ...
|
1.3. 运行脚本 :
操作界面、表格与配置图
1
| [root@services ~]$ /root/DO280/labs/deploy-volume/config-nfs.sh
|
1.4. 确认 services 虚拟机的导出列表包含 /var/export/dbvol。
1
2
3
4
| [root@services ~]# showmount -e
Export list for services.lab.example.com:
+.. QUEpPUE omitted ....
/var/export/dbvol il
|
1.5. 退出services VM:
1
2
| [root@services ~]# exit
[student@workstation ~]$
|
2. 确认 node1 和 node2 都能访问 services 导出的 NFS 卷。
2.1. 登录到 node1 主机作为根用户 :
1
| [student@workstation ~]$ ssh root@node1
|
2.2. 确认 node1 主机可以访问OpenShift服务VM中NFs的份额:
1
| [root@node1 ~]# mount -t nfs services.lab.example.com:/var/export/dbvol /mnt
|
2.3. 验证文件系统是否拥有node1的正确权限 :
1
2
3
4
5
6
7
8
| [root@node1 ~]# ls -la /mnt ; mount | grep /mnt
total 0
drwx------. 2 nfsnobody nfsnobody 6 Jul 31 11:03 .
dr-xr-xr-x. 17 root root 224 Jul 26 14:24 ..
services.lab.example.com:/var/export/dbvol on /mnt \
type nfs4 (rw, relatime, vers=4.1, rsize=262144, wsize=262144,\
namlen=255, hard, proto=tcp, port=0, timeo=600, retrans=2, sec=sys, \
clientaddr=172.25.250.11, local_lock=none, addr=172.25.250.13)
|
2.4. 卸载 NFS 共享 :
1
| [root@node1 ~]# umount /mnt
|
2.5. 登录出 node1 主机 :
操作界面、表格与配置图
1
| Lstuaent@worKkstation ~jo
|
2.6. 登录到 node2 主机作为根用户 :
1
| [student@workstation ~]$ ssh root@node2
|
2.7. 确认node2主机可访问OpenShift服务VM的NFS份额:
1
| [root@node2 ~]# mount -t nfs services.lab.example.com:/var/export/dbvol /mnt |
|
2.8. 验证文件系统是否拥有来自 node2 主机的正确权限 :
1
2
3
4
5
6
7
8
| [root@node2 ~]# ls -la /mnt ; mount | grep /mnt
total 0
drwx------. 2 nfsnobody nfsnobody 6 Jul 31 11:03 .
dr-xr-xr-x. 17 root root 224 Jul 26 14:27 ..
services.lab.example.com:/var/export/dbvol on /mnt \
type nfs4 (rw, relatime, vers=4.1, rsize=262144, wsize=262144,\
~ namlen=255, hard, proto=tcp, port=0, timeo=600, retrans=2, sec=sys, \
clientaddr=172.25.250.12,local_lock=none, addr=172.25.250.13)
|
2.9. 卸载 NFS 共享 :
1
| [root@node2 ~]# umount /mnt
|
2.10. 退出 node2 主机:
1
2
| [root@node2 ~]# exit
[student@workstation ~]$
|
OpenShift 会在需要时自动挂载 NFS 共享。
3. 在 workstation 以 admin 登录 OpenShift,创建供 MySQL Pod 使用的持久卷。
3.1. 登录为admin 用户。 如果提示, 请接受不安全连接。
1
2
| [student@workstation ~]$ oc login -u admin -p redhat \
https://master.lab.example.com
|
3.2. 检查持久卷定义:
1
2
3
4
5
| [student@workstation ~]$ less -Fix \
~/DO280/labs/deploy-volume/mysqldb-volume. ym1
apiVersion: v1
kind: PersistentVolume
metadata:
|
操作界面、表格与配置图
1
2
3
4
5
6
7
8
9
10
| name. mysqlab-volume
spec:
Capacity:
storage: 3Gi
accessModes:
- ReadWriteMany
nfs:
path: /var/export/dbvol
server: services.lab.example.com
persistentVolumeReclaimPolicy: Recycle
|
3.3. 使用提供的 YAML 资源定义文件创建持久卷 :
1
2
3
| [student@workstation ~]$ oc create -f \
~/DO280/labs/deploy-volume/mysqldb-volume. ym1
persistentvolume "mysqlidb-volume" created
|
3.4. 确认持久卷为可用状态,能够由项目中的声明申请:
1
2
3
| [student@workstation ~]$ oc get pv
NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS ewe
mysqldb-volume 3Gi RwX Recycle Available...
|
4. 在 workstation 以 developer 登录 OpenShift,创建 persistent-storage 项目。
4.1. 以developer 用户身份登录, 使用 redhat 作为密码。
1
2
| [student@workstation ~]$ oc login -u developer -p redhat \
https://master.lab.example.com
|
4.2. 创建一个名为 persistent-storage 的新项目 :
1
2
| {
- [student@workstation ~]$ oc new-project persistent-storage
|
5. 使用 oc new-app 创建名为 mysqldb 的应用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [student@workstation ~]$ oc new-app --name=mysqldb \
- -docker -image=registry.lab.example.com/rhscl/mysql-57-rhel7 \
-e MYSQL_USER=ose \
7e MYSQL_PASSWORD=openshift \
-e MYSQL_DATABASE=quotes
-... Output omitted ...
--> Creating resources ...
imagestream "mysqldb" created
deploymentconfig "mysqldb" created
service "mysqldb" created
--> Success
Application is not exposed. You can expose services to the outside world by
executing one or more of the commands below:
'oc expose svc/mysqldb'
Run 'oc status' to view your app.
|
操作界面、表格与配置图
6. 确认 mysqldb 部署成功,然后创建 PVC 并修改部署配置,使其使用持久卷。
6.1. 确认 mysqldb 部署成功:
1
2
3
4
5
6
| [student@workstation ~]$ oc status
In project persistent-storage on server https://master.lab.example.com:443
svc/mysqldb - 172.30.183.123:3306
dc/mysqldb deploys istag/mysqldb:latest
deployment #1 deployed 22 seconds ago - 1 pod
2 infos identified, use 'oc status -v' to see details.
|
6.2. 使用 oc describe pod 确认卷名为 mysqldb-volume-1,当前类型为 EmptyDir:
1
2
3
4
| [student@workstation ~]$ oc describe pod mysqldb | grep -A 2 'Volumes'
Volumes:
mysqldb-volume-1:
Type: EmptyDir (a temporary directory that shares a pod's lifetime)
|
6.3. 使用 oc set volume 修改 DeploymentConfig,并创建持久卷声明:
1
2
3
4
5
6
7
| [student@workstation ~]$ oc set volume dc/mysqldb \
--add --overwrite --name=mysqldb-volume-1 -t pvc \
--claim-name=mysqldb-pvclaim \
--claim-size=3Gi \
--claim-mode='ReadWriteMany'
persistentvolumeclaims/mysqldb-pvclaim
deploymentconfig "mysqldb" updated
|
6.4. 使用 oc describe pod 确认 Pod 已使用持久卷。输出应显示卷名 mysqldb-volume-1、类型 PersistentVolumeClaim,以及声明名 mysqldb-pvclaim。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [student@workstation ~]$ oc describe pod mysqldb | \
grep -E -A 2 'Volumes|ClaimName'
Volumes:
mysqldb-volume-1:
Type: EmptyDir (a temporary directory that shares a pod's lifetime)
Volumes:
mysqldb-volume-1:
Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in
the same namespace)
ClaimName: mysqldb-pvclaim
ReadOnly: false
default -token-h2dp4:
Volumes:
deployer -token-6wwsd:
Type: Secret (a volume populated by a Secret)
|
操作界面、表格与配置图
7. 在workstation主机上,通过使用oc获得 pvc 命令,核实mysqldb-pvclaim的持久卷声明与mysqldb-volume的持久卷有关。
1
2
3
| [student@workstation ~]$ oc get pvc
NAME STATUS VOLUME CAPACITY ACCESSMODES AGE
mysqldb-pvclaim Bound mysqldb-volume 3Gi RWX 15m
|
8. 从 workstation 主机, 使用在 /home/student/DO280/labs/deploy-volume/quote.sql 中可用的 SQL 文件填充数据库。 使用 oc port-forward 命令将本地端口 3306 移至 pod 端口 3306。 使用 Mysql 命令来填充数据库。
8.1. 打开两个终端。在第一个终端用 oc get pods 确认 mysqldb Pod 已就绪并运行,再用 oc port-forward 把本地 3306 转发到 Pod 的 3306。命令会持续运行,直到手动结束。
1
2
3
| [student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
mysqldb-2-k2v1lh 1/1 Running ic] 1h
|
1h
1
2
| [student@workstation ~]$ oc port-forward mysqldb-2-k2vlh 3306:3306
Forwarding from 127.0.0.1:3306 -> 3306
|
8.2. 在第二个终端使用 quote.sql 向 quotes 数据库导入数据。
1
2
| [student@workstation ~]$ mysql -h127.0.0.1 -uose -popenshift \
quotes < /home/student/DO280/labs/deploy-volume/quote.sql
|
8.3. 连接数据库,确认 quote 表中有数据:
1
2
| [student@workstation ~]$ mysql -h127.0.0.1 -uose -popenshift \
quotes -e "select count(*) from quote;"
|
预期应:
8.4. 验证 MySQL 服务器是否初始化了导出文件夹中的数据库。
1
2
3
4
5
| [student@workstation ~]$ ssh root@services ls -la /var/export/dbvol/
total 41036
drwx------. 6 nfsnobody nfsnobody 4096 Jul 341 14:11 .
drwxr-xr-x, 3 root root 19 Jul 314 11:03 ..
- -fw-r-----. 1 nfsnobody nfsnobody 56 Jul 31 11:10 auto.cnf
|
操作界面、表格与配置图
1
2
| ----. 1 nfsnobody nfsnobody 1676 Jul 31 11:10 ca-key.pem
-r--. 1 nfsnobody nfsnobody 1075 Jul 31 11:10 ca.pem
|
Jul 31 11:10 ca.pem
1
2
3
| eg eeeeees a eee See ee ee eee ee
-. 1 nfsnobody nfsnobody 1079 Jul 31 11:10 client-cert.pem
-, 1 nfsnobody nfsnobody 1680 Jul 31 11:10 client-key.pem
|
Jul 31 11:10 client-key.pem
1
2
3
| - eee ps ee oe ee eeee ee eee eee |
1 nfsnobody nfsnobody 352 Jul 31 11:11 ib_buffer_pool
1 nfsnobody nfsnobody 12582912 Jul 31 14:13 ibdata1
|
Jul 31 11:13 ibdata1
1
2
3
| jf TP OMY Fotiy 262v0lvie VMS Vs S242 ew Seo
1 nfsnobody nfsnobody 8388608 Jul 31 11:13 ib_logfileOd
1 nfsnobody nfsnobody 8388608 Jul 31 11:10 ib_logfile1
|
Jul
1
2
3
| ie ee ee Se a2ee
-rw-r-----, 1 nfsnobody nfsnobody 12582912 Jul 31 11:11 ibtmp1
drwxr-x---. 2 nfsnobody nfsnobody 4096 Jul 31 11:10 mysql
|
Jul
1
2
3
| ee eT ee eeee eebee yecoee ll
-rw-r-----, 1 nfsnobody nfsnobody 2 Jul 31 11:11 mysqldb-2-41vs8.pid
drwxr-x---. 2 nfsnobody nfsnobody 8192 Jul 31 11:10 performance_schema
|
Jul 31 11:10o performance_schema
1
2
3
4
5
6
| eee ON ee ee ee 7 eee ee ee ee ee eeee
-rw-------. 1 nfsnobody nfsnobody 1676 Jul 31 11:10 private_key.pem
-rw-r--r--. 1 nfsnobody nfsnobady 452 Jul 34 11:10 public_key.pem
drwxr-x---. 2 nfsnobody nfsnobody 54 Jul 31 11:13 quotes
-rw-r--r--, 1 nfsnobody nfsnobody 1079 Jul 31 11:10 Server-cert.pem
oe wT ae)Gee ee eeeeeeee ee "anwanm a..7 AA rr or)Jee eeeee eee,
|
Jul
1
2
3
| "Tbe bese oh TEE OUyy Ft oly 2G Vue Yt 44-40 OUP Ve eS he ee
-rw--~-----, 1 nfsnobody nfsnobody 1676 Jul 31 11:10 server-key.pem
drwxr-x---, 2 nfsnobody nfsnobody 8192 Jul 31 11:10 sys
|
确认 quotes 目录存在;它与 MySQL Pod 资源定义中的数据库名称对应。
8.5. 确认 MySQL 在 quotes 目录中创建了表元数据:
1
2
3
4
5
6
7
8
| [student@workstation ~]$ ssh root@services ls -la /var/export/dbvol/quotes
total 212
drwxr-x---. 2 nfsnobody nfsnobody 54 Jul 31 11:13 .
drwx------. 6 nfsnobody nfsnobody 4096 Jul 31 11:11 ..
-rw-r-----. 1 nfsnobody nfsnobody 65 Jul 31 11:10 db.opt
-rw-r-----, 4 nfsnobody nfsnobody 8584 Jul 31 11:13 quote.frm
-rw-r-----. 1 nfsnobody nfsnobody 98304 Jul 31 11:13 quote.ibd
SS . aS eae i ee Se = |
|
rw.
1
2
| ee ee eeeee ee ee ee Se aeeeeee
-rw-r-----. 1 nfsnobody nfsnobody 98304 Jul 31 11:13 quote. ibd
|
应该有一个名为 quote.frm 的文件, 与数据库中创建的表格的名称相对应。
8.6. 在第一个终端按 Ctrl+C,结束 oc port-forward。
9. 清理
9.1. 在 workstation 主机上, 删除 persistent-storage 项目, 其中也删除在此实验创建的所有 Pv C 和 pods。
1
| [student@workstation ~]$ oc delete project persistent-storage
|
操作界面、表格与配置图
删除“persistent-storage”项目
9.2. 在 workstation 主机上, 确保您作为admin 用户登录到 OpenShift 上, 并删除 PV , 以便在随后的实验中重建( 和 NFS 共享再利用)。
1
2
3
4
| [student@workstation ~]$ oc login -u admin -p redhat \
https://master.lab.example.com
[student@workstation ~]$ oc delete pv mysqldb-volume
persistentvolume "mysqldb-volume" deleted
|
9.3. 核查位于services VM 上的 NFS 共享目录上的数据库文件是否仍然存在。 这显示即使在删除 Pv 之后文件仍然存在
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| [student@workstation ~]$ ssh root@services ls -la /var/export/dbvol/
total 41032
-rw-r-----. 1 nfsnobody nfsnobody 56 Jul 16 18:53 auto.cnf
-rw---~----, 1 nfsnobody nfsnobody 1676 Jul 16 18:53 ca-key.pem
-rw-r--r--. 1 nfsnobody nfsnobody 1075 Jul 16 18:53 ca.pem
-rw-r--r--. 1 nfsnobody nfsnobody 1079 Jul 16 18:53 client-cert.pem
-rw-------, 1 nfsnobody nfsnobody 1676 Jul 16 18:53 client-key.pem
-rw-r-----. 1 nfsnobody nfsnobody 291 Jul 16 22:16 ib_buffer_pool
-rw-r-----. 1 nfsnobody nfsnobody 12582912 Jul 16 22:16 ibdatat
-rw-r-----. 1 nfsnobody nfsnobody 8388608 Jul 16 22:16 ib_logfile®o
-rw-r-----. 1 nfsnobody nfsnobody 8388608 Jul 16 18:53 ib_logfile1
-rw-r-----, 1 nfsnobody nfsnobody 12582912 Jul 16 22:17 ibtmp1
drwxr-x---. 2 nfsnobody nfsnobody 4096 Jul 16 18:53 mysql
-rw-r-----. 1 nfsnobody nfsnobody 2 Jul 16 22:16 mysqldb-2-k2vlh.pid
drwxr-x---. 2 nfsnobody nfsnobody 8192 Jul 16 18:53 performance_schema
-rw-------, 1 nfsnobody nfsnobody 1680 Jul 16 18:53 private _key.pem
- -rW-r--r--. 1 nfsnobody nfsnobody 452 Jul 16 18:53 public_key.pem
drwxr-x---. 2 nfsnobody nfsnobody 20 Jul 16 18:53 quotes
-rw-r--r--. 1 nfsnobody nfsnobody 1079 Jul 16 18:53 server-cert.pem
-rw-------. 1 nfsnobody nfsnobody 1680 Jul 16 18:53 server-key.pem
drwxr-x---. 2 nfsnobody nfsnobody 8192 Jul 16 18:53 sys
|
9.4. 使用 SSH 远程访问 OpenShift services VM, 然后删除 NFS 共享 /var/export/dbvol/ 目录的内容 :
1
| [student@workstation ~]$ ssh root@services rm -rf /var/export/dbvol/*
|
校验文件已删除 :
1
2
3
| [student@workstation ~]$ ssh root@services ls -la /var/export/dbvol/
total 0
drwx------, 2 nfsnobody nfsnobody 6 Jul 16 19:00 .
|
操作界面、表格与配置图
1
| aOrwxr-xr-xX. 4 root root 33 Jul 16 19:00 ..
|
9.5. 在 workstation 执行 lab deploy-volume cleanup,移除 NFS 共享。
1
| [student@workstation ~]$ lab deploy-volume cleanup
|
本引导练习到此结束。
操作界面、表格与配置图
实验:分配持久存储#
在此实验中, 使用模板来部署一个应用程序, 与数据库整合, 数据库将数据存储在 NFs-backed 的持久卷中。
文件:/home/student/DO280/labs/storage-review (workstationVM)
/root/DO280/labs/storage-review( masterVM)
应用地址:http://instructor.apps.lab.example.com
学习目标#
能够部署一个应用程序,该应用程序与一个数据库相结合,该数据库使用持久性存储存储数据。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab storage-review setup
|
操作步骤#
1. 从服务VM, 运行位于 at/root/DO280/labs/storage-review /config-review-nfs.sh 的脚本, 以配置此实验用于 OpenShift 持久卷的 NFS 共享 /var/export/review-dbvol。
2. 在 workstation 以 admin/redhat 登录 OpenShift,使用 /home/student/DO280/labs/storage-review/review-volume-pv.yaml 创建 review-pv 持久卷。
3. 确认当前以 admin 身份操作 openshift 命名空间,导入 /home/student/DO280/labs/storage-review/instructor-template.yaml 中的 instructor 模板。
操作界面、表格与配置图
务必使用 -n openshift,确保模板在 Web 控制台中可见。
4. 在 workstation 以 developer/redhat 登录 OpenShift,创建名为 instructor 的项目。
5. 从 workstation 访问 https://master.lab.example.com 上的 OpenShift 网络控制台。 登录到 OpenShift 作为developer 用户, 使用 redhat 密码。 选择教官项目, 然后浏览 OpenShift 模板的目录。 选择教官应用程序模板 PHP 模板。
选择 The Instructor Application Template,在 Application Host 字段输入 instructor.apps.lab.example.com,让该主机名路由到 PHP 服务。创建 instructor 应用。
6. 模板已经创建数据库。在 workstation 使用 oc port-forward 将本地 3306 转发到 Pod 的 3306,再通过 /home/student/DO280/labs/storage-review/instructor.sql 导入数据。
使用以下 mysql 命令导入数据:
1
2
| [student@workstation ~]$ mysql -h127.0.0.1 -u instructor -ppassword \
instructor < /home/student/DO280/labs/storage-review/instructor.sql
|
7. 访问 http://instructor.apps.lab.example.com,按下表添加一条记录。
添加 Instructor#
8. 评价
运行以下命令来给您的工作评分 :
1
2
| ee ee eS SK = =<
[student@workstation ~]$ lab storage-review grade
|
如果评分不是 PASS,请检查操作后重新运行评分命令。
操作界面、表格与配置图
清理
在 workstation 主机上, 删除教官项目和 review-pv 持久卷。 在 master 主机上, 删除位于 /var/export/review-dbvol 目录中的数据库文件。
实验到此结束
操作界面、表格与配置图
在此实验中, 使用模板来部署一个应用程序, 与数据库整合, 数据库将数据存储在 NFS 支持的持久卷中。
文件:/home/student/DO280/labs/storage-review (workstationVM)
/root/DO280/labs/storage-review(masterVM)
应用地址:http://instructor.apps.lab.example.com
学习目标#
能够部署一个应用程序,该应用程序与一个数据库相结合,该数据库使用持久性存储存储数据。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
, [student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab storage-review setup
|
操作步骤#
1. 在 services 虚拟机运行 /root/DO280/labs/storage-review/config-review-nfs.sh,配置本实验持久卷所用的 /var/export/review-dbvol NFS 共享。
1.1. 以 root 登录 services 虚拟机:
1
2
| [student@workstation ~]$ ssh root@services
[root@services ~]#
|
1.2. 在 /root/DO280/labs/storage-review/config-review-nfs.sh 上查看脚本, 以便熟悉在运行脚本前配置 NFS 共享的过程 :
1
2
3
| [root@services ~]# less -FiX /root/DO280/labs/storage-review/config-review-
nfs.sh
... Output omitted ...
|
1.3. 运行用于配置 NFS 共享的脚本 :
操作界面、表格与配置图
1
2
| [root@services ~]# /root/DO280/labs/storage-review/config-review-nfs.sh
Export directory /var/export/review-dbvol created.
|
1.4. 运行 showmount -e,确认导出列表包含 /var/export/review-dbvol。
1
2
3
4
5
| [root@services ~]# showmount -e
Export list for services.lab.example.com:
... output omitted ,..
/var/export/review-dbvol *
... Output omitted ,..
|
1.5. 退出 services 虚拟机。
1
2
| [root@services ~]# exit
[student@workstation ~]$
|
2. 在 workstation 以 admin/redhat 登录,使用 /home/student/DO280/labs/storage-review/review-volume-pv.yaml 创建名为 review-pv 的持久卷。
2.1. 登录为 OpenShift admin 用户 :
1
2
| [student@workstation ~]$ oc login -u admin -p redhat \
https://master.lab.example.com
|
2.2. 检查 /home/student/DO280/labs/storage-review/review-volume-pv.yaml 的内容,以正确输入 PV 属性和 NFS 共享 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [student@workstation ~]$ less -Fix \
/home/student/DO280/labs/storage-review/review-volume-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: review-pv
spec:
capacity:
storage: 3Gi
accessModes:
- ReadWriteMany
nfs:
path: /var/export/review-dbvol
server: services.lab.example.com
persistentVolumeReclaimPolicy: Recycle
|
2.3. 使用 oc 创建与持久卷定义相对的指令, 以创建名为 review-pv 的持久卷 :
1
2
3
| [student@workstation ~]$ oc create -f \
/home/student/DO280/labs/storage-review/review-volume-pv.yaml
persistentvolume "review-pv" created
|
操作界面、表格与配置图
3. 确认以 admin 身份操作 openshift 命名空间,导入 /home/student/DO280/labs/storage-review/instructor-template.yaml。
务必使用 -n openshift,确保模板在 Web 控制台中可见。
3.1. 查看模板参数:php:7.0 用于 PHP 应用,mysql:5.7 用于数据库。模板创建的 PVC 将绑定到先前创建的 review-pv。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [student@workstation ~]$ less -Fix \
/home/student/DO280/labs/storage-review/instructor-template.yaml
apiVersion: v1
kind: Template
labels:
template: instructor
... Output omitted ..,
from:
kind: ImageStreamTag
name: php:7.0
... output omitted ..,
from:
kind: ImageStreamTag
name: mysql:5.7
2) OUEDUE OMT HECO oars
|
3.2. Import admin 用户为 /home/student/DO280/labs/storage-review/instructor-template.yaml 教官模板文件。
1
2
3
| [student@workstation ~]$ oc create -n openshift -f \
/home/student/DO280/labs/storage-review/instructor-template.yaml1
template "instructor" created
|
4. 在 workstation 以 developer/redhat 登录 OpenShift,创建名为 instructor 的项目。
4.1. 登录到 OpenShift 作为developer 用户 :
1
2
| [student@workstation ~]$ oc login -u developer -p redhat \
https: //master.lab.example.com
|
4.2. 创建 instructor 项目:
1
| [student@workstation ~]$ oc new-project instructor
|
操作界面、表格与配置图
在 workstation 浏览器访问 https://master.lab.example.com,以 developer 用户登录,密码
操作界面、表格与配置图
为 redhat。选择 instructor 项目,浏览模板目录,并选择 The Instructor Application Template PHP 模板。
选择 The Instructor Application Template 应用模板,在 Application Host 字段填入 instructor.apps.lab.example.com,设置通向 PHP 服务的主机名,然后创建 instructor 应用。
5.1. 打开 Firefox,访问 https://master.lab.example.com。如果出现提示,接受安全证书,以 developer 用户登录,密码为 redhat。
单击 instructor 项目,进入该项目。
dersloper
My Projects Create Project
1 of 1 Projects View All
instructor
createdbydevelcper39minutesagc
Getting Started
o Take Home Page Tour
ocumertaicn
Interactive Learning Pcrtal
o Coritainer Develogment Kit
You Tube
o Blog
Recently Viewed
选择项目
5.2. 单击浏览目录查看可用的组件。
操作界面、表格与配置图
Get started with your project.
Add content to your project from the catalog of web frameworks, databases, and other components.
definitions, or select an item shared from another project.
Browse Catalog
1
| DeployImage | ImportYAML/JSON Selectfrom Project
|
选择模板
5.3. 从目录中单击语言标签并选择 PHP。
单击 The Instructor Application Template,编辑模板属性。
选择要添加到当前项目的项目
1
| Alt Latiyudges Databases Middleware cvcD Other
|
.NET JS
1
2
3
4
5
6
| 4
All al Js SET ™ = &
java JavaScript -NET Perl Ruby PHP Python
"Fitters 4items
CakePHP+MySQL CakePHP+MySQL PHP TheInstructorApplication
(Ephemeral) Template
|
选择应用程序模板
如果 developer 用户看不到模板,请以 admin 用户登录后重试。后续步骤也可以使用 admin 完成。
5.4. 使用模板填入的默认值,但将 Application Hostname 的值改为 instructor.apps.lab.example.com,然后单击 Next。
在下一屏保留默认值,单击 Create 创建应用。
操作界面、表格与配置图
The Instructor Application Template
Information Configuration Binding Results
Set thls to the relative path to your project if It Is not In the root of your reposltory
1
2
| ApplicationHostname
instructor.apps.lab.example.com
|
The exposed hostname that will route to the PHP servlce, If left blank a value will be defaulted.
A secret string used to conflgure the Git Hub webhook.
A secret strling used to configure the Generlc webhook.
设置应用程序主机名
5.5. 单击 Continue to project overview,监视应用构建。在 Provisioned Services 区域单击 instructor。
单击部署配置 instructor, #1 旁的下拉箭头,打开部署面板。构建结束后,Builds 区域的 Complete 旁应出现绿色勾号。Networking 区域显示应用路由 http://instructor.apps.lab.example.com。
Other Resources
DEPLOYMENT CONFIG
1
2
| DEPLOYMENTCONFIG
Instructor,#1
|
CONTAINERS
@ fmage: Iieu u.tul /r su u Ltur 74178fd 206 0 MIB Build: instructor, #1 <> Source: Establish remote repository dd32beb Ports: 8080/TCP
NETWORKING
Service - Internal Traffic Routes - External Traffic instructor
http://instructor.apps.example.comc
项目概况
BUILDS
instructor v Buld #1 is complete created 2
Pushed 1/6 5layers,364complete
1
2
3
4
5
6
7
8
| vyBulld+1 iscomplete created2mm ag "1
Pushed 1/5 layers, 384 comptete
Pesbed 2/6 lepers, 45% complete
Pushed 3/6 layers, 60% complete
Pushed 4/6 layers, 79% complete
Pushed 5/6 layers, 188% complete
Pushed 6/6 layers, 1864 complete
Preah wenewentel
|
项目概况
6. 模板创建了数据库服务器。在 workstation 上,使用 oc port-forward 将本地端口 3306 转发到 Pod 的 3306 端口,并向数据库导入数据。
操作界面、表格与配置图
使用 /home/student/DO280/labs/storage-review/instructor.sql 中可用的 SQL 文件。
使用以下 Mysql 命令来填充数据库 :
1
2
| [student@workstation ~]$ mysql -h127.0.0.1 -u instructor -ppassword \
instructor < /home/student/DO280/labs/storage-review/instructor.sql
|
6.1. 确认已经以 developer 用户登录 OpenShift:
1
2
| [student@workstation ~]$ oc login -u developer -p redhat \
https: //master.lab.example.com
|
6.2. 打开两个终端。在第一个终端执行 oc get pods 查看 Pod 状态,确认 mysql Pod 为 Running。
运行 oc port-forward,将本地端口 3306 转发到 Pod 的端口
3306. 命令保持端口打开, 直到连接被手动终止。
1
2
3
4
5
| [student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
instructor -1-9n6cx al/ik Running (o] 42m
instructor-1-build 0/1 Completed ic) 42m
mysql-1-z95g1 </al Running 0 42m
|
6.3. 在第二个终端使用提供的 instructor.sql 文件,向 instructor 数据库导入数据。
1
2
| [student@workstation ~]$ mysql -h127.0.0.1 -u instructor -ppassword \
instructor < /home/student/DO280/labs/storage-review/instructor.sql
|
6.4. 通过运行 MySQL 命令确保数据库现在包含新记录 :
1
2
3
4
5
6
7
8
9
10
| [student@workstation ~]$ mysql -h127.0.0.1 -u instructor -ppassword \
instructor -e "select * from instructors;"
+--------+----------+-------------- ee eeete ee et
... | instructorName | email | city Katee
+-----------------+----+-----+-----= 5 eeeeeeeeee te ee eeeet
... | DemoUser1 | duseri@workstation.example.com | Raleigh heea
InstructorUser1 | iuseri@workstation.example.com | Rio de Janeiro |
InstructorUser2 | iuser2@workstation.example.com | Raleigh
InstructorUser3 | iuser3@workstation.example.com | Sao Paulo |
+e ---- eee ee te te en rt
|
6.5. 在第一个终端按 Ctrl+C,关闭端口转发连接。
操作界面、表格与配置图
7. 访问可在http://instructor.apps.lab.example.com.查阅的应用程序,根据下表使用该应用程序添加新记录。
添加 Instructor#
7.1. 单击添加新教官以添加新教官,然后使用表格中提供的信息填写表格。
单击 Add New Instructor 更新数据库,讲师列表会自动刷新。
8. 评价
运行以下命令来给您的工作评分 :
1
| [student@workstation ~]$ lab storage-review grade
|
如果评分不是 PASS,请检查操作后重新运行评分命令。
9. 清理
在 workstation 主机上, 删除教官项目和 review-pv 持久卷。 在 master 主机上, 删除位于 /var/export/review-dbvol 目录中的数据库文件。
9.1. 在 workstation 上以 OpenShift 的 admin 用户登录,密码为 redhat:
1
2
| [student@workstation ~]$ oc login -u admin -p redhat \
https://master.lab.example.com
|
9.2. 删除 instructor 项目:
1
2
3
| [student@workstation ~]$ oc delete project instructor
project "instructor" deleted
a a a Se eS ee ee Eee mm nee
|
9.3. 删除 review-pv 持久卷:
1
2
| [student@workstation ~]$ oc delete pv review-pv
persistentvolume "review-pv" deleted
|
9.4. 登录 services 虚拟机,删除数据库文件。
从 workstation 以 root 用户登录 services 虚拟机:
1
| [student@workstation ~]$ ssh root@services
|
操作界面、表格与配置图
删除数据库和 NFs 文件, 然后验证文件是否已被删除 :
1
2
3
4
| [root@services ~]# rm -rf /var/export/review-dbvol
[root@services ~]# ls -la /var/export/review-dbvol/
ls: cannot access /var/export/review-dbvol/: No such file or directory
[root@services ~]# rm -f /etc/exports.d/review-dbvol.exports
|
实验到此结束
操作界面、表格与配置图
自测问题#
C7 管理应用部署#
7.1 扩容与调度#
oc scale dc/web --replicas=3 修改期望值,RC 再创建 Pod。节点容量、配额、选择器及存储条件不足时,实际副本不会达到期望值。
nodeSelector 是硬性标签匹配;所有键都必须匹配。移除节点标签不会自动迁移已经运行的 Pod,但重建后的 Pod 会重新调度。标记节点不可调度只阻止新 Pod,drain 才迁移可驱逐的已有工作负载。
7.2 ImageStream 与触发器#
ImageStreamTag 是可更新的命名引用,ImageStreamImage 通过摘要指向特定镜像。镜像仓库同名 Tag 变化后,ImageStream 需要导入更新,DeploymentConfig 还需要 ImageChange 触发器才能自动部署。
ConfigChange 监控 DC 配置变化;ImageChange 监控所指向的镜像流标签。关闭自动镜像触发可控制上线时机,但不是镜像仓库访问权限控制。
7.3 滚动更新#
maxSurge 允许部署时额外副本,maxUnavailable 允许暂时不可用副本。设置 maxUnavailable: 0 仍需有足够资源容纳新增副本,否则更新会等待。
回滚 DC 不会回滚数据库内容、外部系统和 Secret。上线前需要确认新旧应用与数据结构可以并存。
实验操作#
引导练习:扩展应用#
本实验通过增加运行中的 Pod 数量来扩展应用。
Files http://registry.lab.example.com/scaling
应用地址:http://scaling.apps.lab.example.com
学习成果
应能使用部署配置部署多个 Pod,扩展应用。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do286-ansible
[student@workstation do280-ansible]$ ./install.sh
|
操作步骤#
1. 创建一个新项目。
1.1. 以 developer 用户登录 OpenShift:
1
2
| [student@workstation ~]$ oc login -u developer -p redhat \
https://master.lab.example.com
|
1.2. 创建名为 scaling 的项目:
1
| [student@workstation ~]$ oc new-project scaling
|
2. 创建测试缩放的应用程序。
2.1. 使用 php:7.0 镜像流创建新应用,并将定义导出到 YAML 文件:
1
2
| [student@workstation ~]$ oc new-app -o yaml -i php:7.0 \
http://registry.lab.example.com/scaling > ~/scaling.yml
|
2.2. 在文本编辑器中打开 YAML 资源定义文件 :
1
2
| [student@workstation ~]$ vi ~/scaling.yml
|
|
找到 DeploymentConfig 资源,将 replicas 属性从 1 改为 3。
操作界面、表格与配置图
1
2
3
4
5
6
7
8
9
10
11
12
| - apiVersion: v1
kind: DeploymentConfig
metadata:
annotations:
openshift .io/generated-by: OpenShiftNewApp
creationTimestamp: null
labels:
app: scaling
name: scaling
spec:
replicas: 3
selector:
|
这样,在创建应用时会创建三个 Pod。保存文件并退出编辑器。
2.3. 使用 oc create 创建应用:
1
2
3
4
5
| [student@workstation ~]$ oc create -f ~/scaling.yml
imagestream "scaling" created
- buildconfig "scaling" created
deploymentconfig "scaling" created
service "scaling" created
|
2.4. 审查构建状况:
1
2
3
| [student@workstation ~]$ watch -n 3 oc get builds
NAME TYPE FROM STATUS STARTED DURATION
scaling-1 Source Git@1ba7b7d Complete 48 seconds ago 16s
|
构建可能需要几分钟才能完工。 等到构建状态转换为完成, 然后按 Ctrl+C 键退出监视命令。
2.5. 列出可用的Pod :
1
2
3
4
5
6
| [student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
scaling-1-018wr 41/1 Running 10} 23s
scaling-1-build 0/1 Completed (0) 52s
scaling-1-dq36m 1/1 Running 0} 23s
scaling-1-jb7zj al/al, Running f°) 23s
|
重复上一命令,直到看到 scaling 应用的三个 Pod。所有 Pod 就绪并运行可能需要一些时间。
3. 为应用创建路由,以便在各 Pod 之间分配请求:
1
| [student@workstation ~]$ oc expose service scaling \
|
操作界面、表格与配置图
1
2
| «-hostname=scaling.apps.lab.example.com
- route "scaling" exposed
|
操作界面、表格与配置图
4. 通过 Web 控制台获取应用 Pod 的 IP 地址,与 scaling 应用报告的地址比较。
4.1. 在 workstation 打开浏览器(Applications > Internet > Firefox),访问 https://master.lab.example.com,以 developer 登录,密码为 redhat。
4.2. 在页面右侧的 My Projects 下单击 scaling。
Q Search Catalog
My Projects + Create Project
Browse Catalog PDeploy Image ImportYAML/JSON Select from Project 1of1Projects Languages
Databases Middleware CI/CD scaling
Fllter 101 items
Getting Started
.NET .NET .NET .NET Take Home Page Tour
.NET Core + PostgreSQL .NET Core Builder .NET Core Example .NET Core Runtime Interactive Learning Portal (Persistent)
Images Example
Yoo Tute
Recently Viewed
scaling 项目
4.3. 展开 scaling, #1 左侧的下拉菜单,查看三个应用 Pod。
scaling
Fiterbynom Application
APPLICATION
scaling http://scaling-apps.lab.example.come
scaling,#1
CONTANERS
scaling
@ Image: 5(aling/scaling cc21387 205.6 M8
C Build: scaling, $1
< Source: Establish remote repository ea4a95a
Ports:8080/TCP
NETWORKNG
Serfce -Intermal Traffc Routes - External Traffic scaling
http://scafing.apps.lab.example.comc
8080/TCP (8080-tcp) → 8080 Route scaling, target port 8080-tcp BUILDS scaling
V Bulld #1 is complete crcateo 8 rminutes ago
项目概览页面
4.4. 在左侧导航栏选择 Applications > Pods,查看 Pod。
操作界面、表格与配置图
Pods Overvew Learn Moree
jaqey kq ray Add
Applications
Containers Container Name Status
Ready Restarts Age
Running scaling-1-ciqr8 1/1 11 minute5
Resources 1/1 scaling-1-rrnzc C Running 11 minutes scaling-1-x7qct
Running 1/1 11 minutes
Storage
scaling-1-build v Completed 0/1 11 minutes
Monitoring
Caraiog
应用程序Pod
单击其中的一个Pod名称,以显示该Pod的细节,包括其内部IP地址。
scaling-1-x7qct
Scaling-1-x7qct eresleu 12 miruto ogo Actions
Applications app xc a lng deployment all ig-deployment config st ali rg
Detalls Environment Logs Terminal Events
Status Template
Status: Running Containers
quawkodag >celing, #1 scaling
IP: 10.129.0.72
Image; scaling/scaling cc21387 205 6 MB Node: node1.leb.exampie.com (172.25.250.11) Restart Policy:
Always C Build: scallng, #1 Monitoring
Container scaling </> Source: Establish remote repository ea4a95e authored by root
r Ports: 8080/TCP
State: Running since Aug 14, 2018 5:46:13 AM Mount: default-token-5pw2x - /var/run/secrets/kubermetes.io/sen vice account Ready:
true
Restart Count: Volumes
default-token-5pw2x
Pod 详情
4.5. 在 workstation 上运行 oc get pods,并加上 -o wide 选项,查看每个运行中 Pod 的内部 IP 地址:
1
2
| [student@workstation ~]$ oc get pods -o wide
NAME READY STATUS vou IP
|
IP NODE
1
2
3
4
5
6
| [student@workstation ~]$ oc get pods -o wide
NAME READY STATUS Tole IP NODE
scaling-1-018wr 4/1 Running - 10.128.0.27 node2.lab.example.com
scaling-1-build 0/1 Completed p:0e0 10.128.0.25 node2.lab.example.com
scaling-1-dq36m 1/1 Running mec 10.128.0.26 node2.lab.example.com
scaling-1-jb7zj 1/1 Running wae 10.130.0.31 node1.lab.example.com
|
5. 确认 OpenShift 路由器对应用请求进行负载均衡。使用 for 循环执行 curl 命令。
每个请求由不同的 Pod 处理,因此应返回不同的 IP 地址。当请求数超过可用 Pod 数量时,地址会重复出现。
1
2
3
4
| [student@workstation ~]$ for i in {1..5}; do curl -s \http://
scaling.apps.lab.example.com | grep IP; done
<br/> Server IP: 10.128.0.26
<br/> Server IP: 10.128.0.27
|
操作界面、表格与配置图
1
2
3
| <br/> server IP: 10.130.0.31
<br/> Server IP: 10.128.0.26
<br/> Server IP: 10.128.0.27
|
不能用 Web 浏览器检查此处的负载均衡,因为 OpenShift 路由器采用会话亲和性(也称粘性会话)。同一浏览器的请求会发给同一个 Pod。新开标签页或窗口不能绕过会话亲和性;需要换用另一种浏览器,或在另一台计算机上打开浏览器。
6. 扩展应用,使其运行更多 Pod。
6.1. 查看当前 DeploymentConfig 中指定的副本数:
1
2
3
| [student@workstation ~]$ oc describe dc scaling | grep Replicas
Replicas: 3
Replicas: 3 current / 3 desired
|
6.2. 使用 oc scale命令将Pod数( 复制) 增加到 5 :
1
| [student@workstation ~]$ oc scale --replicas=5 dc scaling
|
操作界面、表格与配置图
1
| geploymentcontig scaling scaled
|
6.3. 您也可以从 OpenShift 网络控制台的概览标签中更改Pod数量。单击蓝甜甜圈旁边的向上箭头或向下箭头,以分别增减Pod数量。
Name List by Application Overview
scaling http://scaling.apps.lab.example.com c
1
2
| = | scaling
™@ Imace:-, ee
|
Image: scaling/scaling c c21387 205.6 MD
C Build: Scalitig, #1
1
2
3
4
| © Build:
Source:Establishremoterepository
~~ Ports:BO8O/TCP
:
|
扩展 Pod
随着Pod数量向上或向下递增,你应该看到概览页中的蓝色甜甜圈相应变化。
6.4. 确保五个Pod现在运行中, 用于此应用程序 :
1
2
| [student@workstation ~]$ oc get pods -o wide
NAME READY STATUS oD 0 IP
|
IP NODE
1
2
3
4
| scaling-1-018wr 1/1 Running = 10.128,0.27 node2.lab.example.com
scaling-1-build 0/1 Completed 7 10.128.0.25 node2.lab.example.com
scaling -1-dq36m 1/4 Running 5 ab 10.128.0.26 node2.lab.example.com
scaling-1-g2jmz a/i Running a. 10.130.0.35 node1.lab.example.com
|
1/1 Running 10.130.0.35 node1.lab.example.com
1
2
3
| VPCALANY "LYS File d/i RUNTILNY ene LULU.U. DD MUGCL.1Aa0, CAGMHPLE, CU
scaling-1-jb7zj 1/1 Running as 10.130.0.31 node1.lab.example.com
scaling-1-rv2pv a/1 Running » 10.128.0.31 node2. lab.example.com
|
6.5. 确保路由器使用相同的 URL 平衡对新Pod的要求:
1
2
3
4
5
6
7
8
| [student@workstation ~]$ for i in {1..5}; do curl -s \
http://scaling.apps.lab.example.com | grep IP; done
<br/> Server IP: 10.128.0.26
<br/> Server IP: 10.128.0.27
<br/> Server IP: 10.128.0.31
<br/> Server IP: 10.130.0.31
<br/> Server IP: 10.130.0.35
a es es
|
与步骤 5 的输出比较,可以看到路由器以轮询方式把请求分配给五个应用 Pod。
7. 清理。在 workstation 执行以下命令,删除 scaling 项目:
1
| [student@workstation ~]$ oc delete project scaling
|
操作界面、表格与配置图
1
| project scaling delered
|
本引导练习到此结束。
操作界面、表格与配置图
引导练习:控制 Pod 调度#
本实验将应用部署到指定的一部分集群节点上,然后使一个节点停止承载工作负载以进行维护,并尽量降低对用户的影响。
学习目标#
应能配置节点选择器,使应用 Pod 只在指定的一部分节点上运行。随后删除待维护节点上的 Pod,并在另一节点重建,为节点维护做好准备。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280- ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab schedule-control setup
|
操作步骤#
1. 查看 node1 和 node2 的标签。两个节点位于同一 region,同一应用的 Pod 可以调度到这两个节点。
1.1. 以 admin 用户登录 OpenShift:
1
2
| [student@workstation ~]$ oc login -u admin -p redhat \
https://master.lab.example.com
|
查看和修改节点标签需要集群管理员权限。实际环境中,管理员通常不负责创建项目和扩展应用;为缩短练习,本实验不在管理员和开发者账户之间来回切换。
1.2. 审查所有集群节点的区域标签。
1
2
3
4
5
| [student@workstation ~]$ oc get nodes -L region
NAME STATUS 80 REGION
master.lab.example.com Ready
node1.lab.example.com Ready Por infra
node2.lab.example.com Ready se: infra
|
1.3. 创建一个新项目和一个新应用程序 :
1
| [student@workstation ~]$ oc new-project schedule-control
|
操作界面、表格与配置图
1
2
3
4
| NOW using project scnedulte-control on server "nttps://
master .lab.example.com:443".
[student@workstation ~]$ oc new-app --name=hello \
--docker -image=registry.lab.example.com/openshift/helio-openshift
|
1.4. 将应用扩展到五个副本。
1
| [student@workstation ~]$ oc scale dc/hello --replicas=5
|
1.5. 确认应用 Pod 分布在多个节点,等待五个 Pod 全部就绪并运行:
1
2
| [student@workstation ~]$ oc get pod -o wide
NAME READY STATUS Ho5 Jt)?
|
IP NODE
1
2
3
4
5
6
7
| [student@workstation ~]$ oc get pod -o wide
NAME READY STATUS or IP NODE
[nfeulal(oealtelictal 9 al//ak Running aia 10.128.0.36 node2.lab.example.com
hello-1-gmpmt 1/4 Running Poy 10.129,0.48 node1.lab.example.com
hello-1-k7sr3 9 1/1 Running lee 10.128.0.35 node2.lab.example.com
hello-1-rd661 1/1 Running eo 10.128.0.50 node2.lab.example.com
hello-1-wfx33 1/1 Running en. 10.129.0.59 node1.lab.example.com
|
node1和node2中应该都有Pod
将 node2 的 region 标签改为 apps。
2.1. 运行以下命令以更改标签 :
1
2
| [student@workstation ~]$ oc label node node2.lab.example.com \
region=apps --overwrite=true
|
2.2. 校验仅在 node2 上更改标签 :
1
2
3
4
5
| [student@workstation ~]$ oc get nodes -L region
NAME STATUS "00 REGION
master.lab.example.com Ready
node1.lab.example.com Ready Aah infra
node2.lab.example.com Ready ate apps
|
操作界面、表格与配置图
3. 修改部署配置,使 Pod 只调度到 region 为 apps 的节点。
3.1. 使用 oc get命令导出由 oc new-app 命令创建的部署配置到 YAML 文件 :
1
| [student@workstation ~]$ oc get dc/hello -o yaml > dc.yml
|
3.2. 在部署配置的 Pod 模板中添加节点选择器。
用文本编辑器打开 dc.yml。部署配置有两个 spec 属性,在第二个(template 内的 spec 部分)加入以下两行:
1
2
3
4
| spec:
nodeSelector:
region: apps
containers:
|
3.3. 对部署配置应用修改 :
1
2
3
| [student@workstation ~]$ oc apply -f dc.yml
deploymentconfig "hello" configured
i i a ee ee ll eee
|
3.4. 确认新部署已触发,并等待所有新的应用 Pod
就绪并运行。五个 Pod 都应调度到 node2:
1
2
3
4
5
6
7
| [student@workstation ~]$ oc get pod -o wide
NAME READY STATUS ane IP. NODE
hello-2-265bh 1/1 Running roe 10.128.0.36 node2.lab.example.com
hello-2-dj136 1/1 Running hoe 10.128.0.48 node2.lab.example.com
hello-2-g91kb 1/1 Running he AD, VA (6) 15) node2.lab.example.com
hello-2-mdjbg a/1 Running -_ 10.128.0.50 node2.lab.example.com
hello-2-v22hv 1/1 Running . 10,128.0.59 node2.lab.example.com |
|
4. 将 node1 加入 apps 区域。
此时只有 node2 的 region 为 apps。如果将其停用,就没有其他符合条件的节点可以运行 hello 应用的 Pod。
4.1. 将 node1 的 region 标签改为 apps:
1
2
| [student@workstation ~]$ oc label node node1.lab.example.com \
region=apps --overwrite=true
|
操作界面、表格与配置图
1
| noge "node1.tab.exampie.com labeled
|
4.2. 确认只修改了 node1 的标签,现在两个节点的 region 都为 apps:
1
2
3
4
5
| [student@workstation ~]$ oc get nodes -L region
NAME STATUS + REGION
master.lab.example.com Ready
node1.lab.exampie.com Ready a apps
node2.lab.example.com Ready a apps
|
5. 停用 node2,准备维护。
5.1. 运行以下命令以禁用 node2 上的调度 :
1
2
3
4
| [student@workstation ~]$ oc adm manage-node --schedulable=false \
node2.1lab.example.com
NAME STATUS AGE
node2.lab.example.com Ready,SchedulingDisabled 1h
|
1h
5.2. 删除node2上的所有Pod,并在node1上创建替代Pod。
运行以下命令 :
1
2
3
4
5
6
7
| [student@workstation ~]$ oc adm drain node2.lab.example.com \
--delete-local-data
node "node2.lab.example.com" already cordoned
pod "router-1-v3rgv" evicted
hello-2-265bh evicted
hello-2-dj136 evicted
node "node2.lab.example.com" drained
|
5.3. 确保所有应用Pod在node1上重新生成:
1
2
| [student@workstation ~]$ oc get pods -o wide
NAME READY STATUS dso IP NODE
|
IP
1
2
3
4
5
6
7
| [student@workstation ~]$ oc get pods -o wide
NAME READY STATUS Phone IP NODE
hello-2-dtbp9 1/1 Running , 10.128.0.36 node1.lab.example.com
hello-2-g502k 1/1 Running we: 10.128.0.48 node1.lab.example.com
hello-2-tr4cz 1/1 Running -_ 10.128.0.35 node1.lab.example.com
hello-2-x3nh5 = 1/1 Running -. 10.128.0.50 node1.lab.example.com
hello-2-Z3w7w = 1/1 Running - 10.128.0.59 node1.lab.example.com
|
6. 清理实验环境。
6.1. 恢复 node2 的可调度状态:
1
2
3
| [student@workstation ~]$ oc adm manage-node --schedulable=true \
node2.1lab.example.com
NAME STATUS AGE
|
操作界面、表格与配置图
1
| noqgez.iabD.example.com Ready in
|
6.2. 将 node1 和 node2 的 region 标签都改回 infra:
1
2
3
4
5
6
| [student@workstation ~]$ oc label node node1.lab.example.com \
region=infra --overwrite=true
node "node1.lab.example.com" labeled
[student@workstation ~]$ oc label node node2.lab.example.com \
region=infra --overwrite=true
node "node2.lab.example.com" labeled
|
6.3. 校验两个节点上的标签被更改, 并且两者都可排期 :
1
2
3
4
5
| [student@workstation ~]$ oc get nodes -L region
NAME STATUS reo REGION
master.lab.example.com Ready
- node1.lab.example.com Ready tov infra
node2.lab.example.com Ready aes infra
|
6.4. 通过运行以下命令删除 schedule-control 项目 :
1
2
| [student@workstation ~]$ oc delete project schedule-control
project "schedule-control" deleted
|
这项工作到此结束。
操作界面、表格与配置图
引导练习:管理镜像流#
在此实验中, 更新现有镜像流, 以使用 OpenShift 内部镜像仓库中最近更新的镜像。
文件:/home/student/D028o/labs/schedule-is
学习目标#
应能在新镜像推送到 OpenShift 内部镜像仓库后,自动更新应用 Pod。
在你开始之前
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab schedule-is setup
|
如果准备脚本失败,请使用以下命令重新允许所有用户创建项目:oc adm policy add-cluster-role-to-group self-provisioner system:authenticated:oauth
操作步骤#
1. 在名为schedule-is的新项目中部署phpmyadmin应用程序。
1.1. 在 workstation 上以 developer 用户登录:
1
2
| [student@workstation ~]$ oc login -u developer -p redhat \
https://master.lab.example.com
|
1.2. 创建一个名为 schedule-is 的项目 :
1
| [student@workstation ~]$ oc new-project schedule-is
|
1.3. 使用来自课堂镜像仓库的 phpmyadmin/phpmyadmin:4.7 镜像创建新应用程序 :
操作界面、表格与配置图
1
2
| [student@workstation ~]$ oc new-app --name=phpmyadmin \
--docker-image=registry.lab.example.com/phpmyadmin/phpmyadmin:4.7
|
2. 创建可使用 root 权限的服务账户,使容器能够以 root 运行。
2.1. 从终端窗口运行以下命令:
1
| [student@workstation ~]$ oc login -u admin -p redhat
|
2.2. 连接到 schedule-is 项目 :
1
| [student@workstation ~]$ oc project schedule-is
|
2.3. 创建一个名为 phpmyadmin-account 的服务账户。
从现有终端窗口运行以下命令 :
1
2
| [student@workstation ~]$ oc create serviceaccount phpmyadmin-account
serviceaccount "phpmyadmin-account" created
|
2.4. 将新服务账户关联到 anyuid 安全上下文约束。
运行以下命令 :
1
2
3
4
| [student@workstation ~]$ oc adm policy add-scc-to-user anyuid \
Wor phpmyadmin-account
scc "anyuid" added to: ["system:serviceaccount: secure-review: phpmyadmin-
account"]
|
3. 以 developer 用户更新部署配置,使用新建的
服务账户。该配置变更会触发新的 phpmyadmin 部署,
导致一个运行的Pod。
3.1. 在 workstation 上以 developer 用户登录:
1
| [student@workstation ~]$ oc login -u developer
|
3.2. 更新管理 phpmyadmin 部署的部署配置,使用新服务账户。可以使用 oc patch 或 oc edit。
可以从 /home/student/DO280/labs/secure-review/patch-dc.sh 脚本复制 oc patch 命令。
运行以下命令 :
1
2
| [student@workstation ~]$ oc patch dc/phpmyadmin --patch \
"{"spec":{"template":{"spec":{"serviceAccountName": "phpmyadmin-account"}}}}'
|
操作界面、表格与配置图
deployment config “phpmyadmin” patched
3.3. 使用 oc get pods 命令来查看 phpmyadmin 是否被重新调配 :
1
2
3
| [student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
phpmyadmin-2-r47m3 1/4 Running 0 27m
|
输出显示有一个正在运行的单机Pod。 数字“ 2 ” 在其名称中表示这是应用程序的第二次部署。
4. 更新内部镜像仓库中的镜像,以触发应用程序的新部署。
使用 workstation 主机上的本地 Docker 守护程序装入新容器镜像, 并将镜像推入 OpenShift 内部镜像仓库。
4.1. workstation 的 /home/student/DO280/labs/schedule-is 目录提供了新的 phpmyadmin Docker 镜像。将其加载到本地 Docker 守护进程。
运行以下命令 :
1
2
3
4
5
6
7
8
| [student@workstation ~]$ cd /home/student/DO280/labs/schedule-is
[student@workstation schedule-is]$ docker load -i phpmyadmin-latest.tar
cd7100a72410: Loading layer [=============s===============>] 4.403 MB/4.403 MB
f06b58790eeb: Loading layer [============================>] 2.873 MB/2.873 MB
730b09e0430c: Loading layer [===============s=============>] 11.78 kB/11.78 kB
931398d7728c: Loading layer [==================s==========>] 3.584 kB/3.584 kB
...output omitted...
Loaded image ID: sha256:93d@d7db5...output omitted...
|
4.2. 从已装入镜像到本地 docker 守护进程缓存中获取镜像 ID。
运行以下命令 :
1
2
3
| [student@workstation schedule-is]$ docker images
REPOSITORY TAG IMAGE ID ...
none none 93dOd7db5ce2 ...
|
4.3. 用内部镜像仓库的 URL 标记镜像。
因为镜像是从文件导入的, 镜像 ID 应该是精确的, 如前一个命令输出所见。
从终端窗口运行以下命令 :
1
2
| [student@workstation schedule-is]$ docker tag 93d0d7db5ce2 \
docker-registry-default.apps.lab.example.com/schedule-is/phpmyadmin:4.7
|
4.4. 验证镜像标记是否设定 :
1
| [student@workstation schedule-is]$ docker images
|
操作界面、表格与配置图
1
2
| REPOSL TORY TAG IMAGE ID ven
docker-registry-default.[{...]/schedule-is/phpmyadmin 4.7 93d@d7db5ce2 shafe
|
4.5. 获取访问 OpenShift API 的身份验证令牌。
从终端窗口运行以下命令 :
1
| [student@workstation schedule-is]$ TOKEN=$(oc whoami -t)
|
4.6. 使用令牌登录内部镜像仓库。
从终端窗口运行以下命令 :
1
2
3
| [student@workstation schedule-is]$ docker login -u developer -p ${TOKEN} \
docker -registry-default.apps.lab.example.com
Error ...output omitted... certificate signed by unknown authority
|
内部镜像仓库使用自签名证书,因此 Docker 守护进程认为它不安全。
4.7. 确保 workstation 上的 docker 守护程序信任 OpenShift 内部镜像仓库。
提供脚本。运行脚本。
1
2
3
4
5
6
7
8
| [student@workstation schedule-is]$ ./trust_internal_registry.sh
Fetching the OpenShift internal registry certificate.
done.
Copying certificate to the correct directory.
done.
System trust updated.
Restarting docker.
done.
|
4.8. 既然内部镜像仓库得到信任, 请尝试再次登录到内部镜像仓库。
1
2
3
| [student@workstation schedule-is]$ docker login -u developer -p ${TOKEN} \
docker -registry-default .apps.lab.example.com
Login Succeeded
|
4.9. 将镜像从本地 docker 守护程序推到内部 Docker 镜像仓库, 从而更新镜像。
从终端窗口运行以下命令 :
1
2
3
4
5
6
7
8
| [student@workstation schedule-is]$ docker push \
docker-registry-default .apps.lab.example.com/schedule-is/phpmyadmin: 4.7
f33dd3bcd071: Pushed
1b337c46652a: Pushed
2b17eb777748: Pushed
3e8fd20cdf2c: Pushed
6e75928f8d95: Pushed
e7fd82f2e8dd: Pushed
|
操作界面、表格与配置图
1
2
3
4
| 040TaG7841192: Pusnea
4.7: digest:
sha256:50f622a84d0208e0a7947903442e37d2046490e00C5255f310cee13e5d0c5598 size:
9754
|
5. 验证新镜像触发了新部署程序。
列出可用于校验构建已完成且新的phpmyadminPod已处于运行状态的可用Pod :
1
2
3
| [student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
phpmyadmin-3-tbt4g 4/1 Running (0) 2m
|
2m
因为新镜像被推到 Docker 镜像仓库。 创建了一个新的Pod。
如果输入命令较慢,可能已经看不到部署器 Pod。检查应用 Pod 名称中的部署序号:它应大于步骤 3 中的序号。
6. 清理。 运行以下命令删除项目 :
1
2
| [student@workstation ~]$ oc delete project schedule-is
project "schedule-is" deleted
|
本引导练习到此结束。
操作界面、表格与配置图
实验:管理应用部署#
在这个实验里,管理Pod, 运行一个 OpenShift 集群的常规维护任务。
应用地址:http://version.apps.lab.example.com
学习目标#
应能通过增加运行中 Pod 数量扩展应用,限制 Pod 在单个节点运行,并将部署回滚到早期版本。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab manage-review setup
|
操作步骤#
1. 将 node1 的 region 标签改为 services,将 node2 的 region 标签改为 applications。
2. 以 OpenShift 的 admin 用户创建 manage-review 项目。
3. 部署新的 version 应用并扩展到三个 Pod。源代码位于 http://registry.lab.example.com/version。
4. 修改部署配置,使 Pod 只调度到 region 为 applications 的节点。
5. 确认新部署已开始,一组新的 version Pod 已运行在
node2 节点。等待三个新的应用 Pod 全部就绪并运行。
6. 将 node1 主机的 region 标签改为 applications,以便
为 node2 主机维护做准备。
7. 将 node2 设置为不可调度并排空节点,准备维护。删除该节点上的 Pod,在 node1 上重建。
8. 创建路由,允许外部访问 version 应用。路由主机名必须为 version.apps.lab.example.com。
操作界面、表格与配置图
9. 使用curl 命令测试应用程序。
精确的版本字符串取决于使用同一 Git 仓库的先前练习。
10. 对实验结果评分。
运行以下命令来给您的工作评分 :
1
| [student@workstation ~]$ lab manage-review grade
|
如果评分不是 PASS,检查操作并重新运行评分命令。
11. 清理。将 node1 和 node2 的 region 标签恢复为 infra,并将两个节点都设为可调度。
实验到此结束
操作界面、表格与配置图
在这个实验里,管理Pod, 运行一个 OpenShift 集群的常规维护任务。
应用地址:http://version.apps.lab.example.com
学习目标#
应能通过增加运行中 Pod 数量扩展应用,限制 Pod 在单个节点运行,并将部署回滚到早期版本。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab manage-review setup
|
操作步骤#
1. 将 node1 的 region 标签改为 services,将 node2 的 region 标签改为 applications。
1.1. 以 admin 用户登录 OpenShift:
1
2
| [student@workstation ~]$ oc login -u admin -p redhat \
https: //master.lab.example.com
|
1.2. 校验当前节点标签。 它们应该由 OpenShift 安装器配置 :
1
| [student@workstation ~]$ oc get nodes -L region
|
预期输出是:
1
2
3
4
| NAME STATUS 40 REGION
master.lab.example.com Ready ie
node1.lab.example.com Ready "eb infra
node2.lab.example.com Ready Jolt infra
|
master.lab.example.com Ready
Ready infra node1.lab.example.com
node2.lab.example.com Ready infra
1.3. 将 node1 的 region 标签改为 services:
1
2
| [student@workstation ~]$ oc label node node1i.lab.example.com \
region=services --overwrite=true
|
操作界面、表格与配置图
1.4. 将 node2 的 region 标签改为 applications:
1
2
| [student@workstation ~]$ oc label node node2.lab.example.com \
region=applications --overwrite=true
|
1.5. 验证两个节点上的标签是否被更改 :
1
| [student@workstation ~]$ oc get nodes -L region
|
预期输出如下:
1
2
3
4
| NAME STATUS _ REGION
master .lab.example.com Ready at
node1.lab.example.com Ready reo services
node2. lab.example.com Ready _ applications
|
2. 以 OpenShift 的 admin 用户创建 manage-review 项目。
运行以下命令创建新项目 :
1
2
| [student@workstation ~]$ oc new-project manage-review
Now using project "manage-review" on server "https://master.lab.example.com:443".
|
3. 部署新的 version 应用并扩展到三个 Pod。源代码位于 http://registry.lab.example.com/version。
3.1. 在终端窗口中运行以下命令 :
1
2
| [student@workstation ~]$ oc new-app -i php:7.0 \
http://registry.lab.example.com/version
|
3.2. 将 version 应用的 Pod 数量增加到三个。
1
2
| [student@workstation ~]$ oc scale dc/version --replicas=3
deploymentconfig "version" scaled
|
3.3. 等待构建完成,并等待三个应用Pod准备运行。 验证它们是否计划在同一节点运行 :
1
2
3
4
5
6
| [student@workstation ~]$ oc get pod -o wide
NAME READY STATUS 0.0 NODE
version-1-build 0/1 Completed ar node1.lab.example.com
version-1-f39t7 1/1 Running = node2.lab.example.com
version-1-j9b41 1/1 Running oe node2.lab.example.com
version-1-rq6q4 1/1 Running an node2.lab.example.com
|
注意,应用 Pod 没有分散到两个集群节点,因为两个节点属于不同的 region,而 OpenShift 默认调度器
操作界面、表格与配置图
启用了 region 亲和性。当前选中哪个节点并不重要,后续会添加节点选择器,强制 Pod 在 node2 运行。
4. 修改部署配置,使 Pod 只调度到 region 为 applications 的节点。
4.1. 导出部署配置定义以更改标签 :
1
| [student@workstation ~]$ oc export dc/version -o yaml > version-dc.yml
|
4.2. 更新 YAML 文件以包含节点选择器。
打开带文本编辑器的 version-dc.yml 文件,并添加以下两行:
1
2
3
4
5
6
7
| template:
metadata:
spec:
nodeSelector:
region: applications
containers:
- image: ...
|
将这两行加在文件的第二个 spec 属性后面,并按上面的示例保持正确缩进。
4.3. 对部署配置应用 YAML 文件的更改 :
1
2
| [student@workstation ~]$ oc replace -f version-dc.yml
deploymentconfig "version" replaced
|
确认新部署已经开始,新的 version Pod 正在 node2 运行。等待三个新的应用 Pod 全部就绪并运行。
1
2
3
4
5
6
7
| as Ee
[student@workstation ~]$ oc get pod -o wide
NAME READY STATUS +. NODE
version-1-build 0/1 Completed -.. node1.lab.example.com
version-2-g36mp 1/1 Running ... node2,.lab.example.com
version-2-kirs8 a/1 Running ... mode2.lab.example.com
version-2-w0sn2 1/1 Running ... node2.lab.example.com
|
6. 将 node1 的 region 标签改为 applications,为 node2 维护做准备。
6.1. 将 node1 的 region 标签改为 applications:
1
| [student@workstation ~]$ oc label node node1.lab.example.com \
|
操作界面、表格与配置图
1
| region=applications --overwrite=true
|
6.2. 校验仅在 node1 主机上更改标签。
1
| [student@workstation ~]$ oc get nodes -L region
|
预期输出如下:
1
2
3
4
| NAME STATUS + REGION
master.lab.example.com Ready nay
node1.lab.example.com Ready i applications
node2.lab.example.com Ready Oh applications
|
7. 将 node2 设置为不可调度并排空节点,准备维护。删除该节点上的 Pod,在 node1 上重建。
7.1. 运行以下命令以禁用 node2 上的调度 :
1
2
3
4
| [student@workstation ~]$ oc adm manage-node --schedulable=false \
node2.lab.example.com
NAME STATUS AGE
node2.lab.example.com Ready, SchedulingDisabled ah
|
1h node2.lab.example.com 准备就绪,安排有障碍
7.2. 删除 node2 上所有 Pod,在 node1 上重建。
1
2
3
4
5
| [student@workstation ~]$ oc adm drain node2.lab.example.com \
--delete-local-data
node "node2.lab.example.com" already cordoned
pod version-2-g36mp evicted
node "node2.lab.example.com" drained
|
7.3. 确保所有三个应用Pod现在都安排在node1上运行。 等待所有三个Pod都准备好并运行。
1
2
3
4
5
6
| [student@workstation ~]$ oc get pods -o wide
NAME READY STATUS wor NODE
version-1-build 0/1 Completed leas node1.iab.example.com
version-2-d5q9s 4/1 Running aan node1.lab.example.com
version-2-sd478 al/a) Running ae node1.lab.example.com
version-2-xmgwt 1/1 Running an node1.lab.example.com
|
您在输出中可能没有构建Pod, 因为它可能已被删除。 构建Pod可能已经排在 node2 上。
8. 创建一个允许与版本应用程序进行外部通信的路由。 此路由必须使用主机名 version.apps.lab.example.com 访问。
1
2
3
| [student@workstation ~]$ oc expose service version \
--hostname=version.apps.lab.example. com
- route "version" exposed
|
操作界面、表格与配置图
9. 使用curl 命令测试应用程序。
1
2
3
4
5
6
7
8
9
| [student@workstation ~]$ curl http://version.apps.lab.example.com
<htmi>
<head>
<title>PHP Test</title>
</head>
<body>
<p>Version v1</p>
</body>
</html>
|
精确的版本字符串取决于使用同一 Git 仓库的先前练习。
10. 对实验结果评分。
运行以下命令来评分您的工作 :
1
2
| ee Pe a Ca Oe Be i a oo 60 OE
[student@workstation ~]$ lab manage-review grade
|
如果评分不是 PASS,请检查操作后重新运行评分命令。
11. 清理。将 node1 和 node2 的 region 标签恢复为 infra,并将两个节点都设为可调度。
11.1. 恢复 node2 的可调度状态。
从终端窗口运行以下命令 :
1
2
3
4
| [student@workstation ~]$ oc adm manage-node --schedulable=true \
node2.lab.example.com
NAME STATUS AGE
node2.lab.example.com Ready ah
|
1h
11.2. 将两个节点的 region 标签改回 infra:
1
2
3
4
5
6
| [student@workstation ~]$ oc label node node1.lab.example.com \
region=infra --overwrite=true
node "node2.lab.example.com" labeled
[student@workstation ~]$ oc label node node2.lab.example.com \
region=infra --overwrite=true
- node "node2.lab.example.com" labeled
|
11.3. 确认两个节点的标签均已修改:
1
| [student@workstation ~]$ oc get nodes -L region
|
预期输出如下:
1
2
3
| NAME STATUS abel REGION
master.lab.example.com Ready nt
node1.ilab.example.com Ready viet infra
|
操作界面、表格与配置图
1
| nodez.iabd.example.com Ready eee intra
|
11.4. 删除 manage-review 项目。
1
2
3
| [student@workstation ~]$ oc delete project manage-review
- project "manage-review" deleted
|
|
实验到此结束
操作界面、表格与配置图
自测问题#
C8 安装指标子系统#
8.1 指标链路#
Heapster 采集节点和容器资源指标;Hawkular Metrics 提供指标服务;Cassandra 保存指标。三者安装于 openshift-infra。应用日志不是该链路中的指标数据。
8.2 存储和访问#
Cassandra 使用 emptyDir 时,Pod 被替换会丢失该卷数据。持久模式需要 PVC 与可用 PV。Route 的 TLS 信任、Hawkular 鉴权和 Console 配置均影响图表展示。
安装后先检查 PVC Bound、Pod Ready、Endpoints 和 Route,再查看 oc adm top。查询为空时,检查采集时间、目标项目和服务日志;不能直接解释为资源用量为零。
实验操作#
引导练习:安装指标子系统#
子系统#
在此练习中,安装和配置 OpenShift 度指标子系统,并核实它是否正常运行。
文件:/home/student/DO280/labs/install-metrics (workstation VM)
应用地址:https://master.lab.example.com https://hawkular-metrics.apps.lab.example.com http://hello-load.apps.lab.example.com/
学习目标#
应能使用 OpenShift 安装器的 Playbook,为现有 Red Hat OpenShift Container Platform 集群安装指标监控子系统。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
- [student@workstation do280-ansible]$ ./install.sh
|
打开 workstation VM 上的终端窗口, 并运行以下命令下载此练习期间使用的文件 :
1
| [student@workstation ~]$ lab install-metrics setup
|
操作步骤#
确认私有镜像仓库包含指标监控子系统所需的以下镜像:
metrics-cassandra
.metrics-hawkular-metrics
metrics-heapster
使用 docker-registry-cli 工具搜索这些镜像 :
1
2
3
4
| [student@workstation ~]$ docker-registry-cli registry.lab.example.com \
search metrics-cassandra ssl
available options: -
1) Name: openshift3/ose-metrics-cassandra
|
操作界面、表格与配置图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| bags: vs.9
1 images found !
[student@workstation ~]$ docker-registry-cli registry.lab.example.com \
search metrics-hawkular-metrics ssl
available options:-
1) Name: openshift3/ose-metrics-hawkular-metrics
Tags: v3.9
1 images found !
[student@workstation ~]$ docker-registry-cli registry.lab.example.com \
search metrics-heapster ssl
available options:-
1) Name: openshift3/ose-metrics-heapster
Tags: v3.9
1 images found !
|
搜索结果中,所有镜像名都以 openshift3/ose- 开头,标签为 v3.9。安装指标子系统的 Playbook 需要这些信息。
2. Atomic OpenShift 卷回收器镜像 ose-recycler 用于清理释放后的持久卷,供后续复用。在 Docker 镜像仓库中查询该镜像。
1
2
3
4
5
6
| [student@workstation ~]$ docker-registry-cli registry.lab.example.com \
search ose-recycler ssl
available options:-
1) Name: openshift3/ose-recycier
- Tags: v3.9
1 images found !
|
3. 查看 services 虚拟机的 NFS 配置。
3.1. 高级安装器已在 services 虚拟机配置 NFS 共享。使用 ssh 连接 services,查看 NFS 配置。
审查 /exports/metrics 文件夹和 /etc/exports.d/openshift-ansible.exports 配置文件的权限。
1
2
3
4
5
6
7
8
9
10
11
12
| [student@workstation ~]$ ssh root@services
[root@services ~]# ls -alZ /exports/metrics/
drwxrwxrwx. nfsnobody nfsnobody unconfined_u:object_r:default_t:so .
drwxr-xr-x. root root unconfined_u:object_r:default_t:so .
[root@services ~]# cat /etc/exports.d/openshift-ansible.exports
"/exports/registry" *(rw, root_squash)
"/exports/metrics" *(rw, root_squash)
"/exports/logging-es" *(rw,root_squash)
"/exports/logging-es-ops" *(rw, root_squash)
"/exports/etcd-vol2" *(rw, root_squash,sync, no_wdelay)
"/exports/prometheus" *({rw, root_squash)
"/exports/prometheus-alertmanager" *(rw, root_squash)
|
操作界面、表格与配置图
1
| ~fexports/prometneus-aiertourrer" "(rw, root_squasn)
|
3.2. 退出服务器
1
| [root@services ~]# exit
|
为 NFS 共享创建持久卷(PV)定义文件。
4.1. 使用以下参数,让 PV 使用安装器创建的 NFS 共享,并预留给 Cassandra Pod 使用。指标 Playbook 创建的 PVC 名为 metrics-1。
capacity.storage
5 GiB
access Modes
Read write once
nfs.path
/exports/metrics
nfs.server
services.lab.example.com
persistent Volumeclaim Policy
Recycle(在生产集群中,许多系统管理员更倾向于使用 Retain)
workstation 的 /home/student/DO280/labs/install-metrics/metrics-pv.yml 已提供可用的 PV 定义。完整内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
| apiVersion: v1
kind: PersistentVolume
metadata:
name: metrics
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
nfs:
path: /exports/metrics
server: services.lab.example.com
persistentVolumeReclaimPolicy: Recycle
|
4.2. 以集群管理员身份登录, 并使用此练习实验文件夹中提供的 YAML 文件, 运行以下两个命令, 创建持久卷。 如果出现提示,接受不安全连接。
1
2
3
| [student@workstation ~]$ oc login -u admin -p redhat \
https://master .lab.example.com
[student@workstation ~]$ oc create -f DO280/labs/install-metrics/metrics-pv.yml
|
操作界面、表格与配置图
1
| persistentvolume "metrics" created
|
4.3. 确认持久卷已经创建,并处于可供 PVC 绑定的状态。
1
2
3
4
5
| [student@workstation ~]$ oc get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS
... Output omitted ...
metrics 5Gi Rwo Recycle Available
eereoo sl
|
操作界面、表格与配置图
5. 使用 Red Hat OpenShift Container Platform 安装器提供的 Playbook,安装指标监控子系统。
5.1. 确定要传给指标安装 Playbook 的 Ansible 变量。
以下Ansible变量需要定制,以适应在课堂环境中安装的集群:
openshift_metrics_image_prefix
指向 services 虚拟机上的私有镜像仓库,并包含镜像名前缀 openshift3/ose-。
openshift_metrics_image_version
要使用的容器镜像标记。 私有镜像仓库向镜像提供v3.9的标记。
openshift_metrics_heapster_requests_memory
300MB 足以适应这种环境。
openshift_metrics_hawkular_requests_memory
750 MB 足以适应这种环境。
openshift_metrics_cassandra_requests_memory
750MB 足以适应这种环境。
openshift_metrics_cassandra_storage_type
使用 pv 选择一个持久卷作为存储类型。
openshift_metrics_cassandra_pvc_size
5 GiB 足以适应这种环境。
openshift_metrics_cassandra_pvc_prefix
使用 metrics 作为持久卷声明名称的前缀。
需要调整各指标组件 Pod 的内存请求。默认值超过课堂环境的容量;不调整会导致 Pod 无法启动。
更多说明见 OpenShift Container Platform 3.9《安装与配置指南》的 Enabling Cluster Metrics 章节,其中的 Ansible Variables 表提供变量定义。
5.2. 进入 /home/student/DO280/labs/install-metrics,打开该目录的 inventory,添加部署指标监控子系统所需的变量。
使用文本编辑器打开 inventory,在 [OSEv3:vars] 组中追加以下变量:
操作界面、表格与配置图
需要时,可将 install-metrics 目录的 metrics-vars.txt 内容复制到 [OSEv3:vars] 组。
1
2
3
4
5
6
7
8
9
10
11
12
| [OSEv3:vars]
... Output omitted .,.
# Metrics Variables
openshift_metrics_install_metrics=True
openshift_metrics_image_prefix=registry.lab.example.com/openshift3/oase-
openshift_metrics_image_version=v3.9
openshift_metrics_heapster_requests_memory=300M
openshift_metrics_hawkular_requests_memory=750M
openshift_metrics_cassandra_requests_memory=750M
openshift_metrics_cassandra_storage_type=pv
openshift_metrics_cassandra_pvc_size=5Gi
openshift_metrics_cassandra_pvc_prefix=metrics
|
5.3. 检查清单文件错误。 为了防止由于清单文件中的错误而重复的冗长安装, 请提供库存检查器。 要检查没有错误, 请在 workstation VM 上运行以下的评分脚本 :
1
| [student@workstation install-metrics]$ lab install-metrics grade
|
如果您的清单文件有效, 评分脚本应该通过, 您应该看到以下输出 :
1
| OVeTarlSanVvencOny! iwe CHECK bck.) an aiih ei send: teins bbe ceeBASS
|
如果您的清单文件有错误且评分脚本失败, 您可以比较您的清单文件与位于 /home/student/DO280/solutions/install-metrics/inventory 的有效回答文件。 校正错误并重新运行评分脚本以确保它通过。
5.4. 使用 ansible-playbook 执行 /usr/share/ansible/openshift-ansible/playbooks/openshift-metrics/config.yml,部署指标子系统。Ansible 使用当前目录的 ansible.cfg 完成权限提升,并读取当前目录的 inventory。
1
2
3
4
5
6
| [student@workstation install-metrics]$ ansible-playbook \
/usr/share/ansible/openshift-ansible/playbooks/openshift -metrics/config.yml
Bray RECAP RRKERRKKKKEKKK KKKKERRKK KKKKKKKKKKEKKKKKKKEKKKKKEKKKKKEKKRKKKKKKEKRKEKKKKEKEEE
localhost : ok=10 changed=0 unreachable=0 failed=0
master .lab.example.com : ok=196 changed=48 unreachable=0 failed=0
node1.lab.example.com : ok=1 changed=0 unreachable=0 failed=0
|
操作界面、表格与配置图
1
| node¢.iabd.example.com » OK=1 cnanged=0 unreacnabie=-0 Tailea=0 |
|
仔细观察 Ansible 输出。如果 FAILED - RETRYING 后紧接着出现 ok,可以忽略这类重试消息。例如:
1
2
3
4
5
6
| RUNNING HANDLER [openshift_metrics : Verify API Server] *************xRRRKEKERRE
FAILED - RETRYING: HANDLER: openshift_metrics : Verify API Server (120 retries
left).
FAILED - RETRYING: HANDLER: openshift_metrics : Verify API Server (119 retries
left).
ok: [master.lab.example.com]
|
在 PLAY REc Ap 消息后, 提醒这些 FAILED 信件不应该增加在Playbook执行结束时的失败计数。 验证所有主机的失败计数为零, 然后继续下一步。
成功安装应显示以下输出:
1
2
3
| INSTALLER STATUS KEKE KKKKKKKKH KEKE KKK KK KEKEKKK KEK KKKKEKEKEKEK KKK KKKKKKK KKK
Initialization : Complete (0:00:14)
Metrics Install : Complete (0:01:43)
|
本步骤或后续步骤若出错,运行 /home/student/DO280/labs/install-metrics/uninstall-metrics.sh 卸载指标子系统。脚本结束后,按步骤 4.2 重新创建持久卷。
验证度指标子系统是否已成功部署。
6.1. 确认指标 Playbook 创建的 PVC 已绑定。以 admin 用户列出 openshift-infra 项目的持久卷声明。
1
2
3
| [student@workstation install-metrics]$ oc get pvc -n openshift-infra
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
metrics-1 Bound metrics 5Gi RWO 20m
|
请注意, 拥有 PvC 约束并不表示 PV 定义是正确的。 只有当 pod 试图挂载卷时, 才会实际使用 Py 属性, 因此 Pv 定义中的任何错误都会在 Cassandra 启动中造成错误。
6.2. 等待所有指标组件 Pod 就绪并运行,READY 列应为 1/1。这可能需要几分钟。
1
2
3
| [student@workstation install-metrics]$ oc get pod -n openshift-infra
NAME READY STATUS oo5 o
hawkular-cassandra-1-kdhgr 1/1 Running Lets
|
292
操作界面、表格与配置图
1
2
| Nawkuiar-metrics-Ojmkm 1/1 Running eee
heapster-43p13 aval Running ae
|
请注意,与上一个输出相比,您的集群可能会安排每个Pod在不同的节点上运行。
如有异常,可对部署配置和 Pod 执行 oc describe 获取诊断信息,对指标组件及对应的部署器 Pod 执行 oc logs。通常需要修正 inventory 中的 Ansible 变量,再按上一步 Important 提示卸载重装。
7. 在网络浏览器中打开 Hawkular 主页以接受 自签名 证书。
7.1. 找到 Hawkular Pod的路由主机名 :
1
2
3
| [student@workstation install-metrics]$ oc get route -n openshift-infra
NAME HOST/PORT site
hawkular-metrics hawkular-metrics.apps.lab.example.com ef o
|
7.2. 在 workstation VM 上打开一个网络浏览器, 打开 Hawkular URL。 确保使用 HTTPS 协议。
https://hawkular-metrics.apps.lab.example.com
按信任程度接受 SsL 证书。 网页只显示 Hawkular 吉祥物和发布号。
您需要为访问 OpenShift 网络控制台所使用的所有网络浏览器这样做。
8. 在 OpenShift 集群中安装一个测试应用程序以生成某些负荷。
8.1. 在 workstation 打开另一个终端,进入 student 的主目录,以 developer 用户登录。创建新项目,并使用 hello-openshift 镜像创建 Hello World 应用:
1
2
3
4
5
6
| [student@workstation install-metrics]$ cd
[student@workstation ~]$ oc login -u developer -p redhat \
https://master.lab.example.com
[student@workstation ~]$ oc new-project load
[student@workstation ~]$ oc new-app --name=hello \
--docker -image=registry.lab.example.com/openshift/hello-openshift
|
8.2. 缩放应用程序, 等待所有九个Pod都准备好并运行 :
1
2
3
4
5
| [student@workstation ~]$ oc scale --replicas=9 dc/hello
deploymentconfig "hello" scaled
[student@workstation ~]$ oc get pod -o wide
NAME READY STATUS g 40 IP NODE
hello-1-0h950 1/1 Running k. 10.130.0.17 node1.lab.example.com |
|
IP NODE
1
2
| IA ROAVT VIATVoO eee ar NOV
hello-1-0h950 1/1 Running a ap 10.130.0.17 node1.lab.example.com
|
操作界面、表格与配置图
1
| nello-1-dasoKn i/ai RUNALNG wae 1U.1d429.0.12 noaez,tabd.example.com
|
8.3. 向外部公开应用:
1
2
| [student@workstation ~]$ oc expose svc/hello
route "hello" exposed
|
8.4. 如果 workstation 尚未安装 httpd-tools,先安装该软件包:
1
| [student@workstation ~]$ sudo yum install httpd-tools
|
8.5. 使用 Apache Bench 产生负载。URL 末尾必须带正斜杠:
1
2
3
4
| [student@workstation ~]$ ab -n 300000 -c 20 \
http://hello-load.apps.lab.example.com/
Benchmarking hello-load.apps.lab.example.com (be patient)
Completed 5000 requests
|
Apache Bench 运行期间继续下一步。
9. 在 workstation 打开另一个终端,以集群管理员登录,使用 oc adm top 从 Heapster 获取当前指标:
1
2
3
4
5
6
7
8
| [student@workstation ~]$ oc login -u admin -p redhat
[student@workstation ~]$ oc adm top node \
--heapster-namespace=openshift-infra \
--heapster-scheme=https
NAME CPU(cores) CPU% MEMORY(bytes) | MEMORY%
master.lab.example.com 164m 8% 1276Mi 73%
node1.lab.example.com 1276m 63% 2382Mi 30%
node2.lab.exampie.com 629m 31% 2167Mi 28%
|
8% 1276Mi 73% master.lab.example.com
1
2
3
| ee eeee 2 a i
node1.lab.example.com 1276m 63% 2382Mi 30%
node2. lab.exampie.com 629m 31% 2167Mi 28%
|
31% 2167Mi 28%
您看到的值可能与此示例不同。
10. 使用curl 命令从 Heapster Pod获取当前测量值。
10.1. 在 workstation 打开另一个终端,查看 node-metrics.sh。脚本通过 Heapster API 获取节点当前的内存和 CPU 使用指标。
运行以下命令以检查脚本内容:
1
2
3
4
5
6
7
| [student@workstation ~]$ cat ~/DO280/labs/install-metrics/node-metrics.sh
#!/bin/bash
oc login -u admin -p redhat
TOKEN=$(oc whoami -t)
APIPROXY=https://master.lab.example.com/api/vi/proxy/namespaces/openshift-infra/
services
HEAPSTER=https:heapster :/api/vi/model
|
操作界面、表格与配置图
1
2
3
4
5
6
| NODE=nodes/node1.ilab.example.com
START=$(date -d '1 minute ago' -u '+%FT%TZ')
curl -kH "Authorization: Bearer $TOKEN" \
-X GET $APIPROXY/SHEAPSTER/$NODE/metrics/memory/working_set?start=$START
curl -kH "Authorization: Bearer $TOKEN" \
-X GET $SAPIPROXY/$HEAPSTER/$NODE/met rics/cpu/usage_rate?start=$START
|
脚本获取 working_set 内存指标(单位为字节)和 CPU usage_rate 指标(单位为 millicores,即千分之一 CPU 核)。显示的是最近一分钟的测量结果。
10.2. 运行脚本,查看当前节点指标。
您的值会与输出值不同。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| [student@workstation ~]$ ./DO280/labs/install-metrics/node-metrics.sh
{
"metrics": [
{
"timestamp": "2018-08-09T02:53:30Z",
"value": 1856569344 @
},
{
"timestamp": "2018-@8-09T02:54:002",
"value": 1860599808 O
}
1,
"latestTimestamp": "2018-08-09T02:54:00Z"
Be
"metrics": [
{
"timestamp": "2018-08-09T02:53:30Z",
"value": 499 3)
Po
{
"timestamp": "2018-08-09T02:54:00Z",
"value": 1213
}
"latestTimestamp": "2018-08-09T02:54:00Z"
|
操作界面、表格与配置图
抽样输出显示:
示例输出显示:实际内存用量(working_set)约 1.7 GiB;一处 CPU 用量(usage_rate)约半个核,另一处约 1.2 个核。
如果脚本不工作, 最有可能的原因是 Heapster pod 尚未准备好并运行。 您可能需要卸载指标监控子系统, 修复您的安装脚本, 并重新安装。
操作界面、表格与配置图
11. 查看 Hawkular 中的项目量度, 使用 OpenShift 网络控制台。
11.1. 打开浏览器,访问 https://master.lab.example.com,以 developer 用户登录,密码为 redhat。
11.2. 进入 load 项目的概览页,展开 hello, #1 部署配置。下图显示多个 Pod 的指标:
DEPLOYMENT CONFIG
1
2
| DEPLOYMENTCO
hello,#1
|
5.4
Mib Memory
hello
Cores Cru Image: openshift/hello-openshit aaea76f 2.1 MiB
0.1 pods Ports: 8080/TCP and 1 other
Kib/s Network
Average Usage ir i51m
Servlce - Internal Traffic Rcutes - External Traffic
1
2
| >)eee NGMERE GARETT) heTN
hella http://hello-load.apps.lab.example.com ur
|
B080/TCP (8080-tcp) → 8080 and 1 other Route hello, target port 80so-tcp
图 8.2:指标概览
如果您在网络浏览器中看不到图形, 最有可能的原因是:
浏览器窗口太窄。 调整窗口大小和图形应该可见。
:您在登录到网络控制台之前没有访问 Hawkular 主页。 请按 7 步骤中所解释的那样这样做, 并刷新网站控制台页面。
Hawkular pod 尚未准备就绪并运行。 您可能需要卸载指标监控子系统, 更新您的安装脚本, 并重新安装。
11.3. 清理。 删除测试应用项目 :
1
| [student@workstation ~]$ oc delete project load
|
本引导练习到此结束。
操作界面、表格与配置图
自测问题#
- 图表空白是否表示 CPU 为零? 不是,可能是采集、存储、证书、授权或时间范围问题。
C9 管理和监控集群#
9.1 配额与容器资源#
requests 参与调度;limits 限制运行时资源。CPU 超限通常表现为节流,内存超限可能触发 OOM 终止。
ResourceQuota 限制项目累计请求、限制和对象数。LimitRange 设置单个对象的范围、默认 request 和 limit。配额拒绝发生在准入阶段,与节点调度失败不同。
9.2 健康探针#
readiness 失败移除可用 Endpoints,不直接重启容器。liveness 连续失败达到阈值后由 kubelet 重启容器。启动较慢的程序需要足够 initialDelaySeconds;3.9 示例不使用后续版本的 startupProbe。
HTTP 返回 200–399 可判定成功,TCP 探针检查连接,exec 探针检查退出码 0。探针不应依赖不必要的远程服务,以免依赖故障导致所有应用被反复重启。
9.3 维护与升级#
原地升级先控制平面再节点;分批排空节点需要剩余容量和应用副本。单节点数据库或单个应用节点不具备无中断迁移条件。
备份至少包括 etcd、Master/Node 配置、安装 inventory 和业务持久数据。导出 oc get all 不能替代这些备份。etcd 快照恢复必须匹配实际 etcd 版本及成员拓扑。
3.9 的升级 Playbook 位于 playbooks/common/openshift-cluster/upgrades/v3_9/。升级路径、目标 RPM 和镜像版本必须一致;不能直接将 3.9 安装 inventory 用于任意跨版本升级。
9.4 分阶段升级命令#
以下只用于已确认能够升级到 3.9、已完成匹配版本备份的独立集群。安装控制端已切换到配套 3.9 软件源与镜像标签,保存完整 inventory,业务数据已备份。单应用节点不能承诺升级期间业务连续。
1
2
3
4
5
6
7
8
9
10
11
| source ~/do280-notes/functions.sh
cd "$WORK"
mkdir -p pre-upgrade
cp -p inventory ansible.cfg pre-upgrade/
oc version > pre-upgrade/versions.txt
oc get nodes -o yaml > pre-upgrade/nodes.yaml
oc get pv -o yaml > pre-upgrade/pv.yaml
oc get pods --all-namespaces -o wide > pre-upgrade/pods.txt
ssh "$MASTER" 'sudo tar czf /var/tmp/notes-master-config.tgz /etc/origin/master /etc/etcd'
ssh "$MASTER" sudo cat /var/tmp/notes-master-config.tgz > pre-upgrade/notes-master-config.tgz
chmod 600 pre-upgrade/*
|
配置归档包含敏感信息;etcd 的数据快照还必须单独完成,/etc/etcd 只是配置与证书。以下使用 etcd v3 API,从实际 etcd 主机执行,证书路径按该主机 ETCD_* 配置输入,不猜测证书文件名。
1
2
3
4
5
6
7
8
9
10
11
12
13
| sudo grep -E 'ETCD_(LISTEN_CLIENT_URLS|ADVERTISE_CLIENT_URLS|TRUSTED_CA_FILE|CERT_FILE|KEY_FILE)' /etc/etcd/etcd.conf
sudo etcdctl --version
read -r -p 'etcd 客户端 HTTPS endpoint: ' ETCD_ENDPOINT
read -r -p 'etcd CA 绝对路径: ' ETCD_CA
read -r -p '有访问权限的 etcd 客户端证书绝对路径: ' ETCD_CERT
read -r -p '对应客户端私钥绝对路径: ' ETCD_KEY
sudo mkdir -p /var/backups/notes-etcd
sudo env ETCDCTL_API=3 etcdctl --endpoints="$ETCD_ENDPOINT" \
--cacert="$ETCD_CA" --cert="$ETCD_CERT" --key="$ETCD_KEY" endpoint health
sudo env ETCDCTL_API=3 etcdctl --endpoints="$ETCD_ENDPOINT" \
--cacert="$ETCD_CA" --cert="$ETCD_CERT" --key="$ETCD_KEY" \
snapshot save /var/backups/notes-etcd/pre-upgrade.db
sudo env ETCDCTL_API=3 etcdctl snapshot status /var/backups/notes-etcd/pre-upgrade.db -w table
|
仅当实际 etcd 采用 v3 数据格式时使用上述快照。存在 v2 数据时需匹配其备份方式,不能把一个 v3 快照当成全部数据。快照应复制到集群外,并完成隔离环境恢复验证后才进行升级。
返回工作站,先控制平面,再逐个应用节点:
1
2
3
4
5
6
7
8
9
10
11
12
13
| source ~/do280-notes/functions.sh
cd "$WORK"
ansible-playbook -i inventory \
/usr/share/ansible/openshift-ansible/playbooks/common/openshift-cluster/upgrades/v3_9/upgrade_control_plane.yml
oc get nodes
oc get pods -n default
ansible-playbook -i inventory \
/usr/share/ansible/openshift-ansible/playbooks/common/openshift-cluster/upgrades/v3_9/upgrade_nodes.yml \
-e openshift_upgrade_nodes_serial=1
oc version
oc get nodes -o wide
oc get pods --all-namespaces
oc get events --all-namespaces --sort-by=.metadata.creationTimestamp
|
验收还要执行原应用的 HTTP 请求与数据库读写,检查 Router、Registry、指标系统和持久数据。失败时保留日志及快照,不能用旧 RPM 覆盖当作完整回滚。
实验操作#
引导练习:限制资源用量#
引导演练:限制资源使用#
本实验用配额限制项目的资源用量,并观察不设置资源配额对 Pod 调度的影响。
学习目标#
应能为项目定义资源配额和限制范围,排查违反配额的错误,以及资源请求不受配额约束造成的调度问题。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
打开 workstation VM 上的终端窗口, 并运行以下命令下载此练习中使用的文件 :
1
| [student@workstation ~]$ lab monitor-limit setup
|
操作步骤#
1. 以集群管理员身份创建项目,确认新 Pod 默认没有资源请求。
为节省时间,本步骤命令已放在 /home/student/DO280/labs/monitor-limit/create-project.sh,可以复制使用。
1.1. 在 workstation 以 admin 用户登录 OpenShift:
1
2
3
4
5
6
7
8
9
| [student@workstation ~]$ oc login -u admin -p redhat \
https://master.lab.example.com
1.2. Display the allocated resources for the two nodes in the OpenShift cluster:
[student@workstation ~]$ oc describe node node1.lab.example.com \
grep -A 4 Allocated
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
CPU Requests CPU Limits Memory Requests Memory Limits
200m (10%) @ (0%) 512Mi (6%) @ (0%)
|
0 (0%)
1
2
3
| [student@workstation ~]$ oc describe node node2.lah.example.com \
grep -A 4 Allocated
Allocated resources:
|
操作界面、表格与配置图
1
2
3
| (rotai 1timits may be over 100 percent, 1.€., overcommitted.)
CPU Requests CPU Limits Memory Requests Memory Limits
200m (10%) @ (0%) 512Mi (6%) @ (0%)
|
0 (0%) 200m (10%) 0 (0%) 512Mi (6%)
您看到的值可能不同。 请记住, 此练习是关于数值变化的方式, 而不是初始值。 请注意每个节点的 CPu 请求 列中的值。
1.3. 创建 resources 项目:
1
| [student@workstation ~]$ oc new-project resources
|
1.4. 从 hello-openshift 镜像创建 Pod:
1
2
| [student@workstation ~]$ oc new-app --name=hello \
--docker -image=registry.lab.example.com/openshift/hello-apenshift
|
1.5. 等待 hello Pod 就绪并运行,查看它所在的节点。
1
2
3
| [student@workstation ~]$ oc get pod -o wide
NAME READY 1.» NODE
hello-1-wbt9n 1/1 ... node1.lab.example.com
|
您可能需要重复上一个命令几次, 直到Pod准备好并运行。
请记下命令报告的节点名称。 下一步需要这些信息。
1.6. 确保节点分配的资源没有变化。 只检查运行 hello pod 的节点, 使用前一步的输出 :
1
2
3
4
5
6
| [student@workstation ~]$ oc describe node node1.lab.example.com \
grep -A 4 Allocated
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
CPU Requests CPU Limits Memory Requests Memory Limits
q
|
操作界面、表格与配置图
200m (10%) 0 (0%) 512Mi(6%) 0 (0%)
也可对 Pod 和部署配置执行 oc describe,确认没有指定资源请求。
1.7. 进入下一步之前,删除 resources 项目内的所有资源,包括部署配置、镜像流、Pod 和 Service。
1
| [student@workstation ~]$ oc delete all -1 app=hello
|
2. 作为集群管理者,在项目中增加配额和限制范围,为项目中的Pod提供默认资源请求。
本步骤命令已放在 /home/student/DO280/labs/monitor-limit/add-quota.sh。
2.1. 检查限制范围定义,该定义只对 CPU 指定默认资源请求。
1
2
3
4
5
6
7
8
9
10
| [student@workstation ~]$ cat ~/DO280/labs/monitor-limit/limits.yml
apiVersion: "v1"
kind: "LimitRange"
metadata:
name: "project-limits"
spec:
limits:
- type: "Container"
default:
cpu: "250m"
|
若从头编写文件,可参考 OpenShift Container Platform 3.9 文档的示例。《集群管理》文档的 Setting Limit Ranges 章节介绍限制范围的使用方法。
2.2. 添加项目限制范围 :
1
2
| [student@workstation ~]$ oc create -f ~/DO280/labs/monitor-limit/limits.yml
limitrange "project-limits" created
|
2.3. 确保为项目设定默认的 CPU 资源请求 :
1
2
3
4
| [student@workstation ~]$ oc describe limits
Name: project-limits
Namespace: resources
Type Resource Min Max Default Request Default Limit
|
操作界面、表格与配置图
1
| Container cpu % © 200m z2oum
|
通知, 设定默认资源请求也会为计算资源设定默认资源限制。
2.4. 检查所提供的配额定义,确定CPU的最大使用量。
1
2
3
4
5
6
7
8
| [student@workstation ~]$ cat ~/DO280/labs/monitor-limit/quota.yml
apiVersion: v1
kind: ResourceQuota
metadata:
name: project-quota
spec:
hard:
cpu: "900m"
|
若从头编写文件,可参考 OpenShift Container Platform 3.9 文档的示例。《集群管理》文档的 Setting Quotas 章节介绍配额的使用方法。
2.5. 为项目增加配额:
1
2
| [student@workstation ~]$ oc create -f ~/DO280/labs/monitor-limit/quota.yml
resourcequota "project-quota" created
|
2.6. 确保为该项目规定CPU配额:
1
2
3
4
5
| [student@workstation ~]$ oc describe quota
- Name: project-quota
Namespace: resources
- Resource Used Hard
cpu 0 900m
|
2.7. 进入下一步前,授予 developer 用户在该项目创建部署的权限:
1
2
| [student@workstation ~]$ oc adm policy add-role-to-user edit developer
role "edit" added: "developer"
|
3. 以 developer 用户在项目内创建 Pod,确认 Pod 消耗项目配额。
为了节省时间,此步骤的所有命令都在 /home/student/DO280/labs/monitor-limit 目录中的 add-pod .sh 脚本中。
3.1. 以 developer 用户登录 OpenShift:
1
| [student@workstation ~]$ oc login -u developer -p redhat \
|
操作界面、表格与配置图
https://master.lab.example.com
3.2. 切换到 resources 项目:
1
| [student@workstation ~]$ oc project resources
|
3.3. 确认 developer 用户可以查看项目的资源限制和配额,但不能修改:
1
2
3
| [student@workstation ~]$ oc get limits
NAME AGE
project-limits 4m
|
4m
1
2
3
4
5
6
7
8
| Ye See Si hs
[student@workstation ~]$ oc delete limits project-limits
Error from server (Forbidden): limitranges "project-limits" is forbidden: \
User "developer" cannot delete limitranges in the namespace "resources": \
User "developer" cannot delete limitranges in project "resources"
[student@workstation ~]$ oc get quota
NAME AGE
project-quota im
|
1m
1
2
3
4
5
6
7
| [student@workstation ~]$ oc get quota
NAME AGE
/ project-quota im
[student@workstation ~]$ oc delete quota project-quota
Error from server (Forbidden): resourcequotas "project-quota" is forbidden: \
User "developer" cannot delete resourcequotas in the namespace "resources": \
User "developer" cannot delete resourcequotas in project "resources"
|
如果这两个 oc delete 命令都没有报错,说明仍以 admin 登录。需要返回步骤 2,重新创建项目的限制范围和资源配额。
也可使用 oc get limitranges,输出相同。
3.4. 从 hello-openshift 镜像创建 Pod:
1
2
| [student@workstation ~]$ oc new-app --name=hello \
--docker-image=registry.lab.example.com/openshift/hello-openshift
|
如果您有错误, 请确保您删除了此操作开始时创建的 pod 及其相关资源。
3.5. 等待 hello Pod 就绪并运行:
1
2
| [student@workstation ~]$ oc get pod
NAME READY STATUS RESTARTS AGE
|
操作界面、表格与配置图
1/1 Running 1d he1lo-1-k9h8k
您可能需要重复上一个命令几次, 直到Pod准备好并运行。
3.6. 校验Hello pod是否消耗部分项目配额:
1
2
3
4
5
| [student@workstation ~]$ oc describe quota
Name: project-quota
Namespace: resources
Resource Used Hard
cpu 250m 900m
|
将此输出与第2.6步的输出比较
尽管这个Pod闲置,没有满足任何HTTP的要求,因此实际上没有使用任何CPU, 但其资源要求使得它从项目的资源配额中消耗。
4. 可选: 检查节点可用的资源是否更少。
本步骤命令位于 /home/student/DO280/labs/monitor-limit/check-nodes.sh,可用于节省输入时间。
4.1. 以 admin 用户登录 OpenShift:
1
2
| [student@workstation ~]$ oc login -u admin -p redhat \
https://master.lab.example.com
|
4.2. 查看 hello Pod 所在的节点。
1
2
3
| [student@workstation ~]$ oc get pod -o wide -n resources
NAME READY ... NODE
hello-1-454cc 1/1 ... node1.lab.example.com
|
在下一步使用该名称时, 给节点名称留个笔记。
4.3. 确保所分配的 CPU 节点请求量增加与 pod 资源请求量相同。 使用前一步的输出, 只检查运行 hello pod 的节点。
1
2
3
4
5
6
| [student@workstation ~]$ oc describe node node1.lab.example.com \
grep -A 4 Allocated
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
CPU Requests CPU Limits Memory Requests Memory Limits
SS ee ee eee ea a ee See 2
|
操作界面、表格与配置图
512Mi (6%) 450m(22%) 250m(12%) 0 (0%)
4.4. 对 Pod 执行 oc describe,检查其 CPU 资源请求。
1
2
3
4
| [student@workstation ~]$ oc describe pod hello-1-k9h8k | grep -A2 Requests
Requests:
cpu: 250m
Environment: <none>
|
4.5. 进入下一步前,切换回 developer 用户:
1
2
| [student@workstation ~]$ oc login -u developer -p redhat \
https://master.lab.example.com
|
5. 扩展 hello 部署配置,增加项目内 Pod 的资源用量,观察其对项目配额的消耗。
本步骤命令位于 /home/student/DO280/labs/monitor-limit/increase-bounded.sh。
5.1. 将 hello 部署配置扩展到两个副本。
1
| [student@workstation ~]$ oc scale dc hello --replicas=2
|
5.2. 等待新Pod准备好并运行 :
1
2
3
4
| [student@workstation ~]$ oc get pod
NAME READY STATUS RESTARTS AGE
hello-1-454cc 1/i Running 0 6m
hello-1-99rkb 1/1 Running 06 6s
|
6m
1
2
3
4
| [student@workstation ~]$ oc get pod
NAME READY STATUS RESTARTS AGE
hello-1-454cc 1/i Running 0 6m
hello-1-99rkb 1/1 Running 06 6s
|
您可能需要重复上一个命令几次, 直到Pod准备好并运行。
5.3. 请检查新Pod符合项目配额 :
1
2
3
4
5
| [student@workstation ~]$ oc describe quota
Name: project-quota
Namespace: resources
Resource Used Hard
cpu 500m 900m
|
5.4. 将 hello 部署配置扩展到四个副本,这会超过项目配额:
1
2
| [student@workstation ~]$ oc scale dc hello --replicas=4
deploymentconfig "hello" scaled
|
5.5. 等待几分钟,确认第三个 hello Pod 已创建,但第四个始终无法创建。第四个会导致 CPU 请求总量
操作界面、表格与配置图
达到 1000 millicores,而配额上限为 900 millicores:
1
2
3
4
5
| [student@workstation ~]$ oc get pod
NAME READY STATUS RESTARTS AGE
hello-1-454cc al Running ic] 7m
Mellor eye) § al/al Running (0) im
hello-1-t2vnm 1/1 Running 0 41s
|
7m
1
2
3
4
5
| [student@workstation ~]$ oc get pod
NAME READY STATUS RESTARTS AGE
hello-1-454cc al Running ic] 7m
Mellor eye) § al/al Running (0) im
hello-1-t2vnm 1/1 Running 0 41s
|
命令本身不显示错误。部署配置会持续等待,直到配置变化允许创建第四个 Pod。
1
2
3
| [student@workstation ~]$ oc describe dc helio | grep Replicas
Replicas: 4
Replicas: 3 current / 4 desired
|
5.6. 获取项目事件列表。 它显示复制控制器无法创建第四个Pod, 因为违反配额 :
1
2
3
4
| [student@workstation ~]$ oc get events | grep -i error
.., output omitted ...
... Error creating: pods "hello-1-4s45n" is forbidden: exceeded quota: project-
quota, requested: cpu=250m, used: cpu=750m, limited: cpu=900m
|
5.7. 将 hello 部署配置缩放到一个副本后再切换到下一步 :
1
| [student@workstation ~]$ oc scale de hello --replicas=1
|
5.8. 稍候,确认只有一个 hello Pod 正在运行。
1
2
3
| [student@workstation ~]$ oc get pod
NAME READY STATUS RESTARTS AGE
hello-1-454cc 1/1 Running (0) 20mm
|
6. 为 hello Pod 添加一种不受当前项目配额约束的资源请求。
要保存时间打字, 此步骤的所有命令都在 /home/student/DO280/labs/monitor-limit 文件夹的 increase-unbounded.sh 脚本中。
6.1. 为 hello 部署配置添加 256 MiB 的内存请求。
1
2
| [student@workstation ~]$ oc set resources dc hello --requests=memory=256Mi
deploymentconfig "hello" resource requirements updated
|
6.2. 等待新Pod已准备好并运行, 旧Pod已被删除 :
1
2
| [student@workstation ~]$ oc get pod
NAME READY STATUS RESTARTS AGE
|
操作界面、表格与配置图
hello-2-d1grk 1/1 Running 1m
您可能需要重复上一个命令几次, 直到新Pod准备好并运行, 旧Pod消失。
6.3. 请检查新Pod除 CPU 请求外是否有内存请求 :
1
2
| [student@workstation ~]$ oc describe pod hello-2-digrk | grep -A 3 Requests
Daniinetknee
|
Requests:
cpu: 250m
memory: 256Mi
Environment
6.4. 检查内存请求未计入项目配额:
1
| [student@workstation ~]$ oc describe quota
|
Name: project-quota
Namespace: resources
Resource Used Hard
cpu 250m 900m
从项目配额的角度看,没有任何变化。
7. 将内存请求调高到超过集群任一节点容量的值。
本步骤命令位于 /home/student/DO280/labs/monitor-limit/increase-toomuch.sh。
7.1. 将 Hello 部署配置的内存请求更改为 8 GiB。 容量更大的节点只有 4 GiB 的内存。
1
2
3
| \ -
[student@workstation ~]$ oc set resources dc hello --requests=memory=8Gi
deploymentconfig "hello" resource requirements updated
|
7.2. 确认新的部署器 Pod 已创建,但新的应用 Pod 无法运行,停留在 Pending 状态。
1
2
3
4
5
| [student@workstation ~]$ oc get pod
NAME READY STATUS RESTARTS AGE
hello-2-digrk al//at Running 0 13m
hello-3-deploy a/1 Running (0) 18s
hello-3-f3b21 0/1 Pending (c} 11s
|
部署器 Pod 会持续运行,等待配置变化使新的应用 Pod 能够就绪。期间不会删除上次部署的 hello Pod,以避免用户服务中断。
不要等待显示以下错误。 继续完成此练习 :
1
2
| [student@workstation ~]$ oc get pod
NAME READY STATUS RESTARTS AGE
|
操作界面、表格与配置图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| hello-2-digrk 1/1 Running 0 14m
hello-3-deploy 0/1 Error (o] 11m
[student@workstation ~]$ oc logs hello-3-deploy
--> Scaling up hello-3 from © to 1, scaling down hello-2 from 1 to © (keep 1
pods available, don't exceed 2 pods)
Scaling hello-3 up to 1
error: timed out waiting for any update progress to be made
[student@workstation ~]$ oc status
In project resources on server https://master.lab.example.com:443
svc/hello - 172.30.207.52 ports 8080, 8888
dc/hello deploys istag/hello:latest
deployment #3 failed 10 minutes ago: config change
deployment #2 deployed 12 minutes ago - 1 pod
deployment #1 deployed 34 minutes ago
3 infos identified, use 'oc status -v' to see details.
|
7.3. 获取项目事件列表。 它显示一个警告, 警告说由于内存不足, 无法将Pod排到任何节点上 :
1
2
| [student@workstation ~]$ oc get events | grep hello-3.*Failed
a A arPT | eeae oea eeee2
|
9m 15m 26 he11o-3-9nw8n.15495845f8ab7b98Pod
1
2
3
4
5
| [student@workstation ~]$ oc get events | grep hello-3.*Failed
9m 15m 26 hello-3-9nw8n.15495845f8ab7b98 Pod \
Warning FailedScheduling default-scheduler \
@/3 nodes are available: 1 MatchNodeSelector, 3 Insufficient memory.
... output omitted ...
|
清理
以 admin 用户登录,删除本练习创建的项目。
1
2
3
4
| [student@workstation ~]$ oc login -u admin -p redhat \
https: //master.lab.example.com
f{student@workstation ~]$ oc delete project resources
|
|
本引导练习到此结束。
操作界面、表格与配置图
引导练习:使用探针监控应用#
本练习为 OpenShift 上的应用配置就绪探针和存活探针。
应用地址:http://probe.apps.lab.example.com
学习目标#
能够:
创建一个新项目。
:创建一个新应用程序。
为应用创建就绪探针和存活探针。
在你开始之前
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do286-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab probes setup
|
操作步骤#
在 workstation 以 developer 登录 OpenShift,创建 probes 项目。
1.1. 在 workstation 打开新终端,使用 oc login,以 developer 用户登录集群。
1
2
3
4
5
| [student@workstation ~]$ oc login -u developer -p redhat \
https://master .1ab.example.com
Login successful.
You don't have any projects. You can try to create a new project, by running
oc new-project <projectname>
|
操作界面、表格与配置图
1.2. 创建 probes 项目。
1
2
3
4
5
6
| [student@workstation ~]$ oc new-project probes
/ Now using project "probes" on server "https://master.lab.example.com:443".
- You can add applications to this project with the 'new-app' command. For
example, try:
oc new-app centos/ruby-22-centos7~https://github.com/openshift/ruby-ex.git
to build a new example application in Ruby.
|
2. 使用课堂私有镜像仓库的 node-hello 镜像创建 probes 应用,查看部署结果。
2.1. 使用 oc new-app 命令创建新应用程序。使用 node-hello 镜像。
1
2
3
4
5
6
7
8
9
| [student@workstation ~]$ oc new-app --name=probes \
http://services.lab. example .com/node-hello
... output omitted ...
--> Success
Build scheduled, use 'oc logs -f bc/probes' to track its progress.
Application is not exposed. You can expose services to the outside world by -
executing one or more of the commands below:
'oc expose svc/probes'
Run 'oc status' to view your app.
|
2.2. 运行 oc status命令以审查应用程序的部署。
1
2
3
4
5
6
7
8
9
10
11
| [student@workstation ~]$ oc status
In project probes on server https://master.lab.example.com:443
svc/probes - 172.30.67.215:3000
dc/probes deploys istag/probes:latest <-
bc/probes docker builds http://registry.lab.example.com/node-hello on istag/
- nodejs-6-rhel7:latest
build #1 running for 16 seconds - bf7e7e9: Establish remote repository
(root <root@services.lab.example.com>)
deployment #1 pending less than a second ago
3 infos identified, use 'oc status -v' to see details.
a ee we 6'l-EeE Ss Eee
|
2.3. 运行 oc get pods,直到有一个 Pod 处于 Running 状态。
1
2
3
4
| [student@workstation ~]$ oc get pods -w
NAME READY STATUS RESTARTS AGE
probes-1-build 0/1 Completed 0 42s
probes-1-ttzb6 1/1 Running (0) 23s
|
3. 为 Service 创建路由,使外部客户端可以访问应用。
1
2
| [student@workstation ~]$ oc expose svc probes \
--hostname=probe.apps.lab.example.com
|
操作界面、表格与配置图
4. 使用curl 命令访问应用程序。
1
2
| [student@workstation ~]$ curl http://probe.apps.lab.example.com
Hi! I am running on host -> probes-1-ttzb6
|
5. 应用提供 /health 和 /ready 两个 HTTP GET 地址,分别用于存活探针和就绪探针。使用 curl 测试这两个地址。
1
2
3
4
| [student@workstation ~]$ curl http://probe.apps.lab.example.com/health
OK
[student@workstation ~]$ curl http://probe.apps.lab.example.com/ready
READY
|
操作界面、表格与配置图
6. 以 developer 用户连接 OpenShift Web 控制台,创建就绪探针。
6.1. 在 workstation 打开 Firefox,访问 https://master.lab.example.com,以 developer 登录,密码为 redhat。单击 probes 进入项目。
6.2. 进入 Applications > Deployments,单击 probes 部署旁的当前版本 #1。可以看到以下横幅:
容器 probes 尚未配置健康检查,无法据此确认应用运行正常。添加健康检查(Add Health Checks)。
要添加检测器,请单击添加健康检查。
probes-1 created 5 minutes ago
app probes openshift.io/deployment-config.nameprobo
Detalls Environment Metrics Logs Events
Status: Active
Deployment Config: probes
Status Reason: config change
Selectors:
deployment=probes-1 pod
deployment config=probes
Replicas: 1 current / 1 desired
Template
1
2
| @) Containerprobesdoesnothavehealth checkstoensureyourapplication
Isrunningcorrectly.Add HealthChecks ,
|
图9.5:增加健康检查
6.3. 在下一页,单击“添加准备状态检测”以创建一个新的就绪探针。使用下表提供的信息:
Readiness Probe#
| 字段 | 值 |
|---|
| Type | HTTP GET |
| Path | /ready |
| Port | 3000 |
| Initial Delay | 3 秒 |
操作界面、表格与配置图
就绪探针
1
2
| Areadinessprobechecksifthecontainertsreadytohandlerequests.A falledreadinessprobemeansthatacontainershouldnotrecelveanytraffic
froma proxy,evenifit'srunning.
|
路径
/ready
Port
Initial Delay
seconds
How longto wait after the container starts before checking itshealth.
Timeout
seconds
Howlong to wal it for the probe to finish.if the time is exceeded,theprobeis considered failed.
图9.6:添加就绪探针
单击保存以创建就绪探针。
6.4. 在下一页上,单击表格中的最新部署来访问最新的部署配置。
这种变化导致新的部署触发,因为探测器更新了部署配置。
7. 通过导航“行动” > 编辑健康检查创建存活探针。
向下滚动,单击 Add Liveness Probe,按下表创建存活探针。注意此处故意将 health 拼成 healtz。该错误会使健康检查失败,并触发重启。
Liveness Probe#
| 字段 | 值 |
|---|
| Type | HTTP GET |
| Path | /healtz |
| Port | 3000 |
| Initial Delay | 3 秒 |
操作界面、表格与配置图
点击 Save 创建存活探针。
A liveness probe checks If the contalner is sill running. If the liveness probe falls, the container ls killed.
1
2
3
4
| *Type
HTTPGET
(CD UseHTTPS
Path
|
/healtz
3000
1
2
3
| InitialDelay
oO
3 >| | seconds
|
How long to walt after the container starts before checking its health.
1
2
3
| Timeout
eee
\3 > | seconds | t
|
How long to walt for the probe to finish. if the time is exceeded, the probe is consldered falled. Remove Uveness Probe Figure 9.7: Adding liveness probes
这种变化导致新的部署触发,因为探测器更新了部署配置。
8. 单击侧栏 Monitoring,观察 Events 面板实时更新。Unhealthy 条目表示存活探针访问 /healtz 失败。
Monitoring Events 2 warpings
Filter by narme Hide older resources pods/probes-3-tvh7b
1
2
3
| poas/probes-s-Wwn/b prhesoy
Livenessprobefailed:HTTPprobefalledw... afewsecondsago
5timesInthelastminute
|
Pods
1
2
3
| pods/probes-3-fvh7b Pulling
) pullingImage"docker-registry.defaultsvc:.... afewsecondsago
timesinthelastminute
|
probes-3-fvh7b 2 times in the last mlnute Running - 1/1 ready probes/probes d5aab3f
1
2
| probes-3-Wh7p
created2minutesago
|
图9.8:观察监测事件
操作界面、表格与配置图
9. 单击右上角 View Details,查看监控事件。
Monitoring > Events
Events
Filter by keywprd
Time Name Kind Message Reason
Liveness probe falled: HTTP prabe failed wth statuscode; 404 1:17:15 PM probes-4-Jditzg Pod Unheathy 1:17:07 PM
probes-4jdzg Pod Created Created contalner with docker id 689ecca6e128; Security:[seccomp=unconfined]
图 9.9: 查看事件
在 workstation 终端执行 oc get events,查看项目事件,查找类型为 Warning、原因为 Unhealthy 的事件。
1
2
3
4
5
| [student@workstation ~]$ oc get events \
--sort-by='.metadata.creationTimestamp' \
grep 'probe failed'
... output omitted...
... Liveness probe failed: HTTP probe failed with statuscode: 404
|
11. 编辑存活探针
11.1. 更新存活探针,使其请求正确的 URL。进入 Applications > Deployments,单击 probes 查看部署配置。
11.2. 在 Action 中选择 Edit Health Checks,滚动到 Liveness Probe 区域,将 Path 的 /healtz 改为 /health。
单击保存以保存您的修改。
这种变化导致新的部署触发,因为探测器更新了部署配置。
12. 在终端重新执行 oc get events。观察部署配置更新后触发的新部署,以及旧 Pod 的终止过程。确认末尾不再出现新的 Unhealthy 事件或因健康检查失败而终止容器的记录。
1
2
3
4
5
| [student@workstation ~]$ oc get events \
--sort-by='.metadata.creationTimestamp '
... output omitted ...
... Successfully pulled image "docker-registry.default.svc:5000...
... Created kubelet, node1.lab.example.com Created container
|
操作界面、表格与配置图
启动 Kubelet, node1.lab.example.com 启动容器
清理
在 workstation 删除 probes 项目,同时删除本实验创建的 Pod。
1
2
| [student@workstation ~]$ oc delete project probes
project "probes" deleted
|
本引导练习到此结束。
操作界面、表格与配置图
引导练习:通过 Web 控制台查看指标#
Web 控制台#
本练习通过 OpenShift Web 控制台查看指标并管理存储。
学习目标#
能够:
创建一个新项目。
:部署和规模化应用程序。
从网络控制台读取图表。
创建持久卷声明。
。将存储添加到部署配置中。
开始之前#
应已完成第 2 章“安装 OpenShift Container Platform”和第 8 章“安装与配置指标子系统”的实验,并拥有一个 master、两个节点及指标子系统。如果未完成,请重置 master、node1 和 node2,在 workstation 执行以下命令准备环境:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
集群安装后,遵循8章详述的步骤,安装和配置计指标子系统,以部署计指标子系统。
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab web-console setup
|
操作步骤#
创建 load 项目。
1.1. 在 workstation 以 developer 用户登录 OpenShift 集群。
1
2
3
4
5
| [student@workstation ~]$ oc login -u developer -p redhat \
https://master.lab.example.com
Login successful.
You don't have any projects. You can try to create a new project, by running
oc new-project <projectname>
|
1.2. 创建 load 项目。
操作界面、表格与配置图
1
2
3
4
5
6
| [student@workstation ~]$ oc new-project load
Now using project "load" on server "https://master.lab.example.com:443".
You can add applications to this project with the 'new-app' command. For
example, try:
ac new-app centos/ruby-22-centos7~https://github.com/openshift/ruby-ex.git |
to build a new example application in Ruby.
|
2. 浏览 Docker 镜像仓库,确认存在 hello-openshift 镜像。使用该镜像创建 load 应用。
2.1. 使用 http://services.lab.example.com/node-hello 仓库创建 load 应用。
1
2
3
4
5
6
7
8
9
| [student@workstation ~]$ oc new-app --name=load \
http: //services.lab.example .com/node- hella
..,output omitted...
--> Success
Build scheduled, use 'oc logs -f bc/load' to track its progress.
Application is not exposed. You can expose services to the outside world by
executing one or more of the commands below:
"oc expose svc/load'
Run 'oc status' to view your app.
|
2.2. 创建应用程序的路由 :
1
2
| [student@workstation ~]$ oc expose svc/load
route "load" exposed
|
2.3. 等待应用 Pod 就绪并运行:
1
2
3
4
| [student@workstation ~]$ oc get pod
NAME READY STATUS RESTARTS AGE
load-1-build 0/1 Completed 0 im
load-1-df4z9 1/1 Running 0 38s
|
1m
1
2
3
4
| [student@workstation ~]$ oc get pod
NAME READY STATUS RESTARTS AGE
load-1-build 0/1 Completed 0 im
load-1-df4z9 1/1 Running 0 38s
|
3. 如果没有存在,请在 workstation 上安装 httpd-tools 软件包,以产生一些负载。
3.1. 使用 yum 在 workstation 安装 httpd-tools。提示输入密码时使用 student。
1
| [student@workstation ~]$ sudo yum install httpd-tools
|
3.2. 运行 ab 命令以生成应用程序上的一些负载。 请注意, 在 URL 结尾处, 尾部前侧斜线是强制性的。 应用程序在 http://load-load.apps.lab.example.com. 处有效
1
2
3
| [student@workstation ~]$ ab -n 3000000 -c 20 \
http://load-load.apps.lab.example.com/
...output omitted...
|
348
操作界面、表格与配置图
确定load-load.apps.lab.example.com(病人)基准 完成 3ooooorequests
1
2
3
| bencnmarking 1loOad-ioad.apps.lab.example.com (be patient)
Completed 300000 requests
...output omitted...
|
4. 登录 OpenShift Web 控制台,扩展 Pod 数量。
4.1. 从 workstation, 打开 Firefox 并导航到 https://master.lab.example.com。 如果提示, 请接受 自签名 证书。
4.2. 以 developer 用户登录 Web 控制台,密码为 redhat。
4.3. 单击 load 项目,进入概览页。
4.4. 查看 Overview,确认蓝色圆环表示一个 Pod 正在运行。展开 load, #1 部署配置,观察圆环旁的图表,将指针移到第一个内存用量图上。
操作界面、表格与配置图
图中显示 Pod 的内存用量。悬停时会弹出具体数值。
将指针移到第二个图表,查看 Pod 的 CPU 用量。
将指针移到第三个图表,查看 Pod 的网络流量。
1
2
3
4
| APPLICATION
load http://load-load.apps.lab.example.com&
<3 DEPLOYMENTCONFIG .
Vl 1 teow ge a
|
load,#1
1
2
3
4
| 27
MibMemory
0.8
CoresCPL
|
Wed11:07 Cores CPU
ECPU 0.43 cores 10000 pod
1
2
3
| eae 1UUUY
-=---__ Kib/sNetwork
AverageUsageLast 15Minutes
|
图 9.24:概览页
4.5. 单击蓝色甜甜圈旁边的向上方向箭头,将此应用程序的Pod数量扩大至两个Pod。
4.6. 进入 Applications > Deployments,查看项目部署。
load
Applications Ovenview
Deployments
Applications Stateful Sets
Pods
Builds
Services
Routes
Provisioned Services 27
图 9.25:部署页
在 Deployments 页面单击 load,查看部署。
4.7. 注意右侧的动作按钮。 单击它并选择编辑 YAML 来编辑部署配置。
操作界面、表格与配置图
1
2
| Deplayments » load
load created8 minutesago Deploy |Actions v
|
app 3IP3
1
2
3
4
5
6
7
8
| PauseRollauts
History Configuration Environment Events AddStorage
AddAutoscaler
2 Deployment#1 is active. View Log EditResourceLimits
created8minutesago
EditHealthChecks
: Edit YAML
lPitee>Bapthot
|
riter by iabe!
1
2
3
4
| Milterby'abel
Delete
Deployment Status Created Trigger
#1 (latest) &Active,2replicas &minutesago Configchange
|
图9.26: 编辑 YAML
查看部署的 YAML,向下找到 replicas,确认值为 2,与运行中 Pod 数量一致。单击 Cancel 返回。
5. 审查项目中Pod的量度。
5.1. 点击部署表的最新部署,以获取当前部署概况。
5.2. 单击 Metrics。应看到四个图表,分别显示内存用量、CPU 用量、
操作界面、表格与配置图
接收的网络数据包和发送的网络数据包。每个图表有两条曲线,各对应一个 Pod。
如果图表没有显示任何值,在重试前再等几分钟。
load-1 created 10 minutes ago Actions
1
| app f load | openshift.io/deployment-config.namebet |
|
Details En v iron r ment Metrics Logs Events
1
| Container:load TimeRange: Lasthour + AboutComputeResources
|
Memory
25
15
10
1
2
3
| 0
Wed10:15 Wed10:21 Wed10:27 Wed10:33 Wed10:39 Wed10:45 Wed10:51 Wed10:57 Wed11:03 Wed11:09
Hl load-1-7q|fs Mi load-1-df4z9
|
图 9.27:应用指标
5.3. 将指针移到内存曲线上,观察弹框中两个 Pod 的详细用量。
6. 查看 Web 控制台的 Monitoring 部分。
6.1. 单击侧栏 Monitoring。Pods 区域应有两个条目,Deployments 区域应有一个条目。
6.2. 向下找到部署,单击名称旁的箭头展开。日志下应有三个图表,分别显示 Pod 的内存用量、
操作界面、表格与配置图
CPU 用量,以及发送和接收的网络数据包。
6.3. 突出显示第一个图表上的图块, Memory. 注意显示的框, 显示Pod所用的内存量。
30
25
20 Wed 11:13
15 load-1-7qifs 26.25 MiB
10
load-1-df4z9 25.45 MiB
Wed 10:37 Wed 10:45 Wed 10:53 Wed 11:01 Wed 11:09 Wed 11:17 Wed 10:29 Wed 11:25 load-1-7qifs load-1-df4z9
图 9.28:部署指标
为应用创建持久卷声明。实验环境已提供可与该声明绑定的持久卷。
7.1. 点击 Storage 以生成一个持久卷声明。
7.2. 单击 Create Storage,定义 PVC。
7.3. Name 填 web-storage,Access Mode 选 Shared Access (RWX),Size 填 1,单位保留 GiB。
Create Storage
1
2
3
4
5
6
7
8
9
10
11
12
| her§
Createarequestforanadministrator-definedstorageassetbyspecityingsizeandpermissionsforabestfit. LeamMore#
*Name
web-storage -
Auniquenameforthestorageclalmwithintheproject.
*AccessMode
(©)SingleUser(RWO) #)SharedAccess(RWX) ( ) ReadOnly(ROX)
Permissionstothemountedvolume.
*size
ss EEE-EE-------------SS
{1
Desiredstoragecapacity.
|
What are GiB?
1
| {LJUselabelselectorstorequeststorage
|
Creare Cancel
图 9.29:创建持久卷声明
单击 Create 创建 PVC。在下一页确认状态从 Pending 变为 Bound,并绑定到 web-storage 卷。
操作界面、表格与配置图
8. 在您的应用程序中添加存储。
8.1. 进入 Applications > Deployments,管理部署。
单击 load,查看部署。
8.2. 单击部署的 Actions > Add Storage,将已有 PVC 加到部署配置模板。选择 web-storage 作为存储声明。
8.3. Mount Path 填 /web-storage,Volume Name 填 web-storage。
1
2
3
| *Storage
@) web-storage 2 GIB {Read-Write-Many) Boundtovolumeweb-storage
Selectstorage touseorcreatestorage.
|
Volume
1
2
3
4
5
| SpecifydetallsabouthowvalumesaregoingtobemountedInsidecontainers.
Mount Path
Aweb-storage
Mountpathforthevolumeinside thecontainer.lfnotspecified,thevolumewillr
Subpath
|
example:pplicatfon/resources
1
2
3
| Optional pathwithinthevolumeframwhichitwill be mountedintothecontatner. Defaultstothevolume's root.
VolumeName
web-storage
|
图9.30:部署时增加储存量
单击添加以添加存储。 该动作触发新的部署, 并返回到部署的历史标签。
9. 检查存储。
9.1. 在 Deployments 页单击带有 latest 标记的最新部署,等待两个副本处于 Active 状态。
9.2. 确保 Volumes 区有web-storage 卷作为持久卷。从底部的 Pods 区中选择一个运行中的Pod。
9.3. 单击终端标签打开Podshell。
9.4. 运行以下命令来定位卷 :
1
2
3
| sh-4.2$ mount | grep web-storage
services.lab.example.com:/var/export/web-storage-ge on /web-storage type
nfs4 va.
|
操作界面、表格与配置图
10. 清理干净
10.1. 在 workstation 主机上, 删除 load 项目, 其中也删除在此实验创建的所有Pod :
1
2
| [student@workstation ~]$ oc delete project load
project "load" deleted
|
清理
运行 lab web-console cleanup 命令以删除 load 项目和项目使用的持久卷。
1
| [student@workstation ~]$ lab web-console cleanup
|
本引导练习到此结束。
操作界面、表格与配置图
实验:管理和监控 OpenShift#
在这个实验中,管理资源限值 并监控在OpenShift上应用的应用
学习目标#
能够:
对项目实行配额和限额。
为应用配置存活探针。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab review-monitor setup
|
操作步骤#
1. 在 workstation 以 developer 登录 OpenShift,创建 load-review 项目。
2. 查看 /home/student/DO280/labs/monitor-review/limits.yml 中的限制范围,为项目内新 Pod 设置默认资源请求。
作为集群管理者,设定文件界定的限值,并审查设定的限值。
作为 developer 用户,使用位于 services.lab.example.com 的 node-hello 镜像在 load-review 项目中创建 load 应用程序。 镜像流为 PHP: 5.6。 该应用程序暴露了/health 和 /ready. 两个 HTTP GET URL
4. 确保Pod的默认请求限制符合为项目设定的限制。
5. 将部署配置的内存请求改为 350 MiB,查看项目事件,找到因超过限制而被拒绝的记录。此限制也会阻止新部署完成。随后将请求恢复为 200 MiB。
操作界面、表格与配置图
查看 /home/student/DO280/labs/monitor-review/quotas.yml,其中定义了 load-review 项目的配额。
作为集群管理者,界定load-review项目的配额,并审查配额。
7. 以 developer 用户将应用扩展到四个副本,查看项目事件。确认第四个 Pod 因超过配额而无法创建,使用 grep 筛选警告。随后缩减到一个副本。
8. 提供load服务线路,让外部客户访问该应用程序。
9. 登录到网络控制台以创建存活探针。 使用下表提供的信息。
Liveness Probe#
| 字段 | 值 |
|---|
| Type | HTTP GET |
| Path | /health |
| Port | 3000 |
| Initial Delay | 10 秒 |
| Timeout | 3 秒 |
10. 确认存活探针已成功创建。
11. 给您的工作评分。 运行以下命令来给您的工作评分 :
1
| [student@workstation ~]$ lab review-monitor grade
|
如果有任务未得到 PASS,检查操作后重新评分。
12. 清理干净
删除 load-review 项目:
操作界面、表格与配置图
在这个实验中,管理资源限值 并监控在OpenShift上应用的应用
学习目标#
能够:
对一个项目实行配额和限额。
核查项目的配额和限额。
为应用配置存活探针。
开始之前#
应已完成“安装 OpenShift Container Platform”章节的全部实验,并拥有一个 master、两个节点组成的集群。否则,请重置 master、node1 和 node2,在 workstation 执行以下命令,确保环境已正确配置:
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
为确认 master、node1 和 node2 已启动,并下载本练习所需文件,请在 workstation 打开终端,执行以下命令:
1
| [student@workstation ~]$ lab review-monitor setup
|
操作步骤#
1. 在 workstation 以 developer 登录 OpenShift,创建 load-review 项目。
1.1. 在 workstation, 上打开终端, 并登录到 OpenShift 集群为 developer 用户 :
1
2
3
4
5
| [student@workstation ~]$ oc login -u developer -p redhat \
https://master.lab.example.com
Login successful.
You don't have any projects. You can try to create a new project, by running
oc new-project <projectname>
|
1.2. 创建 load-review 项目 :
1
2
3
4
5
| [student@workstation ~]$ oc new-project load-review
Now using project "load-review" on server "https://master:443".
You can add applications to this project with the 'new-app' command. For
example, try:
oc new-app centos/ruby-22-centos7~https://github.com/openshift/ruby-ex.git
|
操作界面、表格与配置图
1
| to bulid a new example application in Ruby.
|
2. 查看 /home/student/DO280/labs/monitor-review/limits.yml 中的限制范围,为项目内新 Pod 设置默认资源请求。
作为集群管理者,设定文件界定的限值,并审查设定的限值。
2.1. 查看 /home/student/DO280/labs/monitor-review/limits.yml,内容如下:
1
2
3
4
5
6
7
8
9
10
11
| apiVersion: "v1"
kind: "LimitRange"
metadata:
name: "review-limits"
spec:
limits:
- type: "Container"
max:
memory: "300Mi"
default:
memory: "200Mi"
|
2.2. 登录到 OpenShift 集群作为管理员 :
1
2
3
| [student@workstation ~]$ oc login -u admin -p redhat
Login successful.
...output omitted...
|
2.3. 输入 load-review 项目 :
1
2
| [student@workstation ~]$ oc project load-review
Now using project "load-review" on server "https://master.lab.example.com:443". |
|
2.4. 作为集群管理员,为 load-review 项目设定了文档定义的限值:
1
2
3
4
| EE ENa
[student@workstation ~]$ oc create -f \
/home/student/DO280/labs/monitor-review/limits.yml
limitrange "review-limits" created
|
2.5. 执行 oc describe,查看创建的限制范围:
1
2
3
4
5
| [student@workstation ~]$ oc describe limits
Name: review-limits
Namespace: load-review
Type Resource Min Max Default Request Default Limit ...
Container memory - 300Mi 200Mi 200Mi aH
|
操作界面、表格与配置图
3. 以 developer 用户在 load-review 中创建 load 应用,使用 services.lab.example.com 上的 node-hello 代码和 PHP:5.6 镜像流。应用提供 /health 与 /ready 两个 HTTP GET 地址。
3.1. 以 developer 用户身份登录 :
1
2
| [student@workstation ~]$ oc login -u developer -p redhat \
https://master .lab.example.com
|
3.2. 使用 oc new-app 创建 load 应用:
1
2
3
4
5
6
7
8
9
| [student@workstation ~]$ oc new-app --name=load \
http://services.lab.example.com/node-hello
...output omitted...
--> Success
Build scheduled, use 'oc logs -f bc/load' to track its progress.
Application is not exposed. You can expose services to the outside world by
executing one or more of the commands below:
'oc expose svc/load'
Run 'oc status' to view your app.
|
4. 确认 Pod 的默认资源请求与项目配置一致。
4.1. 运行 oc get pods 命令以列出环境中的Pod :
1
2
3
4
| [student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
load-1-build 0/1 Completed 0 3m
load-1-cgz2x al//iL Running (0) 2m
|
3m
1
2
3
4
| [student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
load-1-build 0/1 Completed 0 3m
load-1-cgz2x al//iL Running (0) 2m
|
4.2. 检查前一个指令返回的Pod,并在容器区找到Limits和Requests:
1
2
3
4
5
6
7
| [student@workstation ~]$ oc describe pod load-1-cgz2x
...output omitted...
Limits:
memory: 200Mi
Requests:
memory: 200Mi
...output omitted...
|
5. 请求 350 MiB 更新部署配置的限制。 审查项目中的事件, 寻找显示请求因违反限制而被拒绝的条目。 这也应防止为部署创建新版本。 将请求回复到 200Mib。
5.1. 请求 35o MiB 运行 oc set resources 命令 :
1
2
| [student@workstation ~]$ oc set resources dc load \
--requests=memory=350Mi
|
操作界面、表格与配置图
1
| geploymentcontig loag" resource requirements updated
|
5.2. 执行 oc get events,查找违反限制的事件。错误可能需要稍候才出现。
1
2
3
4
5
| [student@workstation ~]$ oc get events | grep Warning
...output omitted...
... Error creating: Pod "load-2-q9hm9" is invalid:
spec.containers[0].resources.requests: Invalid value: "350Mi": must be less
than or equal to memory limit
|
5.3. 将限制恢复到 200Mib :
1
2
| [student@workstation ~]$ oc set resources dc load \
--requests=memory=200Mi
|
5.4. 等待第三次部署的新Pod准备就绪并运行:
1
2
3
4
5
6
7
8
9
10
11
12
13
| [student@workstation ~]$ oc status ; oc get pod
In project load-review on server https://master.lab.example.com:443
svc/load - 172.30.110.135:3000
dc/load deploys istag/load:latest >-
bc/load docker builds http://registry.lab.example.com/node-hello on istag/
nodejs-6-rhel7: latest
deployment #3 deployed 24 seconds ago - 1 pod
deployment #2 failed about a minute ago: newer deployment was found running
deployment #1 deployed 6 minutes ago
2 infos identified, use 'oc status -v' to see details.
NAME READY STATUS RESTARTS AGE
load-1-builid 0/1 Completed 0 6m
load-3-fp788 1/1 Running ic) 19s
|
6m
1
2
| dVaUT ATMs Vit VVHIPIOCOU Y om
load-3-fp788 1/1 Running 0 19s
|
6. 查看 /home/student/DO280/labs/monitor-review/quotas.yml,其中定义了 load-review 项目的配额。
作为集群管理者,界定load-review项目的配额,并审查配额。
6.1. 查看用于设置 load-review 配额的 quotas.yml,内容如下:
1
2
3
4
5
6
7
| apiVersion: v1
kind: ResourceQuota
metadata:
name: review-quotas
spec:
hard:
requests.memory: "600Mi"
|
6.2. 以 admin 用户身份登录 :
操作界面、表格与配置图
6.3. 为load-review项目设定配额:
1
2
3
| [student@workstation ~]$ oc create -f \
/home/student/DO280/labs/monitor-review/quotas. ym1
resourcequota "review-quotas" created
|
6.4. 执行 oc describe,查看 load-review 项目的配额:
1
2
3
4
5
| [student@workstation ~]$ oc describe quota
Name: review-quotas
Namespace: load-review
Resource Used Hard
requests.memory 200Mi 600Mi
|
7. 以 developer 用户将应用扩展到四个副本,查看项目事件,确认无法创建第四个
Pod,因为它会超过配额。使用 grep 筛选 Warning 消息,然后将
将复制数降至一个。
7.1. 以 developer 用户身份登录 :
1
2
3
4
| [student@workstation ~]$ oc login -u developer -p redhat
- Login successful.
You have one project on this server: "load-review"
Using project "load-review".
|
7.2. 将应用请求的副本数改为四个。
1
2
| [student@workstation ~]$ oc scale --replicas=4 dc load
deploymentconfig "load" scaled
|
7.3. 运行 oc get pods 命令以列出运行中的Pod数。 等待三个Pod准备就绪并运行。 请注意, 第四个Pod尚未创建 :
1
2
3
4
5
6
| [student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
load-1-build 0/1 Completed 0 8m
load-3-fp788 1/1 Running (0) 2m
load-3-j1kwj 1/1 Running 0 12s
load-3-mdr2g 1/1 Running 0 12s
|
0/1 Completed 8m
1
2
3
4
| fUGaUYUTsa -VUstu Vit VVIIPIeECOU Y Ol
load-3-fp788 1/1 Running (0) 2m
load-3-j1kwj 1/1 Running (0) 12s
IrnrantHt 9 wnt eon a fa eeae ia" 4AaIA
|
1/1
1
2
| 1V0AaU-S- JiKkw]) aft RUT LIY u 4125
load-3-mdr2g 9-1/1 Running (0) 12s
|
7.4. 审查项目事件。 找到显示配额应用的条目, 从而阻止第四个Pod的创建 :
1
2
| [student@workstation ~]$ oc get events | grep Warning
...output omitted...
|
操作界面、表格与配置图
1
2
3
| +». Error creating: pods load-3-qwn/7m' ls Torbidden: exceeded quota: review-
quotas, requested: requests.memory=200Mi, used: requests.memory=6000Mi, limited:
requests.memory=600Mi
|
7.5. 运行 oc scale 命令, 将副本数量缩放为 1 :
1
2
3
| [student@workstation ~]$ oc scale --replicas=1 de load
deploymentconfig "load" scaled
eee Oe Pee eee OS --™L
|
8. 提供load服务线路,让外部客户访问该应用程序。
8.1. 提供 load 服务的路由, 并使用 load-review.apps.lab.example.com 作为主机名 :
1
2
3
4
| [student@workstation ~]$ oc expose svc load \
--hostname=load-review.apps.lab.example.com
route "load" exposed
eni id Se moc Si: a sai ae aia
|
登录到 Web 控制台以创建存活探针。 使用下表提供的信息。
Liveness Probe#
| 字段 | 值 |
|---|
| Type | HTTP GET |
| Path | /health |
| Port | 3000 |
| Initial Delay | 10 秒 |
| Timeout | 3 秒 |
9.1. 在 workstation 打开 Firefox,访问 https://master.lab.example.com,以 developer 登录,密码为 redhat。单击 load-review 进入项目。
9.2. 要添加存活探针, 请导航到 Applications > Deployments, 并单击 load 部署。
单击 Actions 并选择 Edit Health Checks 条目添加存活探针
9.3. 单击 Add Liveness Probe,Type 选 HTTP GET,Path 填 /health,Initial Delay 填 10,Timeout 填 3。
单击 Save 创建存活探针。
操作界面、表格与配置图
10. 确保存活探针成功创建。
10.1. 进入 Applications > Deployments,选择 load 部署。
选择应用程序的最新部署。
10.2. 在 Template 部分找到以下条目:
1
| Liveness Probe: GET /health on port 3000 (HTTP) 10s delay, 3s timeout
|
11. 运行以下命令,对实验结果评分:
1
| [student@workstation ~]$ lab review-monitor grade
|
如果有任务未得到 PASS,检查操作后重新评分。
12. 清理干净
删除 load-review 项目:
1
| [student@workstation ~]$ oc delete project load-review
|
操作界面、表格与配置图
自测问题#
C10 综合复习#
10.1 平台交付与应用交付#
平台交付验证安装、节点、身份、Registry、Router 和监控。应用交付验证权限、Secret、数据库持久化、服务发现、路由、探针、配额及故障恢复。
验收使用目标用户执行允许操作和拒绝操作。管理员能读写不代表普通用户权限正确;数据库 Pod Running 不代表应用能够连接;PVC Bound 不代表数据能够跨 Pod 重建保留。
实验操作#
实验:安装 OpenShift#
综合实验:在三个节点上安装 OpenShift,并确认安装后的集群功能正常。
学习目标#
能够:
准备各主机,使其满足 OpenShift 节点安装条件。
:用一个master和另外两个节点执行OpenShift离线安装。
:执行安装后的任务并核实集群配置是否正确。
在你开始之前
选项 1
此选项允许您创建一个新的 OpenShift 集, 手动执行安装步骤。
重置 master、node1、node2 和 services 虚拟机。不要重置 workstation,它已配置好 Ansible。
运行以下命令, 以验证您的环境是否准备好启动此审查实验, 并下载样本解决方案文件 :
1
| [student@workstation ~]$ lab review-install setup
|
继续遵循以下指示。
选项 2
如果OpenShift已经安装和配置,且您不想建立新集,您可以跳过这个实验,前往下一个实验。
选项 3
若希望自动安装配置新集群,请重置 master、node1、node2 和 services。不要重置 workstation,它已配置好 Ansible。
运行以下命令以自动安装和配置新的 OpenShift 集 :
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
上述步骤完成后,您可以前往下一个实验。
操作说明
使用高级安装方式安装 Red Hat OpenShift Container Platform,满足以下要求:
OpenShift组有三个节点:
操作界面、表格与配置图
master.lab.example.com 是 OpenShift master,设为不可调度节点。
node1.lab.example.com 可以运行应用 Pod 和基础设施 Pod。
node2.lab.example.com 也可以运行应用 Pod 和基础设施 Pod。
所有节点使用 OverlayFS 和 overlay2 驱动作为 Docker 存储。各节点的第二块磁盘 vdb 专用于 Docker 存储。
:所有节点都使用RPM-based安装,使用释放v3.9和OpenShift镜像标记版本v3.9.14。
默认路由域为 apps.lab.example.com。课堂 DNS 已将该域下所有主机名解析到 node1.lab.example.com,也称通配符域。
:OpenShift集群所使用的所有容器镜像都储存在registry.lab.example.com.的私有镜像仓库中。
:使用两个初始用户的 HTPasswd 认证:developer 密码为 redhat 的developer 常规用户,而admin 密码为 redhat 的集群管理员。
- 确保内部OpenShift镜像仓库以使用services.lab.example.com.上的NFS体积的持久性储存作为OpenShift镜像仓库的支撑
services.lab.example.com 虚拟机为集群存储提供 NFS 服务。
etcd 与 master 位于同一节点;其存储配置在 services.lab.example.com 的 NFS 共享上。
集群必须从互联网上断开。
:内部OpenShift镜像仓库应以NFS持久性储存作为后盾。该储存位于services.lab,example.com。
master API 与控制台使用 443 端口。
安装 OpenShift 所需的 RPM 软件包,由各主机已有的 Yum 配置文件指定软件源。
/home/student/DO280/labs/review-install 文件夹为安装 OpenShift 集提供了部分完成的 Ansible 清单文件。 此文件夹中包括所需的 Ansible Playbook, 用于安装前和安装后步骤。
Git 地址 http://services.lab.example.com/php-helloworld 提供简单的 Hello World 测试应用。使用 Source-to-Image 部署,验证集群工作正常。
workstation 桌面提供产品文档。可参考 Installation Guide 和 Developer Guide,查看安装前提、配置文件及集群安装配置命令。
操作界面、表格与配置图
拟议的解决办法步骤
以下是为开展这项工作而采取的步骤摘要。
1. 准备安装主机。补全 Ansible inventory,使用提供的 Playbook 完成安装前任务。完整示例位于 inventory.preinstall。
1.1. 使用 ping.yml Playbook验证 Ansible 与清单文件中列出的所有虚拟机器的连接。
1.2. 使用 prepare_install.yml Playbook为安装 OpenShift 集群准备节点。
2. 使用先进的安装方法安装OpenShift集群,该集群符合指令一节的要求,包括:
2.1. 补全 inventory 的必要变量。部分完成的示例位于 inventory.partial。
2.2. 在 inventory 文件中适当标出节点, 以便它们符合给定的要求。
2.3. 使用评分脚本检查清单文件的值:
1
| [student@workstation review-install]$ lab review-install verify
|
2.4. 安装先进安装方法所需的OpenShift Ansible Playbook。
2.5. 使用先进的安装方法在节点上安装 OpenShift。
2.6. 通过访问网络控制台来验证集群配置。
3. 赋予admin 用户集管理员职能。
4. 使用 admin 用户对集群配置进行基本核查:
4.1. 验证所有节点是否已准备好, 并有正确的标签。
4.2. 验证 docker-registry 和 router Pod是否运行并准备好。
5. 使用 Source-to-Image(S2I)部署 php-helloworld,验证安装。
运行以下命令,对实验结果评分:
1
| [student@workstation review-install]$ lab review-install grade
|
如果总评不是 PASS,检查操作后重新评分。
删除测试项目以清理 :
1
| [student@workstation review-install]$ oc delete project test-s2i
|
操作界面、表格与配置图
综合实验:在三个节点上安装 OpenShift,并确认安装后的集群功能正常。
学习目标#
能够:
准备各主机,使其满足 OpenShift 节点安装条件。
执行OpenShift离线安装,安装一个master和另外两个节点。
执行安装后的任务, 并验证集群配置是否正确。
开始之前#
选项 1
此选项允许您创建一个新的 OpenShift 集, 手动执行安装步骤。
重置 master、node1、node2 和 services。不要重置 workstation,它已配置好 Ansible。
运行以下命令, 以验证您的环境是否准备好启动此审查实验, 并下载样本解决方案文件 :
1
| [student@workstation ~]$ lab review-install setup
|
继续遵循以下指示。
选项 2
如果OpenShift已经安装和配置,且您不想建立新集,您可以跳过这个实验,前往下一个实验。
选项 3
若希望自动安装配置新集群,请重置 master、node1、node2 和 services。不要重置 workstation,它已配置好 Ansible。
运行以下命令以自动安装和配置新的 OpenShift 集 :
1
2
3
| [student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh
|
上述步骤完成后,您可以前往下一个实验。
操作说明
使用高级安装方式安装 Red Hat OpenShift Container Platform,满足以下要求:
OpenShift组有三个节点:
master.lab.example.com 是 OpenShift master,设为不可调度节点。
操作界面、表格与配置图
node1.lab.example.com 可以运行应用 Pod 和基础设施 Pod。
node2.lab.example.com 也可以运行应用 Pod 和基础设施 Pod。
所有节点使用 OverlayFS 和 overlay2 驱动作为 Docker 存储。各节点的第二块磁盘 vdb 专用于 Docker 存储。
:所有节点都使用RPM-based安装,使用释放v3.9和OpenShift镜像标记版本v3.9.14。
。 路径的默认域是 apps.lab.example.com。 课堂 DNS 服务器已经配置好,可以将此域的所有主机名称解析为 node1.Iab.example.com。 域名也称为通配符域。
:OpenShift集群所使用的所有容器镜像都储存在registry.lab.example.com.的私有镜像仓库中。
:使用两个初始用户的 HTPasswd 认证:developer 密码为 redhat 的developer 常规用户,而admin 密码为 redhat 的集群管理员。
:确保内部OpenShift镜像仓库以使用services.lab.example.com.上的NFS体积的持久性储存作为OpenShift镜像仓库的后盾。
services.lab.example.com虚拟机器将提供NFS服务,用于集群储存。
etcd 与 master 位于同一节点;其存储配置在 services.lab.example.com 的 NFS 共享上。
该集群必须与互联网断开连接。
:OpenShift内部镜像仓库应以NFS持久性储存作为后盾。该储存位于services.lab.example.com。
master API 与控制台使用 443 端口。
安装 OpenShift 所需的 RPM 软件包,由各主机已有的 Yum 配置文件指定软件源。
/home/student/DO280/labs/review-install 提供部分完成的 Ansible inventory,并包含
执行安装前和安装后步骤所需的 Ansible Playbook。
Git 地址 http://services.lab.example.com/php-helloworld 提供简单的 Hello World 测试应用。使用 Source-to-Image 部署,验证集群工作正常。
workstation 桌面提供产品文档。可参考 Installation Guide 和 Developer Guide,查看安装前提、配置文件及集群安装配置命令。
操作界面、表格与配置图
拟议的解决办法步骤
以下是为开展这项工作而采取的步骤摘要。
1. 准备安装主机。补全 Ansible inventory,使用提供的 Playbook 完成安装前任务。完整示例位于 inventory.preinstall。
1.1. 使用 ping.yml Playbook验证 Ansible 与清单文件中列出的所有虚拟机器的连接。
1.2. 使用 prepare_install.yml Playbook为安装 OpenShift 集群准备节点。
2. 使用先进的安装方法安装OpenShift集群,该集群符合指令一节的要求,包括:
2.1. 补全 inventory 的必要变量。部分完成的示例位于 inventory.partial。
2.2. 在 inventory 文件中适当标出节点, 以便它们符合给定的要求。
2.3. 使用评分脚本检查清单文件的值:
1
| [student@workstation review-install]$ lab review-install verify
|
2.4. 安装先进安装方法所需的OpenShift Ansible Playbook。
2.5. 使用先进的安装方法在节点上安装 OpenShift。
2.6. 通过访问网络控制台来验证集群配置。
3. 赋予admin 用户集管理员职能。
4. 使用 admin 用户对集群配置进行基本核查:
4.1. 验证所有节点是否已准备好, 并有正确的标签。
4.2. 验证 docker-registry 和 router Pod是否运行并准备好。
5. 使用 Source-to-Image(S2I)部署 php-helloworld,验证安装。
操作步骤#
使用 ping.yml 和 prepare_install.yml,为主机安装 OpenShift 做准备。
1.1. 在 workstation 打开终端,进入 /home/student/DO280/labs/review-install 执行后续步骤。
验证 workstation 主机是否安装了 Ansible 的正确版本。
1
| [student@workstation review-install]$ ansible --version
|
操作界面、表格与配置图
1.2. 创建 inventory,定义集群配置及 prepare_install.yml 所需变量。完整示例见 inventory.preinstall。
1
| [student@workstation review-install]$ cp inventory.preinstall inventory
|
1.3. 运行 Ansible Playbook ping.yml,验证主机连通性及 sudo 权限提升:
1
| [student@workstation review-install]$ ansible-playbook -v ping.yml
|
1.4. 运行 Ansible 手册 prepare_install.yml 为OpenShift 安装准备 VMS :
1
| [student@workstation review-install]$ ansible-playbook prepare_install1.yml
|
没有预期失败。 如果您看到失败, 请检查日志文件 ansible.log, 诊断问题, 并重试运行Playbook。
1.5. 校验您可从私有镜像仓库registry.lab.example.com.调出一个节点上的 docker 镜像
1
| [student@workstation review-install]$ ssh node1 'docker pull rhel7:latest'
|
2. 创建 inventory,列出集群主机、正确的节点标签及满足实验要求的变量。部分完成的示例
操作界面、表格与配置图
位于 inventory.partial。补全缺失的变量或值,形成完整的清单文件。
完整的清单文件可在inventory.complete找到。
可以通过运行实验脚本校验功能来验证清单文件 :
2.1. 将部分完成的清单文件 inventory.partial 复制到名为 inventory: 的文件
1
| [student@workstation review-install]$ cp inventory.partial inventory
|
2.2. 以 inventory 文件的有效值替换所有 # FIxME# 实例。
修正 node1 标签:
1
2
| node1.lab.example.com openshift_node labels="{'region':'infra', 'node-
role.kubernetes.io/compute':'true'}"
|
openshift_node_labels 参数必须是单行的参数。 无法分割成多行。
修正 openshift_deployment_type:
1
| openshift_deployment_type=openshift-enterprise
|
将正确版本字符串添加到 openshift_release 和 openshift_image_tag。
1
2
| openshift_release=v3.9
openshift_image_tag=v3.9.14
|
为 API 和控制台填写正确的端口号:
1
| openshift_master_api_port=443
|
操作界面、表格与配置图
1
| opensnitft_master_console_port=443
|
添加正确的默认子域变量 :
1
| openshift_master_default_subdomain=apps.lab.example.com
|
将 registry.lab.example.com 添加为集群的附加镜像仓库:
1
| openshift_docker_additional_registries=registry.lab.example.com
|
2.3. 使用实验脚本验证 inventory 文件的准确性 :
1
| [student@workstation review-install]$ lab review-install verify
|
纠正任何错误和脚本 re-run ,直到您得到干净的结果。
3. 确保安装 OpenShift Ansible Playbook。
1
| [student@workstation review-install]$ rpm -qa | grep atomic-openshift-utils
|
如果软件包未安装 :
1
| [student@workstation review-install]$ sudo yum install atomic-openshift-utils
|
4. 运行 prerequisites.yml 和 deploy_cluster.yml Playbook以完成OpenShift 集的高级安装。 这些Playbook在 /usr/share/ansible/openshift-ansible/playbooks. 中提供
确保两个Playbook运行中没有任何失败。
4.1. 运行必备的Playbook :
1
2
| [student@workstation review-install]$ ansible-playbook \
/usr/share/ansible/openshift-ansible/playbooks/prerequisites.yml
|
确保没有失败。
4.2. 运行集群部署Playbook :
1
2
| [student@workstation review-install]$ ansible-playbook \
/usr/share/ansible/openshift-ansible/playbooks/deploy_cluster.yml
|
确保没有失败。
操作界面、表格与配置图
5. 验证该组是否通过访问网络控制台成功配置。 登录为 developer 用户。
5.1. 浏览器访问 https://master.lab.example.com,信任 OpenShift 生成的自签名证书。
5.2. 登录为用户 developer 和密码 redhat., 如果您看到您在集上成功安装了 OpenShift 的“ 浏览目录” 页面。
6. 授予 admin 用户集群范围的管理权限。
6.1. 在 master 节点获取集群的系统管理员访问权限:
1
2
3
4
| [student@workstation review-install]$ ssh master
[student@master ~]$ sudo -i
[root@master ~]# oc whoami
system:admin
|
6.2. 授予cluster-admin 角色给 admin 用户:
1
| [root@master ~]# oc adm policy add-cluster-role-to-user cluster-admin admin
|
6.3. 核实admin用户现在是否拥有广泛的行政权 :
1
2
3
4
5
| [root@master ~]# exit
[student@master ~]$ exit
[student@workstation review-install]$ oc login -u admin \
https: //master.lab.example.com
[student@workstation review-install]$ oc get nodes
|
最后命令应该显示集群宽节点信息。 验证 master 是否具有 master 的作用, 另外两个节点是否具有 compute 的作用。 所有节点都应该具有 Ready. 的地位
7. 核实内部 OpenShift 镜像仓库 (docker-registry) 和路由器 (router) Pod正在运行并准备就绪 :
1
| [student@workstation review-install]$ oc get pods -n default
|
8. 使用 Source-to-Image(S2I)部署 php-helloworld,验证安装。
8.1. 登录为 developer:
1
| [student@workstation review-install]$ oc login -u developer -p redhat \
|
操作界面、表格与配置图
1
| nttps://master.lab.example.com
|
8.2. 创建 test-s2i 项目 :
1
| [student@workstation review-install]$ oc new-project test-s2i
|
8.3. 从 Git 仓库部署应用程序 :
1
2
| [student@workstation review-install]$ oc new-app --name=hello \
php:5.6~http://services.lab.example.com/php-helloworld
|
8.4. 等待应用 Pod 就绪并运行:
1
2
3
4
| [student@workstation review-install]$ oc get pods
NAME READY STATUS RESTARTS AGE
hello-1-build 0/1 Completed 0 im
hello-1-g13jp 1/1 Running (c} 59s
|
1m
1
2
3
4
| [student@workstation review-install]$ oc get pods
NAME READY STATUS RESTARTS AGE
hello-1-build 0/1 Completed 0 im
hello-1-g13jp 1/1 Running (c} 59s
|
8.5. 向外部用户公开 Service:
1
| [student@workstation review-install]$ oc expose svc hello
|
8.6. 从路由上使用 URL 访问应用程序 :
1
2
| [student@workstation review-install]$ curl hello-test-s2i.apps.lab.example.com
Helio, World! php version is 5.6.25
|
应用程序可能需要一段时间才能从源构建并安装在 OpenShift 上。 当引用 URL 路由时, 您可能会遇到错误, 如以下错误 :
1
2
3
4
5
6
| <html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1"> _
<hi>Application is not available</hi>
<p>The application is currently not serving requests at this
endpoint. It may not have been started or is still starting.</p> |
|
此错误是因为应用 Pod 尚未处于 Running 状态,路由还没有可用端点。等待部署完成后重试 URL。
评审实验到此结束
运行以下命令来给您的工作评分 :
操作界面、表格与配置图
1
| [student@workstation review-install]$ lab review-install grade
|
如果总评不是 PASS,检查操作后重新评分。
删除测试项目以清理 :
1
| [student@workstation review-install]$ oc delete project test-s2i
|
操作界面、表格与配置图
实验:部署应用#
本综合实验在 OpenShift 部署多容器应用,并排查问题,确保部署成功。
学习目标#
能够:
允许用户在项目内创建应用,但不允许创建新的
- 项目。为项目设置资源配额,以控制资源用量。
使用一个简单的 Source-to-Image (s2l) 应用程序来验证配额设置。 在应用程序部署过程中, 故障排除和修正问题。
:为应用程序使用的 MySQL 数据库建立持久性存储。
。在 OpenShift 上使用模板部署 多容器 应用程序。该模板应提供给网络控制台的用户。
。构建用于 多容器 应用程序的自定义容器镜像。
开始之前#
如果您没有使用 master 和两个节点运行的 OpenShift 集群, 您必须完成在上一节安装 OpenShift 的全面审查实验中的一个选项。
运行以下命令, 以验证您的环境已准备好启动此审查实验, 并下载此实验所需的文件 :
1
| [student@workstation ~]$ lab review-deploy setup
|
workstation 的 /home/student/DO280/labs/review-deploy 提供本实验的配置文件、脚本,以及持久卷等资源的 YAML 示例。
Red Hat OpenShift Container Platform 产品文档在 workstation 主机桌面中提供。 将文档用作此实验所需的命令的参考。
待办事项(TODO List)应用由三个容器组成:
:一个 MySQL 数据库容器,储存TODO清单中的任务数据。
Apache httpd 前端容器 todoui 提供 HTML、CSS 和 JavaScript 静态资源,用户界面使用 AngularJS 框架。
基于 Node.js 的 API 后端容器 todoapi,向前端提供 RESTful 接口,并连接 MySQL 数据库管理应用数据。
操作说明
执行前仔细阅读这些指令。 本实验使用两个用户。 集admin 用户 admin 拥有 redhat, 的密码, developer 用户拥有
操作界面、表格与配置图
redhat. 的密码 这些用户是安装和配置 OpenShift 集时创建的。
1. 在 workstation 以集群管理员 admin 登录。禁止普通用户创建新项目,只允许集群管理员创建项目。
2. 创建名为 todoapp. 的新项目允许 developer 用户访问此项目。 允许 developer 用户在此项目中创建新应用程序。
确定todoapp项目的配额如下:
:配额名为todoapp-quota.
配额将 Pod 数量的硬上限设为 1。
若不想逐条输入,可在 workstation 执行 /home/student/DO280/labs/review-deploy/set-quotas.sh。
3. 登录为 developer 用户。 部署 php-helloworld 应用程序来验证配额设置。 应用程序的源代码可在 http://services.lab.example.com/php-helloworld 中查到。 应用程序的名称应该是 hello.。
验证 php-helloworld 应用程序的部署是否成功。 确保应用程序 Pod处于运行状态。
如果部署失败,解决部署失败的原因,并解决问题。
要取消失败的部署, 您可以使用 oc rollout cancel 命令。 要重新配置应用程序, 请使用 oc rollout latest 命令。
4. 删除 php-helloworld 应用程序的全部资源。 不要删除 todoapp 项目。
5. 为 MySQL 配置持久存储。在 services 创建并导出 /var/export/dbvol NFS 共享,确认 node1、node2 都能读写该目录。
在 workstation VM 上的 /home/student/DO280/labs/review-deploy/config-nfs.sh 文件为您提供了一个自动生成 NFS 共享创建的脚本。
6. 以 admin 登录,创建 mysql-pv,容量为 2 GB,使用 services 的 /var/export/dbvol NFS 共享,访问模式允许多个客户端同时读写(ReadWriteMany)。
在workstation VM的/home/student/DO280/labs/review-deploy/todoapi/openshift/mysql-pv.yaml文件中,为您提供了此持久卷的模板 YAML 配置文件。
确认持久卷处于可供项目 PVC 绑定的状态。
7. todoapi后端包括一个MySQL数据库容器和一个Node.js容器。为简化部署,the/home/student/DO280/labs提供了OpenShift模板,将这两个容器与其他所需资源结合起来。
操作界面、表格与配置图
review-deploy/todoapi/openshift/nodejs-mysql-template.yaml workstation VM 上的 review-deploy/todoapi/openshift/nodejs-mysql-template.yaml 文件。 简要审查此文件的内容。 不要对它做任何修改。
将模板导入 OpenShift , 确保模板在 Javascript 类下出现在 OpenShift 网络控制台。 这样可以让开发者从网络控制台创建此模板的新应用程序。
8. workstation 的 /home/student/DO280/labs/review-deploy/todoui 提供 Dockerfile 和构建文件。查看 Dockerfile,理解如何将 Apache httpd 及 HTML、CSS、JavaScript 静态资源构建为 Docker 镜像。
构建、标记和将镜像推进到 私有镜像仓库 registry.lab.example.com。
从私有镜像仓库导入 registry.lab.example.com/todoapp/todoui:latest 镜像到
将私有 Docker 镜像仓库中的镜像导入 OpenShift,确保 todoapp 命名空间创建了 todoui 镜像流。
9. 使用下列表格中的数值,使用导入的 nodejs-mysql-persistent 模板,创建一个新应用程序,作为 developer 用户:
Node.js + MySQL(持久存储)模板参数#
| 字段 | 值 |
|---|
| Name | todoapi |
| Git Repository URL | http://services.lab.example.com/todoapi |
| Application Hostname | todoapi.apps.lab.example.com |
| MySQL Username | todoapp |
| MySQL Password | todoapp |
| Database name | todoappdb |
| Database Administrator Password | redhat |
默认值由集管理者创建的 nodejs-mysql-persistent 模板中包含。除上一个表格中列出的选项外,将其他所有选项留到此页面中的默认值。
确认应用构建部署成功,应看到 todoapi 和 MySQL 数据库各一个 Pod。
10. workstation VM的/home/student/DO280/labs/review-deploy/todoapi/sql/db.sql档案中为您提供了待办事宜清单应用程序的样本数据。
操作界面、表格与配置图
使用 OpenShift port-forwarding 执行 SQL 语句,并将数据输入MySQL 中的 todoappdb 数据库。
将 workstation 的本地端口 3306 转发到 MySQL Pod 的 3306 端口。
/home/student/DO280/labs/review-deploy/todoapi/sql/import-data.sh 提供数据导入脚本。查看脚本,在建立 MySQL Pod 端口转发后,于 workstation 执行。
11. 确认 todoapi 后端 API 部署运行正常。用 curl 访问 http://todoapi.apps.lab.example.com/todo/api/host,输出应为承载应用的 Pod 名。
12. 使用 curl 命令验证 todoapi 应用程序是否从 MySQL 获取数据
访问 http://todoapi.apps.lab.example.com/todo/api/items,
以 JSON 格式打印 TODO 项列表。
13. 在 workstation 以 developer 用户使用先前导入的 todoui 镜像流,创建 todoui 应用。
核查部署是否成功。
14. 使用todo.apps.lab.example.com.的hostname为thetodoui 服务创建新路由
使用 workstation VM 上的浏览器浏览到待办列表的主页
应用地址 http://todo.apps.lab.example.com。应看到待办事项应用,
应用程序中包含存储在数据库中的项目列表。 添加、 编辑和删除任务, 以确保应用程序正常运行。
运行以下命令来给您的工作评分 :
1
| [student@workstation ~]$ lab review-deploy grade
|
如果总评不是 PASS,检查操作后重新评分。
操作界面、表格与配置图
本综合实验在 OpenShift 部署多容器应用,并排查问题,确保部署成功。
学习目标#
- 能够:
允许用户在项目内创建新应用程序。 不允许用户创建新项目。 对项目应用资源配额来控制资源使用。
使用一个简单的 Source-to-Image (s2l) 应用程序来验证配额设置。 在应用程序部署过程中, 故障排除和修正问题。
:为应用程序使用的 MySQL 数据库建立持久性存储。
:在 OpenShift 上使用模板部署 多容器 应用程序。该模板应提供给网络控制台的用户。
。构建用于 多容器 应用程序的自定义容器镜像。
开始之前#
如果您没有使用master和两个节点运行的 OpenShift 集, 您必须完成上一节中全面审查实验 Installing OpenShift 中的一个选项。
运行以下命令, 以验证您的环境已准备好启动此审查实验, 并下载此实验所需的文件 :
1
| [student@workstation ~]$ lab review-deploy setup
|
workstation 的 /home/student/DO280/labs/review-deploy 提供本实验的配置文件、脚本,以及持久卷等资源的 YAML 示例。
Red Hat OpenShift Container Platform 产品文档在 workstation 主机桌面中提供。使用文档作为此实验所需的命令的参考。
待办事项(TODO List)应用由三个容器组成:
:一个 MySQL 数据库容器,储存TODO清单中的任务数据。
Apache httpd 前端容器 todoui 提供 HTML、CSS 和 JavaScript 静态资源,用户界面使用 AngularJS 框架。
基于 Node.js 的 API 后端容器 todoapi,向前端提供 RESTful 接口,并连接 MySQL 数据库管理应用数据。
操作说明
执行前仔细阅读这些指令。 本实验使用两个用户。 集admin 用户 admin 拥有 redhat, 的密码, developer 用户拥有
操作界面、表格与配置图
redhat. 的密码 这些用户是安装和配置 OpenShift 集时创建的。
1. 在 workstation 以集群管理员 admin 登录。禁止普通用户创建新项目,只允许集群管理员创建项目。
2. 创建 todoapp 项目,允许 developer 访问该项目并在其中创建应用。
确定todoapp项目的配额如下:
:配额名为todoapp-quota.
配额设定了1pod的硬限值。
若不想逐条输入,可在 workstation 执行 /home/student/DO280/labs/review-deploy/set-quotas.sh。
以 developer 登录,部署 php-helloworld 验证配额。源代码地址为 http://
services.lab.example.com/php-helloworld。应用名称应为
hello.
验证 php-helloworld 应用程序的部署是否成功。 确保应用程序 Pod处于运行状态。
如果部署失败,解决部署失败的原因,并解决问题。
要取消失败的部署, 您可以使用 oc rollout cancel 命令。 要重新配置一个应用程序, 请使用 oc rollout latest 命令。
4. 删除 php-helloworld 应用程序的全部资源。 不要删除 todoapp 项目。
5. 为 MySQL 数据库提供持久性存储。 在 /var/export/dbvol. 中为 services VM 创建新的 NFS 共享。 验证 node1 和 node2 VMs 能够读写此 NFS 共享文件夹。
在 workstation VM 上的 /home/student/DO280/labs/review-deploy/config-nfs.sh 文件为您提供了一个自动生成 NFS 共享创建的脚本。
6. 以 admin 登录,创建 mysql-pv,容量为 2 GB,使用 services 的 /var/export/dbvol NFS 共享,访问模式允许多个客户端同时读写(ReadWriteMany)。
此持久卷的模板 YAML 配置文件在
workstation VM 中的 pv.yaml 文件。
确认持久卷处于可供项目 PVC 绑定的状态。
7. todoapi后端包括一个MySQL数据库容器和一个Node.js容器。为了简化部署,/home/student/DO280/labs/中提供了OpenShift模板,将这两个容器与其他所需资源结合起来。
操作界面、表格与配置图
review-deploy/todoapi/openshift/nodejs-mysql-template.yaml workstation VM 上的 review-deploy/todoapi/openshift/nodejs-mysql-template.yaml 文件。 简要审查此文件的内容。 不要对它做任何修改。
将模板导入 OpenShift , 确保模板在 Javascript 类下出现在 OpenShift 网络控制台。 这样可以让开发者从网络控制台创建此模板的新应用程序。
8. workstation 的 /home/student/DO280/labs/review-deploy/todoui 提供 Dockerfile 和构建文件。查看 Dockerfile,理解如何将 Apache httpd 及 HTML、CSS、JavaScript 静态资源构建为 Docker 镜像。
构建、标记和将镜像推进到 私有镜像仓库 registry.lab.example.com。
从私人 Docker 镜像仓库导入 registry.lab.example.com/todoapp/todoui:latest 镜像到 OpenShift。 确保在 todoapp 命名空间创建名为 todoui 的镜像流。
9. 以 developer 用户使用已导入的 nodejs-mysql-
persistent 模板,使用以下表格中的数值:
Node.js + MySQL(持久存储)模板参数#
| 字段 | 值 |
|---|
| Name | todoapi |
| Git Repository URL | http://services.lab.example.com/todoapi |
| Application Hostname | todoapi.apps.lab.example.com |
| MySQL Username | todoapp |
| MySQL Password | todoapp |
| Database name | todoappdb |
| Database Administrator Password | redhat |
默认值由集管理者创建的 nodejs-mysql-persistent 模板中包含。除上一个表格中列出的选项外,将其他所有选项留到此页面中的默认值。
确认应用构建部署成功,应看到 todoapi 和 MySQL 数据库各一个 Pod。
10. workstation 的 /home/student/DO280/labs/review-deploy/todoapi/sql/db.sql 提供待办事项应用的示例数据。
操作界面、表格与配置图
使用 OpenShift port-forwarding 执行 SQL 语句,并将数据输入MySQL 中的 todoappdb 数据库。
将 workstation 的本地端口 3306 转发到 MySQL Pod 的 3306 端口。
/home/student/DO280/labs/review-deploy/todoapi/sql/import-data.sh 提供数据导入脚本。建立 MySQL Pod 端口转发后,查看并在 workstation 执行该脚本。
11. 确认 todoapi 后端 API 部署运行正常。用 curl 访问 http://todoapi.apps.lab.example.com/todo/api/host,输出应为承载应用的 Pod 名。
12. 校验todoapi 应用程序是否通过使用 curl 命令访问URL http://todoapi.apps.lab.example.com/todo/api/items,, 从 MySQL 获取数据, 该命令以 JSON 格式打印 TODO 项列表。
13. 在 workstation 以 developer 用户使用先前导入的 todoui 镜像流,创建 todoui 应用。
核查部署是否成功。
14. 为todo.apps.lab.example.com. 的hostname 创建todoui 服务新路由
在 workstation 浏览器访问 http://todo.apps.lab.example.com,查看数据库中的待办事项,测试添加、编辑和删除,确认应用正常运行。
操作步骤#
1. 在 workstation 以集群管理员登录,禁止普通用户创建项目。
1.1. 在 workstation VM 上打开终端窗口, 以 redhat. 密码登录为 admin 用户
1
2
| [student@workstation ~]$ oc login -u admin -p redhat \
https://master.lab.example.com
|
1.2. 将项目创建限制为只发挥集群管理者的作用。 普通用户不能创建新项目。
1
2
3
4
| [student@workstation ~]$ oc adm policy remove-cluster-role-from-group \
self-provisioner system:authenticated system: authenticated:oauth
- cluster role ""self-provisioner" removed: ["system:authenticated"
"system: authenticated: oauth"]
|
操作界面、表格与配置图
2. 创建新项目,允许 developer 访问,并设置资源配额。
2.1. 创建一个名为 todoapp: 的新项目
1
2
| [student@workstation ~]$ oc new-project todoapp
Now using project "todoapp" on server "https://master.lab.example.com:443",
|
允许 developer 访问 todoapp 项目。 确保 todoapp 是您当前的项目 :
1
2
3
| inainalll ee as a i OM lf
[student@workstation ~]$ oc policy add-role-to-user edit developer
role "edit" added: "developer"
|
2.2. 在 workstation 执行 /home/student/DO280/labs/review-deploy/set-quotas.sh,为 todoapp 设置配额:
1
2
3
4
| [student@workstation ~]$ /home/student/DO280/labs/review-deploy/set-quotas.sh
Setting quotas on the todoapp project...
Already on project "todoapp" on server "https://master.lab.example.com".
resourcequota "todoapp-quota" created
|
3. 使用 Source-to-Image(S2I)部署 php-helloworld,验证项目配额。Git 地址为 http://services.lab.example.com/php-helloworld。
3.1. 在 workstation 打开新终端,以 developer 登录 https://master.lab.example.com,密码为 redhat,并接受不安全连接提示:
1
2
3
4
5
6
7
8
9
| [student@workstation ~]$ oc login -u developer -p redhat \
https://master.lab.example.com
The server uses a certificate signed by an unknown authority.
You can bypass the certificate check, but any data you send to the server could
be intercepted by others.
Use insecure connections? (y/n): y
Login successful.
You have one project on this server: "todoapp"
Using project "todoapp".
|
3.2. 创建一个名为 hello: 的新应用程序
1
2
| [student@workstation ~]$ oc new-app --name=hello \
php:5.6~http://services.lab.example.com/php-helloworld
|
3.3. 验证应用程序是否成功构建 :
1
2
| [student@workstation ~]$ oc logs -f bc/hello
Cloning "http://workstation.lab.example.com/php-helloworld" ...
|
操作界面、表格与配置图
1
2
3
4
5
6
7
8
9
10
11
12
| Commit: TeolofodavscasbdobdT fyubss2yoddadceeodcoscsec (initial commit)
Author: root <root@workstation.lab.example.com>
Date: Fri Jul 28 16:59:33 2017 +0530
---> Installing application source...
Pushing image 172.30.238.48:5000/todoapp/hello:latest ...
Pushed 0/5 layers, 7% complete
Pushed 1/5 layers, 20% complete
- Pushed 2/5 layers, 44% complete
- Pushed 3/5 layers, 77% complete
- Pushed 4/5 layers, 97% complete
Pushed 5/5 layers, 100% complete
- Push successful
|
3.4. 确认应用构建成功,但应用 Pod 尚未创建。
1
2
3
| [student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
heilo-1-build 0/1 Completed 0 3m
|
3m
4. 排查 php-helloworld 部署失败的原因并修复。
4.1. 检查项目的事件日志 :
1
2
3
4
5
| [student@workstation ~]$ oc get events
Warning...FailedCreate...deployer-controller
Error creating deployer pod: pods ""hello-1-deploy" is forbidden: exceeded quota:
todoapp- quota,
requested: pods=1, used: pods=1, limited: pods=1
|
事件日志显示, 应用程序要求的 pod 数量超过了项目允许的 pod 数量。 请检查部署配置状态 :
1
2
3
| [student@workstation ~]$ oc get dc
NAME REVISION DESIRED CURRENT TRIGGERED BY
hello 1 1 0 config, image(hello: latest)
|
注意 CURRENT 列为 0。
4.2. 检查项目的配额设置 :
1
2
3
4
5
| [student@workstation ~]$ oc describe quota
Name: todoapp-quota
Namespace: todoapp
Resource Used Hard
pods al 1
|
命令的输出显示, pod 计数的硬限制设为 1。 这是一个非常低的数字, 不足以构建、 部署和运行应用程序。 回顾您在 step 2.2 中执行了一个脚本来设定项目的配额。 审查 /home/student/DO280/labs/review-deploy/set-quotas.sh 文件的内容。
操作界面、表格与配置图
在 workstation 查看脚本,可以发现管理员创建 todoapp 配额时误输入了数量。
Pod计数被错误地输入为 1 而不是 10。
4.3. 取消应用程序的现有部署 :
1
2
| [student@workstation ~]$ oc rollout cancel de/hella
deploymentconfig "hello" cancelling
|
4.4. 执行以下命令,作为workstation VM 的集群管理器,以此校正配额设置。 确保todoapp 是您当前执行的项目。
在 /home/student/DO280/labs/review-deploy/fix-quotas.sh. 中,命令也可以作为脚本使用。
1
2
3
4
5
| [student@workstation ~]$ oc login -u admin -p redhat
[student@workstation ~]$ oc project todoapp
[student@workstation ~]$ oc patch resourcequota/todoapp-quota \
--patch '{"spec":{"hard":{"pods":"10"}}}'
"todoapp-quota" patched
|
也可使用 oc edit resourcequota todoapp-quota 修改配额。
4.5. 切换到 workstation 上的 OpenShift developer 用户。 请确认Pod数的配额已递增到 10。
1
2
3
4
5
6
| [student@workstation ~]$ oc login -u developer -p redhat
[student@workstation ~]$ oc describe quota
Name: todoapp-quota
Namespace: todoapp
Resource Used Hard
pods (0) 10
|
4.6. 重新部署应用:
1
2
| [student@workstation ~]$ oc rollout latest dc/hello
deploymentconfig "hello" rolled out
|
4.7. 检查项目中的Pod状态 :
1
2
3
| [student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
hello-i-build 0/1 Completed (0) th
|
1h
操作界面、表格与配置图
1
| N€LlO- 2-LxXQKr i/i RUANNANG 10) os
|
应用软件的部署现在应该成功, Pod必须位于 Running 状态。
4.8. 删除 S2I 示例应用:
1
2
3
4
5
| [student@workstation ~]$ oc delete all -1 app=hello
buildconfig "hello" deleted
imagestream "hello" deleted
deploymentconfig "hello" deleted
service "hello" deleted
|
5. 现在开始部署待办事项应用。先为应用使用的 MySQL 数据库提供持久存储。
5.1. 在 services VM. 上新建 NfS 共享
在 services VM 上运行位于 the/home/student/DO280/labs /review-deploy 文件夹中的 config-nfs.sh 脚本。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| [student@workstation ~]$ cd /home/student/D02860/labs/review-deploy
[student@workstation review-deploy]$ scp config-nfs.sh services: /home/student/
config-nfs.sh
Warning: Permanently added 'services' (ECDSA) to the list of known hosts.
config-nfs.sh 100% 779 423.2KB/s 00:00
[student@workstation review-deploy]$ ssh services sudo /home/student/config-
nfs.sh
Export directory /var/export/dbvol created.
Export list for services.lab.example.com:
/exports/prometheus-alertbuffer ~*
/exports/prometheus-alertmanager *
/exports/prometheus '
/exports/etcd-vol2 r
/exports/logging-es-ops -
/exports/logging-es *
/exports/metrics .
/exports/registry *
/var/export/dbvol ia
|
5.2. 确认 node1 和 node2 能访问 NFS 导出卷。
在 workstation 打开新终端,登录 node1 并切换到 root,确认能访问 services 的 NFS 共享。
1
2
3
| [student@workstation review-deploy]$ ssh node1
[student@node1 ~]$ sudo -i
[root@node1 ~]# mount -t nfs services.lab.example.com:/var/export/dbvol /mnt
|
5.3. 在 node1 上, 验证文件系统是否拥有来自 node1 的正确权限 :
1
2
| [root@node1 ~]# ls -la /mnt ; mount | grep /mnt
total @
|
操作界面、表格与配置图
1
2
3
4
5
6
| agrwx------. 2 nfisnobody nfsnobody 6 Jul 18 02:07 .
dr-xr-xr-x. 17 root root 224 Jun 5 08:54 ..
services.lab.example.com:/var/export/dbvol on /mnt type nfs4
(rw, relatime, vers=4.1, rsize=262144,wsize=262144, namlen=255, hard,
proto=tcp, port=0, timeo-600, retrans=2, sec=sys,
clientaddr=172.25.250.11, local_lock=none, addr=172.25.250.13)
|
5.4. 卸载 NFS 共享 :
1
2
3
| f a
[root@node1 ~]# umount /mnt
|
|
5.5. 同样登录 node2 并切换到 root,确认能访问 NFS 共享。
1
| [root@node2 ~]# mount -t nfs services.lab.example.com:/var/export/dbvol /mnt
|
5.6. 在 node2, 上验证文件系统是否拥有node2: 的正确权限
1
2
3
4
5
6
7
8
| [root@node2 ~]# ls -la /mnt ; mount | grep /mnt
total 0
drwx------. 2 nfsnobody nfsnobody 6 Jul 18 02:07
dr-xr-xr-x. 17 root root 224 Jun 5 08:54 ..
services.lab.example.com:/var/export/dbvol on /mnt type nfs4
(rw, relatime, vers=4.1, rsize=262144, wsize=262144, namlen=255, hard,
proto=tcp, port=0, timeo=600, retrans=2, sec=sys,
clientaddr=172.25.250.12,local_lock=none, addr=172.25.250.13)
|
5.7. 卸载 NFS 共享 :
1
| [root@node2 ~]# umount /mnt
|
5.8. 为 MySQL 创建持久卷。
在 workstation VM 上, 编辑 /home/student/DO280/labs/review-deploy/todoapi/openshift/mysql-pv.yaml 文件中提供的 YAML 资源文件。 更改属性以匹配实验指令中提供的值, 以创建持久卷。 最后内容应该是 :
1
2
3
4
5
6
7
8
9
10
11
| apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteMany
nfs:
path: /var/export/dbvol
|
操作界面、表格与配置图
1
| server: services.ilabd.exampie.com
|
5.9. 以集群admin 用户 admin. 身份登录到 OpenShift 使用提供的 YAML 资源定义文件创建持久卷 :
1
2
3
4
| [student@workstation ~]$ oc login -u admin -p redhat
[student@workstation ~]$ oc create -f \
/home/student/DO280/labs/review-deploy/todoapi/openshift/mysql-pv.yaml
- persistentvolume "mysql-pv" created
|
5.10. 确认持久卷处于可供项目 PVC 绑定的状态:
1
2
3
| [student@workstation ~]$ oc get pv
NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS ».. AGE
mysql-pv 2Gi RWX Retain Available ... 8s
|
6. 将 nodejs-mysql-persistent 模板导入 OpenShift。
OpenShift 模板,将 todoapi 和 mysql Pod及其相关服务结合起来,在 workstation VM 上的 the/home/student/DO280/labs/review-deploy /todoapi/openshift/nodejs-mysql-template.yaml 文件里向您提供。
6.1. 简要审查模板文件的内容。 不要编辑或修改此文件。
6.2. 将模板导入 openshift 命名空间, 以便用户可以使用该模板创建应用程序 :
1
2
3
4
| [student@workstation ~]$ oc apply -n openshift -f \
/home/student/DO280/labs/review-deploy/todoapi/openshift/nodejs-mysql-
template.yaml
template "nodejs-mysql-persistent" created
|
7. 在 workstation 构建 todoui Docker 镜像,打标签并推送到课堂私有仓库。
Dockerfile 及相关构建文件位于 workstation 的 /home/student/DO280/labs/review-deploy/todoui。简要查看 Dockerfile。
7.1. 在 workstation 执行 /home/student/DO280/labs/review-deploy/todoui/build.sh,构建 todoui 镜像:
1
2
3
4
5
6
| [student@workstation ~]$ cd /home/student/DO280/labs/review-deploy/todoui/
[student@workstation todoui]$ ./build.sh
Sending build context to Docker daemon 647.2 kB
Step 1/11 : FROM rhel7:7.5
Trying to pull repository registry.lab.example.com/rhel7 ...
7.5: Pulling from registry.lab.example.com/rhel7
|
操作界面、表格与配置图
1
| successtully BUu1lLt 531063448385
|
7.2. 标记新建的 todoui docker 镜像 :
1
2
| [student@workstation todoui]$ docker tag todoapp/todoui:latest \
registry.lab.example.com/todoapp/todoui:latest
|
7.3. 将带新标签的 todoui 镜像推送到 services 上的课堂私有 Docker 仓库:
1
2
3
| [student@workstation todoui]$ docker push \
registry.lab.example.com/todoapp/todoui:latest
The push refers to a repository [registry.lab.example.com/todoapp/todoui]
|
8. 将 todoui docker 镜像导入 OpenShift 并验证镜像流是否创建。
8.1. 从workstation:上的私有镜像仓库导入 todoui docker 镜像
1
2
3
4
5
6
7
8
9
| [student@workstation todoui]$ oc whoami -c
todoapp/master- lab-example-com: 443/admin
[student@workstation todoui]$ oc import-image todoui \
--from=registry.lab.example.com/todoapp/todoui \
--confirm -n todoapp
The import completed successfully.
Name: todoui
Namespace: todoapp
Created: Less than a second ago
|
8.2. 验证 todoui 镜像流是否已创建 :
1
2
| [student@workstation todoui]$ oc get is -n todoapp | grep todoui
todoui docker-registry.default.svc:5000/todoapp/todoui latest 44 seconds ago
|
8.3. 描述 todoui 镜像流, 并校验最新的 todoui docker 镜像是否被引用 :
1
2
3
4
5
6
7
8
| [student@workstation todoui]$ oc describe is todoui -n todoapp
Name: todoui
Namespace: todoapp
Created: 2 minutes ago
Unique Images: 4
Tags: ai
latest
tagged from registry.lab.example.com/todoapp/todoui
|
操作界面、表格与配置图
9. 在 OpenShift Web 控制台使用 nodejs-mysql-persistent 模板创建应用。
9.1. 在 workstation 浏览器访问 https://master.lab.example.com,以 developer 登录,密码为 redhat。确认只有 todoapp 项目可见。如需,接受自签名证书。
9.2. 单击 todoapp > Add to Project > Browse Catalog,再选择 Languages > JavaScript。模板列表应包含 Node.js + MySQL (Persistent)。
如果该模板无法为 developer 用户所可见, 请登录为 admin 用户, 然后再试一次。 以 admin. 进行以下步骤是安全的
9.3. 选择 Node.js + MySQL (Persistent) 模板,按下表创建 todoapi 应用:
Node.js + MySQL(持久存储)模板参数#
| 字段 | 值 |
|---|
| Name | todoapi |
| Git Repository URL | http://services.lab.example.com/todoapi |
| Application Hostname | todoapi.apps.lab.example.com |
| MySQL Username | todoapp |
| MySQL Password | todoapp |
| Database name | todoappdb |
| Database Administrator Password | redhat |
单击 Next 按钮。 忽略 Binding 页面上的值, 单击 Create 按钮。 当显示确认页时, 单击 Close 按钮。
9.4. 点击 Overview 链接以查看应用程序正在建立和部署。
将待办事项应用的 SQL 数据导入 MySQL,测试 todoapi 后端 API。
10.1. 在 workstation 打开新终端,以 developer 登录 https://master.lab.example.com,应只能访问 todoapp 项目。
10.2. workstation 的 /home/student/DO280/labs/review-deploy/todoapi/sql/db.sql 提供示例数据。
操作界面、表格与配置图
使用 oc port-forward 直接连接 MySQL Pod。先用 oc get pods 获取数据库 Pod 名:
1
2
3
| [student@workstation todoui]$ cd
[student@workstation ~]$ oc port-forward mysql-1-crxnw 3306:3306
Forwarding from 127.0.0.1:3306 -> 3306
|
此命令不返回到 shell 提示。 请按原样保留此终端窗口, 然后在 workstation VM 上打开一个新的终端窗口, 以便执行下一步。
10.3. 将 db.sql 数据导入 todoappdb 数据库。
执行 /home/student/DO280/labs/review-deploy/todoapi/sql/import-data.sh。成功后会列出导入数据库的记录:
1
2
3
4
| [student@workstation ~]$ bash \
/home/student/DO280/labs/review-deploy/todoapi/sql/import-data.sh
Importing data into database...
The following records have been imported into the database:
|
id description I done
1
2
3
4
5
| yee ee ee oeeea eee, ee
2 | Pick up newspaper | FALSE |
2 | Buy groceries I) EWE |
oe ise ao co aie Se eo ASE 4
DONE!
|
10.4. 用 curl 访问 http://todoapi.apps.lab.example.com/todo/api/host,确认输出应用所在的 Pod 名和 IP 地址。
1
2
3
4
5
6
7
| [student@workstation ~]$ curl -s \
http: //todoapi.apps.lab.example.com/todo/api/host \
python -m json.tool
{
"hostname": "todoapi-2-gzb54",
"ip": "10.130.0.20"
}
|
10.5. 用 curl 访问 http://todoapi.apps.lab.example.com/todo/api/items,确认能从 MySQL 获取并列出任务。
1
2
3
4
5
6
7
8
| [student@workstation ~]$ curl -s \
http://todoapi.apps.lab.example.com/todo/api/items \
python -m json.tool
ii
"currentPage": 1,
Wiisisiee |p
{
"description": "Pick up newspaper",
|
操作界面、表格与配置图
1
2
3
4
5
| "done": false,
Dsiege ah
"description": "Buy groceries",
"done": true,
"id": 2
|
3,
1
2
3
4
5
| 4dr
"pageSize": 10,
"sortDirections": "asc",
""sortFields": "id",
"totalResults": 2
|
10.6. 在运行 oc port-forward 的终端按 Ctrl+C,停止端口转发。
11. 使用 todoui 镜像流创建新应用程序。
11.1. 根据 todoui 镜像流创建名为 todoui, 的新应用程序 :
1
2
3
4
5
6
7
8
9
10
11
12
13
| [student@workstation ~]$ oc new-app --name=todoui -i todoui
~-> Found image 531b634 (14 minutes old) in image stream "todoapp/todoui" under
tag "latest" for "todoui"
Red Hat Enterprise Linux 7
Tags: base rhel7
* This image will be deployed in deployment config "todoui"
* Port 8080/tcp will be load balanced by service "todoui"
* Other containers can access this service through the hostname "todoui"
--> Creating resources ...
deploymentconfig "todoui" created
service "todoui" created
--> Success
Run 'oc status' to view your app.
|
11.2. 审查项目中的Pod状况:
1
2
3
4
5
6
| [student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
mysql-1-crxnw 1/1 Running 0 th
todoapi-1-buiid 0/1 Completed (0) th
todoapi-1-h37hc 1/1 Running 0 th
todoui-1-dmw7j 1/1 Running 0) 9s
|
1h
1h todoapi-1-build 0/1 Completed
1
2
3
| LUUVaPLrW41 VU Ui VUMNPLOCCU Y at
todoapi-1-h37hc 1/1 Running 0 ah
todoui-1-dmw7j 1/1 Running (0) 9s
|
9s
在 OpenShift 网络控制台, 您现在应该看到在 Overview 页面上为此项目新建一个 todoui pod。
12. 创建 todoui 应用程序的路由。
12.1. 向外部用户公开 Service:
1
2
| [student@workstation ~]$ oc expose svc todoui \
--hostname=todo.apps.lab.example.com
|
操作界面、表格与配置图
12.2. 使用路由 URL 访问 todoui:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| [student@workstation ~]$ curl http://todo.apps.lab.example.com
<!DOCTYPE html>
<html ng-app="items">
<head>
<title>To Do List</title>
<!-- Form buttons. The 'Save' button is only enabled when the form is valid. -->
<div class="buttons">
<button type="button" class="btn btn-primary" ng-
click="clearForm( )">Clear</button>
<button type="submit" class="btn btn-primary" ng-
disabled="itemForm. $invalid">Save</button>
</div>
</form>
</div>
</div>
</body>
</html>
|
13. 最后,在 workstation 浏览器访问 http://todo.apps.lab.example.com,查看数据库中的待办事项,测试添加、编辑和删除,确认应用正常运行。
不要删除从数据库预加载的前两个任务。 这些任务被评分脚本用于验证实验是否成功完成。
To Do L列表应用程序#
To Do List Add Task
PI Descrlptlon: Descriptlon Done
1
2
| Pickup news... false x
Buygroceries true x Add Description
|
Completed:
Clear Save
First Previous Next Last
操作界面、表格与配置图
运行以下命令来给您的工作评分 :
1
| [student@workstation ~]$ lab review-deploy grade
|
如果总评不是 PASS,检查操作后重新评分。
操作界面、表格与配置图
redhat.
操作界面、表格与配置图
自测问题#
常用命令索引#
| 任务 | 命令 |
|---|
| 身份 / 项目 | oc whoami / oc project |
| 全局 Pod | oc get pods --all-namespaces -o wide |
| 选择器 | oc get pods -l app=web --show-labels |
| 对象事件 | oc describe pod POD |
| 项目事件 | oc get events --sort-by=.metadata.creationTimestamp |
| 上次退出日志 | oc logs POD --previous |
| 部署进度 | oc rollout status dc/web --timeout=180s |
| 副本调整 | oc scale dc/web --replicas=3 |
| 镜像切换 | oc set image dc/web web=镜像 |
| 版本回退 | oc rollout undo dc/web |
| 镜像流导入 | oc import-image web:v1 --from=镜像 --confirm |
| 后端地址 | oc get endpoints web -o yaml |
| 项目授权 | oc adm policy add-role-to-user edit 用户 -n 项目 |
| SA 的 SCC | oc adm policy add-scc-to-user anyuid -z SA -n 项目 |
| 卷关系 | oc get pv / oc get pvc -n 项目 |
| 配额 | oc describe quota -n 项目 |
| 节点标签 | oc get nodes -L region |
| 用量 | oc adm top nodes / oc adm top pods -n 项目 |
平台验收需要同时满足对象状态、权限边界、请求路径和数据行为。修改后重复同一个业务请求,再观察对应的控制器、网络后端或持久数据,才能判断修复是否完成。