K8S---kubectl管理工具

K8S—kubectl管理工具

一.什么是kubectl?

Kubectl是管理k8s集群的命令行工具,通过生成的json格式传递给apiserver进行创建、查看、管理的操作。

二.具体命令

1.帮助信息
[root@localhost dashboard]#  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).
[root@localhost dashboard]# 
2.创建并查看(kubectl run命令)
//项目的生命周期,创建–》发布–》更新–》回滚–》删除
[root@localhost k8s]# 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 k8s]# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-dbddb74b8-864v5               1/1     Running   0          4d16h
nginx-deployment-5477945587-6m95v   1/1     Running   0          19s
nginx-deployment-5477945587-rqx56   1/1     Running   0          19s
nginx-deployment-5477945587-v728w   1/1     Running   0          19s
//查看创建的副本所在节点(1个在88.14,2个在88.13)
[root@localhost k8s]# kubectl get pods -o wide
NAME                                READY   STATUS    RESTARTS   AGE     IP            NODE            NOMINATED NODE
nginx-dbddb74b8-864v5               1/1     Running   0          4d16h   172.17.94.2   192.168.88.13   
nginx-deployment-5477945587-6m95v   1/1     Running   0          119s    172.17.29.3   192.168.88.14   
nginx-deployment-5477945587-rqx56   1/1     Running   0          119s    172.17.94.4   192.168.88.13   
nginx-deployment-5477945587-v728w   1/1     Running   0          119s    172.17.94.3   192.168.88.13   
//查看所有资源
[root@localhost k8s]# kubectl get all
NAME                                    READY   STATUS    RESTARTS   AGE
pod/nginx-dbddb74b8-864v5               1/1     Running   0          4d16h
pod/nginx-deployment-5477945587-6m95v   1/1     Running   0          4m1s
pod/nginx-deployment-5477945587-rqx56   1/1     Running   0          4m1s
pod/nginx-deployment-5477945587-v728w   1/1     Running   0          4m1s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.0.0.1             443/TCP   4d20h

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

NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-dbddb74b8               1         1         1       4d16h
replicaset.apps/nginx-deployment-5477945587   3         3         3       4m1s
//删除pod资源
[root@localhost k8s]# kubectl delete deploy/nginx
deployment.extensions "nginx" deleted
[root@localhost k8s]# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5477945587-6m95v   1/1     Running   0          6m8s
nginx-deployment-5477945587-rqx56   1/1     Running   0          6m8s
nginx-deployment-5477945587-v728w   1/1     Running   0          6m8s
[root@localhost k8s]# kubectl delete deploy/nginx-deployment
deployment.extensions "nginx-deployment" deleted
[root@localhost k8s]# kubectl get pods
No resources found.

三.项目周期

1.创建nginx并查看
[root@localhost k8s]# 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 k8s]# kubectl get pods,deployment,replicaset
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-7697996758-bt7dp   1/1     Running   0          36s
pod/nginx-7697996758-dj8k9   1/1     Running   0          36s
pod/nginx-7697996758-vvgtk   1/1     Running   0          36s

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

NAME                                     DESIRED   CURRENT   READY   AGE
replicaset.extensions/nginx-7697996758   3         3         3       36s
2.发布nginx,service提供负载均衡的功能
//发布和查看:

[root@localhost k8s]# kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort
service/nginx-service exposed
[root@localhost k8s]# kubectl get pods,svc
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-7697996758-bt7dp   1/1     Running   0          3m28s
pod/nginx-7697996758-dj8k9   1/1     Running   0          3m28s
pod/nginx-7697996758-vvgtk   1/1     Running   0          3m28s

NAME                    TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
service/kubernetes      ClusterIP   10.0.0.1             443/TCP        4d20h
service/nginx-service   NodePort    10.0.0.27            80:44846/TCP   9s

//查看资源对象简写
[root@localhost k8s]# kubectl api-resources

//查看关联后端的节点
[root@localhost k8s]# kubectl get endpoints
NAME            ENDPOINTS                                      AGE
kubernetes      192.168.88.11:6443,192.168.88.12:6443          4d20h
nginx-service   172.17.29.3:80,172.17.29.4:80,172.17.94.2:80   2m13s

//网络状态详细信息
[root@localhost k8s]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE    IP            NODE            NOMINATED NODE
nginx-7697996758-bt7dp   1/1     Running   0          6m2s   172.17.29.3   192.168.88.14   
nginx-7697996758-dj8k9   1/1     Running   0          6m2s   172.17.29.4   192.168.88.14   
nginx-7697996758-vvgtk   1/1     Running   0          6m2s   172.17.94.2   192.168.88.13   

//服务暴露的端口
[root@localhost k8s]# kubectl get svc
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.0.0.1             443/TCP        4d20h
nginx-service   NodePort    10.0.0.27            80:44846/TCP   4m4s
3.在node01,node2操作,查看负载均衡端口44846
//kubernetes里kube-proxy支持三种模式,在v1.8之前我们使用的是iptables 以及 userspace两种模式,在kubernetes 1.8之后引入了ipvs模式
//安装ipvsadm
[root@localhost ~]# yum install ipvsadm -y
[root@localhost ~]# ipvsadm -L -n

