systmctlkubectl
systemctl 命令
概念
- Linux 服务管理两种方式service和systemctl,systemctl兼容了service;systemd(systemd是Linux系统最新的初始化系统(init),作用是提高系统的启动速度,尽可能启动较少的进程,尽可能更多进程并发启动)对应的进程管理命令是systemctl。systemctl命令管理systemd的资源进程(放在目录/usr/lib/systemd/system(Centos)或/etc/systemd/system(Ubuntu))
- systemd主要有以下四类文件.mount,.service,.target,.wants
常用命令
systemctl –-version #查看系统版本号
systemctl [command] [unit] #其中unit为服务进程名,#command有:start:立刻启动后面接的 unit。stop:立刻关闭后面接的 unit。restart:立刻关闭后启动后面接的 unit,亦即执行 stop 再 start 的意思。reload:不关闭 unit 的情况下,重新载入配置文件,让设置生效。enable:设置下次开机时,后面接的 unit 会被启动。disable:设置下次开机时,后面接的 unit 不会被启动。status:目前后面接的这个 unit 的状态,会列出有没有正在执行、开机时是否启动等信息。is-active:目前有没有正在运行中。is-enable:开机时有没有默认要启用这个 unit。kill :不要被 kill 这个名字吓着了,它其实是向运行 unit 的进程发送信号。show:列出 unit 的配置。mask:注销 unit,注销后你就无法启动这个 unit 了。unmask:取消对 unit 的注销。
//查看系统上的unit
systemctl 提供了子命令可以查看系统上的 unit,命令格式为:
systemctl [command] [--type=TYPE] [--all]
command 有:list-units:列出当前已经启动的 unit,如果添加 -all 选项会同时列出没有启动的 unitlist-unit-files:根据 /lib/systemd/system/ 目录内的文件列出所有的 unit--type=TYPE:可以过滤某个类型的 unit不带任何参数执行 systemctl 命令会列出所有已启动的 unit
//查看已经启动的unit
systemctl list-units (或者直接 sudo systemctl)
//查看系统上一共装了多个unit
systemctl list-unit-files
- 系统类的命令
sudo systemctl poweroff # 系统关机
sudo systemctl reboot # 重新开机
sudo systemctl suspend # 进入暂停模式
sudo systemctl hibernate # 进入休眠模式
sudo systemctl rescue # 强制进入救援模式
sudo systemctl emergency # 强制进入紧急救援模式
- 很多服务之间有依赖的,使用命令查看
systemctl list-dependencies [unit] [--reverse] 选项 --reverse 会反向追踪是谁在使用这个 unit
相关目录文件
/lib/systemd/system/ 大多数 unit 的配置文件都放在这个目录下。
/run/systemd/system/ 系统运行过程中产生的脚本,比如用户相关的脚本和会话相关的脚本。
/etc/systemd/system/ 这个目录中主要的文件都是指向 /lib/systemd/system/ 目录中的链接文件。
注意,在我们自己创建 unit 配置文件时,既可以把配置文件放在 /lib/systemd/system/ 目录下,也可放在 /etc/systemd/system/ 目录下。
kubectl命令
概述
kubectl只是个go编写的可执行程序,只要为kubectl配置合适的kubeconfig,就可以在集群中的任意节点使用。kubectl默认会从$HOME/.kube目录下查找文件名为
config
的文件,也可以通过设置环境变量KUBECONFIG
或者通过设置--kubeconfig
去指定其它 kubeconfig 文件。总之kubeconfig就是为访问集群所作的配置。操作k8s集群可以分为两种方式:
使用kubectl命令行工具;
通过Dashboard页面可视化操作(rancher就是个dashboard)。Dashboard在k8s上是以插件的方式安装,集群出现问题时可能无法正常显示,还需要使用kubectl命令行工具来排查问题,并且命令行的方式效率很高,因此kubectl的安装与配置是管理员必须要掌握的。kubectl本身只是一个可执行文件,不需要处理依赖关系,因此安装很简单,根据安装平台,下载相应的可执行文件然后配置环境变量既可。国内用户最可能遇到的问题就是无法下载。
安装教程
安装流程
https://blog.csdn.net/boling_cavalry/article/details/79624655?ops_request_misc=&request_id=&biz_id=102&utm_term=kubectl%20%E5%AE%89%E8%A3%85&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-5-79624655.pc_search_result_control_group&spm=1018.2226.3001.4187
下载kubectl安装包,直接在linux主机上执行curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
下载好的文件的文件名就是kubectl,这不是一个目录,而是一个二进制的文件(==要改变对于“文件的认识”,文件包括实际的二进制文件、配置文件等,其不可再分,也包括目录,可再进入)
然后对文件附加魔法(附加chmod x 权限)
?将安装包移动到/usr/local/bin/kubectl,这个目录是全局访问目录,在这个目录下的文件,在linux中的任何目录都能执行,所以之一步保证kubectl能被主机发现和使用
在linux的任何地方生成一个config,然后在环境变量的文件(/etc/profile)中追加一个环境变量(设置环境变量
KUBECONFIG
,其值为config的位置),==kubectl默认会从$HOME/.kube目录下查找文件名为config
的文件;==为的是将kubernetes master与主机绑定,从而在主机上能执行kubectl,否则不是不能用kubectl,而是找不到集群,注意更改环境变量后要使其生效,要执行source /etc/profile,这个config集群里放的是rancher集群生成的kubeconfig,这样Kubectl就绑定了rancher创建的集群,将rancher和本机kubectl建立联系,当然如果不用rancher,则这个config也可以自己写,内容是绑定本机的集群,但是config的格式都是固定的
当用kubectl出现问题The connection to the server localhost:8080 was refused - did you specify the right host or port?时,参考https://blog.csdn.net/M82_A1/article/details/99671934?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163264656316780265449010%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=163264656316780265449010&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allbaidu_landing_v2~default-4-99671934.pc_search_result_control_group&utm_term=The+connection+to+the+server+localhost%3A8080+was+refused±+did+you+specify+the+right+host+or+port%3F&spm=1018.2226.3001.4187
==注意:config是建立在~/.kube而不是 ~/kube,注意这个kube文件名!!!==否则就回会报以上的错
rancher kubectl 集群 三者的关系
- 之前因为端口冲突而删除了rancher,重建rancher后由于K8s集群还在,本可在新rancher上导入K8s,但是出错了,而且根据CSDN上的办法,说是可能是原有集群配置文件未删除干净,因此把~/.kube文件夹删除了,里面有config文件,而且恢复不了了因此千万不能删除.kube文件,因为里面有config
- 底层逻辑原因在于,kubectl管理本机上的所有K8s集群,在rancher中不管是新建还是导入K8s集群,其一开始都要生成一段kubectl命令,需要将命令在要部署或者已经部署了K8s的主机上执行,通过kubectl执行这段命令,(若是新建集群)则rancher通过这条命令,在K8s主机上生成一个rancher-agent,来操作这个主机上的kubectl通过kubectl命令再在这台主机上创建集群,(若是导入集群)则通过这条命令在rancher上注册这个集群,后续就可以通过rancher操作K8s主机上的kubectl命令来管理这个集群。而.kube文件是kubectl发现k8s集群的纽带,删除了后Kubectl就和集群失联了,当然就不能用kubectl通过执行rancher生成的命令将K8s注册到rancher上了,因为集群都不见了,还注册个毛线
- 关系就是rancher—>kubectl—>集群
- 一定要看,很好!https://blog.csdn.net/weixin_34409741/article/details/86279147?ops_request_misc=&request_id=&biz_id=102&utm_term=.kube&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-3-86279147.pc_search_result_control_group&spm=1018.2226.3001.4187
常用命令
-o wide/yaml/name\json #以什么形式输出
run #在集群上运行一个镜像
create #使用文件标准输入的方式创建一个资源deployment/configmap-f #可以记录命令,我们可以很方便地查看每次revision的变化
delete
get node(s)pods(s) -o widedeployment(s)rc #就是指ReplicationController,已经换成Deployment了
describe
exec #进入容器
scale #扩容
- kubectl都是在某个命名空间下执行的,默认为default,(使用kubens命令可以切换命名空间?)
- 在用kubectl run时候有时需要很长的参数,没法保存,所以更多时候是用的yaml和json,使用kubectl create或者delete就可以利用这些yaml、json文件
- 所有命令
Basic Commands (Beginner):create Create a resource from a file or from stdin.expose 使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的
Kubernetes Servicerun 在集群中运行一个指定的镜像set 为 objects 设置一个指定的特征Basic Commands (Intermediate):explain 查看资源的文档get 显示一个或更多 resourcesedit 在服务器上编辑一个资源delete Delete resources by filenames, stdin, resources and names, or by resources and label selectorDeploy Commands:rollout Manage the rollout of a resourcescale 为 Deployment, ReplicaSet, Replication Controller 或者 Job 设置一个新的副本数量autoscale 自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController 的副本数量Cluster Management Commands:certificate 修改 certificate 资源.cluster-info 显示集群信息top Display Resource (CPU/Memory/Storage) usage.cordon 标记 node 为 unschedulableuncordon 标记 node 为 schedulabledrain Drain node in preparation for maintenancetaint 更新一个或者多个 node 上的 taintsTroubleshooting and Debugging Commands:describe 显示一个指定 resource 或者 group 的 resources 详情logs 输出容器在 pod 中的日志attach Attach 到一个运行中的 containerexec 在一个 container 中执行一个命令port-forward Forward one or more local ports to a podproxy 运行一个 proxy 到 Kubernetes API servercp 复制 files 和 directories 到 containers 和从容器中复制 files 和 directories.auth Inspect authorizationAdvanced Commands:apply 通过文件名或标准输入流(stdin)对资源进行配置patch 使用 strategic merge patch 更新一个资源的 field(s)replace 通过 filename 或者 stdin替换一个资源wait Experimental: Wait for one condition on one or many resourcesconvert 在不同的 API versions 转换配置文件Settings Commands:label 更新在这个资源上的 labelsannotate 更新一个资源的注解completion Output shell completion code for the specified shell (bash or zsh)Other Commands:alpha Commands for features in alphaapi-resources Print the supported API resources on the serverapi-versions Print the supported API versions on the server, in the form of "group/version"config 修改 kubeconfig 文件plugin Runs a command-line pluginversion 输出 client 和 server 的版本信息
在kubernetes中,提供了很多的资源对象,开发和运维人员可以通过这些对象对容器进行编排。在下表中,是kubectl所支持的资源对象类型(要明确资源这个概念,资源类型就是不同类型的pod,在配置文件中的kind就可以下这些,相当于模板,根据这些模板来创建不同类型的pod)以及它们的缩略别名:
apiservices
certificatesigningrequests csr
clusters
clusterrolebindings
clusterroles
componentstatuses cs
configmaps cm
controllerrevisions
cronjobs
customresourcedefinition crd
daemonsets ds
deployments deploy
endpoints ep
events ev
horizontalpodautoscalers hpa
ingresses ing
jobs
limitranges limits
namespaces ns
networkpolicies netpol
nodes no
persistentvolumeclaims pvc
persistentvolumes pv
poddisruptionbudget pdb
podpreset
pods po
podsecuritypolicies psp
podtemplates
replicasets rs
replicationcontrollers rc
resourcequotas quota
rolebindings
roles
secrets
serviceaccounts sa
services svc
statefulsets
storageclasses注意!用yum下载好kubectl文件后,要配置kubeconfig文件。
RKE会在配置文件所在的目录下部署一个本地文件,该文件中包含kube配置信息以连接到新生成的群集。
默认情况下,kube配置文件被称为.kube_config_cluster.yml。将这个文件复制到你的本地~/.kube/config,就可以在本地使用kubectl了。
需要注意的是,部署的本地kube配置名称是和集群配置文件相关的。例如,如果您使用名为mycluster.yml的配置文件,则本地kube配置将被命名为.kube_config_mycluster.yml。
systmctlkubectl相关推荐
最新文章
- 混合式APP开发中中间件方案Rexsee
- 锋利的jQuery-4--动画方法总结简表
- 计算机科学与技术专业导向ppt,计算机科学与技术专业导向讲座 第讲.ppt
- 外籍主管眼中的阿里巴巴
- 技术员联盟win11旗舰版64位系统v2021.07
- 怎么去掉ECSHOP的Powered by ECShop版权信息
- 获取linux服务器基本信息命令行,centos 查看服务器运行的服务和基本信息
- 达摩院自研数学规划求解器发布,最新技术将通过阿里云对外输出
- 【NOIP 2016】组合数问题
- RTCM1005详解
- NOIP2016总结
- 学生宿舍管理系统java课设_JAVA学生宿舍管理系统
- c++中字符数组与字符串的转换
- 元素地牢服务器维修,《元素地牢》已正式开服!劝退地牢等你挑战!
- java 去除HTML中的所有标签,获取TEXT文本
- git强制拉取代码覆盖本地的
- 【无标题】数据仓库-学习
- discuz手机论坛模板 discuz!超简约手机论坛模板
- 6年老Android面经总结,含泪整理面经
- spring启动加载机制spring.factories使用方法