金鱼哥RHCA回忆录:DO280OpenShift命令及故障排查--访问资源和资源类型

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 第四章 OpenShift命令及故障排查--访问资源和资源类型
🎹 个人简介:大家好,我是 金鱼哥,CSDN运维领域新星创作者,华为云·云享专家,阿里云社区·专家博主
📚个人资质: CCNA、HCNP、CSNA(网络分析师),软考初级、中级网络工程师、RHCSA、RHCE、RHCA、RHCI、ITIL😜
💬格言:努力不一定成功,但要想成功就必须努力🔥

🎈支持我:可点赞👍、可收藏⭐️、可留言📝


📜CLI访问OpenShift资源

📑资源操作

OCP将OpenShift集群中的为由主节点管理的对象统称为资源,如:node、service、pod、project、deployment、user。

即使针对的是不同的资源,OpenShift命令行工具也提供了一种统一的、一致的方法来更新、修改、删除和查询这些资源。

oc命令行工具提供了在软件开发项目的整个交付生命周期中修改和管理资源的常见操作。


📑安装oc工具

在OpenShift安装过程中,oc命令行工具安装在所有master和node节点上,还可以在不属于OpenShift集群的机器。

安装后,可以使用用户名和密码对任何主节点通过身份验证后执行相关命令。

根据使用的平台,安装oc命令行工具有以下几种方式:

yum安装:在RHEL平台上,可通过以下命令安装oc客户端命令。

[user@host ~]$ sudo yum install atomic-openshift-clients

其它 Linux 发行版本和操作系统,需在拥有 OpenShift 订阅后,在 Red Hat Customer Portal 中下载。

oc安装完成后自动补全需要退出一次才可生效,或者source /etc/bash_completion.d/oc。


📑oc主要查询命令

[student@workstation ~]$ oc --help  # 显示帮助信息
[student@workstation ~]$ oc login -u developer -p redhat https://master.lab.example.com #登录到OpenShift集群

从client成功通过身份验证之后,OpenShift将授权令牌保存在用户的主文件夹中。此令牌用于后续请求,从而无需重新输入凭据或完整的主URL。

[root@master ~]# oc whoami
system:admin                    #master的root用户为集群的最高权限的用户
[student@workstation ~]$ oc whoami                #查看当前用户
developer
[student@workstation ~]$ oc new-project working    #创建project
[student@workstation ~]$ oc status                #查看项目状态
In project working on server https://master.lab.example.com:443
You have no services, deployment configs, or build configs.
Run 'oc new-app' to create an application.
[student@workstation ~]$ oc delete project working    #删除project
[student@workstation ~]$ oc logout                #退出该集群。
[student@workstation ~]$ oc get pods        #查看pod
NAME                      READY     STATUS    RESTARTS   AGE
hello-openshift-1-6ls8z   1/1       Running   0          4h
[student@workstation ~]$ oc get all                #查看所有主要组件信息
[student@workstation ~]$ oc get pods -w        #-w表示以监视模式运行

📑oc 其他命令

oc describe:如果oc get提供的摘要不够,可以使用oc describe命令检索关于资源的更详细信息。

[student@workstation ~]$ oc describe pod hello-openshift-1-6ls8z

oc export:使用oc export命令导出资源的定义。典型的用例包括创建备份,或者用于修改定义。默认情况下,export命令以YAML格式输出对象表示,但是可以通过提供-o选项来更改。

oc create:使用oc create命令从资源定义创建资源。通常,这与用于编辑定义的oc export命令相匹配。

oc delete RESOURCE_TYPE name:使用oc delete命令从OpenShift集群中删除资源。

注意:部分资源直接删除后会重新创建,如基于rc的pod,需要对OpenShift体系资源展示形式有一个基本的了解。

oc exec:使用oc exec命令在容器中执行命令,可以使用此命令作为脚本的一部分运行交互式和非交互式批处理命令。

oc rsh POD:oc rsh pod命令打开到容器的远程shell会话,要远程登录到容器shell并执行命令,请运行以下命令。

[student@workstation ~]$ oc rsh

注意:oc rsh需要pod中存在相应的shell,如bash。


📜OpenShift资源类型

📑常见资源

