千家信息网

Pod资源对象

发表于:2025-02-20 作者:千家信息网编辑
千家信息网最后更新 2025年02月20日,Deployment,Service,Pod是k8s最核心的3个资源对象。Deployment:最常见的无状态应用的控制器,支持应用的扩缩容,滚动更新等操作。Service:为弹性变动且存在生命周期的
千家信息网最后更新 2025年02月20日Pod资源对象

Deployment,Service,Pod是k8s最核心的3个资源对象。

Deployment:最常见的无状态应用的控制器,支持应用的扩缩容,滚动更新等操作。

Service:为弹性变动且存在生命周期的Pod对象提供了一个固定的访问接口,用于服务发现和服务访问。

Pod:是运行容器以及调度的最小单位。同一个Pod可以同时运行多个容器,这些容器共享NET,UTS,IPC.除此之外还有USER,PID,MOUNT.

ReplicationController:用于确保每个Pod副本在任意时刻都能满足目标数量,简单来说,它用于保证每个容器或容器组总是运行并且可以访问的:老一代无状态的Pod应用控制器。

ReplicaSet:新一代的无状态的Pod应用控制器,它与RC的不同之处在于支持的标签选择器不同,RC只支持等值选择器,RS还额外支持基于集合的选择器。

StatefulSet:用于管理有状态的持久化应用,如database服务程序,它与Deployment不同之处在于,它会为每一个Pod创建一个独立的持久性标识符,并确保每个Pod之间的顺序性。

DaemonSet:用于确保每一个节点都运行了某个Pod的一个副本,新增的节点一样会被添加此Pod,在节点移除时,此类Pod会被回收。

Job:用于管理运行完成后即可终止的应用,例如批量处理作业任务。

Volume:PV PVC

ConfigMap: 存储通用的配置变量的,类似于配置文件,使用户可以将分布式系统中用于不同模块的环境变量统一到一个对象中管理;而它与配置文件的区别在于它是存在集群的"环境"中的,并且支持K8S集群中所有通用的操作调用方式。

Secret: 用来保存小片敏感数据的k8s资源,例如密码,token,或者秘钥。这类数据当然也可以存放在Pod或者镜像中,但是放在Secret中是为了更方便的控制如何使用数据,并减少暴露的风险。

Role: 表示是一组规则权限,只能累加,Role可以定义在一个namespace中,只能用于授予对单个命名空间中的资源访问的权限比如我们新建一个对默认命名空间中。

ClusterRole:

RoleBinding:

ClusterRoleBinding:

Service account:

Helm:

Namespace:名称空间

默认的名称空间:Default

//查看名称空间

[root@master ~]# kubectl get ns

//查看名称空间详细信息

[root@master ~]# kubectl describe ns default 

//创建名称空间

[root@master ~]# kubectl create ns bdqn

[root@master ~]# kubectl explain ns

[root@master ~]# vim 111-test.yamlapiVersion: v1kind: Namespacemetadata:  name: test
[root@master ~]# kubectl apply -f 111-test.yaml [root@master ~]# kubectl get ns

删除资源的两种方法:

[root@master ~]# kubectl delete ns test [root@master ~]# kubectl delete -f 111-test.yaml 

Ps: namespace资源对象仅用于资源对象的隔离,并不能隔绝不同名称空间的Pod之间的的通信,那是网络策略资源的功能。

查看指定名称空间的资源可以使用--namespace或者-n选项

[root@master ~]# kubectl get pod --namespace=bdqn No resources found.

简写:

[root@master ~]# kubectl get pod -n bdqn No resources found.

查看本集群运行的Pod

[root@master ~]# kubectl get pod -n kube-system 

Pod

[root@master ~]# vim pod.yamlkind: PodapiVersion: v1metadata:  name: test-podspec:  containers:  - name: test-app    image: httpd
[root@master ~]# kubectl apply -f pod.yaml pod/test-pod created

[root@master ~]# kubectl get pod

[root@master ~]# vim pod.yamlkind: PodapiVersion: v1metadata:  name: test-pod  namespace: bdqn           //添加一行spec:  containers:  - name: test-app    image: httpd

重新生成:

[root@master ~]# kubectl apply -f pod.yaml pod/test-pod created

查看bdqn名称空间

[root@master ~]# kubectl get pod -n bdqn NAME       READY   STATUS    RESTARTS   AGEtest-pod   1/1     Running   0          80s

Pod中镜像获取策略:

Always:镜像标签为"latest"或镜像不存在时,总是从指定的仓库中获取镜像。

IfNotPresent:仅当本地镜像不存在时才从目标仓库中下载。

Never:禁止从仓库中下载镜像,即只是用本地镜像。

PS:对于标签"latest"或者是不存在,其默认策略下载及策略为"Always",而对于其他标签的镜像,默认策略为"IfNotPresent"。

[root@master ~]# vim pod.yaml kind: PodapiVersion: v1metadata:  name: test-pod  namespace: bdqnspec:  containers:  - name: test-app    image: httpd    imagePullPolicy: IfNotPresent    ports:    - protocol: TCP      containerPort: 80
[root@master ~]# kubectl delete pod -n bdqn test-pod pod "test-pod" deleted
[root@master ~]# kubectl apply -f pod.yaml pod/test-pod created
[root@master ~]# kubectl apply -f pod.yaml pod/test-pod created
[root@master ~]# kubectl get pod -n bdqnNAME       READY   STATUS    RESTARTS   AGEtest-pod   1/1     Running   0          26s

最终效果:

[root@master ~]# vim pod.yaml kind: PodapiVersion: v1metadata:  name: test-pod  namespace: bdqn  labels:    app: test-webspec:  containers:  - name: test-app    image: httpd    imagePullPolicy: IfNotPresent    ports:    - protocol: TCP      containerPort: 90
[root@master ~]# vim svc.yamlapiVersion: v1kind: Servicemetadata:  name: test-svc  namespace: bdqnspec:  selector:    app: test-web  ports:  - port: 80    targetPort: 90

[root@master ~]# kubectl describe svc -n bdqn test-svc

容器的重启策略

Always:单反Pod对象终止就将其重启,此为默认设定。

OnFailure:仅在Pod对象出现错误时才将其重启。

Never:从不重启。

Pod的默认健康检查

[root@master ~]# vim healcheck.yamlapiVersion: v1kind: Podmetadata:  labels:    test: healcheck  name: healcheckspec:  restartPolicy: OnFailure  containers:  - name: healthcheck    image: busybox    args:    - /bin/sh    - -c    - sleep 20; exit 1

[root@master ~]# kubectl apply -f healcheck.yaml

[root@master ~]# kubectl get pod -w

[root@master ~]# kubectl get pod -n kube-system

0