Kubernetes之Ingress和Ingress Controller

目录

  • Kubernetes之Ingress和Ingress Controller

    • 概念
    • Ingress资源类型
      • 单Service资源型Ingress
    • Ingress Nginx部署
      • 部署Ingress controller
      • 配置ingress后端服务
      • 部署ingress-nginx service
      • 部署Ingress
      • 增加tomcat服务
      • 总结
    • 构建TLS站点

Kubernetes之Ingress和Ingress Controller

概念

通常情况下,service和pod的IP仅可在集群内部访问。集群外部的请求需要通过负载均衡转发到service在Node上暴露的NodePort上,然后再由kube-proxy将其转发给相关的Pod。

NodePort 方式暴露服务面临问题是,服务一旦多起来,NodePort 在每个节点上开启的端口会及其庞大,而且难以维护;这时,我们可以能否使用一个Nginx直接对内进行转发呢?众所周知的是,Pod与Pod之间是可以互相通信的,而Pod是可以共享宿主机的网络名称空间的,也就是说当在共享网络名称空间时,Pod上所监听的就是Node上的端口。那么这又该如何实现呢?简单的实现就是使用 DaemonSet 在每个 Node 上监听 80,然后写好规则,因为 Nginx 外面绑定了宿主机 80 端口(就像 NodePort),本身又在集群内,那么向后直接转发到相应 Service IP就行了。

但是新的问题出现:当每次有新服务加入时怎么办。此时 Ingress 出现了,如果不算上面的Nginx,Ingress 包含两大组件:Ingress ControllerIngress

Ingress就是为进入集群的请求提供路由规则的集合,如下图所示

Ingress可以给service提供集群外部访问的URL、负载均衡、SSL终止、HTTP路由等。为了配置这些Ingress规则,集群管理员需要部署一个Ingress controller,它监听Ingress和service的变化,并根据规则配置负载均衡并提供访问入口。

Ingress也是Kubernetes API的标准资源类型之一,它其实就是一组基于DNS名称(host)或URL路径把请求转发到指定的Service资源的规则。用于将集群外部的请求流量转发到集群内部完成的服务发布。我们需要明白的是,Ingress资源自身不能进行“流量穿透”,仅仅是一组规则的集合,这些集合规则还需要其他功能的辅助,比如监听某套接字,然后根据这些规则的匹配进行路由转发,这些能够为Ingress资源监听套接字并将流量转发的组件就是Ingress Controller。

Ingress 控制器不同于Deployment 控制器的是,Ingress控制器不直接运行为kube-controller-manager的一部分,它仅仅是Kubernetes集群的一个附件,类似于CoreDNS,需要在集群上单独部署。

创建Ingress资源
Ingress资源时基于HTTP虚拟主机或URL的转发规则,需要强调的是,这是一条转发规则。它在资源配置清单中的spec字段中嵌套了rules、backend和tls等字段进行定义。如下示例中定义了一个Ingress资源,其包含了一个转发规则:将发往myapp.magedu.com的请求,代理给一个名字为myapp的Service资源。

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-myapp
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: myapp.magedu.com
    http:
      paths:
      - path:
        backend:
          serviceName: myapp
          servicePort: 80

Ingress 中的spec字段是Ingress资源的核心组成部分,主要包含以下3个字段:

  • rules:用于定义当前Ingress资源的转发规则列表;由rules定义规则,或没有匹配到规则时,所有的流量会转发到由backend定义的默认后端。
  • backend:默认的后端用于服务那些没有匹配到任何规则的请求;定义Ingress资源时,必须要定义backend或rules两者之一,该字段用于让负载均衡器指定一个全局默认的后端。
  • tls:TLS配置,目前仅支持通过默认端口443提供服务,如果要配置指定的列表成员指向不同的主机,则需要通过SNI TLS扩展机制来支持该功能

backend对象的定义由2个必要的字段组成:serviceName和servicePort,分别用于指定流量转发的后端目标Service资源名称和端口。
rules对象由一系列的配置的Ingress资源的host规则组成,这些host规则用于将一个主机上的某个URL映射到相关后端Service对象,其定义格式如下:

spec:
  rules:
  - hosts: <string>
    http:
      paths:
      - path:
        backend:
          serviceName: <string>
          servicePort: <string>

需要注意的是,.spec.rules.host属性值,目前暂不支持使用IP地址定义,也不支持IP:Port的格式,该字段留空,代表着通配所有主机名。
tls对象由2个内嵌的字段组成,仅在定义TLS主机的转发规则上使用。

  • hosts:包含于使用的TLS证书之内的主机名称字符串列表,因此,此处使用的主机名必须匹配tlsSecret中的名称。
  • secretName: 用于引用SSL会话的secret对象名称,在 基于SNI实现多主机路由的场景中,此字段为可选。

Ingress资源类型

Ingress的资源类型有以下4种:

  • 单Service资源型Ingress
  • 基于URL路径进行流量转发
  • 基于主机名称的虚拟主机
  • TLS类型的Ingress资源

单Service资源型Ingress

暴露单个服务的方法有多种,如NodePort、LoadBanlancer等等,当然也可以使用Ingress来进行暴露单个服务,只需要为Ingress指定default backend即可,如下示例:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  backend:
    serviceName: my-svc
    servicePort: 80

Ingress控制器会为其分配一个IP地址接入请求流量,并将其转发至后端my-svc

Ingress Nginx部署

使用Ingress功能步骤:
1、安装部署ingress controller Pod
2、部署后端服务
3、部署ingress-nginx service
4、部署ingress
Ingress 也是标准的 K8S 资源,其定义的方式,也可以使用 explain 进行查看:

[[email protected] ~]# kubectl explain ingress
KIND:     Ingress
VERSION:  extensions/v1beta1

DESCRIPTION:
     Ingress is a collection of rules that allow inbound connections to reach
     the endpoints defined by a backend. An Ingress can be configured to give
     services externally-reachable urls, load balance traffic, terminate SSL,
     offer name based virtual hosting etc.

FIELDS:
   apiVersion   <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#resources

   kind <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds

   metadata     <Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata

   spec <Object>
     Spec is the desired state of the Ingress. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

   status       <Object>
     Status is the current state of the Ingress. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

部署Ingress controller

此处使用ingress-nginx 0.17.1版本,未使用最新的master
下载ingress相关yaml

[[email protected] manifests]# mkdir ingress-nginx
[[email protected] manifests]# cd ingress-nginx
[[email protected] manifests]#  for file in namespace.yaml configmap.yaml rbac.yaml tcp-services-configmap.yaml with-rbac.yaml udp-services-configmap.yaml default-backend.yaml;do wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.17.1/deploy/$file;done

[[email protected] ingress-nginx]# ll
总用量 476
[[email protected] ingress-nginx]# ll
总用量 28
-rw-r--r-- 1 root root  134 4月   1 17:19 configmap.yaml #configmap用于为nginx从外部注入配置的
-rw-r--r-- 1 root root 1216 4月   1 17:20 default-backend.yaml   #配置默认后端服务
-rw-r--r-- 1 root root   68 4月   1 17:19 namespace.yaml #创建独立的名称空间
-rw-r--r-- 1 root root 2390 4月   1 17:19 rbac.yaml  #rbac用于集群角色授权
-rw-r--r-- 1 root root   94 4月   1 17:19 tcp-services-configmap.yaml
-rw-r--r-- 1 root root   94 4月   1 17:20 udp-services-configmap.yaml
-rw-r--r-- 1 root root 2174 4月   1 17:20 with-rbac.yaml

创建ingress-nginx名称空间

[[email protected] ingress-nginx]# cat namespace.yaml
---
apiVersion: v1
kind: Namespace
metadata:
  name: ingress-nginx

[[email protected] ingress-nginx]# kubectl apply -f namespace.yaml
namespace/ingress-nginx created

创建ingress controller的pod

#由于国内网络防火墙问题导致无法正常拉取k8s.grc.io仓库中拉取所需镜像文件,需要修改配置文件,修改镜像地址
[[email protected] ingress-nginx]# vim default-backend.yaml
        #image: gcr.io/google_containers/defaultbackend:1.4
        image: xiaobai20201/defaultbackend-amd64:1.5