OpenShift容器平台中的应用程序由不同类型的资源组成,主要常见的类型有:

  • Container:如何在可移植Linux环境中运行一个或多个进程的定义。容器从一个镜像启动,并且通常与同一机器上的其他容器隔离。
  • Image:一个分层的Linux文件系统,包含应用程序代码、依赖关系和函数库等。image由一个名称标识,该名称可以是当前集群的本地名称,也可以指向远程Docker仓库。
  • Pod:部署在节点上并共享唯一IP地址和卷(持久存储)的一个或多个容器,Pods还为每个容器定义安全性和运行时策略。
  • Label:标签是键值对,可以分配给系统中的任何资源进行分组和选择。通常资源使用标签来标识其他资源集。
  • Volume:默认情况下容器不是持久性的,即容器的内容在重新启动时被清除。volume是挂载在pod及其容器上的文件系统,它们可能由许多本地或网络的存储提供。最简单的卷类型是EmptyDir,它是一台机器上的临时目录。
  • Node:node是集群中用来运行容器的节点,node通常由管理员管理,而不是由最终用户管理。
  • Service:service是表示一组pod的逻辑名称,service被分配一个IP地址和一个DNS名称,可以通过端口或route向集群外部公开。名为SERVICE_HOST的环境变量会自动注入到其他pod中。
  • Route:route是一个DNS条目,创建它是为了指向一个service,以便可以从集群外部访问它。可以配置一个或多个路由器来处理这些route,通常通过HAProxy负载均衡器。
  • Replication Controller:Replication Controller基于匹配一组label的Templates维护特定数量的pod。如果删除了pod,控制器将创建该pod的新副本。Replication Controller最常用来表示基于image的应用程序部分的单个部署。
  • Deployment Configuration:deployment configuration定义pod的模板,并在属性更改时管理部署新镜像或配置更改。单个deployment configuration通常类似于单个微服务。deployment configuration可以支持许多不同的部署模式,包括完全重启、可定制的滚动更新以及生命周期前后的顺序。每个deployment都表示为一个replication controller。
  • Build Configuration:build configuration包含如何将源代码和基本image构建为新image的描述。Build可以是基于源代码的,可以为常见语言(如Java、PHP、Ruby或Python)或基于docker的(从Dockerfile创建构建)使用构建器镜像。每个build configuration都有webhook,可以通过对其基本镜像的更改自动触发。
  • Build:构建从源代码、其他镜像、Dockerfiles或二进制输入创建新image。Build在容器中运行,具有与普通pod相同的限制。Build通常会导致将image推入Docker仓库中,但也可以选择运行post-build测试而不push到image仓库。
  • Image Streams and Image Stream Tags:IS使用标记名称对相关is进行分组。它类似于源代码仓库中的分支。每个is可以有一个或多个标记(默认标记称为“latest”),这些标记可能指向外部Docker仓库、同一is中的其他标记,或者被控制为直接指向已知image。此外,可以通过集成的Docker仓库直接将image push到docker仓库。
  • Secret:secret资源可以保存文本或二进制secrets,以便注入至pod。默认情况下,在/var/run/secrets/kubernetes.io/serviceaccount上,每个容器都有一个secret,其中包含访问API有限特权的令牌。可以创建新的secret并将它们挂载到自己的pod中,也可以引用构建中的secret(用于连接远程服务器),或者使用它们将远程image导入到is中。
  • Project:所有上述资源(node除外)都存在于项目中。项目具有成员列表及其role(如view、edit或admin),以及运行的pod上的一组安全控制,并限制项目可以使用多少资源,资源名称在项目中是惟一的。

使用oc types命令快速查看可用的概念和类型。


📑创建应用

简单的应用程序、复杂的多层应用程序和微服务应用程序都可以使用资源定义文件来描述。

这个文件包含许多pod定义、连接这些pod的服务定义、用于水平伸缩应用程序pod的rc或dc、用于持久存储应用程序数据的持久卷,以及OpenShift可以管理的任何其他需要的内容。

oc new-app命令可以使用-o json或-o yaml选项分别创建以json或yaml格式的定义文件的资源。可以使用oc create -f <filename>命令调用定义文件,并将其用于创建应用程序,或者与其他资源定义文件合并以创建复合应用程序。

