code学习

Kubernetes之Kuberctl介绍与常用使用命令

文章目录

  • ​​简介​​
  • ​​kubectl [kubectl组件全部命令]​​
  • ​​Basic Commands 基本命令(入门者)​​
  • ​​kubectl create -f pod_nginx.yaml [创建pod]​​
  • ​​首先配置创建pod的ymal文件​​
  • ​​kubectl create -f pod_nginx.yaml [启动 ]​​
  • ​​Basic Commands 基本命令(升级)​​
  • ​​kubectl get pods [查看pod信息]​​
  • ​​kubectl get pods -o wide [更全面的查看pod信息]​​
  • ​​Deploy Commands [部署命令]​​
  • ​​Cluster Management Commands[集群管理命令]​​
  • ​​Troubleshooting and Debugging Commands[故障排查和调试命令]​​
  • ​​kubectl describe pod nginx [显示pod的详情]​​
  • ​​kubectl exec -it nginx bash[进入nginx容器]​​
  • ​​Advanced Commands [高级命令]​​
  • ​​Settings Commands [设置命令]​​
  • ​​Other Commands [其他命令]​​
  • ​​kubectl version [查看kubernetes版本]​​
  • ​​kubectl config [kubernetes配置信息相关命令]​​
  • ​​kubectl config get-context​​

简介

1.熟悉kubectl组件命令等,需要首先你搭建模拟kubernetes环境,参考下面地址:      

kubectl [kubectl组件全部命令]

localhost:minikube gaoxinfu$ kubectl
kubectl controls the Kubernetes cluster manager.

 Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/

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          Set a new size for a Deployment, ReplicaSet or Replication Controller
  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:
  diff           Diff live version against would-be applied version
  apply          通过文件名或标准输入流(stdin)对资源进行配置
  patch          使用 strategic merge patch 更新一个资源的 field(s)
  replace        通过 filename 或者 stdin替换一个资源
  wait           Experimental: Wait for a specific condition on one or many resources.
  convert        在不同的 API versions 转换配置文件
  kustomize      Build a kustomization target from a directory or a remote url.

Settings Commands:
  label          更新在这个资源上的 labels
  annotate       更新一个资源的注解
  completion     Output shell completion code for the specified shell (bash or zsh)

Other Commands:
  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         Provides utilities for interacting with plugins.
  version        输出 client 和 server 的版本信息

