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
目录
相关文章
|
2月前
|
存储 运维 Kubernetes
容器服务ACK常见问题之修改service的名字失败如何解决
容器服务ACK(阿里云容器服务 Kubernetes 版)是阿里云提供的一种托管式Kubernetes服务,帮助用户轻松使用Kubernetes进行应用部署、管理和扩展。本汇总收集了容器服务ACK使用中的常见问题及答案,包括集群管理、应用部署、服务访问、网络配置、存储使用、安全保障等方面,旨在帮助用户快速解决使用过程中遇到的难题,提升容器管理和运维效率。
|
3月前
|
存储 运维 Kubernetes
批处理及有状态等应用类型在 K8S 上应该如何配置?
批处理及有状态等应用类型在 K8S 上应该如何配置?
|
5月前
|
Kubernetes 负载均衡 网络协议
k8s学习-Service(概念、模板、创建、外部代理、删除等)
k8s学习-Service(概念、模板、创建、外部代理、删除等)
159 0
|
4月前
|
Kubernetes 负载均衡 网络协议
|
1天前
|
运维 Kubernetes Linux
Kubernetes详解(七)——Service对象部署和应用
Kubernetes详解(七)——Service对象部署和应用
7 3
|
1月前
|
Kubernetes 网络协议 应用服务中间件
kubernetes核心技术之Service知识点总结
kubernetes核心技术之Service知识点总结
21 0
|
4月前
|
Kubernetes 负载均衡 算法
kubernetes—Service详解
kubernetes—Service详解
40 0
|
5月前
|
Kubernetes 前端开发 网络协议
云原生|kubernetes|kubernetes中的资源(一)---service详解
云原生|kubernetes|kubernetes中的资源(一)---service详解
182 0
|
5月前
|
Kubernetes 负载均衡 应用服务中间件
k8s学习-CKA真题-负载均衡service
k8s学习-CKA真题-负载均衡service
40 0
|
5月前
|
Kubernetes 负载均衡 容器
k8s(8)Service(服务)
Service(服务)
42 0