oc new-app命令可以以许多不同的方式创建在OpenShift上运行的pod应用程序。它可以使用source-to-image (S2I)流程从现有docker镜像、Dockerfiles或原始源代码创建pod。

运行oc new-app -h命令,了解在OpenShift上创建新应用程序的所有不同选项。最常见的选项如下:

运行以下命令创建应用程序。OpenShift根据Docker配置文件的ADD_REGISTRY选项定义的仓库 pull image。

$ oc new-app mysql MYSQL_USER=user MYSQL_PASSWORD=pass MYSQL_DATABASE=testdb -l
db=mysql

根据私有仓库中的image创建应用程序。

$ oc new-app --docker-image=myregistry.com/mycompany/myapp --name=myapp

根据存储在Git库中的源代码创建应用程序。

$ oc new-app https://github.com/openshift/ruby-hello-world --name=ruby-hello

创建基于存储在Git库中的源代码并引用IS的应用程序。

$ oc new-app https://mygitrepo/php-hello -i php:7.0 --name=php-hello

从Docker配置文件的ADD_REGISTRY指令定义的可用仓库之一创建一个基于mysql镜像的应用程序。l db=mysql选项定义了一个值为mysql的db标签。

$ oc new-app mysql MYSQL_USER=user \
MYSQL_PASSWORD=pass \
MYSQL_DATABASE=testdb \
-l db=mysql

下图显示了oc new-app命令在参数为容器image时创建的Kubernetes和OpenShift资源。该命令创建dc、is和svc,可以通过端口或route从外部访问。

在这里插入图片描述

通过使用带有源代码的oc new-app,将创建一个build configuration,而bc又从源代码创建一个新的应用程序。但是,如果命令中没有使用源代码,则不会创建bc。该命令始终为应用程序创建dc和svc。


📜课本练习

📑环境准备

[student@workstation ~]$ lab install-prepare setup
[student@workstation ~]$ cd /home/student/do280-ansible
[student@workstation do280-ansible]$ ./install.sh

提示:若已经拥有一个完整环境,可不执行。


📑本练习准备

[student@workstation ~]$ lab manage-oc setup

📑验证OpenShift

[student@workstation ~]$  oc login -u admin -p redhat https://master.lab.example.com
Login successful.

You have access to the following projects and can switch between them with 'oc project <projectname>':

  * default
    kube-public
    kube-service-catalog
    kube-system
    logging
    management-infra
    openshift
    openshift-ansible-service-broker
    openshift-infra
    openshift-node
    openshift-template-service-broker
    openshift-web-console

Using project "default".

[student@workstation ~]$ oc project default 
Already on project "default" on server "https://master.lab.example.com:443".


[student@workstation ~]$ oc get nodes
NAME                     STATUS    ROLES     AGE       VERSION
master.lab.example.com   Ready     master    3d        v1.9.1+a0ce1bc657
node1.lab.example.com    Ready     compute   3d        v1.9.1+a0ce1bc657
node2.lab.example.com    Ready     compute   3d        v1.9.1+a0ce1bc657
[student@workstation ~]$ oc describe node master.lab.example.com
Name:               master.lab.example.com
Roles:              master
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
Annotations:        volumes.kubernetes.io/controller-managed-attach-detach=true
Taints:             <none>
CreationTimestamp:  Thu, 25 Feb 2021 17:09:58 +0800
Conditions:
  Type             Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----             ------  -----------------                 ------------------                ------                       -------
  OutOfDisk        False   Sun, 28 Feb 2021 22:16:28 +0800   Thu, 25 Feb 2021 17:09:58 +0800   KubeletHasSufficientDisk     kubelet has sufficient disk space available
  MemoryPressure   False   Sun, 28 Feb 2021 22:16:28 +0800   Thu, 25 Feb 2021 17:09:58 +0800   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure     False   Sun, 28 Feb 2021 22:16:28 +0800   Thu, 25 Feb 2021 17:09:58 +0800   KubeletHasNoDiskPressure     kubelet has no disk pressure
  Ready            True    Sun, 28 Feb 2021 22:16:28 +0800   Sun, 28 Feb 2021 21:21:00 +0800   KubeletReady                 kubelet is posting ready status
