文章目录

  • kubectl管理
  • 使用kubectl命令管理项目的生命周期
  • 来吧!展示!!
  • 创建
  • 发布
  • 更新
  • 回滚
  • 删除
  • 其他常见操作


kubectl管理

kubectl是管理k8s的命令行工具,通过生成json格式传递给apiserver进行一些操作
可以使用 kubectl --help ,查看常见选项

[root@localhost ~]# 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 <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).

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

项目的生命周期包括:创建 → 发布 → 更新 → 回滚 → 删除

来吧!展示!!

创建

创建一个nginx的pod

[root@localhost ~]# kubectl run nginx-bate1 --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-bate1 created

[root@localhost ~]# kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
nginx-bate1-89fb849f4-flszp   1/1     Running   0          9m1s
nginx-bate1-89fb849f4-rjxns   1/1     Running   0          9m1s
nginx-bate1-89fb849f4-z5x4h   1/1     Running   0          9m1s

[root@localhost ~]# kubectl get pods -o wide	##查看pod资源在哪个节点上
NAME                          READY   STATUS    RESTARTS   AGE     IP            NODE       NOMINATED NODE
nginx-bate1-89fb849f4-flszp   1/1     Running   0          9m23s   172.17.54.2   20.0.0.5   <none>
nginx-bate1-89fb849f4-rjxns   1/1     Running   0          9m23s   172.17.13.4   20.0.0.4   <none>
nginx-bate1-89fb849f4-z5x4h   1/1     Running   0          9m23s   172.17.54.3   20.0.0.5   <none>

[root@localhost ~]# kubectl get all		##查看全部
NAME                              READY   STATUS    RESTARTS   AGE
pod/nginx-bate1-89fb849f4-flszp   1/1     Running   0          11m
pod/nginx-bate1-89fb849f4-rjxns   1/1     Running   0          11m
pod/nginx-bate1-89fb849f4-z5x4h   1/1     Running   0          11m

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

NAME                          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-bate1   3         3         3            3           11m

NAME                                    DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-bate1-89fb849f4   3         3         3       11m


[root@localhost ~]# kubectl get deploy,replicaset	##仅查询pod资源的两个项
NAME                                DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/nginx-bate1   3         3         3            3           12m

NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.extensions/nginx-bate1-89fb849f4   3         3         3       12m

发布

查看原有的service

[root@localhost ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   9d

发布服务

[root@localhost ~]# kubectl expose deployment nginx-bate1 --port=80 --target-port=80 --name=nginx-service --type=NodePort
service/nginx-service exposed
[root@localhost ~]# kubectl get svc	##再次查看service
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        9d
nginx-service   NodePort    10.0.0.79    <none>        80:48968/TCP   62s

[root@localhost ~]# kubectl get endpoints	##查看pods在哪些个容器里
NAME            ENDPOINTS                                      AGE
kubernetes      20.0.0.3:6443,20.0.0.6:6443                    9d
nginx-service   172.17.13.4:80,172.17.54.2:80,172.17.54.3:80   5m35s
[root@localhost ~]# kubectl get svc
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        9d
nginx-service   NodePort    10.0.0.79    <none>        80:48968/TCP   7m52s

kubectl 如何连接到 Kubernetes 集群 kubectl cluster-info_linux

更新

查看刚刚的nginx界面数据报头

kubectl 如何连接到 Kubernetes 集群 kubectl cluster-info_linux_02

[root@localhost ~]# kubectl set image deployment/nginx-bate1 nginx-bate1=nnx:1.13
deployment.extensions/nginx-bate1 image updated##把版本改成1.13
[root@localhost ~]# kubectl get pods -w	##动态查看状态

kubectl 如何连接到 Kubernetes 集群 kubectl cluster-info_linux_03

回滚

查看历史

[root@localhost ~]# kubectl rollout history deployment/nginx-bate1
deployment.extensions/nginx-bate1 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

执行回滚

[root@localhost ~]# kubectl rollout undo deployment/nginx-bate1
deployment.extensions/nginx-bate1
[root@localhost ~]# kubectl get pods -w

kubectl 如何连接到 Kubernetes 集群 kubectl cluster-info_linux_02

删除

删除不仅仅是pod,还有service

[root@localhost ~]# kubectl delete deployment/nginx-bate1
[root@localhost ~]# kubectl get pods
No resources found.
[root@localhost ~]# kubectl get svc
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        11d
nginx-service   NodePort    10.0.0.79    <none>        80:48968/TCP   2d3h
[root@localhost ~]# kubectl delete svc/nginx-service
service "nginx-service" deleted
[root@localhost ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   11d

其他常见操作

查看所以资源的详细信息

[root@localhost ~]# kubectl get all

查看service资源

[root@localhost ~]# kubectl describe service/kubernetes
Name:              kubernetes
Namespace:         default
Labels:            component=apiserver
                   provider=kubernetes
Annotations:       <none>
Selector:          <none>
Type:              ClusterIP
IP:                10.0.0.1
Port:              https  443/TCP
TargetPort:        6443/TCP
Endpoints:         20.0.0.3:6443,20.0.0.6:6443
Session Affinity:  None
Events:            <none>

查看deployment资源

[root@localhost ~]# kubectl describe deployment.apps/apa
Name:                   apa
Namespace:              default
CreationTimestamp:      Sun, 11 Oct 2020 22:19:32 +0800
Labels:                 run=apa
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=apa
……略

进入pod

[root@localhost ~]# 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