Kubernetes,Ingress资源配置,到同一主机的路由,但是不同的端口

时间:2018-05-31 10:28:30

标签: kubernetes kubernetes-ingress

此问题与another question相关。从this link,我可以说使用入口路由到不同的端口服务是可行的。

我首先列出了我的两项服务:(eureka和config),ingress_nginx_res.yaml如下:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - host:
    http:
      paths:
      - backend:
          serviceName: gearbox-rack-eureka-server
          servicePort: 8761
  - host:
    http:
      paths:
      - path:
        backend:
          serviceName: gearbox-rack-config-server
          servicePort: 8888

正如人们所见,gearbox-rack-eureka-server正在侦听端口8761,而gearbox-rack-config-server正在侦听端口8888。

现在,所有服务和k8都安装在我的本地虚拟机箱(centos 7x),172.16.100.83,在kubectl apply -f ingress_nginx_res.yaml之后,我可以看到它已启动。

[root@master3 ingress]# kubectl get ing
NAME         HOSTS     ADDRESS   PORTS     AGE
my-ingress   *                   80        11s

我尝试验证入口,然后我放172.16.100.83:8761,它说无法访问此网站。所以我相信我的配置有问题。我把所有相关的配置和步骤如下:

eureka_pod.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: gearbox-rack-eureka-server
  labels:
    app: gearbox-rack-eureka-server
    purpose: platform_eureka_demo
spec:
  containers:
  - name:  gearbox-rack-eureka-server
    image: 192.168.1.229:5000/gearboxrack/gearbox-rack-eureka-server
    ports:
        - containerPort: 8761

eureka_svc.yaml:

apiVersion: v1
kind: Service
metadata:
  name: gearbox-rack-eureka-server
  labels:
    name: gearbox_rack_eureka_server
spec:
  selector:
    app: gearbox-rack-eureka-server
  type: NodePort
  ports:
    - port: 8761
      nodePort: 31501
      name: tcp

config_pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: gearbox-rack-config-server
  labels:
    app: gearbox-rack-config-server
    purpose: platform-demo
spec:
  containers:
  - name:  gearbox-rack-config-server
    image: 192.168.1.229:5000/gearboxrack/gearbox-rack-config-server
    ports:
    - containerPort: 8888
    env:
      - name: EUREKA_SERVER
        value: http://172.16.100.83:8761

config_svc.yaml:

apiVersion: v1
kind: Service
metadata:
  name: gearbox-rack-config-server
  labels:
    name: gearbox-rack-config-server
spec:
  selector:
    app: gearbox-rack-config-server
  type: NodePort
  ports:
    - port: 8888
      nodePort: 31502
      name: tcp

ingress_nginx_role_rb.yaml:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: lb
  namespace: kube-system

---

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: nginx-ingress-normal
rules:
  - apiGroups:
      - ""
    resources:
      - configmaps
      - endpoints
      - nodes
      - pods
      - secrets
    verbs:
      - list
      - watch
  - apiGroups:
      - ""
    resources:
      - nodes
    verbs:
      - get
  - apiGroups:
      - ""
    resources:
      - services
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - "extensions"
    resources:
      - ingresses
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - ""
    resources:
        - events
    verbs:
        - create
        - patch
  - apiGroups:
      - "extensions"
    resources:
      - ingresses/status
    verbs:
      - update

---

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:
  name: nginx-ingress-minimal
  namespace: kube-system
rules:
  - apiGroups:
      - ""
    resources:
      - configmaps
      - pods
      - secrets
      - namespaces
    verbs:
      - get
  - apiGroups:
      - ""
    resources:
      - configmaps
    resourceNames:
      - "ingress-controller-leader-dev"
      - "ingress-controller-leader-prod"
    verbs:
      - get
      - update
  - apiGroups:
      - ""
    resources:
      - configmaps
    verbs:
      - create
  - apiGroups:
      - ""
    resources:
      - endpoints
    verbs:
      - get

---

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
  name: nginx-ingress-minimal
  namespace: kube-system
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: nginx-ingress-minimal
subjects:
  - kind: ServiceAccount
    name: lb
    namespace: kube-system
---

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: nginx-ingress-normal
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: nginx-ingress-normal
subjects:
  - kind: ServiceAccount
    name: lb
    namespace: kube-system

nginx_default-backend.yaml

kind: Service
apiVersion: v1
metadata:
  name: nginx-default-backend
  namespace: kube-system
spec:
  ports:
  - port: 80
    targetPort: http
  selector:
    app: nginx-default-backend
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: nginx-default-backend
  namespace: kube-system
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-default-backend
    spec:
      terminationGracePeriodSeconds: 60
      containers:
      - name: default-http-backend
        image: chenliujin/defaultbackend
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 30
          timeoutSeconds: 5
        resources:
          limits:
            cpu: 10m
            memory: 20Mi
          requests:
            cpu: 10m
            memory: 20Mi
        ports:
        - name: http
          containerPort: 8080
          protocol: TCP

ingress_nginx_ctl.yaml

kind: Service
apiVersion: v1
metadata:
  name: ingress-nginx
