当前位置: 首页 > news >正文

k8s:service资源详解

k8s:service资源详解

Service的作用

  • 解决Pod IP变化问题
  • 解决Pod IP集群外不可访问问题
  • 作为一组同类Pod的对外访问接口
  • 实现服务发现和负载均衡

Service类型

  • ClusterIP:集群内部访问

  • NodePort:集群外部访问

service操作示例

创建集群内部访问的service
[root@master ~]# vim deploy-nginx.yamlapiVersion: apps/v1
kind: Deployment
metadata:name: nginxnamespace: xy
spec:replicas: 3selector:matchLabels:run: nginxtemplate:metadata:labels:run: nginxspec:containers:- image: nginx:latestname: nginxports:- containerPort: 80protocol: TCP#通过service管理需要标签labels相同
[root@master ~]# kubectl apply -f deploy-nginx.yaml
[root@master ~]# kubectl get pods -n xy
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6c45cbd8c5-dg47s   1/1     Running   0          60s
nginx-6c45cbd8c5-h4mrw   1/1     Running   0          60s
nginx-6c45cbd8c5-pqpnl   1/1     Running   0          60s
#暴露service
[root@master ~]# kubectl expose deploy nginx --name=svc-nginx --type=ClusterIP --port=80 --target-port=80 -n xy
service/svc-nginx exposed
#查看service
[root@master ~]# kubectl get svc svc-nginx -n xy -o wide
NAME        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE   SELECTOR
svc-nginx   ClusterIP   10.103.221.167   <none>        80/TCP    54s   run=nginx
#这里产生了一个CLUSTER-IP,这就是service的IP,在Service的生命周期中,这个地址是不会变动的 可以通过这个IP访问当前service对应的POD
[root@master ~]# curl 10.103.221.167:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
创建集群外可访问的service
#上面创建的service的type类型为ClusterIP,只有集群内部可访问,将type类型修改为NodePort可以让外部访问
[root@master ~]# kubectl expose deploy nginx --name=svc-nginx1 --type=NodePort --port=80 --target-port=80 -n xy
service/svc-nginx1 exposed
[root@master ~]# kubectl get svc svc-nginx1 -n xy -o wide
NAME         TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   SELECTOR
svc-nginx1   NodePort   10.106.202.74   <none>        80:31766/TCP   59s   run=nginx
#外部访问ip:端口号,集群内的ip都可以访问

在这里插入图片描述

删除service
[root@master ~]# kubectl delete svc svc-nginx -n xy
service "svc-nginx" deleted
[root@master ~]# kubectl get svc -n xy
NAME         TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
svc-nginx1   NodePort   10.106.202.74   <none>        80:31766/TCP   3m49s
创建svc-nginx.yaml
#如果不会创建也可以去看官方文档
apiVersion: v1
kind: Service
metadata:name: svc-nginx3namespace: xy
spec:clusterIP: 10.96.0.11ports:- port: 80protocol: TCPtargetPort: 80selector:run: nginxtype: ClusterIP
#这里的clusterIP必须跟下面clusterIP的网段相同
[root@master ~]# kubectl get svc -n kube-system 
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   3d
pod详解
#pod资源清单apiVersion: v1           # 必选,版本号
kind: Pod               # 必选,资源类型
metadata:               # 必选,元数据name: string          # 必选,Pod名称namespace: string     # Pod所属的命名空间,默认为"default"labels:               # 自定义标签列表- name: string          
spec:                   # 必选,Pod中容器的详细定义containers:           # 必选,Pod中容器列表- name: string        # 必选,容器名称image: string       # 必选,容器的镜像名称imagePullPolicy: [Always | Never | IfNotPresent]  # 获取镜像的策略 command: [string]   # 容器的启动命令列表args: [string]      # 容器的启动命令参数列表workingDir: string  # 容器的工作目录# 挂载到容器内部的存储卷配置volumeMounts:       - name: string      # 引用pod定义的共享存储卷名称mountPath: string # 存储卷在容器内mount的绝对路径readOnly: boolean # 是否为只读模式# 需要暴露的端口号列表ports:             - name: string        # 端口名称containerPort: int  # 容器需要监听的端口号hostPort: int       # 容器所在主机需要监听的端口号protocol: string    # 端口协议,支持TCP和UDP,默认TCP# 容器运行前需设置的环境变量列表env:               - name: string   # 环境变量名称value: string  # 环境变量的值# 资源限制和请求的设置resources:        limits:         # 资源限制的设置cpu: string    # CPU限制,单位为core数memory: string # 内存限制,单位可以为MiB/GiBrequests:       # 资源请求的设置cpu: string    # CPU请求,容器启动的初始可用数量memory: string # 内存请求,容器启动的初始可用数量# 生命周期钩子lifecycle:        postStart:      # 容器启动后立即执行此钩子preStop:        # 容器终止前执行此钩子# 容器健康检查设置livenessProbe:    exec:           # exec检查方式command: [string]  # exec方式需要执行的命令或脚本httpGet:        # HttpGet检查方式path: stringport: numberhost: stringscheme: stringhttpHeaders:- name: stringvalue: stringtcpSocket:      # tcpSocket检查方式port: numberinitialDelaySeconds: 0  # 容器启动后首次探测时间(秒)timeoutSeconds: 0       # 探测等待响应的超时时间(秒)periodSeconds: 0        # 定期探测时间间隔(秒)successThreshold: 0failureThreshold: 0securityContext:privileged: false# Pod的重启策略restartPolicy: [Always | Never | OnFailure]# 节点调度相关nodeName: <string>        # 指定调度到特定节点nodeSelector: object      # 根据label选择调度节点# 镜像拉取秘钥imagePullSecrets:         - name: string# 网络配置hostNetwork: false        # 是否使用主机网络模式# 存储卷配置volumes:                  # 共享存储卷列表- name: string           # 存储卷名称emptyDir: {}           # 临时目录,与Pod同生命周期hostPath:              # 宿主机目录挂载path: string         # 宿主机目录路径secret:                # Secret存储卷secretName: string  items:     - key: stringpath: stringconfigMap:             # ConfigMap存储卷name: stringitems:- key: stringpath: string

