千家信息网

Kubernetes命令行管理工具Kubectl怎么用

发表于:2024-10-18 作者:千家信息网编辑
千家信息网最后更新 2024年10月18日,本篇文章给大家分享的是有关Kubernetes命令行管理工具Kubectl怎么用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。一、国内安
千家信息网最后更新 2024年10月18日Kubernetes命令行管理工具Kubectl怎么用

本篇文章给大家分享的是有关Kubernetes命令行管理工具Kubectl怎么用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

一、国内安装Kubectl

官网提供的安装命令是:

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会失败主要是访问了 google 的地址,但是可以通过Github上来找到可以进行使用编译好的二进制文件


执行安装命令

wget https://dl.k8s.io/v1.15.3/kubernetes-client-linux-amd64.tar.gztar -zxvf kubernetes-client-linux-amd64.tar.gzcd kubernetes/client/binchmod +x ./kubectlsudo mv ./kubectl /usr/local/bin/kubectl

kubectl version 能够正常看到输出即可

二、配置文件

光有Kubectl命令想操作K8S集群还是不够的,必须要把链接K8S的相关配置信息填写进去,默认kubectl会在 ~/.kube/config 寻找配置文件

vim ~/.kube/configapiVersion: v1clusters:- cluster:    server: https://xxxxxxxxxx:443    insecure-skip-tls-verify: true  name: kubernetescontexts:- context:    cluster: kubernetes    user: "kubernetes-admin"  name: kubernetes-admin-xxxxxxcurrent-context: kubernetes-admin-xxxxxxxxkind: Configpreferences: {}users:- name: "kubernetes-admin"  user: xxxxxxxx

就可以通过命令行执行对K8S集群的控制了

kubectl versionClient Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.5", GitCommit:"32ac1c9073b132b8ba18aa830f46b77dcceb0723", GitTreeState:"clean", BuildDate:"2018-06-21T11:46:00Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"darwin/amd64"}Server Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.5", GitCommit:"753b2dbc622f5cc417845f0ff8a77f539a4213ea", GitTreeState:"clean", BuildDate:"2018-11-26T14:31:35Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}

当然也可以指定对应的文件路径作为配置文件

kubectl  --kubeconfig=~/.kubu/config xxxxxxxxxx

三、Kubectl 常用命令

kubectl apply - 通过文件名或控制台输入,对资源进行配置。kubectl create - 通过文件名或控制台输入,创建资源。kubectl delete - 通过文件名、控制台输入、资源名或者label selector删除资源。kubectl edit - 编辑服务端的资源。kubectl exec - 在容器内部执行命令。kubectl get - 输出一个/多个资源。kubectl logs - 输出pod中一个容器的日志。kubectl namespace -(已停用)设置或查看当前使用的namespace。kubectl port-forward - 将本地端口转发到Pod。kubectl rolling-update - 对指定的replication controller执行滚动升级。kubectl stop - (已停用)通过资源名或控制台输入安全删除资源。kubectl version - 输出服务端和客户端的版本信息。
Basic Commands (Beginner):  create         Create a resource from a file or from stdin.  expose         使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的Kubernetes Service  run            在集群中运行一个指定的镜像  set            为 objects 设置一个指定的特征  run-container  在集群中运行一个指定的镜像. This command is deprecated, use "run" insteadBasic Commands (Intermediate):  get            显示一个或更多 resources  explain        查看资源的文档  edit           在服务器上编辑一个资源  delete         Delete resources by filenames, stdin, resources and names, or by resources and label selectorDeploy Commands:  rollout        Manage the rollout of a resource  rolling-update 完成指定的 ReplicationController 的滚动升级  scale          为 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 为 unschedulable  uncordon       标记 node 为 schedulable  drain          Drain node in preparation for maintenance  taint          更新一个或者多个 node 上的 taintsTroubleshooting and Debugging Commands:  describe       显示一个指定 resource 或者 group 的 resources 详情  logs           输出容器在 pod 中的日志  attach         Attach 到一个运行中的 container  exec           在一个 container 中执行一个命令  port-forward   Forward one or more local ports to a pod  proxy          运行一个 proxy 到 Kubernetes API server  cp             复制 files 和 directories 到 containers 和从容器中复制 files 和 directories.  auth           Inspect authorizationAdvanced Commands:  apply          通过文件名或标准输入流(stdin)对资源进行配置  patch          使用 strategic merge patch 更新一个资源的 field(s)  replace        通过 filename 或者 stdin替换一个资源  convert        在不同的 API versions 转换配置文件Settings Commands:  label          更新在这个资源上的 labels  annotate       更新一个资源的注解  completion     Output shell completion code for the specified shell (bash or zsh)Other Commands:  api-versions   Print the supported API versions on the server, in the form of "group/version"  config         修改 kubeconfig 文件  help           Help about any command  plugin         Runs a command-line plugin  version        输出 client 和 server 的版本信息

以上就是Kubernetes命令行管理工具Kubectl怎么用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

0