Kubectl常用命令(一)

版权声明:欢迎转载,转载请注明出处! https://blog.csdn.net/miss1181248983/article/details/88037531

Kubectl是一个用于操作kubernetes集群的命令行接口,通过利用kubectl的各种命令可以实现各种功能,是在使用kubernetes中非常常用的工具。

接下来我们会通过一些简单的实例来展现其中一些高频命令的使用方法,更为重要的是这些命令使用的场景以及能够解决什么样的问题。

环境构成

  • 集群:
类型 主机名 IP
Master master 192.168.30.128
Master master2 192.168.30.150
Node node1 192.168.30.129
Node node2 192.168.30.130
# kubectl get nodes
NAME              STATUS                     ROLES     AGE        VERSION
192.168.30.129    Ready                      node      10d        v1.11.6
192.168.30.130    Ready                      node      10d        v1.11.6
192.168.30.128    Ready,SchedulingDisabled   master    10d        v1.11.6
192.168.30.150    Ready,SchedulingDisabled   master    10d        v1.11.6
  • 版本:
# kubectl version
Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.6", GitCommit:"b1d75deca493a24a2f87eb1efde1a569e52fc8d9", GitTreeState:"clean", BuildDate:"2018-12-16T04:39:52Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.6", GitCommit:"b1d75deca493a24a2f87eb1efde1a569e52fc8d9", GitTreeState:"clean", BuildDate:"2018-12-16T04:30:10Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}

基础命令

kubectl help可以查看kubectl相关的命令。

Basic Commands (Beginner):
  create         Create a resource from a file or from stdin.
  expose         使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的
Kubernetes Service
  run            在集群中运行一个指定的镜像
  set            为 objects 设置一个指定的特征
 
Basic Commands (Intermediate):
  explain        查看资源的文档
  get            显示一个或更多 resources
  edit           在服务器上编辑一个资源
  delete         Delete resources by filenames, stdin, resources and names, or by resources and label selector
 
Deploy Commands:
  rollout        Manage the rollout of a resource
  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 上的 taints
 
Troubleshooting 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 authorization
 
Advanced Commands:
  apply          通过文件名或标准输入流(stdin)对资源进行配置
  patch          使用 strategic merge patch 更新一个资源的 field(s)
  replace        通过 filename 或者 stdin替换一个资源
  wait           Experimental: Wait for one condition on one or many resources
  convert        在不同的 API versions 转换配置文件
 
Settings Commands:
  label          更新在这个资源上的 labels
  annotate       更新一个资源的注解
  completion     Output shell completion code for the specified shell (bash or zsh)
 
Other Commands:
  alpha          Commands for features in alpha
  api-resources  Print the supported API resources on the server
  api-versions   Print the supported API versions on the server, in the form of "group/version"
  config         修改 kubeconfig 文件
  plugin         Runs a command-line plugin
  version        输出 client 和 server 的版本信息

本文将会简单介绍一下基础命令:

命令 说明
create 使用文件或者标准输入的方式创建一个资源
expose 将一个资源公开为为新的Kubernetes Service
run 创建并运行一个或多个容器镜像
set 配置应用资源
explain 显示资源文档信息
get 获取显示一个或多个资源
edit 编辑服务器上定义的资源
delete 按文件名、stdin、资源和名称或按资源和标签选择器删除资源

create

kubectl create可以通过配置文件名或stdin创建一个集群资源对象。支持JSON和YAML格式的文件。

# vim mysql-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.6
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
# kubectl create -f mysql-deploy.yaml 
deployment.apps/mysql created

# kubectl get deploy
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
mysql     1         1         1            1           1m

# kubectl get pod
NAME                     READY     STATUS    RESTARTS   AGE
mysql-5fb6c74b86-xg97j   1/1       Running   0          48s

expose

kubectl expose将RC、Service、Deployment或Pod作为新的Kubernetes Service公开。

下面为Deployment的mysql创建service,并通过Service的33060端口转发至容器的3306端口上。

# kubectl expose deploy mysql --port=33060 --target-port=3306
service/mysql exposed               #自动创建一个新的service

# kubectl get svc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)     AGE
kubernetes   ClusterIP   10.68.0.1     <none>        443/TCP     10d
mysql        ClusterIP   10.68.86.66   <none>        33060/TCP   32s

run

kubectl run创建并运行一个或多个容器镜像。

kubectl run和docker run一样,它能将一个镜像运行起来。下面使用kubectl run来将一个nginx的镜像启动起来。

# kubectl run nginx --image=nginx --expose --port=80            #指定镜像,指定暴露端口
service/nginx created
deployment.apps/nginx created

使用kubectl run会自动创建deployment,加上--export会自动创建service。

# kubectl get svc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.68.0.1     <none>        443/TCP   10d
nginx        ClusterIP   10.68.56.89   <none>        80/TCP    1m

