k8s Ingress 服务部署方式

k8s Ingress 服务部署方式

通常情况下,service和pod仅可在集群内部网络中通过IP地址访问。所有到达边界路由器的流量或被丢弃或被转发到其他地方

官网对 Ingress 的定义为管理对外服务到集群内服务之间规则的集合,通俗点讲就是它定义规则来允许进入集群的请求被转发到集群中对应服务上,从来实现服务暴漏。Ingress 能把集群内 Service 配置成外网能够访问的 URL,流量负载均衡,终止SSL,提供基于域名访问的虚拟主机等等。

Ingress

Ingress 使用开源的反向代理负载均衡器来实现对外暴漏服务,比如 Nginx、Apache、Haproxy等。Nginx Ingress 一般有三个组件组成:

Nginx 反向代理负载均衡器

Ingress Controller

Ingress Controller 可以理解为控制器,它通过不断的跟 Kubernetes API 交互,实时获取后端 Service、Pod 等的变化,比如新增、删除等,然后结合 Ingress 定义的规则生成配置,然后动态更新上边的 Nginx 负载均衡器,并刷新使配置生效,来达到服务自动发现的作用。

Ingress

Ingress 则是定义规则,通过它定义某个域名的请求过来之后转发到集群中指定的 Service。它可以通过 Yaml 文件定义,可以给一个或多个 Service 定义一个或多个 Ingress 规则。

以上三者有机的协调配合起来,就可以完成 Kubernetes 集群服务的暴漏

Kubernetes 使用 Nginx Ingress 暴漏tomcat服务,前提我们需要有一个正常运行的集群服务,根据之前创建的的kubernetes集群,进行测试

官网地址:https://kubernetes.io/docs/concepts/services-networking/ingress/

    https://github.com/kubernetes/ingress-nginx/

使用到的镜像

gcr.io/google_containers/defaultbackend:1.4

quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.18.0

ingress-nginx组件有几个部分组成:

configmap.yaml:提供configmap可以在线更行nginx的配置

default-backend.yaml:提供一个缺省的后台错误页面 404

namespace.yaml:创建一个独立的命名空间 ingress-nginx

rbac.yaml:创建对应的role rolebinding 用于rbac

tcp-services-configmap.yaml:修改L4负载均衡配置的configmap

udp-services-configmap.yaml:修改L4负载均衡配置的configmap

nginx-ingress-controller.yaml:有应用rbac的nginx-ingress-controller组件

YAML配置文件

namespace.yaml

apiVersion: v1

kind: Namespace

metadata:

  name: ingress-nginx

default-backend.yaml

apiVersion: extensions/v1beta1

kind: Deployment

metadata:

  name: default-http-backend

  labels:

   app.kubernetes.io/name: default-http-backend

   app.kubernetes.io/part-of: ingress-nginx

  namespace: ingress-nginx

spec:

  replicas: 1

  selector:

   matchLabels:

     app.kubernetes.io/name: default-http-backend

     app.kubernetes.io/part-of: ingress-nginx

  template:

   metadata:

     labels:

       app.kubernetes.io/name: default-http-backend

       app.kubernetes.io/part-of: ingress-nginx

   spec:

     terminationGracePeriodSeconds: 60

     containers:

       - name: default-http-backend

         image: googlecontainer/defaultbackend-amd64:1.4

         #image:

         livenessProbe:

           httpGet:

             path: /healthz

             port: 8080

             scheme: HTTP

           initialDelaySeconds: 30

           timeoutSeconds: 5

         ports:

           - containerPort: 8080

         resources:

           limits:

             cpu: 10m

             memory: 20Mi

           requests:

             cpu: 10m

             memory: 20Mi

---

apiVersion: v1

kind: Service

metadata:

  name: default-http-backend

  namespace: ingress-nginx

  labels:

   app.kubernetes.io/name: default-http-backend

   app.kubernetes.io/part-of: ingress-nginx

spec:

  ports:

   - port: 80

     targetPort: 8080

  selector:

   app.kubernetes.io/name: default-http-backend

   app.kubernetes.io/part-of: ingress-nginx

---

configmap.yaml

kind: ConfigMap

apiVersion: v1

metadata:

  name: nginx-configuration

  namespace: ingress-nginx

  labels:

   app: ingress-nginx

tcp-services-configmap.yaml

kind: ConfigMap

apiVersion: v1

metadata:

  name: tcp-services

  namespace: ingress-nginx

udp-services-configmap.yaml

kind: ConfigMap

apiVersion: v1

metadata:

  name: udp-services

  namespace: ingress-nginx

rbac.yaml

apiVersion: v1

kind: ServiceAccount

metadata:

  name: nginx-ingress-serviceaccount

  namespace: ingress-nginx

  labels:

   app.kubernetes.io/name: ingress-nginx

   app.kubernetes.io/part-of: ingress-nginx

---

apiVersion: rbac.authorization.k8s.io/v1beta1

kind: ClusterRole

metadata:

  name: nginx-ingress-clusterrole

  labels:

   app.kubernetes.io/name: ingress-nginx

   app.kubernetes.io/part-of: ingress-nginx

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-role

  namespace: ingress-nginx

  labels:

   app.kubernetes.io/name: ingress-nginx

   app.kubernetes.io/part-of: ingress-nginx