通过explain查看某种资源可以配置的一级属性

[root@master ~]# kubectl explain pod
KIND:       Pod
VERSION:    v1DESCRIPTION:Pod is a collection of containers that can run on a host. This resource iscreated 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, andmay reject unrecognized values. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resourceskind	<string>Kind is a string value representing the REST resource this objectrepresents. Servers may infer this from the endpoint the client submitsrequests to. Cannot be updated. In CamelCase. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kindsmetadata	<ObjectMeta>Standard object's metadata. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadataspec	<PodSpec>Specification of the desired behavior of the pod. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-statusstatus	<PodStatus>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#查看
[root@master ~]# kubectl explain pod.kind
KIND:       Pod
VERSION:    v1FIELD: kind <string>DESCRIPTION:Kind is a string value representing the REST resource this objectrepresents. Servers may infer this from the endpoint the client submitsrequests to. Cannot be updated. In CamelCase. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
在kubernetes中基本所有资源的一级属性都是一样的,主要包含5部分:
1、apiVersion 版本,由kubernetes内部定义,版本号必须可以用 kubectl api-versions 查询到
2、kind 类型,由kubernetes内部定义,版本号必须可以用 kubectl api-resources 查询到
3、metadata 元数据,主要是资源标识和说明,常用的有name、namespace、labels等
4、spec描述,这是配置中最重要的一部分,里面是对各种资源配置的详细描述
5、status状态信息,里面的内容不需要定义,由kubernetes自动生成
containers - 容器列表
类型:<[]Object>(对象数组)作用:定义Pod中运行的一个或多个容器2. nodeName - 节点名称
作用:强制将Pod调度到指定名称的Node节点使用场景:需要精确控制Pod运行位置时注意:跳过正常的调度器,直接指定节点3. nodeSelector - 节点选择器
类型:<map[]>(键值对映射)作用:根据Node的label标签选择调度目标工作机制:选择包含指定label的Node进行调度4. hostNetwork - 主机网络模式
类型:布尔值默认值:false作用:false:使用Pod独立网络命名空间true:直接使用宿主机网络(共享网络栈)5. volumes - 存储卷
类型:<[]Object>(对象数组)作用:定义Pod级别的存储卷用途:容器间共享数据、持久化存储常见类型:emptyDir、hostPathconfigMap、secret持久卷(PersistentVolume)6. restartPolicy - 重启策略
作用:定义Pod在遇到故障时的处理策略可选值:Always:总是重启(默认)OnFailure:失败时重启Never:从不重启
pod配置
[root@master ~]# kubectl explain pod.spec.containers
KIND:       Pod
VERSION:    v1FIELD: containers <[]Container>DESCRIPTION:List of containers belonging to the pod. Containers cannot currently beadded or removed. There must be at least one container in a Pod. Cannot beupdated.A single application container that you want to run within a pod.FIELDS:args	<[]string>Arguments to the entrypoint. The container image's CMD is used if this isnot provided. Variable references $(VAR_NAME) are expanded using thecontainer's environment. If a variable cannot be resolved, the reference inthe input string will be unchanged. Double $$ are reduced to a single $,which allows for escaping the $(VAR_NAME) syntax: i.e. "$$(VAR_NAME)" willproduce the string literal "$(VAR_NAME)". Escaped references will never beexpanded, regardless of whether the variable exists or not. Cannot be
.........name  <string>     # 容器名称image <string>     # 容器需要的镜像地址imagePullPolicy  <string> # 镜像拉取策略 command  <[ ]string> # 容器的启动命令列表,如不指定,使用打包时使用的启动命令args     <[ ]string> # 容器的启动命令需要的参数列表env      <[ ]Object> # 容器环境变量的配置ports    <[ ]Object>     # 容器需要暴露的端口号列表resources <Object>      # 资源限制和资源请求的设置
创建pod-baseyaml
apiVersion: v1
kind: Pod
metadata:name: pod-basenamespace: xylabels:user: user1
spec:containers:- name: nginximage: nginx:latest- name: busyboximage: busybox:1.30
[root@master ~]# kubectl apply -f pod-base.yaml 
pod/pod-base created
[root@master ~]# kubectl get pod -n xy
NAME                     READY   STATUS     RESTARTS      AGE
nginx-6c45cbd8c5-dg47s   1/1     Running    0             70m
nginx-6c45cbd8c5-h4mrw   1/1     Running    0             70m
nginx-6c45cbd8c5-pqpnl   1/1     Running    0             70m
pod-base                 1/2     NotReady   3 (31s ago)   51s
#这里有一个NotReady,原因是busybox并不是一个程序,而是类似于一个工具类的集合,kubernetes集群启动管理后,它会自动关闭。解决方法就是让其一直在运行,需要用到command配置apiVersion: v1
kind: Pod
metadata:name: pod-basenamespace: xylabels:user: user1
spec:containers:- name: nginximage: nginx:latest- name: busyboximage: busybox:1.30command: ["/bin/sh","-c","touch /tmp/hello.txt;while true;do /bin/echo $(date +%T) >> /tmp/hello.txt; sleep 3; done;"]#每隔3秒向文件中写入当前时间,能够让buybox一直运行[root@master ~]# kubectl delete -f pod-base.yaml
pod "pod-base" deleted
[root@master ~]# kubectl apply -f pod-base.yaml 
pod/pod-base created
[root@master ~]# kubectl get pod -n xy
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6c45cbd8c5-dg47s   1/1     Running   0          72m
nginx-6c45cbd8c5-h4mrw   1/1     Running   0          72m
nginx-6c45cbd8c5-pqpnl   1/1     Running   0          72m
pod-base                 2/2     Running   0          15s
#再次查看发现变成了Running
#进入pod中的busybox容器
格式:kubectl exec  pod名称 -n 命名空间 -it -c 容器名称 /bin/sh
[root@master ~]# kubectl exec pod-base -n xy -it -c busybox /bin/sh
[root@master ~]# kubectl exec pod-base -n xy -it -c busybox /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ # tail -f /tmp/hello.txt
07:51:34
07:51:37
07:51:40
07:51:43
07:51:46
07:51:49
07:51:52
07:51:55
#Kubernetes 中的 command 和 args 字段用于覆盖 Docker 镜像中的默认启动命令,它们与 Dockerfile 中的 ENTRYPOINT 和 CMD 指令存在对应关系场景	适用情况	示例
只设置 command	需要完全自定义启动流程	执行特定脚本或命令
只设置 args	保持原有启动程序,仅修改参数	调整服务配置参数
两者都设置	需要自定义解释器和命令	使用 shell 执行复杂命令
具体覆盖规则如下:
如果command和args均没有写:那么使用Docker镜像中的默认配置,即ENTRYPOINT和CMD。如果只写了command:那么容器启动时执行command中指定的命令,Docker镜像中的ENTRYPOINT和CMD都被忽略。args不会被使用。如果只写了args:那么Docker镜像中的ENTRYPOINT命令会被执行,并使用args中的参数替换Docker镜像中的CMD。即ENTRYPOINT不变,CMD被替换为args。如果command和args都写了:那么容器启动时执行command中指定的命令,并使用args中的参数。Docker镜像中的ENTRYPOINT和CMD都被忽略
imagePullPolicy,用于设置镜像拉取策略,kubernetes支持配置三种拉取策略
Always:总是从远程仓库拉取镜像(一直远程下载)
IfNotPresent:本地有则使用本地镜像,本地没有则从远程仓库拉取镜像(本地有就本地 本地没远程下载)
Never:只使用本地镜像,从不去远程仓库拉取,本地没有就报错 (一直使用本地)
如果镜像tag为具体版本号, 默认策略是:IfNotPresent
如果镜像tag为:latest(最终版本) ,默认策略是always
env用于在pod中的容器设置环境变量
apiVersion: v1
kind: Pod
metadata:name: pod-basenamespace: xylabels:user: user1
spec:containers:- name: nginximage: nginx:latest- name: busyboximage: busybox:1.30command: ["/bin/sh","-c","touch /tmp/hello.txt;while true;do /bin/echo $(date +%T) >> /tmp/hello.txt; sleep 3; done;"]env:- name: "username"value: "admin"- name: "password"value: "redhat"这种方式不推荐,推荐将这些配置单独存储在配置文件中
查看ports支持的子选项
[root@master ~]# kubectl explain pod.spec.containers.ports
KIND:       Pod
VERSION:    v1FIELD: ports <[]ContainerPort>DESCRIPTION:List of ports to expose from the container. Not specifying a port here DOESNOT prevent that port from being exposed. Any port which is listening on thedefault "0.0.0.0" address inside a container will be accessible from thenetwork. Modifying this array with strategic merge patch may corrupt thedata. For more information Seehttps://github.com/kubernetes/kubernetes/issues/108255. Cannot be updated.ContainerPort represents a network port in a single container.FIELDS:containerPort	<integer> -required-Number of port to expose on the pod's IP address. This must be a valid portnumber, 0 < x < 65536.hostIP	<string>What host IP to bind the external port to.hostPort	<integer>Number of port to expose on the host. If specified, this must be a validport number, 0 < x < 65536. If HostNetwork is specified, this must matchContainerPort. Most containers do not need this.name	<string>If specified, this must be an IANA_SVC_NAME and unique within the pod. Eachnamed port in a pod must have a unique name. Name for the port that can bereferred to by services.protocol	<string>Protocol for port. Must be UDP, TCP, or SCTP. Defaults to "TCP".Possible enum values:- `"SCTP"` is the SCTP protocol.- `"TCP"` is the TCP protocol.- `"UDP"` is the UDP protocol#  name         <string>  # 端口名称,如果指定,必须保证name在pod中是唯一的		containerPort<integer> # 容器要监听的端口(0<x<65536)hostPort     <integer> # 容器要在主机上公开的端口,如果设置,主机上只能运行容器的一个副本(一般省略) hostIP       <string>  # 要将外部端口绑定到的主机IP(一般省略)protocol     <string>  # 端口协议。必须是UDP、TCP或SCTP。默认为“TCP”
创建pod-ports.yaml
[root@master ~]# vim pod-ports.yamlapiVersion: v1
kind: Pod
metadata:name: pod-portsnamespace: xy
spec:containers:- name: nginximage: nginx:latestports:- name: nginx-portcontainerPort: 80protocol: TCP[root@master ~]# kubectl apply -f pod-ports.yaml 
pod/pod-ports created
[root@master ~]# kubectl get pods -n xy
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6c45cbd8c5-dg47s   1/1     Running   0          88m
nginx-6c45cbd8c5-h4mrw   1/1     Running   0          88m
nginx-6c45cbd8c5-pqpnl   1/1     Running   0          88m
pod-base                 2/2     Running   0          16m
pod-ports                1/1     Running   0          35s
#访问容器中的程序需要使用的是Podip:containerPort
[root@master ~]# kubectl get pod pod-ports -n xy -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP               NODE    NOMINATED NODE   READINESS GATES
pod-ports   1/1     Running   0          73s   172.16.166.143   node1   <none>           <none>[root@master ~]# curl 172.16.166.143
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
需要注意如果一个pod里面的两个容器都监听一个80端口,那么有一个容器不会运行,如果运行的是nginx,那么即使在yaml文件里修改端口不相同,也会有一个不运行,因为nginx的配置文件里面默认监控80端口,需要修改nginx的配置文件中的监听端口来解决
预先规划:在设计多容器 Pod 时,确保各容器使用不同的监听端口配置检查:确认容器内应用程序的实际监听端口与 YAML 配置一致测试验证:部署前通过 kubectl logs 和 kubectl describe 检查容器状态