spec:
  type: LoadBalancer
  selector:
    app: ingress-nginx
  ports:
  - name: http
    port: 80
    targetPort: http
  - name: https
    port: 443
    targetPort: https
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: ingress-nginx
  namespace: kube-system
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: ingress-nginx
    spec:
      terminationGracePeriodSeconds: 60
      serviceAccount: lb
      containers:
      - image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.15.0
        name: ingress-nginx
        imagePullPolicy: Always
        ports:
          - name: http
            containerPort: 80
            protocol: TCP
          - name: https
            containerPort: 443
            protocol: TCP
        livenessProbe:
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
          initialDelaySeconds: 30
          timeoutSeconds: 5
        env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
        args:
        - /nginx-ingress-controller
        - --default-backend-service=$(POD_NAMESPACE)/nginx-default-backend

我的配置有什么问题?或任何有关故障排除命令的提示,以便我可以自己管理?

=============================================== ==

第二版

1。 我的虚拟机centOs(7.x)在我的主机win10专业版中运行。我不使用谷歌云或AWS。我没有任何负载均衡器,我认为nginx是反向代理,所以它有负载均衡器功能。 ingress_nginx_ctl.yamlquay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.15.0中的图像是ingress和nginx之间的连接器吗?它本身不是nginx吗?

2
a)我编辑/ etc / hosts,然后输入以下两个条目:

  172.16.100.83 gearbox-rack-eureka-server.sup.com
  172.16.100.83 gearbox-rack-config-server.sup.com

b)我看到k8s集群中有dns服务器,如何将这两个条目添加到dns中?或者dns控制台在哪里,以便我可以放这两个条目?

2 个答案:

答案 0 :(得分:0)

我不知道你的环境,你确定你有'loadbalancer'吗?为了简化,您可以使用'hostNetwork:true'来设置您的nginx-controller-deploy,这样您就可以直接通过主机IP访问控制器。

而且,既然我们通过入口控制器访问各种服务,我们如何区分不同的服务?使用域名。我们可以将gearbox-rack-eureka-server.your.domain指向gearbox-rack-eureka-server:8761,同时使用gearbox-rack-config-server.your.domain指向gearbox-rack-config-server:8888 in入口,像这样:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - host: gearbox-rack-eureka-server.your.domain
    http:
      paths:
      - backend:
          serviceName: gearbox-rack-eureka-server
          servicePort: 8761
  - host: gearbox-rack-config-server.your.domain
    http:
      paths:
      - backend:
          serviceName: gearbox-rack-config-server
          servicePort: 8888

将domain domainname配置为主机ip,然后使用domainname访问这些服务。

答案 1 :(得分:0)

我在这里发现了几个问题:

  1. 就Kubernetes而言,当您为服务指定cli packages: (/usr/lib/node_modules) @ionic/cli-utils : 1.19.2 ionic (Ionic CLI) : 3.20.0 global packages: cordova (Cordova CLI) : 8.0.0 local packages: @ionic/app-scripts : 3.1.8 Cordova Platforms : android 7.1.0 browser 5.0.3 Ionic Framework : ionic-angular 3.9.2 System: Node : v8.11.2 npm : 5.6.0 OS : Linux 4.15 Environment Variables: ANDROID_HOME : not set Misc: backend : pro 时,它依赖于云提供商提供的外部负载均衡器。因此,如果群集中没有此类负载均衡器,则无法在设置中使用type: LoadBalancer进行ingress-nginx服务。但是你仍然必须分享你对外部世界的影响,而最简单的方法就是使用type: LoadBalancer。例如:

    type: NodePort

    现在,您可以使用地址kind: Service apiVersion: v1 metadata: name: ingress-nginx spec: type: NodePort selector: app: ingress-nginx ports: - port: 80 nodePort: 31080 name: http 与Ingress联系。例如,http://<ip-address-of-any-node>:31080

  2. 下一步,您需要为Ingress提供配置。您可以将Ingress抽象想象为Ingress-controller的配置(在您的情况下为Nginx配置)。

    http://172.16.100.83:31080

    正如您所提到的,此处Nginx作为Ingress控制器扮演反向代理的角色,并将apiVersion: extensions/v1beta1 kind: Ingress metadata: name: my-ingress spec: rules: - http: paths: - path: /eureka backend: serviceName: gearbox-rack-eureka-server servicePort: 8761 - path: /config backend: serviceName: gearbox-rack-config-server servicePort: 8888 中的路径与服务和端口相匹配。

  3. 此外,对于使用Ingress公开的服务使用http://<ip-address-of-any-node>:31080/<path>是不正确的。最好使用type: NodePort。所以,对于您的服务:

    type: ClusterIP

    现在,您可以apiVersion: v1 kind: Service metadata: name: gearbox-rack-eureka-server labels: name: gearbox_rack_eureka_server spec: selector: app: gearbox-rack-eureka-server type: ClusterIP ports: - port: 8761 name: tcp --- apiVersion: v1 kind: Service metadata: name: gearbox-rack-config-server labels: name: gearbox-rack-config-server spec: selector: app: gearbox-rack-config-server type: ClusterIP ports: - port: 8888 name: tcp 与尤里卡联系并在http://172.16.100.83:31080/eureka上配置