[[email protected] ingress-nginx]# kubectl apply -f .
configmap/nginx-configuration created
deployment.extensions/default-http-backend created
service/default-http-backend created
namespace/ingress-nginx unchanged
serviceaccount/nginx-ingress-serviceaccount created
clusterrole.rbac.authorization.k8s.io/nginx-ingress-clusterrole created
role.rbac.authorization.k8s.io/nginx-ingress-role created
rolebinding.rbac.authorization.k8s.io/nginx-ingress-role-nisa-binding created
clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-clusterrole-nisa-binding created
configmap/tcp-services created
configmap/udp-services created
deployment.extensions/nginx-ingress-controller created

查看结果

[[email protected] ingress-nginx]# kubectl get pod -n ingress-nginx
NAME                                        READY   STATUS    RESTARTS   AGE
default-http-backend-788bdcf46f-7b5ds       1/1     Running   0          24s
nginx-ingress-controller-7db86988c8-jmv72   1/1     Running   0          3m50s

配置ingress后端服务

查看配置清单:

[[email protected] ~]# kubectl explain ingress.spec.
KIND:     Ingress
VERSION:  extensions/v1beta1

RESOURCE: spec <Object>

DESCRIPTION:
     Spec is the desired state of the Ingress. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

     IngressSpec describes the Ingress the user wishes to exist.

FIELDS:
   backend      <Object>   #定义后端主机

   rules        <[]Object>  #定义规则

   tls  <[]Object>

部署:

[[email protected] ingress-nginx]# cd ../ && mkdir ingress  &&  cd ingress
[[email protected] ingress]# vim deploy-demo.yaml
apiVersion: v1
kind: Service
metadata:
  name: myapp
  namespace: default
spec:
  selector:
    app: myapp
    release: canary
  ports:
  - name: http
    targetPort: 80
    port: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-backend-pod
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      labels:
        app: myapp
        release: canary
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v2
        ports:
        - name: http
          containerPort: 80

查看部署结果

[[email protected] ingress]# kubectl get pods,svc
NAME                                     READY   STATUS    RESTARTS   AGE
pod/filebeat-ds-h8rwk                    1/1     Running   0          18h
pod/filebeat-ds-kzhxw                    1/1     Running   0          18h
pod/myapp-backend-pod-6b56d98b6b-2dh5h   1/1     Running   0          78s
pod/myapp-backend-pod-6b56d98b6b-hwzws   1/1     Running   0          78s
pod/myapp-backend-pod-6b56d98b6b-ztwn2   1/1     Running   0          78s
pod/readiness-httpget-pod                1/1     Running   0          3d16h

NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/kubernetes       ClusterIP   10.96.0.1       <none>        443/TCP    5d16h
service/myapp            ClusterIP   10.100.41.152   <none>        80/TCP     7m47s
service/myapp-headless   ClusterIP   None            <none>        80/TCP     16h

部署ingress-nginx service

通过ingress-controller对外提供服务,现在还需要手动给ingress-controller建立一个service,接收集群外部流量。
下载ingress-controller的yaml文件

[[email protected] ingress]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.17.1/deploy/provider/baremetal/service-nodeport.yaml

[[email protected] ingress]# vim service-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    targetPort: 80
    protocol: TCP
    nodePort: 31111 #默认是随机端口,此处指定
  - name: https
    port: 443
    targetPort: 443
    protocol: TCP
    nodePort: 31443  #默认是随机端口,此处指定
  selector:
    app: ingress-nginx

查看部署结果

[[email protected] ingress]# kubectl apply -f service-nodeport.yaml
service/ingress-nginx created
[[email protected] ingress]# kubectl get svc -n ingress-nginx
NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
default-http-backend   ClusterIP   10.98.233.231    <none>        80/TCP                       33m
ingress-nginx          NodePort    10.103.142.142   <none>        80:31111/TCP,443:31443/TCP   8s

此时尝试访问10.0.0.10:31111 应该是404,因为调度器工作正常,但是后端服务还没有关联

部署Ingress

编写清单

