不做大哥好多年 不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核

逍遥子

不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核
  • Docker

  • K8S

    • 01_Kubernetes概述

    • 02_二进制搭建K8s集群

    • 03_kubeadm部署K8s

    • 04_Kubectl命令行管理工具

    • 05_资源编排(YAML)

    • 06_Pod对象管理

    • 07_Pod对象调度

    • 08_常用负载控制器

    • 09_Service统一入口访问

      • 01.Service同一访问入口
        • 1.1 Service是什么?
        • 1.2 Service存在的意义
        • 1.3 Pod与Service的关系
        • 2.1 先使用yaml文件部署三个nginx
        • 2.2 Service定义与创建
        • 2.3 通过Service访问nginx集群
        • 3.1 ClusterIP(VIP)
        • 3.2 NodePort
          • 3.2.1 先使用yaml文件部署三个nginx
          • 3.2.2 使用NodePort创建Service
          • 3.2.3 访问
          • 3.2.4 NodePort存在弊端
        • 3.3 LoadBalancer
          • 3.3.1 LB说明
          • 3.3.2 配置LB
    • 10_Ingress对外暴露应用

    • 11_项目中实际应用K8s

  • 容器原理

  • Istio

  • 容器
  • K8S
  • 09_Service统一入口访问
xiaonaiqiang
2021-02-12
目录

01.Service同一访问入口

# 01.Service基本概念

# 1.1 Service是什么?

  • 背景:每个Pod具有IP地址,当使用Deployment控制器时,Pod的IP地址往往动态变化。
  • 解决方法:通过Service可以获得稳定的IP地址,且在Service的生命周期有效,与Pod的IP地址变化与否无关。
  • 实质:Service本质就是一个LB负载均衡器

# 1.2 Service存在的意义

# 1.3 Pod与Service的关系

# 02.Service定义和创建

# 2.1 先使用yaml文件部署三个nginx

[root@k8s-master ~]# vim deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  • 可以查看刚刚部署nginx服务标签:nginx
[root@k8s-master ~]# kubectl get pods --show-labels
NAME                       READY   STATUS    RESTARTS     AGE   LABELS
nginx-6799fc88d8-s5rnz           1/1    Running   0          24d   app=nginx,pod-template-hash=6799fc88d8
nginx-deployment-66b6c48dd5-hxv7h   1/1     Running   0          70s   app=nginx,pod-template-hash=66b6c48dd5
nginx-deployment-66b6c48dd5-jjkk4   1/1     Running   0          70s   app=nginx,pod-template-hash=66b6c48dd5
nginx-deployment-66b6c48dd5-tsgvb   1/1     Running   0          70s   app=nginx,pod-template-hash=66b6c48dd5
1
2
3
4
5
6
  • 查看当前机器部署了那些服务
[root@k8s-master ~]# kubectl get svc
NAME         TYPE      CLUSTER-IP    EXTERNAL-IP    PORT(S)        AGE
kubernetes   ClusterIP     10.96.0.1     <none>        443/TCP        24d
nginx        NodePort    10.108.41.4   <none>        80:31434/TCP    24d
1
2
3
4

# 2.2 Service定义与创建

[root@k8s-node2 ~]# vim service.yaml
apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: default
spec:
  ports:
  - port: 80        # Service端口
    protocol: TCP    # 协议
    targetPort: 80   # 容器端口(程序启动端口,比如django默认是8000端口)
  selector: 
    app: nginx      # 指定关联Pod的标签(上面已经查到了,app=nginx)
  type: ClusterIP      # 服务类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  • 创建service
[root@k8s-master ~]# kubectl apply -f service.yaml 
1

# 2.3 通过Service访问nginx集群

[root@k8s-master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes    ClusterIP    10.96.0.1       <none>      443/TCP        24d
nginx        NodePort    10.108.41.4      <none>      80:31434/TCP    24d
web         ClusterIP    10.105.53.183     <none>       80/TCP       2m40s
[root@k8s-master ~]# curl 10.105.53.183        # 可以通过web的地址访问后面三个nginx集群

[root@k8s-master ~]# kubectl delete svc web2     # 可以删除其中一个
1
2
3
4
5
6
7
8

# 03.Service三种常用类型

  • ClusterIP:集群内部使用

  • NodePort:对外暴露应用(集群外)

  • LoadBalancer:对外暴露应用,适用公有云

# 3.1 ClusterIP(VIP)

# 3.2 NodePort

# 3.2.1 先使用yaml文件部署三个nginx

[root@k8s-master ~]# vim deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  • 可以查看刚刚部署nginx服务标签:nginx
[root@k8s-master ~]# kubectl get pods --show-labels
NAME                       READY   STATUS    RESTARTS     AGE   LABELS
nginx-6799fc88d8-s5rnz           1/1    Running   0          24d   app=nginx,pod-template-hash=6799fc88d8
nginx-deployment-66b6c48dd5-hxv7h   1/1     Running   0          70s   app=nginx,pod-template-hash=66b6c48dd5
nginx-deployment-66b6c48dd5-jjkk4   1/1     Running   0          70s   app=nginx,pod-template-hash=66b6c48dd5
nginx-deployment-66b6c48dd5-tsgvb   1/1     Running   0          70s   app=nginx,pod-template-hash=66b6c48dd5
1
2
3
4
5
6

# 3.2.2 使用NodePort创建Service

[root@k8s-node2 ~]# vim service-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
  name: web-nodeport
  namespace: default
spec:
  ports:
  - port: 80        # Service端口
    protocol: TCP    # 协议
    targetPort: 80    # 容器端口(程序启动端口,比如django默认是8000端口)
    nodePort: 30706   # 指定NodePort监听的外网端口
  selector: 
    app: nginx      # 指定关联Pod的标签(上面已经查到了,app=nginx)
  type: NodePort      # 服务类型(只需要把服务类型修改成NodePort即可)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  • 创建Service
[root@k8s-master ~]# kubectl apply -f service-nodeport.yaml 

1
2

# 3.2.3 访问

  • 访问地址:<任意NodeIP>:NodePort

  • 端口范围:30000-32767

# 3.2.4 NodePort存在弊端

# 3.3 LoadBalancer

# 3.3.1 LB说明

  • 负责均衡器有哪些:nginx、LVS、haproxy

  • 私有云:SLB

# 3.3.2 配置LB

  • 直接找一个nginx配置反向代理即可
stream {
    log_format  main  '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
    access_log  /var/log/nginx/django-access.log  main;
    upstream django-apiserver {
                server 192.168.56.62:30706;
                server 192.168.56.63:30706;
            }
   
    server {
       listen 88;
       proxy_pass django-apiserver;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
上次更新: 2024/3/13 15:35:10
01.常用负载控制器
01.Ingress暴露应用

← 01.常用负载控制器 01.Ingress暴露应用→

最近更新
01
04.数组双指针排序_子数组
03-25
02
08.动态规划
03-25
03
06.回溯算法
03-25
更多文章>
Theme by Vdoing | Copyright © 2019-2025 逍遥子 技术博客 京ICP备2021005373号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式