码迷,mamicode.com
首页 > Web开发 > 详细

kubernetes 之ingress

时间:2018-02-06 10:19:27      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:roo   seconds   info   pac   default   ali   通过   本机   bubuko   

1、Ingress组件介绍

  • Ingress解决的是新的服务加入后,域名和服务的对应问题,基本上是一个ingress的对象,通过yaml进行创建和更新进行加载。
  • Ingress Controller是将Ingress这种变化生成一段Nginx的配置,然后将这个配置通过Kubernetes API写到Nginx的Pod中,然后reload.

2、生成一个默认的后端,如果遇到解析不到的URL就转发到默认后端页面

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: default-http-backend
  labels:
    app: default-http-backend
  namespace: ingress-nginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: default-http-backend
    spec:
      terminationGracePeriodSeconds: 60
      containers:
      - name: default-http-backend
        # Any image is permissable as long as:
        # 1. It serves a 404 page at /
        # 2. It serves 200 on a /healthz endpoint
        image: registry.cn-hangzhou.aliyuncs.com/google_containers/defaultbackend:1.4
        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: default-http-backend
spec:
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: default-http-backend

[root@manager Ingress]# kubectl create -f default-backend.yaml
deployment "default-http-backend" created
service "default-http-backend" created
[root@manager Ingress]#

 

[root@manager Ingress]# kubectl get pods -n ingress-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE
default-http-backend-5d8887b7f-l8r29 1/1 Running 0 28s 10.0.91.6 192.168.10.221

 

 

3、部署Ingress Controller

[root@manager Ingress]# cat nginx-ingress-controller.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-ingress-lb
  labels:
    name: nginx-ingress-lb
  namespace: ingress-nginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: nginx-ingress-lb
      annotations:
        prometheus.io/port: 10254
        prometheus.io/scrape: true
    spec:
      terminationGracePeriodSeconds: 60
      hostNetwork: true
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:0.8.3
        name: nginx-ingress-lb
        readinessProbe:
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
        livenessProbe:
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
          initialDelaySeconds: 10
          timeoutSeconds: 1
        ports:
        - containerPort: 80
          hostPort: 80
        - containerPort: 443
          hostPort: 443
        env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: KUBERNETES_MASTER
            value: http://192.168.10.220:8080
        args:
        - /nginx-ingress-controller
        - --default-backend-service=$(POD_NAMESPACE)/default-http-backend
#        - --apiserver-host=http://192.168.10.220:8080

[root@manager Ingress]# kubectl get pod -n ingress-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE
default-http-backend-5d8887b7f-l8r29 1/1 Running 0 21m 10.0.91.6 192.168.10.221
nginx-ingress-lb-gj4sg 1/1 Running 0 1m 192.168.10.221 192.168.10.221

 

技术分享图片

 

4、本实验通过nginx 容器,这里通过ingress 做入口访问svc

[root@manager ~]# cat nginx-deployment.yaml 
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: ingress-nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.10
        ports:
        - containerPort: 80

[root@manager ~]# kubectl create -f nginx-deployment.yaml

 

[root@manager ~]# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: ingress-nginx
labels:
app: nginx
spec:
ports:
- port: 88
targetPort: 80
selector:
app: nginx

技术分享图片

 

 


[root@manager ~]# kubectl create -f nginx-service.yaml

 

[root@manager ~]# cat ingress-nginx.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: nginxservice
namespace: ingress-nginx
spec:
rules:
- host: test.io
http:
paths:
- path: /
backend:
serviceName: nginx-service
servicePort: 80
- path: /
# backend:
# serviceName: kubernetes-dashboard
# servicePort: 80
[root@manager ~]# kubectl create -f ingress-nginx.yaml

 

技术分享图片

 

 

本机写host 文件进行访问,外部写host 文件,指向node ip 即可

技术分享图片

 

kubernetes 之ingress

标签:roo   seconds   info   pac   default   ali   通过   本机   bubuko   

原文地址:https://www.cnblogs.com/hellojackyleon/p/8420911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!