[[email protected] ingress]# vim ingress-myapp.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myapp-ingress  #ingress的名称
  namespace: default  #所属名称空间
  annotations: #注解信息
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:  #定义后端转发的规则
  - host: myapp.white.com  #通过域名进行转发
    http:
      paths:
      - path:   #配置访问路径,如果通过url进行转发,需要修改;空默认为访问的路径为根"/"
        backend:  #配置后端服务
          serviceName: myapp
          servicePort: 80

创建后查看结果:

[[email protected] ingress]# kubectl apply -f ingress-myapp.yaml
ingress.extensions/myapp-ingress created
[[email protected] ingress]# kubectl get ingress
NAME            HOSTS             ADDRESS   PORTS   AGE
myapp-ingress   myapp.white.com             80      12s

查看myapp-ingress的详细信息

[[email protected] ingress]# kubectl describe ingress myapp-ingress
Name:             myapp-ingress
Namespace:        default
Address:
Default backend:  default-http-backend:80 (<none>)
Rules:
  Host             Path  Backends
  ----             ----  --------
  myapp.white.com
                      myapp:80 (<none>)
Annotations:
  kubectl.kubernetes.io/last-applied-configuration:  {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"kubernetes.io/ingress.class":"nginx"},"name":"myapp-ingress","namespace":"default"},"spec":{"rules":[{"host":"myapp.white.com","http":{"paths":[{"backend":{"serviceName":"myapp","servicePort":80},"path":null}]}}]}}

  kubernetes.io/ingress.class:  nginx
Events:
  Type    Reason  Age   From                      Message
  ----    ------  ----  ----                      -------
  Normal  CREATE  62s   nginx-ingress-controller  Ingress default/myapp-ingress
[[email protected] ingress]# 

进入nginx-ingress-controller进行查看是否注入了nginx的配置

[[email protected] ingress]# kubectl get pods -n ingress-nginx
NAME                                        READY   STATUS    RESTARTS   AGE
default-http-backend-788bdcf46f-7b5ds       1/1     Running   0          41m
nginx-ingress-controller-7db86988c8-jmv72   1/1     Running   0          45m

[[email protected] ingress]# kubectl exec -it  nginx-ingress-controller-7db86988c8-jmv72 -n ingress-nginx -- /bin/sh
$ cat  nginx.conf
......
        upstream default-myapp-80 { #自动配置负载均衡到后端pod
                least_conn;

                keepalive 32;

                server 10.244.1.44:80 max_fails=0 fail_timeout=0;
                server 10.244.2.49:80 max_fails=0 fail_timeout=0;
                server 10.244.2.48:80 max_fails=0 fail_timeout=0;

        }
