kubectl命令管理工具

kubectl命令管理工具

Kubectl是管理k8s集羣的命令行工具,經過生成的json格式傳遞給apiserver進行建立、查看、管理的操做。html

//幫助信息
[root@localhost bin]# 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

項目的生命週期,建立--發佈--更新--回滾--刪除node

//建立   kubectl run命令
kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas]
[--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...] [options]
示例:
[root@localhost bin]# kubectl run nginx-deployment --image=nginx --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-deployment created
[root@localhost bin]# kubectl get pods
]NAME                                READY   STATUS    RESTARTS   AGE
nginx-dbddb74b8-whwhl               1/1     Running   0          2d17h
nginx-deployment-5477945587-b8r6m   1/1     Running   0          70s
nginx-deployment-5477945587-dz8hb   1/1     Running   0          70s
nginx-deployment-5477945587-wd82l   1/1     Running   0          70s
[root@localhost bin]# kubectl get all
NAME                                    READY   STATUS    RESTARTS   AGE
pod/nginx-dbddb74b8-whwhl               1/1     Running   0          2d17h
pod/nginx-deployment-5477945587-b8r6m   1/1     Running   0          3m9s
pod/nginx-deployment-5477945587-dz8hb   1/1     Running   0          3m9s
pod/nginx-deployment-5477945587-wd82l   1/1     Running   0          3m9s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   12d

NAME                               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx              1         1         1            1           4d23h
deployment.apps/nginx-deployment   3         3         3            3           3m9s

NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-dbddb74b8               1         1         1       4d23h
replicaset.apps/nginx-deployment-5477945587   3         3         3       3m9s

//刪除
[root@localhost bin]# kubectl delete deploy/nginx
deployment.extensions "nginx" deleted
[root@localhost bin]# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5477945587-b8r6m   1/1     Running   0          12m
nginx-deployment-5477945587-dz8hb   1/1     Running   0          12m
nginx-deployment-5477945587-wd82l   1/1     Running   0          12m
[root@localhost bin]# kubectl delete deploy/nginx-deployment
[root@localhost bin]# kubectl get pods
No resources found.

//項目週期
1:建立nginx
[root@localhost bin]# kubectl run nginx --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 created
[root@localhost bin]# kubectl get pods,deployment
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-7697996758-jbln5   1/1     Running   0          47s
pod/nginx-7697996758-xgxzd   1/1     Running   0          47s
pod/nginx-7697996758-xjdlz   1/1     Running   0          47s

NAME                          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/nginx   3         3         3            3           47s

[root@localhost bin]# kubectl get pods,deployment,replicaset
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-7697996758-jbln5   1/1     Running   0          87s
pod/nginx-7697996758-xgxzd   1/1     Running   0          87s
pod/nginx-7697996758-xjdlz   1/1     Running   0          87s

NAME                          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/nginx   3         3         3            3           87s

NAME                                     DESIRED   CURRENT   READY   AGE
replicaset.extensions/nginx-7697996758   3         3         3       87s

發佈nginx service提供負載均衡的功能nginx

kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP]
[--target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type]
[options]

[root@localhost bin]# kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort
service/nginx-service exposed
[root@localhost bin]# kubectl get pods,svc
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-7697996758-jbln5   1/1     Running   0          10m
pod/nginx-7697996758-xgxzd   1/1     Running   0          10m
pod/nginx-7697996758-xjdlz   1/1     Running   0          10m

NAME                    TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
service/kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        12d
service/nginx-service   NodePort    10.0.0.247   <none>        80:38804/TCP   2m35s
//查看資源對象簡寫
[root@localhost bin]# kubectl api-resources
//查看關聯後端的節點
[root@localhost bin]# kubectl get endpoints
NAME            ENDPOINTS                                      AGE
kubernetes      192.168.195.131:6443,192.168.195.149:6443      12d
nginx-service   172.17.27.3:80,172.17.31.3:80,172.17.31.4:80   5m40s
//網絡狀態詳細信息
[root@localhost bin]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP            NODE              NOMINATED NODE
nginx-7697996758-jbln5   1/1     Running   0          14m   172.17.27.3   192.168.195.151   <none>
nginx-7697996758-xgxzd   1/1     Running   0          14m   172.17.31.3   192.168.195.150   <none>
nginx-7697996758-xjdlz   1/1     Running   0          14m   172.17.31.4   192.168.195.150   <none>
//服務暴露的端口
[root@localhost bin]# kubectl get svc
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        12d
nginx-service   NodePort    10.0.0.247   <none>        80:38804/TCP   7m55s