Addresses:
  InternalIP:  172.25.250.10
  Hostname:    master.lab.example.com
……………


[student@workstation ~]$ oc describe node node1.lab.example.com
Name:               node1.lab.example.com
Roles:              compute
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/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:  Thu, 25 Feb 2021 17:09:58 +0800
Conditions:
  Type             Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----             ------  -----------------                 ------------------                ------                       -------
  OutOfDisk        False   Sun, 28 Feb 2021 22:17:54 +0800   Fri, 26 Feb 2021 16:13:43 +0800   KubeletHasSufficientDisk     kubelet has sufficient disk space available
  MemoryPressure   False   Sun, 28 Feb 2021 22:17:54 +0800   Fri, 26 Feb 2021 16:13:43 +0800   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure     False   Sun, 28 Feb 2021 22:17:54 +0800   Fri, 26 Feb 2021 16:13:43 +0800   KubeletHasNoDiskPressure     kubelet has no disk pressure
  Ready            True    Sun, 28 Feb 2021 22:17:54 +0800   Sun, 28 Feb 2021 21:21:05 +0800   KubeletReady                 kubelet is posting ready status
Addresses:
  InternalIP:  172.25.250.11
  Hostname:    node1.lab.example.com
…………


[student@workstation ~]$ oc describe node node2.lab.example.com
Name:               node2.lab.example.com
Roles:              compute
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/hostname=node2.lab.example.com
                    node-role.kubernetes.io/compute=true
                    region=infra
Annotations:        volumes.kubernetes.io/controller-managed-attach-detach=true
Taints:             <none>
CreationTimestamp:  Thu, 25 Feb 2021 17:09:58 +0800
Conditions:
  Type             Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----             ------  -----------------                 ------------------                ------                       -------
  OutOfDisk        False   Sun, 28 Feb 2021 22:18:19 +0800   Sun, 28 Feb 2021 21:23:51 +0800   KubeletHasSufficientDisk     kubelet has sufficient disk space available
  MemoryPressure   False   Sun, 28 Feb 2021 22:18:19 +0800   Sun, 28 Feb 2021 21:23:51 +0800   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure     False   Sun, 28 Feb 2021 22:18:19 +0800   Sun, 28 Feb 2021 21:23:51 +0800   KubeletHasNoDiskPressure     kubelet has no disk pressure
  Ready            True    Sun, 28 Feb 2021 22:18:19 +0800   Sun, 28 Feb 2021 21:24:01 +0800   KubeletReady                 kubelet is posting ready status
Addresses:
  InternalIP:  172.25.250.12
  Hostname:    node2.lab.example.com
……………


[student@workstation ~]$  oc get pods -o wide
NAME                       READY     STATUS    RESTARTS   AGE       IP              NODE
docker-registry-1-bcht6    1/1       Running   2          2d        10.128.0.32     node1.lab.example.com
docker-registry-1-p8lxc    1/1       Running   2          2d        10.128.0.34     node1.lab.example.com
registry-console-1-wzgkp   1/1       Running   2          2d        10.128.0.35     node1.lab.example.com
router-1-wggf5             1/1       Running   2          2d        172.25.250.11   node1.lab.example.com
router-1-x8k9z             1/1       Running   2          2d        172.25.250.12   node2.lab.example.com


[student@workstation ~]$ oc describe pod docker-registry-1-bcht6 
Name:           docker-registry-1-bcht6
Namespace:      default
Node:           node1.lab.example.com/172.25.250.11
Start Time:     Fri, 26 Feb 2021 16:13:55 +0800
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
IP:             10.128.0.32
Controlled By:  ReplicationController/docker-registry-1
……………

📑pod操作

[student@workstation ~]$ oc exec docker-registry-1-bcht6 hostname
docker-registry-1-bcht6

[student@workstation ~]$ oc exec router-1-wggf5 ls /
bin
boot
dev
……………

[student@workstation ~]$ oc exec docker-registry-1-bcht6 cat /etc/resolv.conf 
nameserver 172.25.250.11
search default.svc.cluster.local svc.cluster.local cluster.local lab.example.com example.com
options ndots:5