......
        ## start server myapp.white.com
        server {
                server_name myapp.white.com ;

                listen 80;

                listen [::]:80;

                set $proxy_upstream_name "-";

                location / {

                        set $namespace      "default";
                        set $ingress_name   "myapp-ingress";
                        set $service_name   "myapp";
                        set $service_port   "80";
                        set $location_path  "/";

                        rewrite_by_lua_block {

                        }
...

修改本地host文件 访问
10.0.0.10 master myapp.white.com
10.0.0.11 node01 myapp.white.com
10.0.0.12 node02 myapp.white.com

增加tomcat服务

编写清单

[[email protected] ingress]# vim tomcat-deploy.yaml
apiVersion: v1
kind: Service
metadata:
  name: tomcat
  namespace: default
spec:
  selector:
    app: tomcat
    release: canary
  ports:
  - name: http
    targetPort: 8080
    port: 8080
  - name: ajp
    targetPort: 8009
    port: 8009

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deploy
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: tomcat
      release: canary
  template:
    metadata:
      labels:
        app: tomcat
        release: canary
    spec:
      containers:
      - name: tomcat
        image: tomcat:8.5-alpine
        ports:
        - name: http
          containerPort: 8080
        - name: ajp
          containerPort: 8009

编写tomcat的ingress规则,并创建ingress资源

[[email protected] ingress]# vim ingress-tomcat.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: tomcat.white.com
    http:
      paths:
      - path:
        backend:
          serviceName: tomcat
          servicePort: 8080

执行

[[email protected] ingress]# kubectl apply -f tomcat-deploy.yaml 

[[email protected] ingress]# kubectl get pods
NAME                                 READY   STATUS    RESTARTS   AGE
filebeat-ds-h8rwk                    1/1     Running   0          19h
filebeat-ds-kzhxw                    1/1     Running   0          19h
myapp-backend-pod-6b56d98b6b-2dh5h   1/1     Running   0          62m
myapp-backend-pod-6b56d98b6b-hwzws   1/1     Running   0          62m
myapp-backend-pod-6b56d98b6b-ztwn2   1/1     Running   0          62m
readiness-httpget-pod                1/1     Running   0          3d17h
tomcat-deploy-5f554cd88d-7gzc7       1/1     Running   0          44s
tomcat-deploy-5f554cd88d-c42t6       1/1     Running   0          44s
tomcat-deploy-5f554cd88d-qhc4j       1/1     Running   0          44s

[[email protected] ingress]# kubectl get svc
NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)             AGE
kubernetes       ClusterIP   10.96.0.1       <none>        443/TCP             5d17h
myapp            ClusterIP   10.100.41.152   <none>        80/TCP              70m
myapp-headless   ClusterIP   None            <none>        80/TCP              17h
tomcat           ClusterIP   10.107.88.118   <none>        8080/TCP,8009/TCP   3m4s

查看tomcat-deploy是否监听8080和8009

[[email protected] ingress]# kubectl exec -it tomcat-deploy-5f554cd88d-7gzc7 -- netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:8005          0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8009            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN   

创建ingress资源

[[email protected] ingress]# kubectl apply -f ingress-tomcat.yaml
ingress.extensions/tomcat-ingress created
[[email protected] ingress]# kubectl get ingress
NAME             HOSTS              ADDRESS   PORTS   AGE
myapp-ingress    myapp.white.com              80      45m
tomcat-ingress   tomcat.white.com             80      5s

查看tomcat-ingress详细信息

[[email protected] ingress]# kubectl describe ingress tomcat-ingress
Name:             tomcat-ingress
Namespace:        default
Address:
Default backend:  default-http-backend:80 (<none>)
Rules:
  Host              Path  Backends
  ----              ----  --------
  tomcat.white.com
                       tomcat:8080 (<none>)
Annotations:
  kubectl.kubernetes.io/last-applied-configuration:  {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"kubernetes.io/ingress.class":"nginx"},"name":"tomcat-ingress","namespace":"default"},"spec":{"rules":[{"host":"tomcat.white.com","http":{"paths":[{"backend":{"serviceName":"tomcat","servicePort":8080},"path":null}]}}]}}

  kubernetes.io/ingress.class:  nginx
Events:
  Type    Reason  Age   From                      Message
  ----    ------  ----  ----                      -------
  Normal  CREATE  71s   nginx-ingress-controller  Ingress default/tomcat-ingress

修改本地host文件映射后测试访问

10.0.0.10 master myapp.white.com tomcat.white.com
10.0.0.11 node01 myapp.white.com tomcat.white.com
10.0.0.12 node02 myapp.white.com tomcat.white.com

总结

从前面的部署过程中,可以再次进行总结部署的流程如下:

  1. 下载Ingress-controller相关的YAML文件,并给Ingress-controller创建独立的名称空间;
  2. 部署后端的服务,如myapp,并通过service进行暴露;
  3. 部署Ingress-controller的service,以实现接入集群外部流量;
  4. 部署Ingress,进行定义规则,使Ingress-controller和后端服务的Pod组进行关联。

构建TLS站点

准备证书

[[email protected] ingress]# openssl genrsa -out tls.key 2048
Generating RSA private key, 2048 bit long modulus
......+++
.....................................................+++
e is 65537 (0x10001)
[[email protected] ingress]# openssl req -new -x509 -key tls.key -out tls.crt -subj /C=CN/ST=Guangdong/L=Zhongshan/O=DevOps/CN=tomcat.white.com
[[email protected] ingress]# ls
deploy-demo.yaml    ingress-tomcat.yaml    tls.crt  tomcat-deploy.yaml
ingress-myapp.yaml  service-nodeport.yaml  tls.key

