一、Service
1.1 准备条件(创建一个deployment)
创建deployment_nginx.yaml文件,内容如下
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
run: nginx
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx:1.17.1
name: nginx
ports:
- containerPort: 80
protocol: TCP
使用如下命令创建deployment资源
[root@master demo]# kubectl create -f deployment-nginx.yaml
namespace/dev created
deployment.apps/nginx created
[root@master demo]#
如下,可以查看到deployment和pod信息
[root@master demo]# kubectl get deployment,pod -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 3/3 3 3 44s
NAME READY STATUS RESTARTS AGE
pod/nginx-66ffc897cf-9mtc6 1/1 Running 0 44s
pod/nginx-66ffc897cf-d5pfj 1/1 Running 0 44s
pod/nginx-66ffc897cf-xnk9j 1/1 Running 0 44s
[root@master demo]#
为查看到访问的ip和端口,需要使用如下命令
[root@master demo]# kubectl get deployment,pod -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/nginx 3/3 3 3 92s nginx nginx:1.17.1 run=nginx
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-66ffc897cf-9mtc6 1/1 Running 0 92s 10.244.1.14 node1 <none> <none>
pod/nginx-66ffc897cf-d5pfj 1/1 Running 0 92s 10.244.2.23 node2 <none> <none>
pod/nginx-66ffc897cf-xnk9j 1/1 Running 0 92s 10.244.1.15 node1 <none> <none>
[root@master demo]#
1.2 集群内部访问nginx的方式
如下,在集群内部,使用上述查询到的ip和端口是可以访问的
[root@master demo]# curl 10.244.1.14: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>
[root@master demo]#
1.3 仅使用Deployment访问Pod存在的问题
通过Deployment创建一组pod提供高可用的服务,虽然每个Pod都会分配一个单独的Pod IP,但是存在如下问题:
- Pod IP会随着pod的重建产生变化
- Pod IP仅仅是集群内可见的虚拟IP,外部无法访问
二、命令方式操作Service
2.1 暴露集群IP和端口号,解决IP随Pod变化的问题
如下,暴露80端口
[root@master demo]# kubectl expose deployment nginx --name=service-nginx --type=ClusterIP --port=80 --target-port=80 -n dev
service/service-nginx exposed
[root@master demo]#
如下:可查询到Service的集群IP
[root@master demo]# kubectl get service -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service-nginx ClusterIP 10.100.195.186 <none> 80/TCP 13m
[root@master demo]#
此时,在集群环境中访问nginx,如下,没有任何问题,及时将三个pod删除,然后deployment控制器重新自动创建,此时改变的是pod的ip,而service的集群ip始终不变,这就解决了Pod的ip随着重新创建而发生改变的问题
[root@master demo]# curl 10.100.195.186: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>
[root@master demo]#
2.2 暴露NodePort类型,解决集群外无法访问的问题
如下,再创建一个service,此时类型为NodePort类型
[root@master demo]# kubectl expose deployment nginx --name=service-nginx-2 --type=NodePort --port=80 --target-port=80 -n dev
service/service-nginx-2 exposed
[root@master demo]#
此时通过如下命令查询暴露出来的ip和端口
[root@master demo]# kubectl get service -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service-nginx ClusterIP 10.100.195.186 <none> 80/TCP 21m
service-nginx-2 NodePort 10.108.250.205 <none> 80:30141/TCP 42s
[root@master demo]#
这里,集群之外可以通过 http://192.168.16.40:30141/ 访问,其中192.168.16.40为master节点的ip地址
2.3 通过命令的方式删除Service
如下,将上述创建的额两个service删除
[root@master demo]# kubectl delete service service-nginx -n dev
service "service-nginx" deleted
[root@master demo]# kubectl delete service service-nginx-2 -n dev
service "service-nginx-2" deleted
[root@master demo]#
三、配置文件方式操作Service
3.1 配置文件方式配置Cluster类型的Service
编辑service-nginx.yaml文件,内容如下:
apiVersion: v1
kind: Service
metadata:
name: service-nginx
namespace: dev
spec:
clusterIP: 10.108.179.231
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: nginx
type: ClusterIP
如下命令创建
[root@master demo]# kubectl create -f service-nginx.yaml
service/service-nginx created
[root@master demo]#
如下,查询集群ip和端口
[root@master demo]# kubectl get service -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service-nginx ClusterIP 10.108.179.231 <none> 80/TCP 40s
[root@master demo]#
此时在集群内部通过集群ip和端口可以访问nginx
[root@master demo]# curl 10.108.179.231: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>
[root@master demo]#
3.2 配置文件方式创建NodePort类型的Service
编辑service-nginx2.yaml文件,内容如下:
apiVersion: v1
kind: Service
metadata:
name: service-nginx2
namespace: dev
spec:
ports:
- port: 30030
protocol: TCP
targetPort: 80
selector:
run: nginx
type: NodePort
[root@master demo]# kubectl create -f service-nginx2.yaml
service/service-nginx2 created
使用如下命令创建Service
[root@master demo]# kubectl create -f service-nginx2.yaml
service/service-nginx2 created
[root@master demo]#
查询service创建结果,如下:
[root@master demo]# kubectl get service -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service-nginx ClusterIP 10.108.179.231 <none> 80/TCP 9m15s
service-nginx2 NodePort 10.101.58.195 <none> 30030:31081/TCP 2m58s
[root@master demo]#
此时通过访问http://192.168.16.40:31081/即可在集群外访问nginx,其中192.168.16.40为master节点的主机ip地址,
3.3 使用配置文件的方式删除Service
如下:
[root@master demo]# kubectl delete -f service-nginx.yaml
service "service-nginx" deleted
[root@master demo]# kubectl delete -f service-nginx2.yaml
service "service-nginx2" deleted
[root@master demo]#