Usage:
  kubectl [flags] [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
localhost:minikube gaoxinfu$      

Basic Commands 基本命令(入门者)

create         Create a resource from a file or from stdin.
expose         使用 replication controller, service,      

kubectl create -f pod_nginx.yaml [创建pod]

首先配置创建pod的ymal文件

apiVersion: v1
kind: Pod
metadata:
 name: nginx
spec:
 containers:
   - name: nginx
     image:      

kubectl create -f pod_nginx.yaml [启动 ]

localhost:demo-nginx gaoxinfu$ kubectl create -f pod_nginx.yaml
localhost:demo-nginx gaoxinfu$      

Basic Commands 基本命令(升级)

explain        查看资源的文档
get            显示一个或更多 resources
edit           在服务器上编辑一个资源
delete         Delete resources by filenames, stdin, resources and names,      

kubectl get pods [查看pod信息]

localhost:demo-nginx gaoxinfu$ kubectl get pods
NAME    READY   STATUS              RESTARTS   AGE
nginx   0/1     ContainerCreating   0          27s      

kubectl get pods -o wide [更全面的查看pod信息]

localhost:demo-nginx gaoxinfu$ kubectl get pods -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          58s   172.17.0.6   minikube   <none>           <none>      

Deploy Commands [部署命令]

rollout        Manage the rollout of a resource
  scale          Set a new size for a Deployment, ReplicaSet or Replication Controller
  autoscale      自动调整一个 Deployment, ReplicaSet,      

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      

Troubleshooting and Debugging Commands[故障排查和调试命令]

-forward   Forward one or more local ports to a pod
  proxy          运行一个 proxy 到 Kubernetes API server
  cp             复制 files 和 directories 到 containers 和从容器中复制 files 和 directories.      

kubectl describe pod nginx [显示pod的详情]

localhost:demo-nginx gaoxinfu$ kubectl describe pod nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         minikube/192.168.99.103
Start Time:   Sun, 29 Dec 2019 08:48:11 +0800
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           172.17.0.6
IPs:
  IP:  172.17.0.6
Containers:
  nginx:
    Container ID:   docker://f763e419c3d2c1f75a627685615b7c62ca96a4cf4634dd746239b35c138e5690
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:b2d89d0a210398b4d1120b3e3a7672c16a4ba09c2c4a0395f18b9f7999b768f2
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sun, 29 Dec 2019 08:48:51 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-nqwpt (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-nqwpt:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-nqwpt
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age        From               Message
  ----    ------     ----       ----               -------
  Normal  Scheduled  <unknown>  default-scheduler  Successfully assigned default/nginx to minikube
  Normal  Pulling    102s       kubelet, minikube  Pulling image "nginx"
  Normal  Pulled     64s        kubelet, minikube  Successfully pulled image "nginx"
  Normal  Created    64s        kubelet, minikube  Created container nginx
  Normal  Started    63s        kubelet, minikube  Started container nginx
localhost:demo-nginx gaoxinfu$      

kubectl exec -it nginx bash[进入nginx容器]

localhost:demo-nginx gaoxinfu$ kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          21m
localhost:demo-nginx gaoxinfu$ kubectl exec -it nginx bash
root@nginx:/#      
localhost:demo-nginx gaoxinfu$ minikube ssh
                         _             _            
            _         _ ( )           ( )           
  ___ ___  (_)  ___  (_)| |/')  _   _ | |_      __  
/' _ ` _ `\| |/' _ `\| || , <  ( ) ( )| '_`\  /'__`\
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )(  ___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)

$ docker ps
CONTAINER ID        IMAGE                                                           COMMAND                  CREATED             STATUS              PORTS               NAMES
f763e419c3d2        nginx                                                           "nginx -g 'daemon of…"   23 minutes ago      Up 23 minutes                           k8s_nginx_nginx_default_6efd7547-6865-4338-8b1f-2f03399945b0_0
$ docker exec -it f763e419c3d2 bash
root@nginx:/#      
1.因为我这里kubernetes是通过minikube进行搭建的,所以需要首先执行
  minikube ssh 进行登录一下
2.f763e419c3d2 是nginx容器的id      

Advanced Commands [高级命令]

diff           Diff live version against would-be applied version
  apply          通过文件名或标准输入流(stdin)对资源进行配置
  patch          使用 strategic merge patch 更新一个资源的 field(s)
  replace        通过 filename 或者 stdin替换一个资源
  wait           Experimental: Wait for a specific condition on one or many resources.
  convert        在不同的 API versions 转换配置文件
  kustomize      Build a kustomization target from a directory or a remote url.      

Settings Commands [设置命令]

label          更新在这个资源上的 labels
  annotate       更新一个资源的注解
  completion     Output shell completion code for the specified shell (bash or zsh)      

Other Commands [其他命令]

kubectl version [查看kubernetes版本]

localhost:minikube gaoxinfu$ kubectl version
Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.0", GitCommit:"70132b0f130acc0bed193d9ba59dd186f0e634cf", GitTreeState:"clean", BuildDate:"2019-12-07T21:20:10Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.0", GitCommit:"70132b0f130acc0bed193d9ba59dd186f0e634cf", GitTreeState:"clean", BuildDate:"2019-12-07T21:12:17Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"linux/amd64"}
localhost:minikube gaoxinfu$      

kubectl config [kubernetes配置信息相关命令]

localhost:minikube gaoxinfu$ kubectl config --help
Modify kubeconfig files using subcommands like "kubectl config set current-context my-context"

 The loading order follows these rules:

  1.  If the --kubeconfig flag is set, then only that file is loaded. The flag may only be set once and no merging takes
place.
  2.  If $KUBECONFIG environment variable is set, then it is used as a list of paths (normal path delimiting rules for
your system). These paths are merged. When a value is modified, it is modified in the file that defines the stanza. When
a value is created, it is created in the first file that exists. If no files in the chain exist, then it creates the
last file in the list.
  3.  Otherwise, ${HOME}/.kube/config is used and no merging takes place.

Available Commands:
  current-context 显示 current_context
  delete-cluster  删除 kubeconfig 文件中指定的集群
  delete-context  删除 kubeconfig 文件中指定的 context
  get-clusters    显示 kubeconfig 文件中定义的集群
  get-contexts    描述一个或多个 contexts
  rename-context  Renames a context from the kubeconfig file.
  set             设置 kubeconfig 文件中的一个单个值
  set-cluster     设置 kubeconfig 文件中的一个集群条目
  set-context     设置 kubeconfig 文件中的一个 context 条目
  set-credentials 设置 kubeconfig 文件中的一个用户条目
  unset           取消设置 kubeconfig 文件中的一个单个值
  use-context     设置 kubeconfig 文件中的当前上下文
  view            显示合并的 kubeconfig 配置或一个指定的 kubeconfig 文件

Usage:
  kubectl config SUBCOMMAND [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
localhost:minikube gaoxinfu$      

kubectl config get-context

localhost:minikube gaoxinfu$ kubectl config get-context
error: unknown command "get-context"
See 'kubectl config -h' for help and examples
localhost:minikube gaoxinfu$ kubectl config get-contexts
CURRENT   NAME       CLUSTER    AUTHINFO   NAMESPACE
*         minikube   minikube   minikube   
localhost:minikube gaoxinfu$ kubectl config current-context
minikube
localhost:minikube gaoxinfu$      

继续阅读