资源配额

资源管理的必要性
容器在运行过程中需要消耗 CPU、内存等系统资源。如果不对容器资源使用进行限制,可能出现以下问题:资源抢占:单个容器过度占用资源,导致其他容器无法正常运行系统不稳定:资源耗尽可能引起节点故障或系统崩溃性能不可预测:缺乏资源保障导致应用性能波动资源配额机制
Kubernetes 通过 resources 选项提供精细化的资源管理能力,包含两个关键子选项:1. requests - 资源请求
作用:定义容器需要的最小资源保障调度依据:Kubernetes 根据此值选择有足够资源的节点启动条件:如果集群无法满足资源请求,容器将无法启动2. limits - 资源限制
作用:设置容器能够使用的最大资源上限强制约束:当容器资源使用超过限制时会被强制终止并重启保护机制:防止单个容器耗尽节点资源
创建pod-resources.yaml
apiVersion: v1
kind: Pod
metadata:name: pod-resourcesnamespace: xy
spec:containers:- name: nginximage: nginx:1.17.1imagePullPolicy: Neverresources:       limits:     cpu: "2"     memory: "10Gi"   requests:     cpu: "1"memory: "10Mi"
#cpu:core数,可以为整数或小数
memory: 内存大小,可以使用Gi、Mi、G、M等形式
#这里要注意,resources内存的下限值不能超过宿主机的内存通过 requests 和 limits 的配合使用,Kubernetes 实现了:资源预留:确保容器获得基本资源保障资源隔离:防止容器间相互干扰资源限制:维持集群整体稳定性合理调度:基于实际需求进行智能调度决策这种机制既保证了关键应用的资源可用性,又有效防止了资源滥用,是 Kubernetes 集群稳定运行的重要保障
http://www.dtcms.com/a/552902.html

