云原生之K8s管理工具—kubectl 详解(二)

目录

引言:

一、项目的生命周期

二、创建kubectl run命令

使用run跑,报错了

三、发布kubectl expose命令

3.1、service的作用

3.2、Service的类型

3.3、查看Pod网络状态详细信息和Service暴露端口

3.4、查看关联后端的节点

3.5、查看service的详细描述信息

3.6、访问内部IP查看

​编辑

3.7、查看访问日志

四、更新kubectl set

4.1、获取修改模板

4.2、查看当前nginx的版本号

4.3、将nginx版本更新为1.24

4.4、监听pod状态

4.5、查看pod的IP变化

五、回滚kubectl rollout

5.1、查看历史版本

5.2、执行回滚到上一个版本

5.3、执行回滚到执行版本

5.4、检查回滚状态

六、删除kubectl delete

6.1 删除副本控制器

6.2 删除service


引言:

K8S模拟项目

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

[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         Set a new size for a Deployment, ReplicaSet or Replication
Controller
  autoscale     Auto-scale a Deployment, ReplicaSet, StatefulSet, or
ReplicationController

Cluster Management Commands:
  certificate   修改 certificate 资源.
  cluster-info  显示集群信息
  top           显示 Resource (CPU/Memory) 使用.
  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
  debug         Create debugging sessions for troubleshooting workloads and
nodes

Advanced Commands:
  diff          Diff live version against would-be applied version
  apply         通过文件名或标准输入流(stdin)对资源进行配置
  patch         Update field(s) of a resource
  replace       通过 filename 或者 stdin替换一个资源
  wait          Experimental: Wait for a specific condition on one or many
resources.
  kustomize     Build a kustomization target from a directory or URL.

Settings Commands:
  label         更新在这个资源上的 labels
  annotate      更新一个资源的注解
  completion    Output shell completion code for the specified shell (bash or
zsh)

Other Commands:
  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 run命令

①创建并运行一个或多个容器镜像

②创建一个deployment或job来管理容器

③kubectl run --help查看使用帮助

启动nginx实例,暴露容器端口80,设置副本数3

kubectl run nginx-deployment --image=nginx:1.14 --port=80 --replicas=3

使用run跑,报错了

k8sv1.18.0以后的版本, --replicas以后弃用该命令,推荐使用deployment创建pods
我这里用的是1.21.3版本

  1. 想创建多个实例时可以使用:kubectl create deployment nginx --images=nginx --port=80 --replicas=3来进行创建
  2. 查看pod: kubectl get pod,用来查看使用命令创建的所有实例
  3. 查看deploy:kubectl get deploy,用来查看实例所创建的数量;
  4. 高于1.17版本的建议以后直接使用create deployment创建pod管理器方式创建pod;

kubectl create deployment nginx --image=nginx --port=80 --replicas=3

三、发布kubectl expose命令

将资源暴露为新的Service

将Deployment的nginx创建Service,并通过Service的80端口转发至容器的80的端口上,Service的名称为nginx,类型为NodePort

kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx --type=NodePort

3.1、service的作用

  1. Kubernetes之所以需要Service,一方面是因为Pod的IP不是固定的(Pod可能会重建),另一方面是因为一组Pod实例之间总会有负载均衡的需求
  2. Service通过Label Selector实现对一组Pod进行访问
  3. 对于容器应用而言,Kubernetes提供了基于VIP(虚拟IP)的网桥的方式访问Service,再由Service重定向到相应的Pod

3.2、Service的类型

  1. ClusterIP:提供一个集群内部的虚拟IP以供Pod访问(Service默认类型)
  2. NodePort:在每个Node上打开一个端口以供外部访问,Kubernetes将会在每个Node上打开一个端口并且每个Node的端口都是一样的,通过NodeIP:NodePort的方式
  3. LoadBalancer:通过外部的负载均衡器来访问,通常在云平台部署LoadBalancer还需要额外的费用。

3.3、查看Pod网络状态详细信息和Service暴露端口

kubectl get pods,svc -o wide

3.4、查看关联后端的节点

kubectl get endpoints

3.5、查看service的详细描述信息

kubectl describe svc nginx

3.6、访问内部IP查看

kubectl describe svc nginx | grep NodePort
curl 192.168.100.6:31407

去浏览器查看三个节点能否访问nginx 

3.7、查看访问日志

kubectl logs []

四、更新kubectl set

1、更改现有应用资源一些信息。

2、kubectl set --help查看使用帮助

4.1、获取修改模板

kubectl set image --help获取

[root@master ~]# kubectl set image --help
Update existing container image(s) of resources.

 Possible resources include (case insensitive):

  pod (po), replicationcontroller (rc), deployment (deploy), daemonset (ds),
replicaset (rs)

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
  
  # Update all deployments' and rc's nginx container's image to 'nginx:1.9.1'
  kubectl set image deployments,rc nginx=nginx:1.9.1 --all
  
  # Update image of all containers of daemonset abc to 'nginx:1.9.1'
  kubectl set image daemonset abc *=nginx:1.9.1
  
  # Print result (in yaml format) of updating nginx container image from local
file, without hitting the server
  kubectl set image -f path/to/file.yaml nginx=nginx:1.9.1 --local -o yaml

Options:
      --all=false: Select all resources, including uninitialized ones, in the
namespace of the specified resource types
      --allow-missing-template-keys=true: If true, ignore any errors in
templates when a field or map key is missing in the template. Only applies to
golang and jsonpath output formats.
      --dry-run='none': Must be "none", "server", or "client". If client
strategy, only print the object that would be sent, without sending it. If
server strategy, submit server-side request without persisting the resource.
      --field-manager='kubectl-set': Name of the manager used to track field
ownership.
  -f, --filename=[]: Filename, directory, or URL to files identifying the
resource to get from a server.
  -k, --kustomize='': Process the kustomization directory. This flag can't be
used together with -f or -R.
      --local=false: If true, set image will NOT contact api-server but run
locally.
  -o, --output='': Output format. One of:
json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
      --record=false: Record current kubectl command in the resource annotation.
If set to false, do not record the command. If set to true, record the command.
If not set, default to updating the existing annotation value only if one
already exists.
  -R, --recursive=false: Process the directory used in -f, --filename
recursively. Useful when you want to manage related manifests organized within
the same directory.
  -l, --selector='': Selector (label query) to filter on, not including
uninitialized ones, supports '=', '==', and '!='.(e.g. -l
key1=value1,key2=value2)
      --show-managed-fields=false: If true, keep the managedFields when printing
objects in JSON or YAML format.
      --template='': Template string or path to template file to use when
-o=go-template, -o=go-template-file. The template format is golang templates
[http://golang.org/pkg/text/template/#pkg-overview].

Usage:
  kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1
... CONTAINER_NAME_N=CONTAINER_IMAGE_N [options]

Use "kubectl options" for a list of global command-line options (applies to all
commands).

4.2、查看当前nginx的版本号

curl -I 192.168.100.6:

4.3、将nginx版本更新为1.24

kubectl set image deployment/nginx-service nginx=nginx:1.15

此时就已经升级完成了

4.4、监听pod状态

处于动态监听pod状态,由于使用的是滚动更新方式,所以会先生成一个新的pod,然后删除一个旧的pod,往后以此类推

kubectl get pods -w

更新规则可通过“kubetl describe deployment nginx”的“RollingUpdateStrategy”查看,默认配置为“25% max unavailable, 25% max surge”,即按照25%的比例进行滚动更新。

4.5、查看pod的IP变化

kubectl get pod -o wide

五、回滚kubectl rollout

1、对资源进行回滚管理

2、kubectl rollout --help查看使用帮助

5.1、查看历史版本

kubectl rollout history deployment/nginx-service

5.2、执行回滚到上一个版本

kubectl rollout undo deployment/nginx
kubectl get pods -o wide

5.3、执行回滚到执行版本

查看历史版本

回到revision2,即1.15版本

kubectl rollout undo deployment/nginx-service --to-revision=2

5.4、检查回滚状态

kubectl rollout status deployment/nginx-service

六、删除kubectl delete

6.1 删除副本控制器

kubectl delete deployment/nginx

6.2 删除service

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值