欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > K8S-标签管理,探针,名称空间,rc控制器,svc服务发现

K8S-标签管理,探针,名称空间,rc控制器,svc服务发现

2025/2/24 23:50:02 来源:https://blog.csdn.net/qq_63826739/article/details/145301989  浏览:    关键词:K8S-标签管理,探针,名称空间,rc控制器,svc服务发现

1. k8s的两类API:

        响应式:可以理解为基于命令行的方式创建资源。换句话说,就是不通过配置文件创建资源

        声明式:可以理解为通过资源清单的方式创建资源。换句话说,就是通过配置文件创建资源

1. 标签管理

## 创建资源清单
kind: Pod
metadata:name: myweb-labels-002
spec:containers:- name: nginximage: harbor.oldboyedu.com/web/nginx:1.20.1-alpine 

1.1 基于响应式给pod查看标签

1.2 基于响应式给pod创建标签

 1.3 基于响应式给pod修改标签

1.4 基于响应式给pod删除标签

 1.5 基于声明式给pod创建标签

cat 13-web-labels.yaml 
apiVersion: v1
kind: Pod
metadata:name: myweb-labels-002## 创建标签labels:## 键值对的形式school: oldboyedu
spec:containers:- name: nginximage: harbor.oldboyedu.com/web/nginx:1.20.1-alpine 

2. 探针 

1. 常用探针

        (1)livenessProbe:健康状态检查,周期性检查服务是否存活,检查如果失败,将重启重启。(删掉原容器并创建新的容器),默认状态下是success

          (2)   readnessProbe:可用性检查,周期检服务是否可用,从而判断容器是否就绪。默认状态下是success

          (3)   startupProbe: 如果提供了启动探针,则所有其他探针都会被禁用,知道此探针成功为止。认状态下是success

2. 探针检测Pod服务方法:

        (1) exec: 执行一段命令,根据返回值判断执行结果。返回值为0或者非0.

        (2) httpGet:发送http请求,根据返回的状态码来判断服务是否正常

        (3) tcpSocket: 测试某个tcp端口是否能够连接。类似于telenet,nc等测试工具。

 2.1 livenessProbe健康性检查之exec探测

cat 14-livenessProbe-exec.yaml 
apiVersion: v1
kind: Pod
metadata:name: linux85-exec-001labels:apps: myweb
spec:containers:- name: linux85-execimage: harbor.oldboyedu.com/web/nginx:1.20.1-alpinecommand:- /bin/sh- -c - touch /tmp/oldboyedu-linux85-healthy; sleep 5; rm -f /tmp/oldboyedu-linux85-healthy; sleep 600## 健康状态检查,周期性检查服务是否存活,检查如果失败,将重启容器。livenessProbe:exec:command:- cat- /tmp/oldboyedu-linux85-healthy## 检测服务失败次数的累加值,默认值是3,最小值是1,当检测成功后,该值会被重置failureThreshold: 3## 指定多久进行健康检查,即此时间内检测服务失败并不会对failureThreshold进行技术initialDelaySeconds: 15## 指定探针检测的频率,默认值是10s,最小值是1periodSeconds: 1## 检测服务成功次数的累加值,默认值是1,最小值是1successThreshold: 1## 一次检测周期超时的秒数,默认值是1,最小值是1timeoutSeconds: 1

注意观察:"(x3 over 10s)"的内容,表示第三次检查失败,其中距离第一次检查失败已经经过了"10s",而开始调度成功的时间是"26s",根据两者时间差可以得出,第一次检查失败的时间是"16s"

2.2 livenessProbe健康性检查之httpGet探测

cat 15-livenessProbe-httpget.yaml 
apiVersion: v1
kind: Pod
metadata:name: linux85-httpget-001labels:apps: myweb
spec:volumes:- name: dataemptyDir: {}containers:- name: linux85-httpgetimage: harbor.oldboyedu.com/web/nginx:1.20.1-alpinevolumeMounts:- name: datamountPath: /usr/share/nginx/html## 健康状态检查,周期性检查服务是否存活,检查如果失败,将重启重启。livenessProbe:## 使用httpGet的方式去做健康检查httpGet:## 指定访问的端口号port: 80## 检测指定的访问路径  path: /index.html## 检测服务失败次数的累加值,默认值是3,最小值是1,当检测成功后,该值会被重置failureThreshold: 3## 指定多久进行健康检查,即此时间内检测服务失败并不会对failureThreshold进行技术initialDelaySeconds: 15## 指定探针检测的频率,默认值是10s,最小值是1periodSeconds: 1## 检测服务成功次数的累加值,默认值是1,最小值是1successThreshold: 1## 一次检测周期超时的秒数,默认值是1,最小值是1timeoutSeconds: 1

