kubectl管理与项目的完整周期

一、kubectl管理

  • Kubectl是管理k8s集群的命令行工具,通过生成的json格式传递给apiserver进行创建、查看、管理的操作。
[root@localhost bin]# kubectl --help
kubectl controls the Kubernetes cluster manager. 
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 andlabel 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替换一个资源   ## 往往在 yaml 会用到
  wait           Experimental: Wait for a specific condition on one or many resources.
  convert        在不同的 API versions 转换配置文件       ## 最K8S 二次开发的时候会用到

Settings Commands:
  label          更新在这个资源上的 labels     ## 打标签
  annotate       更新一个资源的注解          ## 打注释
  completion     Output shell completion code for the specified shell (bash or zsh) ## 输出shell终端当中的代码,输出在指令性的bash界面
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"    ## 可以使用哪些api的版本
  config         修改 kubeconfig 文件
  plugin         Provides utilities for interacting with plugins.
  version        输出 client 和 server 的版本信息

二、项目的完整周期

  • 项目的生命周期,创建–》发布–》更新–》回滚–》删除
  • deployment : 控制器,更新,控制版本回滚等 控制器被干掉 所有的pod也会被删掉
  • replicaset: pod宕机,自动创建到设置的副本数,先创建再删除
  • service 对pod做负载均衡的 不会跟着pod一起被删,要单独删

2.1 创建项目

[root@localhost ~]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 ## 镜像用nginx  port端口为80  副本用三个
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@master01 ~]# kubectl get pod,deploy,replicaset -o wide
NAME                                    READY   STATUS    RESTARTS   AGE     IP            NODE              NOMINATED NODE
pod/nginx-deployment-5477945587-6s276   1/1     Running   0          3m10s   172.17.48.2   192.168.233.200   <none>
pod/nginx-deployment-5477945587-j2nzn   1/1     Running   0          3m10s   172.17.48.3   192.168.233.200   <none>
pod/nginx-deployment-5477945587-qglpf   1/1     Running   0          3m10s   172.17.52.2   192.168.233.180   <none>

NAME                                     DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS         IMAGES   SELECTOR
deployment.extensions/nginx-deployment   3         3         3            3           3m10s   nginx-deployment   nginx    run=nginx-deployment

NAME                                                DESIRED   CURRENT   READY   AGE     CONTAINERS         IMAGES                            SELECTOR
replicaset.extensions/nginx-deployment-5477945587   3         0         0       3m10s   nginx-deployment   nginx                             pod-template-hash=5477945587,run=nginx-deployment

2.2 项目的发布

[root@master01 ~]# kubectl expose deployment nginx-deployment  --port=80 --target-port=80 --name=nginx-service --type=NodePort      ## port 端口为80  target-port端口也是80  服务的端口为NodePort  
[root@master01 ~]# 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.83    <none>        80:46784/TCP   19s      ## 对外提供服务的端口,不指定就是随机的

service服务提供负载均衡的功能

