k8s学习(七)service的使用
一、Service Service 是对一组提供相同功能的 Pods 的抽象,并为它们提供一个统一的入口。借助 Service,应用可以方便的实现服务发现与负载均衡,并实现应用的零宕机升级。二、ClusterIP自动分配一个仅 cluster 内部可以访问的虚拟 IP1、nginx-service.yaml[root@k8s-master k8s]#
·
一、Service
Service 是对一组提供相同功能的 Pods 的抽象,并为它们提供一个统一的入口。借助 Service,应用可以方便的实现服务发现与负载均衡,并实现应用的零宕机升级。
二、ClusterIP
自动分配一个仅 cluster 内部可以访问的虚拟 IP
1、nginx-service.yaml
[root@k8s-master k8s]# cat nginx-service.yaml
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx-deployment-test-02
name: nginx-service
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx-service
sessionAffinity: None
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-02
spec:
replicas: 3
selector:
matchLabels:
app: nginx-deployment-test-02
template:
metadata:
labels:
app: nginx-deployment-test-02
spec:
containers:
- name: nginx-deployment-test-02
image: nginx:1.20
ports:
- containerPort: 80
2、创建
[root@k8s-master k8s]# kubectl create -f nginx-service.yaml
service/nginx-service created
deployment.apps/nginx-deployment-02 created
3、查看
[root@k8s-master k8s]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d11h
nginx-service ClusterIP 10.109.102.140 <none> 80/TCP 6s
[root@k8s-master k8s]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment-02 3/3 3 3 41s
[root@k8s-master k8s]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-02-565f47cc54 3 3 3 2m46s
[root@k8s-master k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-02-565f47cc54-2mgrq 1/1 Running 0 3m19s
nginx-deployment-02-565f47cc54-5zz2l 1/1 Running 0 3m19s
nginx-deployment-02-565f47cc54-tmxjr 1/1 Running 0 3m19s
4、查看详细信息
[root@k8s-master k8s]# kubectl describe svc nginx-service
Name: nginx-service
Namespace: default
Labels: app=nginx-deployment-test-02
name=nginx-service
namespace=default
Annotations: <none>
Selector: app=nginx-service
Type: ClusterIP
IP Families: <none>
IP: 10.109.102.140
IPs: 10.109.102.140
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: <none>
Session Affinity: None
Events: <none>
5、访问 IP
访问nginx-service的ip
[root@k8s-master k8s]# curl 10.109.102.140:80
<!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>
<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>
三、NodePort
在 ClusterIP 基础上为 Service 在每台机器上绑定一个端口。
1、nginx-service-nodeport.yaml
[root@k8s-master k8s]# cat nginx-service-nodeport.yaml
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service-nodeport
labels:
app: nginx-deployment-test-03
name: nginx-service-nodeport
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
nodePort: 32000
selector:
app: nginx-deployment-test-03
type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-03
spec:
replicas: 3
selector:
matchLabels:
app: nginx-deployment-test-03
template:
metadata:
labels:
app: nginx-deployment-test-03
spec:
containers:
- name: nginx-deployment-test-03
image: nginx:1.20
ports:
- containerPort: 80
2、创建
[root@k8s-master k8s]# kubectl create -f nginx-service-nodeport.yaml
service/nginx-service-nodeport created
deployment.apps/nginx-deployment-03 created
3、查看
[root@k8s-master k8s]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d
nginx-service-nodeport NodePort 10.106.65.70 <none> 80:32000/TCP 3m5s
[root@k8s-master k8s]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment-03 3/3 3 3 2m34s
4、查看详细信息
[root@k8s-master k8s]# kubectl describe svc nginx-service-nodeport
Name: nginx-service-nodeport
Namespace: default
Labels: app=nginx-deployment-test-03
name=nginx-service-nodeport
namespace=default
Annotations: <none>
Selector: app=nginx-deployment-test-03
Type: NodePort
IP Families: <none>
IP: 10.106.65.70
IPs: 10.106.65.70
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 32000/TCP
Endpoints: 10.244.85.241:80,10.244.85.246:80,10.244.85.254:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
5、访问内部ip
[root@k8s-master k8s]# curl 10.106.65.70:80
<!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>
<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>
6、访问外部端口
访问虚拟机ip 172.16.10.158
[root@k8s-master k8s]# curl 172.16.10.158:32000
<!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>
<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>
更多推荐
所有评论(0)