zoukankan      html  css  js  c++  java
  • .net core i上 K8S(六).netcore程序的service网络代理模式

    上一章我们讲了pod的hostip模式,但在生产环境中,我们都是通过service来访问k8s集群的,service有两种模式来暴漏端口,今天我们来分享一下

    1.clusterIP模式

    我们在创建service的时候,默认创建的时clusterIP模式,clusterIP模式的特点是只能在node节点上访问,创建方法如下:

    1.1首先创建Deployment

    apiVersion: apps/v1beta2
    kind: Deployment
    metadata:
      name: cys-netcore
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: cys-netcore
      template:
        metadata:
          labels:
            app: cys-netcore
        spec:
          containers:
          - name: cys-netcore
            image: chenyishi/webdocker_s_provider
            ports:
            - containerPort: 1000
            env:
            - name: serverport
              value: "1000"

    1.2创建service,并指定clusterip(不指定也可以,会自动随机分配)

    注意:clusterIP设置须按照集群搭建时指定的范围设置,我们查看一下我们制定的范围

    可以看到范围是10.10.10.1~10.10.10.255,编写yaml文件如下

    apiVersion: v1
    kind: Service
    metadata:
      name: netcore-service
      labels:
        app: netcore
    spec:
      ports:
      - name: http
        protocol: TCP
        port: 1001
        targetPort: 1000
      clusterIP: "10.10.10.10"    #指定ip
      selector:
        app: cys-netcore

    可查看信息,如下

    1.3验证地址

    我们可以去node节点验证一下

    至此clusterip介绍完毕

    2.NodePort模式

    我们可以看到clusterIP模式只能在node节点访问,这无法满足我们的生产环境,因此我们在生产环境中都是采用NodePort模式,映射到宿主机的port,我们的Deployment的yaml文件还是采用上面的,需要改一下service的yaml文件

    apiVersion: v1
    kind: Service
    metadata:
      name: netcore-service
      labels:
        app: netcore
    spec:
      ports:
      - name: http
        protocol: TCP
        port: 1001
        targetPort: 1000
        nodePort: 31111    #指定宿主机暴漏的端口
      selector:
        app: cys-netcore
      type: NodePort      #指定类型

    注意:nodePort的端口号也是从集群搭建时指定的范围中设置的,不可超出范围,如下

    2.1创建service

    查看信息

    2.2验证

    访问地址http://192.168.8.202:31111/api/values

     

    成功访问!!!

  • 相关阅读:
    MySQL之ORM
    MySQL之索引补充
    MySQL存储过程
    c primer plus 7编程练习
    c语言中统计单词数目程序
    c语言统计输入字符数及行数
    c语言中getchar()、putchar()函数例子
    c primer plus 6编程练习
    c语言 %c 一次输出多个字符 (特殊程序)
    c语言 复合赋值运算符的优先级低于算术运算符
  • 原文地址:https://www.cnblogs.com/chenyishi/p/10121980.html
Copyright © 2011-2022 走看看