当前位置:网站首页>kubernetes资源对象介绍及常用命令(三)
kubernetes资源对象介绍及常用命令(三)
2022-07-03 17:06:00 【傻啦猫@[email protected]】
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
本文由 mdnice 多平台发布
版权声明
本文为[傻啦猫@[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45842494/article/details/125567362
边栏推荐
- Mysql database DDL and DML
- 智慧之道(知行合一)
- Squid service startup script
- C language string inversion
- One brush 148 force deduction hot question-5 longest palindrome substring (m)
- Talk about several methods of interface optimization
- Define a structure fraction to represent a fraction, which is used to represent fractions such as 2/3 and 5/6
- 远程办公之如何推进跨部门项目协作 | 社区征文
- Recommendation of good books on learning QT programming
- [2. Basics of Delphi grammar] 1 Identifiers and reserved words
猜你喜欢
智慧之道(知行合一)
What is your income level in the country?
One brush 145 force deduction hot question-2 sum of two numbers (m)
美团一面:为什么线程崩溃崩溃不会导致 JVM 崩溃
设计电商秒杀
C语言按行修改文件
Life is still confused? Maybe these subscription numbers have the answers you need!
C language modifies files by line
Simulink oscilloscope data is imported into Matlab and drawn
Take you to API development by hand
随机推荐
What material is 12cr1movr? Chemical property analysis of pressure vessel steel plate 12cr1movr
Vs code plug-in korofileheader
Shentong express expects an annual loss of nearly 1billion
数据分析必备的能力
[combinatorics] recursive equation (example 1 of recursive equation | list recursive equation)
Life is still confused? Maybe these subscription numbers have the answers you need!
One brush 145 force deduction hot question-2 sum of two numbers (m)
Squid 服务启动脚本
Visual studio "usually, each socket address (Protocol / network address / port) can only be used once“
SVN完全备份svnadmin hotcopy
Rsync远程同步
Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
深入理解 SQL 中的 Grouping Sets 语句
The most complete postman interface test tutorial in the whole network, API interface test
LeetCode13.罗马数字转整数(三种解法)
线程池:业务代码最常用也最容易犯错的组件
Kotlin learning quick start (7) -- wonderful use of expansion
智慧之道(知行合一)
Simulink oscilloscope data is imported into Matlab and drawn
免费数据 | 新库上线 | CnOpenData中国保险中介机构网点全集数据