当前位置:网站首页>kubernetes资源对象介绍及常用命令(三)
kubernetes资源对象介绍及常用命令(三)
2022-07-01 23:31:00 【江小南】
Service
将一组Pods公开为网络服务的抽象方法。也可以理解为Pod的服务发现与负载均衡。
服务发现与负载均衡:Service会检测pod的状态,流量只会较为平均得分发到正常的pod中。

创建Service
暴露Deploy。端口8000。
命令行方式
[[email protected] ~]# kubectl expose deployment my-app --port=8000 --target-port=80
service/my-app exposed
[[email protected] ~]# kubectl get service
yaml方式,service.yml
apiVersion: v1
kind: Service
metadata:
labels:
app: my-app
name: my-app
spec:
selector:
app: my-app
ports:
- port: 8000
protocol: TCP
targetPort: 80
[[email protected] ~]# kubectl apply -f service.yml
service/my-app created
[[email protected] ~]#
查看Service
[[email protected] ~]# kubectl get service -n default
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h
my-app ClusterIP 10.96.2.98 <none> 8000/TCP 6s
[[email protected] ~]#
可以看到我们上面创建的Service的ip为10.96.2.98。
[[email protected] ~]# curl 10.96.2.98:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
...
</html>
[[email protected] ~]#
注意:我们这里可以使用ip:8000的形式在集群内的任意节点访问。我们也可以在pod内使用域名:端口的形式访问。域名的形式:service名称.命名空间.svc:端口。例如:my-app.default.svc:8000。
扩展:
查看pod标签
[[email protected] ~]# kubectl get pod [podname] --show-labels
NAME READY STATUS RESTARTS AGE LABELS
my-app-5ff664f457-l7792 1/1 Running 1 20h app=my-app,pod-template-hash=5ff664f457
my-app-5ff664f457-r69z5 1/1 Running 1 20h app=my-app,pod-template-hash=5ff664f457
my-app-5ff664f457-w2d6q 1/1 Running 1 20h app=my-app,pod-template-hash=5ff664f457
my-app-5ff664f457-xsz9l 1/1 Running 1 20h app=my-app,pod-template-hash=5ff664f457
[[email protected] ~]#
使用标签检索Pod
[[email protected] ~]# kubectl get pod -l app=my-app
NAME READY STATUS RESTARTS AGE
my-app-5ff664f457-l7792 1/1 Running 1 20h
my-app-5ff664f457-r69z5 1/1 Running 1 20h
my-app-5ff664f457-w2d6q 1/1 Running 1 20h
my-app-5ff664f457-xsz9l 1/1 Running 1 20h
[[email protected] ~]#
删除Service
[[email protected] ~]# kubectl delete service my-app
service "my-app" deleted
[[email protected] ~]#
ClusterIP

我们上面方式创建Service默认使用的是ClusterIP,称为集群ip,只能在集群内部使用。等同于:
kubectl expose deployment my-app --port=8000 --target-port=80 --type=ClusterIP
apiVersion: v1
kind: Service
metadata:
labels:
app: my-app
name: my-app
spec:
ports:
- port: 8000
protocol: TCP
targetPort: 80
selector:
app: my-app
type: ClusterIP
NodePort

这种方式创建的Service在集群外也可以访问。因为他会在每个pod上映射一个新的端口。
命令行方式
[[email protected] ~]# kubectl expose deploy my-app --port=8000 --target-port=80 --type=NodePort
service/my-app exposed
[[email protected] ~]#
yaml方式。service.yml
apiVersion: v1
kind: Service
metadata:
labels:
app: my-app
name: my-app
spec:
ports:
- port: 8000
protocol: TCP
targetPort: 80
selector:
app: my-app
type: NodePort
kubectl apply -f service.yml
[[email protected] ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 25h
my-app NodePort 10.96.198.191 <none> 8000:32047/TCP 39s
[[email protected] ~]#
对比发现在PORT(S)中多了一个映射端口32047,这个端口在每个节点上都有,可以通过节点ip:30247的方式在集群外进行访问。

NodePort除了可以在集群外访问以外,其余和ClusterIP一致。
小结
NodePort范围在 30000-32767 之间随机生成。 ClusterIP只能在集群内使用。 NodePort可以通过 节点ip:新端口的方式在集群外访问。无论是ClusterIP还是NodePort,都会生成 CLUSTER-IP,可在集群内使用。使用 ip:端口要在集群节点上使用,而域名:端口要在pod内使用。域名的写法:服务名.所在名称空间.svc
边栏推荐
- Airserver latest win64 bit personal screen projection software
- Li Kou today's question -241 Design priorities for operational expressions
- Typescript enumeration
- How to display real-time 2D map after rviz is opened
- [micro service sentinel] sentinel integrates openfeign
- 13 MySQL-约束
- 2022年最佳智能家居开源系统:Alexa、Home Assistant、HomeKit生态系统介绍
- 每日三题 6.30(2)
- What is mosaic?
- Stm32f030f4 drives tim1637 nixie tube chip
猜你喜欢

De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'

flutter Unable to load asset: assets/images/888. png

Distance measurement - Hamming distance

学成在线案例实战

纪念成为首个DAYUs200三方demo贡献者

Redis master-slave synchronization

从第三次技术革命看企业应用三大开发趋势

from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘

2022年最佳智能家居开源系统:Alexa、Home Assistant、HomeKit生态系统介绍

问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c
随机推荐
硅谷产品实战学习感触
notBlank 和 notEmpty
Redis master-slave synchronization
Redis数据类型和应用场景
Applet form verification encapsulation
2021 RoboCom 世界机器人开发者大赛-高职组初赛
Postgresql随手记(10)动态执行EXECUTING语法解析过程
每日三题 6.28
Anomaly-Transformer (ICLR 2022 Spotlight)复现过程及问题
神经网络物联网的未来趋势与发展
[micro service sentinel] @sentinelresource details
2021 robocom world robot developer competition - preliminary competition of higher vocational group
云信小课堂 | IM及音视频中常见的认知误区
物联网技术应用属于什么专业分类
Postgresql源码(57)HOT更新为什么性能差距那么大?
Postgresql源码(58)元组拼接heap_form_tuple剖析
每日三题 6.29
Linux基础 —— CentOS7 离线安装 MySQL
Paramètres communs de matplotlib
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee