当前位置:网站首页>Kubernetes resource object introduction and common commands (III)
Kubernetes resource object introduction and common commands (III)
2022-07-03 17:11:00 【Silly [email protected]】
Service
A set of Pods An abstract method that is exposed as a network service . It can also be understood as Pod Service discovery and load balancing .
Service discovery and load balancing :Service Will detect pod The state of , Traffic will only be sent to normal with average scores pod in .

establish Service
expose Deploy. port 8000.
Command line mode
[[email protected] ~]# kubectl expose deployment my-app --port=8000 --target-port=80
service/my-app exposed
[[email protected] ~]# kubectl get service
yaml The way ,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] ~]#
see 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] ~]#
You can see what we created above Service Of ip by 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] ~]#
Be careful : We can use ip:8000
In the form of any node access in the cluster . We can also do that pod Inside Use domain name : port
Form access . The form of domain name :service name . Namespace .svc: port . for example :my-app.default.svc:8000
.
Expand :
see pod label
[[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] ~]#
Use tags to retrieve 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] ~]#
Delete Service
[[email protected] ~]# kubectl delete service my-app
service "my-app" deleted
[[email protected] ~]#
ClusterIP

We created Service The default is ClusterIP, It's called clustering ip, It can only be used inside the cluster . Equate to :
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

Created this way Service It can also be accessed outside the cluster . Because he will be in every pod Show a new port .
Command line mode
[[email protected] ~]# kubectl expose deploy my-app --port=8000 --target-port=80 --type=NodePort
service/my-app exposed
[[email protected] ~]#
yaml The way .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] ~]#
Contrast found in PORT(S) A mapping port is added in 32047, This port is available on every node , Can pass node ip:30247
Access outside the cluster .

NodePort In addition to being accessible outside the cluster , The rest and ClusterIP Agreement .
Summary
NodePort The scope is 30000-32767 Randomly generated between . ClusterIP Can only be used within a cluster . NodePort Can pass node ip: new port
Access outside the cluster .Whether it's ClusterIP still NodePort, Will generate CLUSTER-IP
, It can be used in the cluster .Use ip: port
To use on cluster nodes , anddomain name : port
To be in pod Internal use .How to write a domain name : service name . In the namespace .svc
This paper is written by mdnice Multi platform Publishing
版权声明
本文为[Silly [email protected][email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/184/202207031706234030.html
边栏推荐
- How to delete a specific line from a text file using the SED command?
- 【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
- Luogu: p2685 [tjoi2012] Bridge
- 新库上线 | CnOpenData中国保险机构网点全集数据
- The largest matrix (H) in a brush 143 monotone stack 84 histogram
- 【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
- Great changes! National housing prices fell below the 10000 yuan mark
- Static program analysis (I) -- Outline mind map and content introduction
- Apache服务挂起Asynchronous AcceptEx failed.
- New library online | cnopendata China bird watching record data
猜你喜欢
Why is WPA3 security of enterprise business so important?
【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用
One brush 147-force deduction hot question-4 find the median of two positive arrays (H)
New features of C 10
kubernetes资源对象介绍及常用命令(三)
Depth first search of graph
Life is still confused? Maybe these subscription numbers have the answers you need!
UCORE overview
Great changes! National housing prices fell below the 10000 yuan mark
Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
随机推荐
SSH连接远程主机等待时间过长的解决方法
Meituan side: why does thread crash not cause JVM crash
RedHat 6.2 配置 Zabbix
图之深度优先搜索
深入理解 SQL 中的 Grouping Sets 语句
C language string inversion
[combinatorial mathematics] recursive equation (example of recursive equation 2 Hanoi Tower | example of recursive equation 3 insertion sequencing)
IL Runtime
[combinatorics] recursive equation (constant coefficient linear homogeneous recursive equation | constant coefficient, linear, homogeneous concept description | constant coefficient linear homogeneous
[combinatorics] recursive equation (definition of general solution | structure theorem of general solution of recursive equation without multiple roots)
[JDBC] API parsing
Rsync远程同步
vs code 插件 koroFileHeader
On Lagrange interpolation and its application
新库上线 | CnOpenData中国保险机构网点全集数据
數據分析必備的能力
One brush 145 force deduction hot question-2 sum of two numbers (m)
Thread pool: the most common and error prone component of business code
Why is WPA3 security of enterprise business so important?
MySQL Basics