Kubernetes----ClusterIP类型的Service

简介: 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]#
相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务&nbsp;ACK 容器服务&nbsp;Kubernetes&nbsp;版(简称&nbsp;ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
目录
相关文章
|
Kubernetes 网络协议 关系型数据库
Kubernetes----ExternalName类型的Service
Kubernetes----ExternalName类型的Service
2048 0
|
4月前
|
Kubernetes 负载均衡 网络协议
|
4月前
|
Kubernetes 负载均衡 算法
kubernetes—Service详解
kubernetes—Service详解
40 0
|
7月前
|
Kubernetes 网络协议 应用服务中间件
|
9月前
|
存储 Kubernetes 应用服务中间件
k8s--Service 环境准备、ClusterIP 使用
k8s--Service 环境准备、ClusterIP 使用
|
10月前
|
Kubernetes 负载均衡 应用服务中间件
22-Kubernetes-Service详解-Ingress介绍
22-Kubernetes-Service详解-Ingress介绍
|
10月前
|
存储 Kubernetes 负载均衡
21-Kubernetes-Service详解-Service使用
21-Kubernetes-Service详解-Service使用
|
10月前
|
Kubernetes 监控 算法
20-Kubernetes-Service详解-Service介绍
20-Kubernetes-Service详解-Service介绍
|
10月前
|
Kubernetes 负载均衡 网络协议
Kubernetes Service解析
我们都知道,在K8S集群中,每个Pod都有自己的私有IP地址,并且这些IP地址不是固定的。这意味着其不依赖IP地址而存在。例如,当我们因某种业务需求,需要对容器进行更新操作,则容器很有可能在随后的启动运行过程中被分配到其他IP地址。此外,在K8S集群外部看不到该Pod。因此,Pod若单独运行于K8S体系中,在实际的业务场景中是不现实的,故我们需要通过其他的策略去解决,那么解决方案是什么? 由此,我们引入了Serivce这个概念以解决上述问题。
89 0
|
12月前
|
Kubernetes 负载均衡 算法
kubernetes中service探讨
kubernetes中service探讨
86 0