rules:

  - apiGroups:

     - ""

   resources:

     - configmaps

     - pods

     - secrets

     - namespaces

   verbs:

     - get

  - apiGroups:

     - ""

   resources:

     - configmaps

   resourceNames:

     # Defaults to "<election-id>-<ingress-class>"

     # Here: "<ingress-controller-leader>-<nginx>"

     # This has to be adapted if you change either parameter

     # when launching the nginx-ingress-controller.

     - "ingress-controller-leader-nginx"

   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-role-nisa-binding

  namespace: ingress-nginx

  labels:

   app.kubernetes.io/name: ingress-nginx

   app.kubernetes.io/part-of: ingress-nginx

roleRef:

  apiGroup: rbac.authorization.k8s.io

  kind: Role

  name: nginx-ingress-role

subjects:

  - kind: ServiceAccount

   name: nginx-ingress-serviceaccount

   namespace: ingress-nginx

---

apiVersion: rbac.authorization.k8s.io/v1beta1

kind: ClusterRoleBinding

metadata:

  name: nginx-ingress-clusterrole-nisa-binding

  labels:

   app.kubernetes.io/name: ingress-nginx

   app.kubernetes.io/part-of: ingress-nginx

roleRef:

  apiGroup: rbac.authorization.k8s.io

  kind: ClusterRole

  name: nginx-ingress-clusterrole

subjects:

  - kind: ServiceAccount

   name: nginx-ingress-serviceaccount

   namespace: ingress-nginx

---

nginx-ingress-controller.yaml

apiVersion: extensions/v1beta1

kind: DaemonSet

metadata:

  name: nginx-ingress-controller

  namespace: ingress-nginx

spec:

  selector:

   matchLabels:

     app: ingress-nginx

  template:

   metadata:

     labels:

       app: ingress-nginx

     annotations:

       prometheus.io/port: '10254'

       prometheus.io/scrape: 'true'

   spec:

     serviceAccountName: nginx-ingress-serviceaccount

     hostNetwork: true

     containers:

       - name: nginx-ingress-controller

         image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.18.0

         args:

           - /nginx-ingress-controller

           - --default-backend-service=$(POD_NAMESPACE)/default-http-backend

           - --configmap=$(POD_NAMESPACE)/nginx-configuration

           - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services

           - --udp-services-configmap=$(POD_NAMESPACE)/udp-services

           - --annotations-prefix=nginx.ingress.kubernetes.io

         env:

           - name: POD_NAME

             valueFrom:

               fieldRef:

                 fieldPath: metadata.name

           - name: POD_NAMESPACE

             valueFrom:

               fieldRef:

                 fieldPath: metadata.namespace

         ports:

         - name: http

           containerPort: 80

         - name: https

           containerPort: 443

         livenessProbe:

           failureThreshold: 3

           httpGet:

             path: /healthz

             port: 10254

             scheme: HTTP

           initialDelaySeconds: 10

           periodSeconds: 10

           successThreshold: 1

           timeoutSeconds: 1

         readinessProbe:

           failureThreshold: 3

           httpGet:

             path: /healthz

             port: 10254

             scheme: HTTP

           periodSeconds: 10

           successThreshold: 1

           timeoutSeconds: 1

     #nodeSelector:

       #custom/ingress-controller-ready: "true"

说明:

kind: DaemonSet:官方文档给的是deployment,replicate 为 1,这样将会在某一台节点上启动对应的nginx-ingress-controller pod。外部流量访问至该节点,由该节点负载分担至内部的service。测试环境考虑防止单点故障,改为DaemonSet,配合亲和性部署在制定节点上启动nginx-ingress-controller pod,确保有多个节点启动nginx-ingress-controller pod,后续将这些节点加入到外部硬件负载均衡组实现高可用性。

hostNetwork: true:添加该字段,暴露nginx-ingress-controller pod的服务端口(80)

nodeSelector: 加入亲和性部署,有custom/ingress-controller-ready label的才会部署该DaemonSet(未使用)

加载yaml文件

kubectl create -f .

查看pod创建情况

#kubectl get pod,svc,deployment -n ingress-nginx -o wide


在上面已经部署了 ingress-nginx,下面我们部署一个tomcat作为对外暴露的服务

使用ingress部署一个对外暴露的服务包含一个pod和一个service

创建tomcat service yaml

vi 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: 1

  selector:

   matchLabels:

     app: tomcat

     release: canary

  template:

   metadata:

     labels:

       app: tomcat

       release: canary

   spec:

     containers:

     - name: tomcat

       image: tomcat

       ports:

       - name: http

         containerPort: 8080

创建tomcat pod yaml文件

vi ingress-tomcat.yaml

复制下面内容:

apiVersion: extensions/v1beta1

kind: Ingress

metadata:

  name: ingress-tomcat

  namespace: default

  annotations:

   kubernetes.io/ingress.class: "nginx"

spec:

  rules:

  - host: k8s.master ##这个地方使用主机名或者域名,不能使用IP地址

   http:

     paths:

     - path:

       backend:

         serviceName: tomcat

         servicePort: 8080

然后执行:

kubectl apply -f tomcat-deploy.yaml

kubectl apply -f ingress-tomcat.yaml

服务启动以后就可以通过上面配置的主机名或者域名进行访问tomcat 了

http://k8s.master/


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272

推荐阅读更多精彩内容