一、简介

我们只知道能够利用Deployment来创建一组Pod来提供具有高可用性的服务。

虽然每个Pod都会分配一个单独的Pod IP,然而却存在如下两问题:

  • Pod IP 会随着Pod的重建产生变化
  • Pod IP 仅仅是集群内可见的虚拟IP,外部无法访问(主从结点外的机器无法访问)

这样对于访问这个服务带来了难度。因此,kubernetes设计了Service来解决这个问题。

Service可以看作是一组同类Pod对外的访问接口。借助Service,应用可以方便地实现服务发现和负载均衡。

在这里插入图片描述

deployment和service都通过标签选择器label selector来选择pod 


二、命令方式操作Service

1、service之ClusterIP(内网ip),暴露集群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、service之NodePort(外网ip),暴露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地址

在这里插入图片描述

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

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]#

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、使用配置文件的方式删除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]#
Logo

华为云1024程序员节送福利,参与活动赢单人4000元礼包,更有热门技术干货免费学习

更多推荐