0
点赞
收藏
分享

微信扫一扫

Kubernetes----ClusterIP类型的Service

一、环境准备

编写deployment.yaml文件,内容如下:

apiVersion: v1
kind: Namespace
metadata:
name: dev

---

apiVersion: apps/v1
kind: Deployment
metadata:
name: pc-deployment
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80

然后使用如下命令创建资源

[root@master service]# kubectl apply -f deployment.yaml
namespace/dev created
deployment.apps/pc-deployment created
[root@master service]#

查看创建的资源如下:

[root@master service]# kubectl get deploy,pod -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/pc-deployment 3/3 3 3 69s nginx nginx:1.17.1 app=nginx-pod

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/pc-deployment-5ffc5bf56c-gvgts 1/1 Running 0 69s 10.244.2.144 node2 <none> <none>
pod/pc-deployment-5ffc5bf56c-l6dln 1/1 Running 0 69s 10.244.2.145 node2 <none> <none>
pod/pc-deployment-5ffc5bf56c-q2pbn 1/1 Running 0 69s 10.244.1.16 node1 <none> <none>
[root@master service]#

二、创建ClusterIP类型的Service

编写cluster_ip.yaml文件,内容如下:

apiVersion: v1
kind: Service
metadata:
name: cluster-ip
namespace: dev
spec:
selector:
app: nginx-pod
type: ClusterIP
ports:
- port: 80
targetPort: 80

使用如下命令创建Service

[root@master service]# kubectl apply -f cluster_ip.yaml
service/cluster-ip created
[root@master service]#

通过如下命令查询资源

[root@master service]# kubectl get service,deployment,pod -n dev -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/cluster-ip ClusterIP 10.110.180.51 <none> 80/TCP 20s app=nginx-pod

NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/pc-deployment 3/3 3 3 18m nginx nginx:1.17.1 app=nginx-pod

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/pc-deployment-5ffc5bf56c-gvgts 1/1 Running 0 18m 10.244.2.144 node2 <none> <none>
pod/pc-deployment-5ffc5bf56c-l6dln 1/1 Running 0 18m 10.244.2.145 node2 <none> <none>
pod/pc-deployment-5ffc5bf56c-q2pbn 1/1 Running 0 18m 10.244.1.16 node1 <none> <none>
[root@master service]#

通过如下命令可以查看到创建的Service的更加详细的信息

[root@master service]# kubectl describe service cluster-ip -n dev
Name: cluster-ip
Namespace: dev
Labels: <none>
Annotations: <none>
Selector: app=nginx-pod
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.110.180.51
IPs: 10.110.180.51
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.1.16:80,10.244.2.144:80,10.244.2.145:80
Session Affinity: None
Events: <none>
[root@master service]#

使用ClusterIP访问nginx服务

[root@master service]# curl 10.110.180.51: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 service]#

三、删除资源

使用如下命令删除即可

[root@master service]# kubectl delete -f cluster_ip.yaml
service "cluster-ip" deleted
[root@master service]# kubectl delete -f deployment.yaml
namespace "dev" deleted
deployment.apps "pc-deployment" deleted
[root@master service]#


举报

相关推荐

0 条评论