相关文章:

  • 大型系统性能优化问题
  • 国际版多语言语聊语音厅交友源码开发:技术特性、中外差异与核心注意事项
  • 面对网络蠕虫的紧急处理方式和防护措施
  • 深入掌握 Maven Settings:从配置到实战
  • 厦门做网站seo的惠州哪家做网站比较好
  • 目标检测原理分享
  • AI驱动的VSCode插件开发:从模型研发到性能优化的全栈实践
  • 6 种无误的方法:如何备份和恢复华为手机
  • Spring Boot 整合 ShedLock 处理定时任务重复
  • 静态网站开发课程相关新闻做设计图的网站
  • MCU(微控制器单元)上的语音识别
  • 【普中Hi3861开发攻略--基于鸿蒙OS】-- 第 31 章 WIFI 实验-华为 IoTDA 设备接入
  • hive的全连接
  • 集团企业网站设计方案专业做化学招聘的网站有哪些
  • win11 wsl安装Ubuntu-22.04并迁移D盘 以及安装docker
  • 频闪拍照中的相机和光源同步问题
  • 乐吾乐3D可视化案例【智慧汽车三维可视化】
  • 网站图片验证码出不来潜江资讯网电脑版
  • 大模型在百度电商机审应用的落地实践
  • esp8266+dht11传感器实时上报温湿度
  • WPF中为Button设置IsMouseOver和IsPressed事件中改变背景颜色不起作用
  • vagrant nat网络问题-虚拟机同时存在两个IP,并且不能访问外网
  • 微信小程序代理商加盟多少钱郑州网站优化公司价位
  • Ingress-Nginx故障排除
  • WPF ListView 列表
  • 国内资深易经风水专家谷晟阳
  • seo网站优化优化排名关于网站建设电话销售的开场白
  • Docker Swarm K8s云原生分布式部署2025年版
  • 量化投资从入门到入土:什么是股市
  • Windows 下 ComfyUI + Comfy CLI + PyTorch(CUDA)完整源码安装教程