[student@workstation ~]$ oc rsh docker-registry-1-bcht6 
sh-4.2$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
3: eth0@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP 
    link/ether 0a:58:0a:80:00:20 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.128.0.32/23 brd 10.128.1.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5064:91ff:fef5:890b/64 scope link 
       valid_lft forever preferred_lft forever
sh-4.2$ 
sh-4.2$ exit
exit

📑oc其他操作

[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 3 days 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 registry.lab.example.com/openshift3/registry-console:v3.9 
    deployment #1 deployed 3 days ago - 1 pod

svc/router - 172.30.0.234 ports 80, 443, 1936
  dc/router deploys registry.lab.example.com/openshift3/ose-haproxy-router:v3.9.14 
    deployment #1 deployed 3 days ago - 2 pods

View details with 'oc describe <resource>/<name>' or list everything with 'oc get all'.

[student@workstation ~]$ oc get events     # 查看集群生命周期事件
[student@workstation ~]$ oc get all        # 获取所有资源信息

📑导出资源

[student@workstation ~]$ oc export pod docker-registry-1-bcht6

提示:oc export命令通常用于导出现有资源,并将它们转换为配置文件(YAML或JSON),以便备份或在集群的其他地方重新创建资源。

[student@workstation ~]$ oc export svc,dc docker-registry --as-template=docker-registry
# 通过将--as-template选项传递给oc export命令,将多个资源作为OpenShift模板同时导出。
[student@workstation ~]$ oc export svc,dc docker-registry > docker-registry.yaml 
# 也可以使用重定向导出
[student@workstation ~]$ oc export --help #查看帮助

💡总结

RHCA认证需要经历5门的学习与考试,还是需要花不少时间去学习与备考的,好好加油,可以噶🤪。

以上就是【金鱼哥】对 第四章 OpenShift命令及故障排查--访问资源和资源类型 的简述和讲解。希望能对看到此文章的小伙伴有所帮助。

💾 红帽认证专栏系列:
RHCSA专栏: 戏说 RHCSA 认证
RHCE专栏: 戏说 RHCE 认证
此文章收录在RHCA专栏: RHCA 回忆录

如果这篇【文章】有帮助到你,希望可以给【金鱼哥】点个赞👍,创作不易,相比官方的陈述,我更喜欢用【通俗易懂】的文笔去讲解每一个知识点。

如果有对【运维技术】感兴趣,也欢迎关注❤️❤️❤️ 【金鱼哥】❤️❤️❤️,我将会给你带来巨大的【收获与惊喜】💕💕!

相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务&nbsp;ACK 容器服务&nbsp;Kubernetes&nbsp;版(简称&nbsp;ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
目录
相关文章
|
运维 关系型数据库 Linux
金鱼哥RHCA回忆录:DO280OpenShift命令及故障排查--常见故障排除和章节实验
第四章 OpenShift命令及故障排查--常见故障排除和章节实验
352 0
金鱼哥RHCA回忆录:DO280OpenShift命令及故障排查--常见故障排除和章节实验
|
运维 安全 Linux
测试理论基础--Linux故障排查思路及常用命令(上)
Linux系统与Windows系统,应急常见的问题解决方法与思路
170 0
测试理论基础--Linux故障排查思路及常用命令(上)
|
缓存 运维 网络协议
Linux 网络延迟故障排查
当我们遇到这些原因造成的延误时,我们该怎么办呢?如何定位网络延迟的根本原因?让我们在本文中讨论网络延迟。
558 0
Linux 网络延迟故障排查
|
Linux 数据安全/隐私保护 Windows
|
运维 Kubernetes 监控
金鱼哥RHCA回忆录:DO280管理和监控OpenShift平台--资源限制
第九章 管理和监控OpenShift平台--资源限制
168 0
金鱼哥RHCA回忆录:DO280管理和监控OpenShift平台--资源限制
|
运维 监控 Kubernetes
金鱼哥RHCA回忆录:DO280管理和监控OpenShift平台--使用probes监视应用
第九章 管理和监控OpenShift平台--使用probes监视应用
215 0
金鱼哥RHCA回忆录:DO280管理和监控OpenShift平台--使用probes监视应用

热门文章

最新文章