# kubectl get deploy
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
mysql     1         1         1            1           16m
nginx     1         1         1            1           1m

# kubectl get pod
NAME                     READY     STATUS    RESTARTS   AGE
mysql-5fb6c74b86-xg97j   1/1       Running   0          21m
nginx-6f858d4d45-dt44v   1/1       Running   0          6m

set

kubectl set用来配置应用资源,可以更改现有应用资源的一些信息。

例如,将deployment的nginx容器cpu限制为200m,将内存设置为512Mi。

# kubectl set resources deployment nginx -c=nginx --limits=cpu=200m,memory=512Mi
deployment.extensions/nginx resource requirements updated

子命令:

命令 说明
kubectl set resources 为资源对象中的Pod指定计算资源需求
kubectl set selector 设置资源的selector,若已存在则覆盖
kubectl set image 更新现有的资源对象的容器镜像
kubectl set subject 更新RoleBinding / ClusterRoleBinding中User、Group 或 ServiceAccount

explain

kubectl explain可以了解各个部分的说明和组成部分,类似于kubectl help。

# kubectl explain deploy
KIND:     Deployment
VERSION:  extensions/v1beta1

DESCRIPTION:
     DEPRECATED - This group version of Deployment is deprecated by
     apps/v1beta2/Deployment. See the release notes for more information.
     Deployment enables declarative updates for Pods and ReplicaSets.

FIELDS:
   apiVersion	<string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#resources

   kind	<string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds

   metadata	<Object>
     Standard object metadata.

   spec	<Object>
     Specification of the desired behavior of the Deployment.

   status	<Object>
     Most recently observed status of the Deployment.

get

kubectl get获取列出一个或多个资源的信息。

例如,分别获取当前集群中pod、deployment和svc的信息。

# kubectl get pod
NAME                     READY     STATUS    RESTARTS   AGE
mysql-5fb6c74b86-xg97j   1/1       Running   0          42m
nginx-566c464789-w7669   1/1       Running   0          15m

# kubectl get deploy
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
mysql     1         1         1            1           43m
nginx     1         1         1            1           28m

# kubectl get svc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)     AGE
kubernetes   ClusterIP   10.68.0.1     <none>        443/TCP     10d
mysql        ClusterIP   10.68.86.66   <none>        33060/TCP   36m

edit

kubectl edit用来编辑服务器上定义的资源。

使用命令行工具获取的任何资源都可以使用edit命令编辑。edit命令会打开使用KUBE_EDITOR,GIT_EDITOR 或者EDITOR环境变量定义的编辑器,可以同时编辑多个资源,但所编辑过的资源只会一次性提交。edit除命令参数外还接受文件名形式。

文件默认输出格式为YAML。要以JSON格式编辑,请指定“-o json”选项。

例如,编辑名为“mysql”的service。

# kubectl edit svc/mysql
service/mysql edited                #如果没有更改,则会提示 Edit cancelled, no changes made.
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: 2019-03-04T07:40:31Z
  labels:
    app: mysql
  name: mysql
  namespace: default
  resourceVersion: "1403097"
  selfLink: /api/v1/namespaces/default/services/mysql
  uid: c9fa11a3-3e50-11e9-83ba-000c29d20ca7
spec:
  clusterIP: 10.68.86.66
  ports:
  - port: 33070             #端口由33060改为33070
    protocol: TCP
    targetPort: 3306
  selector:
    app: mysql
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}
# kubectl get svc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)     AGE
kubernetes   ClusterIP   10.68.0.1     <none>        443/TCP     10d
mysql        ClusterIP   10.68.86.66   <none>        33070/TCP   42m

可以看到端口已经改成了33070。

delete

kubectl delete可以通过配置文件名、stdin、资源名称或label选择器来删除资源。支持JSON和YAML格式的文件。

下面删除nginx的pod。

# kubectl get pod
NAME                     READY     STATUS    RESTARTS   AGE
mysql-5fb6c74b86-xg97j   1/1       Running   0          55m
nginx-566c464789-w7669   1/1       Running   0          27m

# kubectl delete pod/nginx-566c464789-w7669
pod "nginx-566c464789-w7669" deleted
# kubectl get pod
NAME                     READY     STATUS              RESTARTS   AGE
mysql-5fb6c74b86-xg97j   1/1       Running             0          55m
nginx-566c464789-8lr2x   0/1       ContainerCreating   0          1m

又生成了一个新的nginx的pod,为什么?这是因为对应的deployment还存在。

# kubectl delete deploy/nginx
deployment.extensions "nginx" deleted

# kubectl get deploy
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
mysql     1         1         1            1           1h

# kubectl get pod
NAME                     READY     STATUS    RESTARTS   AGE
mysql-5fb6c74b86-xg97j   1/1       Running   0          1h

删除掉deployment,其对应的pod也会被删除。


猜你喜欢

转载自blog.csdn.net/miss1181248983/article/details/88037531