k8s,盘他!kubectl常规命令操作集锦

文章目录

  • 前言
  • 一:kubectl管理
      • 1.1:使用kubectl命令管理项目的生命周期
          • 1.1.1:创建
          • 1.1.2、发布
          • 1.1.3:更新
          • 1.1.4:回滚
          • 1.1.5:删除
      • 1.2:其他常规操作
      • 如有疑问可评论区交流!

前言

一:kubectl管理

  • kubectl是管理k8s的命令行工具,通过生成json格式传递给apiserver进行一些操作

  • 更全的kubectl命令请查看kubectl --help,以下仅列出常用的命令

    [root@master ~]# kubectl --help
    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          为 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 a specific 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         Provides utilities for interacting with plugins.
      version        输出 client 和 server 的版本信息
    
    Usage:
      kubectl [flags] [options]
    
    Use "kubectl  --help" for more information about a given command.
    Use "kubectl options" for a list of global command-line options (applies to all commands).
    
    

1.1:使用kubectl命令管理项目的生命周期

  • 项目的生命周期:创建、发布、更新、回滚、删除
1.1.1:创建
  • 创建一个nginx的pod

    [root@master ~]# kubectl run nginx-test --image=nginx:latest --port=80 --replicas=3
    kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
    deployment.apps/nginx-test created
    [root@master ~]# kubectl get pods -w	'//-w:动态查看'
    [root@master ~]# kubectl  get pods -o wide	'//查看资源创建在那个节点上'
    [root@master ~]# kubectl get all	'//查看更详细信息:副本资源和控制器资源'
    [root@master ~]# kubectl get deployment,replicaset	'//仅查询pod资源的两个项'
    
    
1.1.2、发布
  • [root@master ~]# kubectl get svc	'//查看原有的service服务'
    NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
    kubernetes   ClusterIP   10.0.0.1             443/TCP   92m
    [root@master ~]# kubectl expose deployment nginx-test --port=80 --target-port=80 --name=nginx-service --type=NodePort	'//将刚刚创建的暴露出来'
    service/nginx-service exposed
    [root@master ~]# kubectl get svc	'//再次查看'
    NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
    kubernetes      ClusterIP   10.0.0.1             443/TCP        92m
    nginx-service   NodePort    10.0.0.176           80:36355/TCP   3s
    [root@master ~]# kubectl get pods -o wide	'//查看pods在哪个节点上'
    NAME                          READY   STATUS    RESTARTS   AGE     IP            NODE              NOMINATED NODE
    nginx-dbddb74b8-5s6h7         1/1     Running   0          7d10h   172.17.26.2   192.168.233.132   
    nginx-test-59f87d55d6-4rvpl   1/1     Running   0          9m55s   172.17.4.3    192.168.233.133   
    nginx-test-59f87d55d6-qwrc6   1/1     Running   0          9m55s   172.17.4.2    192.168.233.133   
    nginx-test-59f87d55d6-xhtpb   1/1     Running   0          9m55s   172.17.26.4   192.168.233.132   
    [root@master ~]# kubectl get endpoints	'//查看pod资源的endpoint'
    NAME            ENDPOINTS                                    AGE
    kubernetes      192.168.233.130:6443,192.168.233.131:6443    93m
    nginx-service   172.17.26.4:80,172.17.4.2:80,172.17.4.3:80   38s
    
    
  • 可以查看资源对象的缩写

    [root@master dashboard]# kubectl api-resources
    
    
  • pod资源的调度

    当我们创建多个pod、service资源时,kube-proxy会做负载均衡,此时我们通过访问任意node节点ip可以访问所有的资源

    kubernetes中kube-proxy支持三种模式,在v1.8之前我们使用的是iptables以及userspace两种模式,在kubernetes1.8之后加入了ipvs

    [root@node01 ~]# yum -y install ipvsadm 
    [root@node01 ~]# ipvsadm -L -n	'//查看调度'
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  127.0.0.1:30005 rr
      -> 172.17.26.3:8443             Masq    1      0          0         
    TCP  127.0.0.1:36355 rr	'//发现可以访问本地地址可以自动轮询给三个pod资源,node02也是一样的'
      -> 172.17.4.2:80                Masq    1      0          0         
      -> 172.17.4.3:80                Masq    1      0          0         
      -> 172.17.26.4:80               Masq    1      0          0         
    
     
    
  • 访问网页,查看日志

k8s,盘他!kubectl常规命令操作集锦_第1张图片

[root@master ~]# kubectl logs nginx-test-59f87d55d6-4rvpl
[root@master ~]# kubectl logs nginx-test-59f87d55d6-qwrc6
[root@master ~]# kubectl logs nginx-test-59f87d55d6-xhtpb	'//网页只访问了一次,所以只有一个pod资源有日志'
    ...省略内容
172.17.26.1 - - [08/May/2020:04:03:06 +0000] "GET /favicon.ico HTTP/1.1" 404 556 "http://192.168.233.132:36355/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36" "-"

