文章目录
Service对外暴露应用
Service是什么
- 其实就是k8s中的服务注册与负载均衡
- 最终能够实现,提供一个唯一的地址,供我们来访问地址,而不需要具体的去了解,这个服务起的Pod的ip是什么
Service存在的意义
Service引入主要是解决Pod的动态变化,提供统一访问入口
- 防止Pod失联,准备找到提供同一个服务的Pod(服务发现)
- 定义一组Pod的访问策略(负载均衡)

Pod与Service的关系 - Service通过标签关联一组Pod
- Service使用iptables或者ipvs为一组Pod提供负载均衡能力

service三种类型 - ClusterlP
默认类型,分配一个稳定的IP地址,即VIP,只能在集群内部访问

一个简单的service清单文件
---
apiVersion: v1
kind: Service
metadata:
name: test
namespace: default
spec:
ports:
- port: 80 #Service端口
protocol: TCP #协议
targetPort: 80 #容器端口
selector:
app: httpd #指定关联Pod的标签
type: ClusterIP #服务类型,如果不指定类型,默认类型为ClusterIP
... #文件结束用...
-
NodePort
在每个节点上启用一个端口来暴露服务,可以在集群外部访问。也会分配一个稳定内部集群IP地址。
访问地址:<任意NodelP> :<NodePort> 加上NodelP以防有多个IP
端口范围:30000-32767

-
LoadBalancer
与NodePort类似,在每个节点上启用一个端口来暴露服务。
除此之外,Kubernetes会请求底层云平台(例如阿里云、腾讯云、AWS等)上的负载均衡器,将每个Node ([NodelP]:[NodePort])作为后端添加进去。

操作练习
- 创建一个deployment副本数3,然后滚动更新镜像版本,并记录这个更新记录,最后再回滚到上一个版本
//清单文件
[root@master manifest]# cat test.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
namespace: default
spec:
replicas: 3 //三个副本
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: soumnswa/httpd:v1.0 //1.01版本镜像
imagePullPolicy: IfNotPresent
//运行pod
[root@master manifest]# kubectl apply -f test.yml
deployment.apps/test created
[root@master manifest]# kubectl get pods
NAME READY STATUS RESTARTS AGE
test-7746d6b875-8wqfw 1/1 Running 0 49s
test-7746d6b875-rtnbv 1/1 Running 0 49s
test-7746d6b875-tphkd 0/1 ContainerCreating 0 49s
//更换镜像,升级版本
[root@master manifest]# cat test.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: soumnswa/httpd:v1.1 //1.1版本镜像
imagePullPolicy: IfNotPresent
//升级应用
[root@master manifest]# kubectl apply -f test.yml
deployment.apps/test configured
[root@master manifest]# kubectl get pods
NAME READY STATUS RESTARTS AGE
test-6f778f5576-6ljqz 1/1 Running 0 12s
test-6f778f5576-bq7bz 1/1 Running 0 49s
test-6f778f5576-hgf5c 1/1 Running 0 14s
//查看历史发布版本
[root@master manifest]# kubectl rollout history deployment/test
deployment.apps/test
REVISION CHANGE-CAUSE
1 <none>
2 <none>
//查看指定版本详细信息
[root@master manifest]# kubectl rollout history deployment/test --revision=2
deployment.apps/test with revision #2
Pod Template:
Labels: app=web
pod-template-hash=6f778f5576
Containers:
web:
Image: soumnswa/httpd:v1.1 #1.1镜像版本
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
//回滚到上一版本
[root@master manifest]# kubectl rollout undo deployment/test --to-revision=1
deployment.apps/test rolled back
- 给一个应用扩容副本数为5
[root@master manifest]# kubectl scale deploy/test --replicas=5
deployment.apps/test scaled
[root@master manifest]# kubectl get pods
NAME READY STATUS RESTARTS AGE
test-7746d6b875-6msxw 1/1 Running 0 7s
test-7746d6b875-6v9cr 1/1 Running 0 3m39s
test-7746d6b875-dlv92 1/1 Running 0 3m37s
test-7746d6b875-nwlcx 1/1 Running 0 3m38s
test-7746d6b875-qhr52 1/1 Running 0 7s
- 创建一个pod,其中运行着nginx、redis、memcached 3个容器
//清单文件
[root@master manifest]# cat test.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
namespace: default
spec:
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
- name: redis
image: redis
imagePullPolicy: IfNotPresent
- name: memcached
image: memcached
imagePullPolicy: IfNotPresent
//查看pod
[root@master manifest]# kubectl apply -f test.yml
deployment.apps/test created
[root@master manifest]# kubectl get pods
NAME READY STATUS RESTARTS AGE
test-77d687f47b-nr9pc 3/3 Running 0 3s
- 给一个pod创建service,并可以通过ClusterlP/NodePort访问
//清单文件
[root@master manifest]# cat test.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
name: web
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
nodePort: 30000
selector:
app: web
type: NodePort
//运行pod
[root@master manifest]# kubectl apply -f test.yml
deployment.apps/web created
service/web created
[root@master manifest]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/web-59b9bb7664-ppnmh 1/1 Running 0 51s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d22h
service/web NodePort 10.103.165.44 <none> 80:30000/TCP 51s
//NodePort访问测试
[root@master manifest]# curl 10.103.165.44
<!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>
//ClusterlP访问测试
[root@master manifest]# curl 192.168.218.133:30000
<!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>
- 创建deployment和service,使用busybox容器nslookup解析service
[root@master manifest]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d22h
web NodePort 10.103.165.44 <none> 80:30000/TCP 7m10s
[root@master manifest]# kubectl run -it b1 --image busybox -- /bin/sh
If you don't see a command prompt, try pressing enter.
/ # nslookup web.default.svc.cluster.local
Server: 10.96.0.10
Address: 10.96.0.10:53
Name: web.default.svc.cluster.local
Address: 10.103.165.44
*** Can't find web.default.svc.cluster.local: No answer
本文介绍了Kubernetes Service,它是集群内的服务注册和负载均衡机制,提供统一访问入口,解决Pod动态变化问题。Service通过标签选择Pod并进行负载均衡。讨论了ClusterIP、NodePort和LoadBalancer三种类型的Service,以及如何进行操作练习,包括Deployment滚动更新、扩容、创建多容器Pod及Service。

805

被折叠的 条评论
为什么被折叠?



