基本常识
什么是资源
在Kubernetes中,资源(Resources)指的是集群中可以被管理的实体。
这些实体可以是一个Pod、一个服务、一个卷、一个配置文件,或者是任何其他Kubernetes可以管理的对象。
在Kubernetes中,资源有称为对象。
都有哪些资源
核心资源
- Pod:运行容器的基本单位。
- Service:定义如何访问一组Pods。
- Deployment:管理Pod的副本数和升级。
- DaemonSet:确保每个节点上运行一个Pod的副本。
- StatefulSet:管理有状态的Pod集合。
- Job:运行一次性或有限次数的Pod。
- CronJob:运行基于Cron表达式的定时任务。
配置资源
- ConfigMap:存储非敏感的配置数据。
- Secret:存储敏感数据,如密码和OAuth令牌。
- Volume:为Pod提供存储。
- PersistentVolume:集群中的存储资源。
- PersistentVolumeClaim:声明对存储资源的请求。
网络资源
- NetworkPolicy:定义网络策略,控制Pod之间的通信。
- Ingress:暴露集群外部可访问的Service。
自定义资源
- Custom Resource Definition (CRD):允许用户创建自己的API对象,这些对象可以被创建、更新、删除和列出。
集群资源
- Node:Kubernetes集群中的工作节点。
- Namespace:逻辑上组织资源的容器。
- Role 和 RoleBinding:基于角色的访问控制。
- ClusterRole 和 ClusterRoleBinding:跨命名空间的访问控制。
- PodSecurityPolicy:控制Pod的安全策略。
- LimitRange:为Pod和容器设置资源请求和限制的上下限。
- ResourceQuota:限制命名空间内资源的使用总量。
其他资源
- Event:事件记录。
- HorizontalPodAutoscaler (HPA):自动扩展或缩小Pod的副本数。
- ClusterAutoscaler:自动扩展或缩小节点的数量。
资源之间的关系
在Kubernetes中,核心资源之间的关系是紧密相连的,它们共同工作以实现集群的自动化管理和高效运行。
Pod与容器
- Pod 是 Kubernetes 中可以被创建和管理的最低级别的资源。一个Pod可以包含一个或多个容器,这些容器共享存储和网络。
Pod与Deployment/StatefulSet/DaemonSet/Job/CronJob
- Deployment 用于管理Pod的副本数,并支持滚动更新。
- StatefulSet 用于部署有状态应用程序,并确保每个Pod都有一个唯一的标识。
- DaemonSet 确保所有或指定的节点上运行一个Pod的副本,通常用于系统守护进程。
- Job 用于运行一个一次性或有限次数的Pod,可以提交批处理任务。
- CronJob 用于在指定的时间间隔运行Job。
Pod与Service
- Service 定义了一个可以访问Pod的逻辑方式。它提供了一种抽象,允许你通过一个单一的IP地址和端口号来访问多个Pod。
Pod与Volume
- Volume 为Pod提供存储,可以挂载到Pod中的容器中。
Pod与ConfigMap/Secret
- ConfigMap 用于存储非敏感的配置数据,如环境变量、命令行参数等。
- Secret 用于存储敏感数据,如密码和OAuth令牌。
Pod与NetworkPolicy:
- NetworkPolicy 定义网络策略,控制Pod之间的通信。
Pod与Ingress:
- Ingress 暴露集群外部可访问的Service,用于外部访问集群中的服务。
Pod与Namespace:
- Namespace 用于对集群中的资源进行逻辑分组。每个命名空间都有自己的一套资源,如Pod、Service等。
Pod与Role/RoleBinding:
- Role 和 RoleBinding 用于基于角色的访问控制,可以用来限制Pod的访问权限。
Pod与ClusterRole/ClusterRoleBinding:
- ClusterRole 和 ClusterRoleBinding 用于跨命名空间的访问控制。
Pod与PodSecurityPolicy:
- PodSecurityPolicy 用于控制Pod的安全策略,如文件系统挂载、特权模式等。
Pod与LimitRange/ResourceQuota:
- LimitRange 用于为Pod和容器设置资源请求和限制的上下限。
- ResourceQuota 用于限制命名空间内资源的使用总量。
创建资源的方式
在Kubernetes中资源创建有很多种,在学习的过程中要特别注意两种,即:命令行创建、YAML文件创建。
命令行创建资源
以命名空间为例
注意:名称空间用来隔离资源
kubectl create namespace test-namespace
YAML文件创建资源
以命名空间为例
注意:名称空间用来隔离资源
- 编辑yaml
#test-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: test-namespace
- 创建命名空间
kubect create -f test-namespace.yaml
基础命令
帮助文档
查看kubectl命令帮助文档
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 Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
run Run a particular image on the cluster
set Set specific features on objects
Basic Commands (Intermediate):
explain Documentation of resources
get Display one or many resources
edit Edit a resource on the server
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, or ReplicationController
Cluster Management Commands:
certificate Modify certificate resources.
cluster-info Display cluster info
top Display Resource (CPU/Memory/Storage) usage.
cordon Mark node as unschedulable
uncordon Mark node as schedulable
drain Drain node in preparation for maintenance
taint Update the taints on one or more nodes
Troubleshooting and Debugging Commands:
describe Show details of a specific resource or group of resources
logs Print the logs for a container in a pod
attach Attach to a running container
exec Execute a command in a container
port-forward Forward one or more local ports to a pod
proxy Run a proxy to the Kubernetes API server
cp Copy files and directories to and from containers.
auth Inspect authorization
debug Create debugging sessions for troubleshooting workloads and nodes
Advanced Commands:
diff Diff live version against would-be applied version
apply Apply a configuration to a resource by filename or stdin
patch Update field(s) of a resource
replace Replace a resource by filename or stdin
wait Experimental: Wait for a specific condition on one or many resources.
kustomize Build a kustomization target from a directory or a remote url.
Settings Commands:
label Update the labels on a resource
annotate Update the annotations on a resource
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 Modify kubeconfig files
plugin Provides utilities for interacting with plugins.
version Print the client and server version information
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 explain pod
KIND: Pod
VERSION: v1
DESCRIPTION:
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
metadata <Object>
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
spec <Object>
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
status <Object>
Most recently observed status of the pod. This data may not be up to date.
Populated by the system. Read-only. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
YAML常用命令
以Pod为例
演示YAML文件
文件名称为pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: default
labels:
app: nginx
type: web
spec:
containers:
- name: nginx-web-01
image: registry.cn-beijing.aliyuncs.com/publicspaces/nginx:1.22.1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8001
name: http
protocol: TCP
workingDir: /usr/local/nginx
restartPolicy: Always
根据YAML文件创建服务
#格式
kubectl create -f YAML文件.yaml
#举例
kubectl create -f pod.yaml
根据YAML文件更新服务
- replace
#格式
kubectl replace -f YAML文件.yaml
#举例
kubectl replace -f pod.yaml
- apply
#格式
kubectl apply -f YAML文件.yaml
#举例
kubectl apply -f pod.yaml
根据YAML文件删除服务
#格式
kubectl delete -f YAML文件.yaml
#举例
kubectl delete -f pod.yaml
常用命令
以Pod为例
查看资源运行情况
- 查看所有Pod
#格式
kubectl get 资源名称
#举例
kubectl get pod
- 查看某个Pod
#格式
kubectl get 资源名称 资源创建的Pod
#举例
kubectl get pod nginx
查看资源运行事件
- 查看所有Pod
#格式
kubectl describe 资源名称
#举例
kubectl describe pod
- 查看某个Pod
#格式
kubectl describe 资源名称 资源创建的Pod
#举例
kubectl describe pod nginx
查看资源运行日志
- 查看所有Pod
- 查看某个Pod
#格式
kubectl logs 资源创建的Pod
#举例
kubectl logs nginx
查看资源详细情况
- 查看某个资源详细情况
#格式
kubectl get 资源名称 -o wide
#举例
kubectl get pod -o wide
查看资源详细信息
- 查看所有Pod
#格式
kubectl get 资源名称 -o yaml
#举例
kubectl get pod -o yaml
- 查看某个Pod
#格式
kubectl get 资源名称 资源创建的Pod -o yaml
#举例
kubectl get pod nginx -o yaml
特别注意
命名空间
- 各个资源之间都有命名空间的隔离;
- 相同命名空间Pod名称不能一样;
- 不同命名空间Pod名称可以重复;
- 当不指定命名空间时,默认命名空间为default;
命名空间的切换
#格式
kubectl get 资源名称 -n 命名空间名称
kubectl get 资源名称 --namespace 命名空间名称
#举例
kubectl get pod -n kube-system
查看所有命名空间
kubectl get namespace
查看所有命名空间下的资源
#格式
kubectl get 资源名称 --all-namespaces
#举例
kubectl get pod --all-namespaces