1.1.3:更新
  • 开发者模式查看版本号(我使用的是谷歌浏览器)

    k8s,盘他!kubectl常规命令操作集锦_第2张图片

    [root@master ~]# kubectl set image deployment/nginx-test nginx-test=nginx:1.13 	'//更新版本'
    deployment.extensions/nginx-test image updated
    [root@master ~]# kubectl get pods -w	'//动态查看'
    '//容器的更新:滚动更新,容器只有删除和创建,没有重启的'
    
  • 重新访问网页,再次查看版本

    k8s,盘他!kubectl常规命令操作集锦_第3张图片

1.1.4:回滚
  • 查看历史状态

    [root@master ~]# kubectl rollout history deployment/nginx-test
    deployment.extensions/nginx-test 
    REVISION  CHANGE-CAUSE
    1         
    2         
    
  • 执行回滚

    [root@master ~]# kubectl rollout undo deployment/nginx-test
    deployment.extensions/nginx-test
    [root@master ~]# kubectl get pods -w	'//动态查看'
    
    
  • 检查回滚状态

    k8s,盘他!kubectl常规命令操作集锦_第4张图片

1.1.5:删除
  • 删除不仅仅是pod,还有service

    [root@master ~]# kubectl delete deployment/nginx-test
    deployment.extensions "nginx-test" deleted
    [root@master ~]# kubectl get pods -w
    NAME                          READY   STATUS        RESTARTS   AGE
    nginx-dbddb74b8-5s6h7         1/1     Running       0          7d10h
    nginx-test-59f87d55d6-xgbrd   0/1     Terminating   0          3m13s
    nginx-test-59f87d55d6-zs5nf   0/1     Terminating   0          2m59s
    nginx-test-59f87d55d6-zs5nf   0/1   Terminating   0     3m4s
    nginx-test-59f87d55d6-zs5nf   0/1   Terminating   0     3m4s
    nginx-test-59f87d55d6-xgbrd   0/1   Terminating   0     3m18s
    nginx-test-59f87d55d6-xgbrd   0/1   Terminating   0     3m18s
    ^C[root@master ~]# kubectl get pods 	'//再次查看pod资源'
    NAME                    READY   STATUS    RESTARTS   AGE
    nginx-dbddb74b8-5s6h7   1/1     Running   0          7d10h
    [root@master ~]# kubectl get svc	'//查看service'
    NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
    kubernetes      ClusterIP   10.0.0.1             443/TCP        137m
    nginx-service   NodePort    10.0.0.176           80:36355/TCP   44m
    [root@master ~]# kubectl delete svc/nginx-service	'//删除service'
    service "nginx-service" deleted
    [root@master ~]# kubectl get svc	'//重新查看'
    NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
    kubernetes   ClusterIP   10.0.0.1             443/TCP   137m
    
    

1.2:其他常规操作

  • 查看资源的详细信息

    [root@master ~]# kubectl get all
    NAME                        READY   STATUS    RESTARTS   AGE
    pod/nginx-dbddb74b8-5s6h7   1/1     Running   0          7d11h
    
    NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
    service/kubernetes   ClusterIP   10.0.0.1             443/TCP   163m
    
    NAME                    DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    deployment.apps/nginx   1         1         1            1           7d11h
    
    NAME                              DESIRED   CURRENT   READY   AGE
    replicaset.apps/nginx-dbddb74b8   1         1         1       7d11h
    
    
  • 查看service资源

    [root@master ~]# kubectl describe service/kubernetes
    Name:              kubernetes
    Namespace:         default
    Labels:            component=apiserver
                       provider=kubernetes
    Annotations:       
    Selector:          
    Type:              ClusterIP
    IP:                10.0.0.1
    Port:              https  443/TCP
    TargetPort:        6443/TCP
    Endpoints:         192.168.233.130:6443,192.168.233.131:6443
    Session Affinity:  None
    Events:            
    
    
  • 查看deployment资源

    [root@master ~]# kubectl describe deployment.apps/nginx
    Name:                   nginx
    Namespace:              default
    CreationTimestamp:      Fri, 01 May 2020 01:31:30 +0800
    Labels:                 run=nginx
    Annotations:            deployment.kubernetes.io/revision: 1
    Selector:               run=nginx
    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:  run=nginx
      Containers:
       nginx:
        Image:        nginx
        Port:         
        Host Port:    
        Environment:  
        Mounts:       
      Volumes:        
    Conditions:
      Type           Status  Reason
      ----           ------  ------
      Available      True    MinimumReplicasAvailable
      Progressing    True    NewReplicaSetAvailable
    OldReplicaSets:  
    NewReplicaSet:   nginx-dbddb74b8 (1/1 replicas created)
    Events:          
    
    
  • 进入pod

    [root@master ~]# kubectl exec -it nginx-dbddb74b8-5s6h7 bash
    root@nginx-dbddb74b8-5s6h7:/# ls
    bin   dev  home  lib64	mnt  proc  run	 srv  tmp  var
    boot  etc  lib	 media	opt  root  sbin  sys  usr
    root@nginx-dbddb74b8-5s6h7:/# exit 
    exit
    
    

如有疑问可评论区交流!

你可能感兴趣的:(Kubernetes/K8S)