2.3 livenessProbe健康性检查之tcpSocket探测

cat 16-livenessProbe-socket.yaml 
apiVersion: v1
kind: Pod
metadata:name: linux85-tcpsocket-001labels:apps: myweb
spec:containers:- name: linux85-execimage: harbor.oldboyedu.com/web/nginx:1.20.1-alpinecommand:- /bin/sh- -c - nginx; sleep 10; nginx -s stop ; sleep 600 livenessProbe:## 使用tcpSocket方式去做健康检查tcpSocket:## 检测80端口是否存在port: 80 failureThreshold: 3initialDelaySeconds: 15periodSeconds: 1successThreshold: 1timeoutSeconds: 1

 注意观察:"(x3 over 10s)"的内容,表示第三次检查失败,其中距离第一次检查失败已经经过了"3s",而开始调度成功的时间是"19s",根据两者时间差可以得出,第一次检查失败的时间是"16s"

2.4 readnessProbe可用性检查探针之exec探测

cat 02-rc-readinessprobe.yaml 
## pod 资源清单
apiVersion: v1
kind: ReplicationController
metadata:name: oldboyedu-linux85-web-rc-readinessprobelabels:school: oldboyeduclass: linux85namespace: default
spec:replicas: 3selector:classroom: jiaoshi05address: oldboyedu-shahetemplate:metadata:labels:classroom: jiaoshi05address: oldboyedu-shahespec:containers:- name: nginximage: harbor.oldboyedu.com/web/nginx:1.20.1-alpinecommand:- /bin/sh- -c- touch /tmp/oldboyedu-linux85-healthy; sleep 5 ;rm -f /tmp/oldboyedu-linux85-healthy;sleep 600## 可用性检查,周期性检查服务是否可用,从而判断容器是否就绪。## 若检测到服务不可用,则会将pod从svc的ep列表中移除## 若检测到服务可用,则会将pod重新添加到svc的ep列表中## 如果容器没有提供可用性检查,则默认式successreadinessProbe:## 使用exec的方式去做检查exec:## 自定义检查命令command:- cat- /tmp/oldboyedu-linux85-healthy## 检测服务失败次数的累加值,默认值是3,最小值是1,当检测成功后,该值会被重置failureThreshold: 3## 指定多久进行健康检查,即此时间内检测服务失败并不会对failureThreshold进行技术initialDelaySeconds: 15## 指定探针检测的频率,默认值是10s,最小值是1periodSeconds: 1## 检测服务成功次数的累加值,默认值是1,最小值是1successThreshold: 1## 一次检测周期超时的秒数,默认值是1,最小值是1timeoutSeconds: 1---------------------
## service资源清单
apiVersion: v1
kind: Service
metadata:name: oldboyedu-linux85-web-readinessrobenamespace: defaultlabels:apps: oldboyedu-svcclass: linux85
spec:selector:classroom: jiaoshi05address: oldboyedu-shahe type: ClusterIPports:- port: 88targetPort: 80protocol: TCP

此时我们发现pod处于未就绪状态,我们的sevice服务明明是根据标签来匹配pod的,是由于我们配置了readnessProbe,因为服务没有发现 /tmp/oldboyedu-linux85-healthy文件,所以容器会处于未就绪状态。

 当我们分别在各个容器中创建文件后,我们会发现容器处于就绪状态,因为探针检查到了该文件,便会让容器处于就绪状态

 2.5 readnessProbe可用性检查探针之httpGet探测

apiVersion: v1
kind: ReplicationController
metadata:name: oldboyedu-linux85-web-rc-httpgetlabels:school: oldboyeduclass: linux85namespace: default
spec:replicas: 3selector:classroom: jiaoshi05address: oldboyedu-shahetemplate:metadata:labels:classroom: jiaoshi05address: oldboyedu-shahespec:containers:- name: nginximage: harbor.oldboyedu.com/web/nginx:1.20.1-alpinecommand:- /bin/sh- -c- touch /tmp/oldboyedu-linux85-healthy; sleep 5 ;rm -f /tmp/oldboyedu-linux85-healthy;sleep 600readinessProbe:## httpGet 探测,检查80端口下的文件是否存在httpGet:port: 80path: /index.htmlfailureThreshold: 3initialDelaySeconds: 15periodSeconds: 1successThreshold: 1timeoutSeconds: 1timeoutSeconds: 1---
apiVersion: v1
kind: Service
metadata:name: oldboyedu-linux85-web-httpgetnamespace: defaultlabels:apps: oldboyedu-svcclass: linux85
spec:selector:classroom: jiaoshi05address: oldboyedu-shahe type: ClusterIPports:- port: 88targetPort: 80protocol: TCP

