CKA认证K8skubernetes

【K8s 精选】CKA - 了解 Kubernetes 对象的管

2022-03-01  本文已影响0人  熊本极客

参考管理 Kubernetes 对象,需要安装工具 kubectl

1.使用配置文件对 Kubernetes 对象进行声明式管理

Deployment 配置文件 nginx-deployment.yaml 示例:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  minReadySeconds: 5
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

① 打印 yaml 将要创建出来的对象:kubectl diff -f nginx-deployment.yaml
② 创建对象:kubectl apply -f nginx-deployment.yaml
③ 打印 yaml 已经创建出来的对象:kubectl get -f nginx-deployment.yaml -o yaml
④ 更新对象:kubectl apply -f nginx-deployment.yaml
⑤ 删除对象:kubectl delete -f nginx-deployment.yaml

2.使用指令式命令管理 Kubernetes 对象

kubectl 工具支持动词驱动的命令,用来创建一些最常见的对象类别:
run:创建一个新的 Pod 来运行一个容器。
expose:创建一个新的 Service 对象为若干 Pod 提供流量负载均衡。
autoscale:创建一个新的 Autoscaler 对象来自动对某控制器(如 Deployment) 执行水平扩缩。

2.1 创建对象

利用命令 kubectl create <对象类别> [<子类别>] <对象实例名称> 创建对象,利用 kubectl create <对象类别> [<子类别>] -h 查看创建对象所需的参数。

$kubectl create service nodeport -h
Create a NodePort service with the specified name.

Examples:
  # Create a new NodePort service named my-ns
  kubectl create service nodeport my-ns --tcp=5678:8080

Options:
      --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-create': Name of the manager used to track field ownership.
      --node-port=0: Port used to expose the service on each node in a cluster.
  -o, --output='': Output format. One of:
json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
      --save-config=false: If true, the configuration of current object will be saved in its annotation. Otherwise, the
annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.
      --tcp=[]: Port pairs can be specified as '<port>:<targetPort>'.
      --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].
      --validate=true: If true, use a schema to validate the input before sending it

Usage:
  kubectl create service nodeport NAME [--tcp=port:targetPort] [--dry-run=server|client|none] [options]

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

2.2 查看对象

① 查看对象的详情,可以利用命令 kubectl get <对象类别>/<对象实例名称> -o yamlkubectl describe <对象类别>/<对象实例名称> -o yaml
② 查看对 Pod 中运行的容器日志,可以利用命令 kubectl logs pod <容器名称>

# 查看 deployment 对象的详情
$kubectl get deployment/nginx-deployment -o yaml
$kubectl describe deployment/nginx-deployment -o yaml
# 查看 pod 对象的详情
$kubectl describe pod/nginx-deployment-cd6d8b5cf-d9px2

# 查看对 Pod 中运行的容器日志
$kubectl logs pod/nginx-deployment-cd6d8b5cf-d9px2

2.3 更新对象

kubectl set 直接修改对象
kubectl set <资源类型> <对象类别>/<对象实例名称> <资源名称>=<修改的新内容>,例如,kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1

# set 帮助
$kubectl set -h
Configure application resources

 These commands help you make changes to existing application resources.

Available Commands:
  env            Update environment variables on a pod template
  image          Update image of a pod template
  resources      Update resource requests/limits on objects with pod templates
  selector       Set the selector on a resource
  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).


# set image 帮助
$kubectl set image -h
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)
      --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).

kubectl edit 编辑对象的配置

$kubectl edit deployment/nginx-deployment

2.4 删除对象

删除对象可以利用命令 kubectl delete <对象类别>/<对象实例名称>

# 删除 deployment 对象
$kubectl delete deployment/nginx-deployment
# 删除 pod 对象,删除后会自动启动新 pdo
$kubectl delete pod/nginx-deployment-cd6d8b5cf-d9px2
上一篇下一篇

猜你喜欢

热点阅读