此时生成的证书不能直接被nginx的pod调用,需要转换成secret(领一个标准的kubernetes对象)
生成secret

[[email protected] ingress]# kubectl create secret tls tomcat-ingress-secret --cert=tls.crt --key=tls.key
secret/tomcat-ingress-secret created
[[email protected] ingress]# kubectl get secret
NAME                    TYPE                                  DATA   AGE
default-token-dqd2f     kubernetes.io/service-account-token   3      5d18h
tomcat-ingress-secret   kubernetes.io/tls                     2      11s
[[email protected] ingress]# kubectl describe secret tomcat-ingress-secret
Name:         tomcat-ingress-secret
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  kubernetes.io/tls

Data
====
tls.crt:  1302 bytes
tls.key:  1679 bytes

创建ingress

[[email protected] ingress]# vim ingress-tomcat-tls.yaml
[[email protected] ~]# kubectl explain ingress.spec.tls.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat-ingress-tls
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  tls:
  - hosts:
    - tomcat.white.com
    secretName: tomcat-ingress-secret
  rules:
  - host: tomcat.white.com
    http:
      paths:
      - path:
        backend:
          serviceName: tomcat
          servicePort: 8080

[[email protected] ingress]# kubectl apply -f ingress-tomcat-tls.yaml
ingress.extensions/tomcat-ingress-tls created
[[email protected] ingress]# kubectl get ingress
NAME                 HOSTS              ADDRESS   PORTS     AGE
myapp-ingress        myapp.white.com              80        61m
tomcat-ingress       tomcat.white.com             80        16m
tomcat-ingress-tls   tomcat.white.com             80, 443   25s

#查看描述
[[email protected] ingress]# kubectl describe ingress  tomcat-ingress-tls
Name:             tomcat-ingress-tls
Namespace:        default
Address:
Default backend:  default-http-backend:80 (<none>)
TLS:
  tomcat-ingress-secret terminates tomcat.white.com
Rules:
  Host              Path  Backends
  ----              ----  --------
  tomcat.white.com
                       tomcat:8080 (<none>)
Annotations:
  kubectl.kubernetes.io/last-applied-configuration:  {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"kubernetes.io/ingress.class":"nginx"},"name":"tomcat-ingress-tls","namespace":"default"},"spec":{"rules":[{"host":"tomcat.white.com","http":{"paths":[{"backend":{"serviceName":"tomcat","servicePort":8080},"path":null}]}}],"tls":[{"hosts":["tomcat.white.com"],"secretName":"tomcat-ingress-secret"}]}}

  kubernetes.io/ingress.class:  nginx
Events:
  Type    Reason  Age   From                      Message
  ----    ------  ----  ----                      -------
  Normal  CREATE  107s  nginx-ingress-controller  Ingress default/tomcat-ingress-tls

进入pod内查看

[[email protected] ingress]# kubectl exec -it nginx-ingress-controller-7db86988c8-jmv72 -n ingress-nginx -- /bin/sh
$ cat nginx.conf
······
upstream default-tomcat-8080 {
                least_conn;

                keepalive 32;

                server 10.244.1.45:8080 max_fails=0 fail_timeout=0;
                server 10.244.2.51:8080 max_fails=0 fail_timeout=0;
                server 10.244.2.50:8080 max_fails=0 fail_timeout=0;

        }