TCP  192.168.88.13:44846 rr
  -> 172.17.29.3:80               Masq    1      0          0         
  -> 172.17.29.4:80               Masq    1      0          0         
  -> 172.17.94.2:80               Masq    1      0          0         
4.在master01操作 查看访问日志
(这时还没有访问端口,所有没有日志)
[root@localhost k8s]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7697996758-bt7dp   1/1     Running   0          14m
nginx-7697996758-dj8k9   1/1     Running   0          14m
nginx-7697996758-vvgtk   1/1     Running   0          14m
[root@localhost k8s]# kubectl logs nginx-7697996758-bt7dp
[root@localhost k8s]# kubectl logs nginx-7697996758-dj8k9
[root@localhost k8s]# kubectl logs nginx-7697996758-vvgtk

//访问pod资源

K8S---kubectl管理工具_第1张图片

//再去查看日志

[root@localhost k8s]# kubectl logs nginx-7697996758-dj8k9
172.17.29.1 - - [10/May/2020:12:39:00 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" "-"
172.17.29.1 - - [10/May/2020:12:39:00 +0000] "GET /favicon.ico HTTP/1.1" 404 556 "http://192.168.88.14:44846/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" "-"
2020/05/10 12:39:00 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.29.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.88.14:44846", referrer: "http://192.168.88.14:44846/"
5.更新nginx
打开谷歌浏览器,点击右上角三个点—选择更多工具—点击开发者选项—>找到network—>刷新访问 —>找到headers头部信息,就可以看到nginx版本信息。

K8S---kubectl管理工具_第2张图片

6.更新nginx版本为1.14
[root@localhost k8s]# kubectl set image deployment/nginx nginx=nginx:1.14
deployment.extensions/nginx image updated

//处于动态监听状态
[root@localhost k8s]# kubectl get pods -w
NAME                     READY   STATUS              RESTARTS   AGE
nginx-6ff7c89c7c-2vbcr   0/1     ContainerCreating   0          6s
nginx-6ff7c89c7c-4swbz   1/1     Running             0          34s
nginx-6ff7c89c7c-nx725   1/1     Running             0          23s
nginx-7697996758-bt7dp   1/1     Running             0          31m
nginx-7697996758-dj8k9   0/1     Terminating         0          31m

....

(ctrl+C结束进程)
7.刷新查看版本变为1.14

K8S---kubectl管理工具_第3张图片

8.回滚nginx
//查看历史版本(有2个版本)
[root@localhost k8s]# kubectl rollout history deployment/nginx
deployment.extensions/nginx 
REVISION  CHANGE-CAUSE
1         
2         

//执行回滚
[root@localhost k8s]# kubectl rollout undo deployment/nginx
deployment.extensions/nginx

//检查回滚状态
[root@localhost k8s]# kubectl rollout status deployment/nginx
Waiting for deployment "nginx" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx" rollout to finish: 1 old replicas are pending termination...
deployment "nginx" successfully rolled out

//页面查看

K8S---kubectl管理工具_第4张图片

9.删除nginx
//查看资源名称
[root@localhost k8s]# kubectl get deploy
NAME    DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx   3         3         3            3           37m

//删除
[root@localhost k8s]# kubectl delete deployment/nginx
deployment.extensions "nginx" deleted

//查看
[root@localhost k8s]# kubectl get deploy
No resources found.
[root@localhost k8s]# kubectl get pods
No resources found.

//删除服务SVC
[root@localhost k8s]# kubectl get svc
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.0.0.1             443/TCP        4d21h
nginx-service   NodePort    10.0.0.27            80:44846/TCP   36m
[root@localhost k8s]# kubectl delete svc/nginx-service
service "nginx-service" deleted
[root@localhost k8s]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1             443/TCP   4d21h
10.查看具体资源的详细信息
[root@localhost k8s]# 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 k8s]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7697996758-ksqr7   1/1     Running   0          21s
nginx-7697996758-s52gb   1/1     Running   0          21s
nginx-7697996758-v9qzw   1/1     Running   0          21s

K8S---kubectl管理工具_第5张图片

11.查看deployment资源
[root@localhost k8s]# kubectl describe deployment/nginx

K8S---kubectl管理工具_第6张图片

12.进入pod(与docker容器相似)
[root@localhost k8s]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7697996758-ksqr7   1/1     Running   0          7m18s
nginx-7697996758-s52gb   1/1     Running   0          7m18s
nginx-7697996758-v9qzw   1/1     Running   0          7m18s
[root@localhost k8s]# kubectl exec -it nginx-7697996758-ksqr7 bash
root@nginx-7697996758-ksqr7:/# exit
//exit退出

你可能感兴趣的:(K8S)