在node01操做,查看負載均衡端口38804
kubernetes裏kube-proxy支持三種模式,在v1.8以前咱們使用的是iptables 以及 userspace兩種模式,在kubernetes 1.8以後引入了ipvs模式docker

[root@localhost ~]# yum install ipvsadm -y
[root@localhost ~]# ipvsadm -L -n

TCP  192.168.195.150:38804 rr
  -> 172.17.27.3:80               Masq    1      0          0         
  -> 172.17.31.3:80               Masq    1      0          0         
  -> 172.17.31.4:80               Masq    1      0          0  
//在node02操做 一樣安裝ipvsadmin工具查看
[root@localhost ~]# ipvsadm -L -n
TCP  192.168.195.151:38804 rr
  -> 172.17.27.3:80               Masq    1      0          0         
  -> 172.17.31.3:80               Masq    1      0          0         
  -> 172.17.31.4:80               Masq    1      0          0         
//在master01操做 查看訪問日誌(注意:若是訪問其餘node沒法訪問檢查proxy組件)
[root@localhost bin]# kubectl get pods
]NAME                     READY   STATUS    RESTARTS   AGE
nginx-7697996758-jbln5   1/1     Running   0          30m
nginx-7697996758-xgxzd   1/1     Running   0          30m
nginx-7697996758-xjdlz   1/1     Running   0          30m
[root@localhost bin]# kubectl logs nginx-7697996758-jbln5
172.17.27.1 - - [10/Feb/2020:04:51:56 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" "-"
2020/02/10 04:51:56 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.27.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.195.151:38804", referrer: "http://192.168.195.151:38804/"
172.17.27.1 - - [10/Feb/2020:04:51:56 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.195.151:38804/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" "-"
[root@localhost bin]# kubectl logs nginx-7697996758-xgxzd
[root@localhost bin]# kubectl logs nginx-7697996758-xjdlz
172.17.31.1 - - [10/Feb/2020:04:51:43 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" "-"
2020/02/10 04:51:44 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.31.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.195.150:38804", referrer: "http://192.168.195.150:38804/"
172.17.31.1 - - [10/Feb/2020:04:51:44 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.195.150:38804/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" "-"

更新nginx 爲1.14版本
kubectl命令管理工具json

[root@localhost bin]# kubectl set --help
]Configure application resources 

These commands help you make changes to existing application resources.

Available Commands:
  env            Update environment variables on a pod template
  image          更新一個 pod template 的鏡像
  resources      在對象的 pod templates 上更新資源的 requests/limits
  selector       設置 resource 的 selector
  serviceaccount Update ServiceAccount of a resource
  subject        Update User, Group or ServiceAccount in a
RoleBinding/ClusterRoleBinding

Usage:
  kubectl set 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).

//獲取修改模板
[root@localhost bin]# kubectl set image --help
Examples:
  # Set a deployment's nginx container image to 'nginx:1.9.1', and its busybox
container image to 'busybox'.
  kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1

[root@localhost bin]# kubectl set image deployment/nginx nginx=nginx:1.14
deployment.extensions/nginx image updated
//處於動態監聽狀態
[root@localhost bin]# kubectl get pods -w
NAME                     READY   STATUS              RESTARTS   AGE
nginx-6ff7c89c7c-7hfsm   0/1     ContainerCreating   0          17s
nginx-7697996758-jbln5   1/1     Running             0          70m
nginx-7697996758-xgxzd   1/1     Running             0          70m
nginx-7697996758-xjdlz   1/1     Running             0          70m
nginx-6ff7c89c7c-7hfsm   1/1   Running   0     35s
nginx-6ff7c89c7c-v2vww   0/1   Pending   0     0s
nginx-7697996758-xgxzd   1/1   Terminating   0     70m
nginx-6ff7c89c7c-v2vww   0/1   Pending   0     0s
nginx-6ff7c89c7c-v2vww   0/1   ContainerCreating   0     0s
nginx-7697996758-xgxzd   0/1   Terminating   0     70m
//Ctrl+c中斷監聽,更新速度快
[root@localhost bin]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6ff7c89c7c-7hfsm   1/1     Running   0          2m55s
nginx-6ff7c89c7c-qj2hd   1/1     Running   0          2m10s
nginx-6ff7c89c7c-v2vww   1/1     Running   0          2m20s

kubectl命令管理工具
回滾nginx後端

[root@localhost bin]# kubectl rollout --help
Manage the rollout of a resource.

Valid resource types include: 

  * deployments  
  * daemonsets  
  * statefulsets

Examples:
  # Rollback to the previous deployment
  kubectl rollout undo deployment/abc

  # Check the rollout status of a daemonset
  kubectl rollout status daemonset/foo

Available Commands:
  history     顯示 rollout 歷史
  pause       標記提供的 resource 爲停止狀態
  resume      繼續一箇中止的 resource
  status      顯示 rollout 的狀態
  undo        撤銷上一次的 rollout

Usage:
  kubectl rollout 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).
//查看歷史版本
[root@localhost bin]# kubectl rollout history deployment/nginx 
deployment.extensions/nginx 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
//執行回滾
[root@localhost bin]# kubectl rollout undo deployment/nginx
deployment.extensions/nginx
//檢查回滾狀態
[root@localhost bin]# kubectl rollout status deployment/nginx
deployment "nginx" successfully rolled out

kubectl命令管理工具

//刪除nginx
//查看deployment
[root@localhost bin]# kubectl get deploy
NAME    DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx   3         3         3            3           118m
[root@localhost bin]# kubectl delete deployment/nginx
deployment.extensions "nginx" deleted
[root@localhost bin]# kubectl get deploy
No resources found.
[root@localhost bin]# kubectl get pods
No resources found.
//刪除服務SVC
[root@localhost bin]# kubectl get svc
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        12d
nginx-service   NodePort    10.0.0.247   <none>        80:38804/TCP   114m
[root@localhost bin]# kubectl delete svc/nginx-service
service "nginx-service" deleted
[root@localhost bin]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   12d

//查看具體資源的詳細信息
[root@localhost bin]# kubectl run nginx --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 created
[root@localhost bin]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7697996758-75shs   1/1     Running   0          20s
nginx-7697996758-b7tjw   1/1     Running   0          20s
nginx-7697996758-jddc5   1/1     Running   0          20s
[root@localhost bin]# kubectl describe pod nginx-7697996758-75shs
Name:               nginx-7697996758-75shs
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               192.168.195.150/192.168.195.150
Start Time:         Mon, 10 Feb 2020 14:33:04 +0800
Labels:             pod-template-hash=7697996758
                    run=nginx
Annotations:        <none>
Status:             Running
IP:                 172.17.31.3
Controlled By:      ReplicaSet/nginx-7697996758
Containers:
  nginx:
    Container ID:   docker://e8eafbc943299091943728bf6a378b0752eb6e0320c9bb9fbed933b4af3b1753
    Image:          nginx:latest
    Image ID:       docker-pullable://nginx@sha256:ad5552c786f128e389a0263104ae39f3d3c7895579d45ae716f528185b36bc6f
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 10 Feb 2020 14:33:10 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-g4gcx (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-g4gcx:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-g4gcx
    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  2m58s  default-scheduler         Successfully assigned default/nginx-7697996758-75shs to 192.168.195.150
  Normal  Pulling    2m56s  kubelet, 192.168.195.150  pulling image "nginx:latest"
  Normal  Pulled     2m52s  kubelet, 192.168.195.150  Successfully pulled image "nginx:latest"
  Normal  Created    2m52s  kubelet, 192.168.195.150  Created container
  Normal  Started    2m52s  kubelet, 192.168.195.150  Started container
//查看deployment資源
[root@localhost bin]# kubectl describe deployment/nginx
Name:                   nginx
Namespace:              default
CreationTimestamp:      Mon, 10 Feb 2020 14:33:04 +0800
Labels:                 run=nginx
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=nginx
  Containers:
   nginx:
    Image:        nginx:latest
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-7697996758 (3/3 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  6m15s  deployment-controller  Scaled up replica set nginx-7697996758 to 3

//進入pod
[root@localhost bin]# kubectl exec -it nginx-7697996758-75shs bash
root@nginx-7697996758-75shs:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
root@nginx-7697996758-75shs:/#
相關文章
相關標籤/搜索