······
        ## start server _
        server {
                server_name _ ;

                listen 80 default_server  backlog=511;

                listen [::]:80 default_server  backlog=511;

                set $proxy_upstream_name "-";

                listen 443  default_server  backlog=511 ssl http2;

                listen [::]:443  default_server  backlog=511 ssl http2;

                # PEM sha: 07ee66d47cf4e5ef25baa6f91d62296e05243cfe
                ssl_certificate                         /etc/ingress-controller/ssl/default-fake-certificate.pem;
                ssl_certificate_key                     /etc/ingress-controller/ssl/default-fake-certificate.pem;
......

客户端访问31443查看

由于证书问题,提示不安全,但是可以访问https 443端口。

参考资料

https://www.cnblogs.com/linuxk
马永亮. Kubernetes进阶实战 (云计算与虚拟化技术丛书)
Kubernetes-handbook-jimmysong-20181218

原文地址:https://www.cnblogs.com/wlbl/p/10652908.html

时间: 2024-10-08 22:34:35

Kubernetes之Ingress和Ingress Controller的相关文章

Kubernetes之(十一)Ingress和Ingress Controller

目录 Kubernetes之(十一)Ingress和Ingress Controller 概念 Ingress资源类型 单Service资源型Ingress Ingress Nginx部署 部署Ingress controller 配置ingress后端服务 部署ingress-nginx service 部署Ingress 增加tomcat服务 总结 构建TLS站点 Kubernetes之(十一)Ingress和Ingress Controller 概念 通常情况下,service和pod的I

kubernetes集群traefik ingress实现同一命名空间不同微服务模块的访问

背景:kubernetes集群traefik ingress实现同一命名空间不同微服务模块的访问1.安装traefik ingresscat > traefik-ingress.yaml <<EOF kind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1metadata:name: traefik-ingress-controllerrules: apiGroups: ""resources: pods se

Kubernetes中安装traefik ingress

Kubernetes中安装traefik ingress # 下载配置清单 wget https://github.com/containous/traefik/tree/v1.7/examples/k8s # 链接中以traefik-开头的文件有3个,都可以见名知意,其中traefik-deployment.yaml我们这里没有用到 # traefik-deployment.yaml跟traefik-ds.yaml二者选其一即可,由于底下的配置是根据traefik-ds.yaml来的所以建议使

七,ingress及ingress cluster

目录 Service 类型 namespace 名称空间 Ingress Controller Ingress Ingress-nginx 进行测试 创建对应的后端Pod和Service 创建 Ingress 查看 Ingress-controller 对应的Pod配置信息 访问测试 模拟测试 Ingress 后端 Tomcat 访问 创建 tomcat 的 Pod 和Service 创建 tomcat-ingress 测试访问 tomcat 模拟测试 Https 自签SSL证书 创建secre

Kubernetes学习之路(十五)之Ingress和Ingress Controller

1.部署Ingress controller (1)下载ingress相关的yaml [[email protected] ~]# mkdir ingress-nginx [[email protected] ~]# cd ingress-nginx/ [[email protected] ingress-nginx]# ll total 0 [[email protected] ingress-nginx]# for file in namespace.yaml configmap.yaml

12、kubernetes之ingress及Ingress Controller

一.概念 ClusterIP:例如svc所分配的ip地址 NodePort:k8s集群物理机机通信地址,client --> NodeIP:NodePort --> ClusterIP:ServicePort --> PodIP:containerPort No ClusterIP:Headless Service ,ServiceName --> PodIP Ingress:负责7层调度 二.ingress-nginx部署 # cat mandatory.yaml apiVers

Kubernetes 学习11 kubernetes ingress及ingress controller

一.上集回顾 1.Service 3种模型:userspace,iptables,ipvs 2.Service类型 ClusterIP,NodePort NodePort:client -> NodeIP:NodePort -> ClusterIP:ServicePort -> PodIP:containerPort LoadBalancer ExternelName No ClusterIP: Hedless Service serviceName -> PodIP 二.ingr

ingress及Ingress Controller

补充 启用ipvs代替iptables做service # vim /etc/sysconfig/kubelet KUBE_PROXY_MODE=ipvs 注意:需要装入ip_vs, ip_vs_rr, ip_vs_wrr, ip_vs_sh, nf_conntrack_ipv4等模块 一.Ingress Controller 可选择的ingress controller: Nginx Envoy Traefik externalLB --> Service --> IngressContro

11. Ingress及Ingress Controller(主nginx ingress controller)

11. Ingress,Ingress Controller拥有七层代理调度能力 什么是Ingress: Ingress是授权入站连接到达集群服务的规则集合 Ingress是一个Kubernetes资源,允许您为在Kubernetes上运行的应用程序配置HTTP负载均衡器,由一个或多个服务代表.这样的负载平衡器是将这些应用程序交付给Kubernetes集群之外的客户端所必需的. Ingress资源支持以下功能:1 基于内容的路由: 基于主机的路由.例如,将具有主机头的请求路由foo.exampl