2.6 readnessProbe可用性检查探针之tcpSocket探测

cat 04-rc-readinessprobe-tcpsocket.yaml
apiVersion: v1
kind: ReplicationController
metadata:name: oldboyedu-linux85-web-rc-tcpsocketlabels:school: oldboyeduclass: linux85namespace: default
spec:replicas: 3selector:classroom: jiaoshi05address: oldboyedu-shahetemplate:metadata:labels:classroom: jiaoshi05address: oldboyedu-shahespec:containers:- name: nginximage: harbor.oldboyedu.com/web/nginx:1.20.1-alpinecommand:- /bin/sh- -c- sleep 25 ; nginx -g "daemon off;" readinessProbe:tcpSocket:port: 80failureThreshold: 3initialDelaySeconds: 15periodSeconds: 1successThreshold: 1timeoutSeconds: 1timeoutSeconds: 1---
apiVersion: v1
kind: Service
metadata:name: oldboyedu-linux85-web-tcpsocketnamespace: defaultlabels:apps: oldboyedu-svcclass: linux85
spec:selector:classroom: jiaoshi05address: oldboyedu-shahe type: ClusterIPports:- port: 88targetPort: 80protocol: TCP

3. 名称空间的使用

1. 在同一个名称空间下,同一种资源类型,是无法同时创建多个名称空间相同的资源

2. 名称空间是用来隔离k8s集群的资源。我们通常使用名称空间对企业业务进行逻辑的划分。

3. k8s集群一切皆资源,有的资源支持名称空间,我们将其成为全局资源。有的资源不支持名称空间,我们将其成为局部资源。

4. 我们可以通过kubectl api-resource 命令来判断一个资源是否支持名称空间

3.1 查看名称空间

查看现有的名称空间

 查看默认的名称空间

 查看指定的名称空间

查看所有的名称空间

3.2 创建名称空间

响应式创建名称空间

声明式创建名称空间

cat 01-ns.yml 
apiVersion: v1
## 指定类型为namespace
kind: Namespace
metadata:name: oldboyedu-linux86## 标签选项可以删除labels:school: oldboyeduclass: linux86 

 3.3 修改名称空间

名称空间一旦创建将无法修改!

3.4 删除名称空间

一旦删除名称空间,该名空间下的所有资源都会被删除

4. rc控制器 

apiVersion: v1
kind: ReplicationController
metadata:name: oldboyedu-linux85-web-rclabels:school: oldboyeduclass: linux85namespace: default
spec:## 指定pod的副本数量默认值为1replicas: 3## 指定标签选择器selector:classroom: jiaoshi05address: oldboyedu-shahe## 指定创建pod模板template:metadata:labels:classroom: jiaoshi05address: oldboyedu-shahespec:containers:- name: nginximage: harbor.oldboyedu.com/web/nginx:1.20.1-alpine

此时我们会发现,当我们删除pod的时候,控制器会帮我们重新创建pod。

 rc控制器通过标签来管理pod的

当我们想删除pod的时候,需要要先删除rc控制器,pod才会被随之删除

 

5. svc的ClusterIP类型

apiVersion: v1
kind: Service
metadata:name: oldboyedu-linux85-webnamespace: defaultlabels:apps: oldboyedu-svcclass: linux85
spec:## 关联后端的podselector:classroom: jiaoshi05address: oldboyedu-shahe ## 指定svc的类型,有效值为ExternalName,ClusterIP,NodePort,LoadBalancer## ExternalName:可以将k8s集群外部的服务映射为一个svc服务,类似于cname技术## ClusterIP:仅用于k8s内部使用。提供统一的vip地址## NodePort:基于ClusterIP基础之上,会监听所有的worker工作节点的端口,k8s外部可以基于监听端口访问k8s内部服务。## LoadBalancer:主要用于云平台ports: ## 指定端口映射## 指定svc的端口号- port: 88## 指定port的端口号targetPort: 80protocol: TCP

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词