我们已经可以通过Controller创建应用,可是当我们访问应用时,发现一个问题,由于Pod的状态不是人为控制的,Pod IP是在创建时分配的,如果在Pod被误删除,被Controller重新拉起一个新的Pod时,我们发现Pod IP地址是变化着的,我们如果访问必须更换IP地址,这样对于大量的Pod运行应用来说,我们对Pod完全无法控制的,因此在k8s集群中我们引入另一个概念 Service
类型 | 解释 | yaml文件注意字段 |
---|---|---|
cluster(default) | 内部客户端向稳定的内部IP地址发送请求 | type:ClusterIP |
NodePort | 客户端向使用Service指定的一个或多个nodePort值的节点的IP地址发送请求 | type:NodePort |
LoadBalancer | 客户端向网络负载均衡的ip地址发送请求 | type:LoadBalancer |
ExternalName | 内部客户端使用service的DNS服务作为外部DNS名称的别名 | type:ExternalName |
Headless | 如果你需要对Pod分组,但是不需要稳定的IP地址,就可以使用Headless服务 | type:Headless |
Service的创建在工作中有两种方式,一是命令行创建,二是通过资源清单文件YAML文件创建
创建Service时,kubernetes会创建一个与Service同名的EndPoints对象。kubernetes使用Endpoints对象来跟踪哪些Pod属于Service的成员
编写用于创建Service资源清单文件
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-apps
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginxapps
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 88
resources:
limits:
cpu: '1'
memory: 1Gi
requests:
cpu: 200m
memory: 512Mi
---
apiVersion: v1
kind: Service
metadata:
name: nginx-apps
spec:
selector:
app: nginx # 具有app=nginx-app标签的pod都属于该服务
type: ClusterIP
ports:
- protocol: TCP
port: 88 # 该服务的可用端口
targetPort: 80 # 服务将连接转发的容器的端口
应用用于创建Service资源清单文件
root@k8s1:/home# kubectl apply -f 04-create-deployment-service-clusterip.yaml
deployment.apps/nginx-apps created
service/nginx-apps created
验证资源对象创建结果
root@k8s1:/home# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-apps 1/1 1 1 10s
root@k8s1:/home# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
nginx-apps ClusterIP 10.233.7.4 <none> 88/TCP 2m24s
root@k8s1:/home# curl http://10.233.7.4:88
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
编写资源清单文件
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-app-nodeport
labels:
app: nginx-app
spec:
replicas: 1
selector:
matchLabels:
app: nginx-app
template:
metadata:
labels:
app: nginx-app
spec:
containers:
- name: nginxapp
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 88
resources:
limits:
cpu: '1'
memory: 1Gi
requests:
cpu: 200m
memory: 512Mi
---
apiVersion: v1
kind: Service
metadata:
name: nginx-app
spec:
selector:
app: nginx-app # 具有app=nginx-app标签的pod都属于该服务
type: NodePort
ports:
- protocol: TCP
nodePort: 30001
port: 88 # 该服务的可用端口
targetPort: 80 # 服务将连接转发的容器的端口
应用资源清单文件
root@k8s1:/home# kubectl apply -f 05-create-deployment-service-nodeport.yaml
deployment.apps/nginx-app-nodeport created
service/nginx-app created
验证资源对象创建情况
root@k8s1:/home# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
nginx-app NodePort 10.233.12.32 <none> 88:30001/TCP 63s
查看k8s集群所有主机30001端口侦听状态
root@k8s1:/home# ss -anput | grep ":30001"
tcp LISTEN 0 128 0.0.0.0:30001 0.0.0.0:* users:(("kube-proxy",pid=1607146,fd=19))
在k8s集群外访问Service
在物理机浏览器中访问即可验证
地址:服务器ip:30001****
root@k8s1:/home# kubectl delete service nginx-app
service "nginx-app" deleted
root@k8s1:/home# kubectl delete -f 05-create-deployment-service-nodeport.yaml
deployment.apps "nginx-app-nodeport" deleted
service "nginx-app" deleted
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_43296313/article/details/121348389
内容来源于网络,如有侵权,请联系作者删除!