node01 节点
[root@node01 ~]# yum install  ipvsadm -y
[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  192.168.233.200:46784 rr           ## 发现192.168.233.200:46784 轮询到下面几个节点
  -> 172.17.48.2:80               Masq    1      0          0         
  -> 172.17.48.3:80               Masq    1      0          0         
  -> 172.17.52.2:80               Masq    1      0          0     
node02节点
[root@node02 ~]# yum install  ipvsadm -y     
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.233.180:31268 rr           ## 发现192.168.233.180:46784 轮询到下面几个节点
  -> 172.17.48.2:80               Masq    1      0          0         
  -> 172.17.48.3:80               Masq    1      0          0         
  -> 172.17.52.3:80               Masq    1      0          0         

2.3 更新版本

在这里插入图片描述

[root@master01 ~]# kubectl set image deployment/nginx-deployment   nginx-deployment=nginx:1.14   ## 将nginx 版本“更新”为1.14
deployment.extensions/nginx-deployment image updated
[root@master01 ~]# kubectl get pod -w         ## 监听pod状态
NAME                                READY   STATUS              RESTARTS   AGE
nginx-deployment-5477945587-6s276   1/1     Running             0          25m
nginx-deployment-5477945587-j2nzn   1/1     Terminating         0          25m
nginx-deployment-5477945587-qglpf   1/1     Running             0          25m
nginx-deployment-8f66bcd89-lc72j    0/1     ContainerCreating   0          1s
nginx-deployment-8f66bcd89-zrmgf    1/1     Running             0          7s
nginx-deployment-5477945587-j2nzn   0/1   Terminating   0     25m
nginx-deployment-5477945587-j2nzn   0/1   Terminating   0     25m
nginx-deployment-5477945587-j2nzn   0/1   Terminating   0     25m
nginx-deployment-8f66bcd89-lc72j   1/1   Running   0     10s
nginx-deployment-5477945587-6s276   1/1   Terminating   0     25m
nginx-deployment-8f66bcd89-4ct2r   0/1   Pending   0     0s
nginx-deployment-8f66bcd89-4ct2r   0/1   Pending   0     0s
nginx-deployment-8f66bcd89-4ct2r   0/1   ContainerCreating   0     0s
nginx-deployment-5477945587-6s276   0/1   Terminating   0     25m
nginx-deployment-5477945587-6s276   0/1   Terminating   0     25m
nginx-deployment-5477945587-6s276   0/1   Terminating   0     25m
nginx-deployment-8f66bcd89-4ct2r   1/1   Running   0     21s
nginx-deployment-5477945587-qglpf   1/1   Terminating   0     25m
nginx-deployment-5477945587-qglpf   0/1   Terminating   0     25m
nginx-deployment-5477945587-qglpf   0/1   Terminating   0     26m
nginx-deployment-5477945587-qglpf   0/1   Terminating   0     26m

在这里插入图片描述

2.4 版本回滚

[root@master01 bin]# kubectl rollout --help
  history     显示 rollout 历史
  pause       标记提供的 resource 为中止状态
  resume      继续一个停止的 resource
  status      显示 rollout 的状态
  undo        撤销上一次的 rollout
  • 查看历史版本记录
[root@master01 ~]# kubectl rollout history deployment.extensions/nginx-deployment
deployment.extensions/nginx-deployment 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
  • 回滚
[root@master01 ~]#  kubectl rollout undo deployment/nginx-deployment  --to-revision=1
deployment.extensions/nginx-deployment
[root@localhost ~]# kubectl get pod -w       ##监听状态可以发现是先创建后删除
NAME                                READY   STATUS              RESTARTS   AGE
nginx-deployment-5477945587-tgnzk   0/1     ContainerCreating   0          3s
nginx-deployment-8f66bcd89-4ct2r    1/1     Running             0          16m
nginx-deployment-8f66bcd89-lc72j    1/1     Running             0          17m
nginx-deployment-8f66bcd89-zrmgf    1/1     Running             0          17m
nginx-deployment-5477945587-tgnzk   1/1   Running   0     10s
nginx-deployment-8f66bcd89-4ct2r   1/1   Terminating   0     16m
nginx-deployment-5477945587-wjcnz   0/1   Pending   0     0s
nginx-deployment-5477945587-wjcnz   0/1   Pending   0     0s
nginx-deployment-5477945587-wjcnz   0/1   ContainerCreating   0     1s
nginx-deployment-8f66bcd89-4ct2r   0/1   Terminating   0     17m
nginx-deployment-8f66bcd89-4ct2r   0/1   Terminating   0     17m
nginx-deployment-8f66bcd89-4ct2r   0/1   Terminating   0     17m
nginx-deployment-5477945587-wjcnz   1/1   Running   0     17s
nginx-deployment-8f66bcd89-lc72j   1/1   Terminating   0     17m
nginx-deployment-5477945587-4nbcp   0/1   Pending   0     0s
nginx-deployment-5477945587-4nbcp   0/1   Pending   0     0s
nginx-deployment-5477945587-4nbcp   0/1   ContainerCreating   0     0s
nginx-deployment-8f66bcd89-lc72j   0/1   Terminating   0     17m
nginx-deployment-8f66bcd89-lc72j   0/1   Terminating   0     17m
nginx-deployment-8f66bcd89-lc72j   0/1   Terminating   0     17m
nginx-deployment-5477945587-4nbcp   1/1   Running   0     5s
nginx-deployment-8f66bcd89-zrmgf   1/1   Terminating   0     17m
nginx-deployment-8f66bcd89-zrmgf   0/1   Terminating   0     17m
nginx-deployment-8f66bcd89-zrmgf   0/1   Terminating   0     17m
nginx-deployment-8f66bcd89-zrmgf   0/1   Terminating   0     17m

在这里插入图片描述

  • 查看回滚状态
[root@master01 ~]# kubectl rollout status deployment.extensions/nginx-deployment 
deployment "nginx-deployment" successfully rolled out     ## 回滚成功

2.5 删除资源

[root@master01 ~]# kubectl get deployment
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3         3         3            3           48m
[root@master01 ~]# kubectl delete deployment/nginx-deployment    ## 删除控制器
deployment.extensions "nginx-deployment" deleted
[root@master01 ~]# kubectl get pods   ## 没有pod 资源被释放
No resources found.
[root@master01 ~]# 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.83    <none>        80:46784/TCP   43m
[root@master01 ~]# kubectl delete svc/nginx-service   ## 删除service服务
service "nginx-service" deleted

  • 查看pod详细信息 一般排错时用
[root@master01 ~]# kubectl describe pod pod名称
[root@master01 ~]# kubectl describe deployment/nginx ## 查看deploy详细信息
  • 进入pod
[root@master01 ~]# kubectl exec -it pod名称 bash
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值