Kubectl常用命令(二)

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

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 的版本信息

本文将会简单介绍一下如下命令,与故障排查相关:

命令 说明
describe 显示特定资源或资源组的详细信息
logs 在pod中打印容器的日志
attach 连接到一个正在运行的容器
exec 在容器内部执行命令
port-forward 将一个或多个本地端口转发到pod
proxy 运行到Kubernetes API服务器的代理
cp 将文件和目录复制到容器或从容器复制到容器
auth 身份验证检查授权

describe

kubectl describe用来显示特定资源或资源组的详细信息。

例如,显示mysql的deployment的详细信息。

# kubectl describe deploy/mysql
Name:                   mysql
Namespace:              default
CreationTimestamp:      Mon, 04 Mar 2019 15:33:58 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision=1
Selector:               app=mysql
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=mysql
  Containers:
   mysql:
    Image:      mysql:5.6
    Port:       3306/TCP
    Host Port:  0/TCP
    Environment:
      MYSQL_ROOT_PASSWORD:  123456
    Mounts:                 <none>
  Volumes:                  <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   mysql-5fb6c74b86 (1/1 replicas created)
Events:          <none>

logs

kubectl logs输出pod中一个容器的日志。如果pod只包含一个容器则可以省略容器名。

kubectl logs [-f] [-p] POD [-c CONTAINER]
  -c, --container="": 容器名。
  
  -f, --follow[=false]: 指定是否持续输出日志。
      --interactive[=true]: 如果为true,当需要时提示用户进行输入。默认为true。
      --limit-bytes=0: 输出日志的最大字节数。默认无限制。
      
  -p, --previous[=false]: 如果为true,输出pod中曾经运行过,但目前已终止的容器的日志。
      --since=0: 仅返回相对时间范围,如5s、2m或3h,之内的日志。默认返回所有日志。只能同时使用since和since-time中的一种。
      --since-time="": 仅返回指定时间(RFC3339格式)之后的日志。默认返回所有日志。只能同时使用since和since-time中的一种。
      --tail=-1: 要显示的最新的日志条数。默认为-1,显示所有的日志。
      --timestamps[=false]: 在日志中包含时间戳。

例如,输出mysql的pod的容器日志。

# kubectl logs mysql-5fb6c74b86-xg97j
2019-03-04 07:35:16 1 [Note] InnoDB: Waiting for purge to start
2019-03-04 07:35:16 1 [Note] InnoDB: 5.6.43 started; log sequence number 1625997
2019-03-04 07:35:16 1 [Note] Server hostname (bind-address): '*'; port: 3306
2019-03-04 07:35:16 1 [Note] IPv6 is available.
2019-03-04 07:35:16 1 [Note]   - '::' resolves to '::';
2019-03-04 07:35:16 1 [Note] Server socket created on IP: '::'.
2019-03-04 07:35:16 1 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2019-03-04 07:35:16 1 [Warning] 'proxies_priv' entry '@ root@mysql-5fb6c74b86-xg97j' ignored in --skip-name-resolve mode.
2019-03-04 07:35:16 1 [Note] Event Scheduler: Loaded 0 events
2019-03-04 07:35:16 1 [Note] mysqld: ready for connections.
Version: '5.6.43'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)

-f 选项可以动态查看日志。

attach

kubectl attach连接到现有容器中一个正在运行的容器。

attach命令类似于docker的attach命令,可以直接查看容器中以daemon形式运行的进程的输出,效果类似于logs -f

  -c, --container="": 容器名。
  
  -i, --stdin[=false]: 将控制台输入发送到容器。
  
  -t, --tty[=false]: 将标准输入控制台作为容器的控制台输入。
# kubectl attach mysql-5fb6c74b86-xg97j -c mysql 
If you don't see a command prompt, try pressing enter.

exec

kubectl exec在容器内部执行命令。类似于docker的exec命令。

例如登录到容器中。

# kubectl exec -it mysql-5fb6c74b86-xg97j bash
root@mysql-5fb6c74b86-xg97j:/# exit

直接在容器中执行命令。

# kubectl exec mysql-5fb6c74b86-xg97j date
Mon Mar  4 09:30:35 UTC 2019

# kubectl exec mysql-5fb6c74b86-xg97j ls
bin
boot
dev
docker-entrypoint-initdb.d
entrypoint.sh
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

port-forward

kubectl port-forward将一个或多个本地端口转发到pod。该命令不常用。

例如,把本地的port映射到pod的port。

# kubectl port-forward pod/mysql-5fb6c74b86-xg97j 3306:3306
Forwarding from 127.0.0.1:3306 -> 3306
Forwarding from [::1]:3306 -> 3306

proxy

kubectl proxy运行一个到Kubernetes API服务器的代理。建立一条通往API服务器的隧道,可以方便查看API服务器上的资源。该命令不常用。

# kubectl proxy
Starting to serve on 127.0.0.1:8001

cp

kubectl cp将文件和目录复制到容器或从容器复制到容器。

例如,在容器中创建1.txt,再复制到外部。

# kubectl exec -it mysql-5fb6c74b86-xg97j bash

root@mysql-5fb6c74b86-xg97j:/# echo "111222" > 1.txt

root@mysql-5fb6c74b86-xg97j:/# pwd      
/
root@mysql-5fb6c74b86-xg97j:/# exit

# kubectl cp mysql-5fb6c74b86-xg97j:/1.txt 1.txt
tar: Removing leading `/' from member names

# ls
1.txt  

# cat 1.txt 
111222

更改一下再复制到容器内。

# kubectl cp 1.txt mysql-5fb6c74b86-xg97j:/1.txt

# kubectl exec mysql-5fb6c74b86-xg97j cat /1.txt
333333

auth

kubectl auth用于身份验证检查授权。该命令不常用。

可以检查一个动作是否被允许,协调RBAC角色、角色绑定、集群角色和集群角色绑定对象的规则。


猜你喜欢

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