• k8s自定义Endpoint实现内部pod访问外部应用


    自定义endpoint实现内部pod访问外部应用

    endpoint除了可以暴露pod的IP和端口还可以代理到外部的ip和端口

    使用场景

    1. 公司业务还还没有完成上云, 一部分云原生的,一部分是实体的

    2. 业务上云期间逐步实现上云,保证各个模块之间的解耦性

    比如使用云数据库或者实体数据库服务器啥的,因为像数据库实现容器化的话在实际生产环境中是不推荐的

    所以一些静态服务上云以后pod还是需要访问外部应用服务的

    k8s的Endpoint自定义实验

    还是用tomcat+mysql的zrlog来实验

    首先准备好tomcat的zrlog代码,我直接用上个博文实验用的yaml文件,因为现在主要探讨的是pod通过service与外部网络通信

    [root@server153 test]# cat tomcat-deploy.yaml 
    apiVersion: v1
    kind: Service  # 声明版本为Service
    metadata:
      name: tomcat-service   # 定义Service的名字
      labels:
        name: show-tomcat-pod    # 定义Service的标签
    spec:
      type: NodePort   # 定义Service的类型,自动分配一个集群serviceip
      selector:
        app: tomcat-deploy   #定义标签选择器,会代理后端app=tomcat-deploy的Pod
      ports:
      - port: 80   #内部暴露的端口    
        targetPort: 8080  #代理的pod的端口 
        nodePort: 31111 #暴露给主机外部访问的端口(default: 30000-32767)
    
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: tomcat-deploy
      name: tomcat-deploy
      namespace: default
    spec:
      progressDeadlineSeconds: 600
      replicas: 1
      revisionHistoryLimit: 10
      selector:
        matchLabels:
          app: tomcat-deploy
      strategy:
        rollingUpdate:
          maxSurge: 25%
          maxUnavailable: 25%
        type: RollingUpdate
      template:
        metadata:
          labels:
            app: tomcat-deploy
        spec:
          #创建init容器
          initContainers:
            #代码镜像
          - image: www.test.com/mytest/zrlog:v1
            #init容器名字
            name: init
            #将代码复制到匿名数据卷
            command: ["cp","-r","/tmp/ROOT.war","/www"]
            #将匿名数据卷挂载到容器中的/www目录下
            volumeMounts:
            - mountPath: /www
              name: tomcat-volume
          #创建tomcat容器
          containers:
          - image: oxnme/tomcat
            imagePullPolicy: Always
            name: tomcat
            terminationMessagePath: /dev/termination-log
            terminationMessagePolicy: File
            #将数据卷挂载到tomcat的代码目录下
            volumeMounts:
            - mountPath: /usr/local/tomcat/webapps/
              name: tomcat-volume
          dnsPolicy: ClusterFirst
          restartPolicy: Always
          schedulerName: default-scheduler
          terminationGracePeriodSeconds: 10
    
          #创建匿名数据卷
          volumes:
          - name: tomcat-volume
            emptyDir: {}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    tomcat的文件yaml文件这样就可以了,还是暴露主机的31111端口

    然后就去配置我们的mysql数据库,创建数据库并创建一个连接数据库的用户,给与权限

    [root@server160 ~]# mysql -uroot -pMySQL@666
    
    
    mysql> CREATE USER 'zrtest'@'%' IDENTIFIED BY 'MySQL@666';
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> CREATE DATABASE Zrlog;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> GRANT ALL PRIVILEGES ON `Zrlog`.* TO 'zrtest'@'%';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | Zrlog              |
    | mysql              |
    | performance_schema |
    | sys                |
    | zabbix             |
    +--------------------+
    6 rows in set (0.00 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    数据库这样就配置好了

    然后去配置我们自定义的Endpoint 和service

    [root@server153 test]# cat endpoint.yaml 
    apiVersion: v1
    kind: Endpoints
    metadata: 
      name: mysql
      namespace: default
    #指定自定义的point的目标地址
    subsets:
    - addresses:
      #外部的reids ip
      - ip: 192.168.121.160
      # 外部redis的真实的工作端口
      ports:
       - port: 3306
         # 定义端口的名称,必须与 service 中的 ports.name 一致
         name: mysqlport
    ---
    #这里的service配置大家都熟悉了,主要就是上面的endpoint而已
    kind: Service
    apiVersion: v1
    metadata:
      name: mysql
      namespace: default
    spec:
      ports:
      - port: 3306
        protocol: TCP
        name: mysqlport
        targetPort: 3306
      type: ClusterIP
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    这样配置就可以了,然后执行配置文件

    [root@server153 test]# kubectl apply -f tomcat-deploy.yaml 
    [root@server153 test]# kubectl apply -f endpoint.yaml 
    
    • 1
    • 2

    然后去查看mysql endpoint的详细信息

    [root@server153 test]# kubectl describe endpoints mysql 
    Name:         mysql
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    Subsets:
      Addresses:          192.168.121.160
      NotReadyAddresses:  <none>
      Ports:
        Name       Port  Protocol
        ----       ----  --------
        mysqlport  3306  TCP
    
    Events:  <none>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    还有service的信息

    [root@server153 test]# kubectl describe services mysql 
    Name:              mysql
    Namespace:         default
    Labels:            <none>
    Annotations:       <none>
    Selector:          <none>
    Type:              ClusterIP
    IP Family Policy:  SingleStack
    IP Families:       IPv4
    IP:                10.1.30.160
    IPs:               10.1.30.160
    Port:              mysqlport  3306/TCP
    TargetPort:        3306/TCP
    Endpoints:         192.168.121.160:3306
    Session Affinity:  None
    Events:            <none>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    可以看到service是代理到了160主机,然后去浏览器访问31111端口安装测试

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    查看数据库内容

    mysql> use Zrlog;
    mysql> show tables;
    +-----------------+
    | Tables_in_Zrlog |
    +-----------------+
    | comment         |
    | link            |
    | log             |
    | lognav          |
    | plugin          |
    | tag             |
    | type            |
    | user            |
    | website         |
    +-----------------+
    9 rows in set (0.00 sec)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    可以看到安装完毕就是这样的,只靠service的自动发现服务是没办法访问到外部网络的

    所以自定义的Endpoint作用就体现出来了,这个还是比较有必要了解的

    因为数据库数据的特殊性,一般是不容器化的

    希望对大家有帮助

  • 相关阅读:
    简单聊聊 分布式
    Java以form-data(表单)的形式调用第三方接口
    关于MongoDb查询Decimal128转BigDecimal问题
    VS Code For Web 深入浅出 -- 进程间通信篇
    Spring 随笔 ioc/di 2-依赖循环
    从Matlab实例学习遗传算法
    数字电路与逻辑设计 之 组合逻辑电路
    微软大中华区商业应用事业部高级产品经理张诗源,将出席“ISIG-低代码/零代码技术与应用发展峰会”
    【单调栈】739. 每日温度、496. 下一个更大元素 I
    docker-创建自定义网络,并给容器分配静态ip
  • 原文地址:https://blog.csdn.net/m0_58833554/article/details/134398997