ingress-nginx部署及配置https

1、Ingress

在Kubernetes中,服务和Pod的IP地址仅可以在集群网络内部使用,对于集群外的应用是不可见的。为了使外部的应用能够访问集群内的服务,在Kubernetes中可以通过NodePort和LoadBalancer这两种类型的服务,或者使用Ingress。Ingress本质是通过http代理服务器将外部的http请求转发到集群内部的后端服务。

2、ingress-nginx部署

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
  • with-rbac.yaml:有应用rbac的nginx-ingress-controller组件
  • service-nodeport:指定nginx的80端口和443端口

下载部署文件,整理的github

for file in configmap.yaml default-backend.yaml namespace.yaml rbac.yaml tcp-services-configmap.yaml udp-services-configmap.yaml with-rbac.yaml service-nodeport.yaml;do
    wget https://raw.githubusercontent.com/fungitive/kubernetes/master/ingress-nginx/$file
done

创建名称空间

kubectl apply -f namespace.yaml

部署其他

kubectl apply -f .

部署成功如下

[root@master ~]# kubectl get pods -n ingress-nginx
NAME                                        READY     STATUS    RESTARTS   AGE
default-http-backend-8477465f57-fzgr8       1/1       Running   0          1d
nginx-ingress-controller-6bd7c597cb-hwgwz   1/1       Running   0          1d

[root@master ~]# kubectl get svc -n ingress-nginx
NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
default-http-backend   ClusterIP   10.97.181.78            80/TCP                       1d
ingress-nginx          NodePort    10.109.51.251           80:30080/TCP,443:30643/TCP   1d

部署一个nginx应用,使用ingress-nginx服务

vi nginx-test.yaml

apiVersion: v1
kind: Service
metadata:
  name: test-ingress
  namespace: default
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: test-ingress
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: test-ingress
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: test-ingress
    spec:
      containers:
      - image: nginx:latest
        imagePullPolicy: IfNotPresent
        name: test-nginx
        ports:
        - containerPort: 80

vi nginx-ingress-yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: feiutest.cn
    http:
      paths:
      - path:
        backend:
          serviceName: test-ingress
          servicePort: 80
  • host指虚拟出来的域名
  • path:/console匹配后面的应用路径
  • servicePort主要是定义服务的时候的端口,不是NodePort.
  • path:/ 匹配后面应用的路径

创建成功,在pods所在node上解析域名

vi /etc/hosts

192.168.0.22 feiutest.cn

测试

[root@master demo]# curl http://feiutest.cn:30080
this is test ingress-nginx

3、https配置

第一步:制作自签证书

[root@master demo]# openssl genrsa -out tls.key 2048

[root@master demo]# openssl req -new -x509 -key tls.key -out tls.crt -subj /C=CN/ST=Guangdong/L=Guangzhou/O=devops/CN=feiutest.cn

生成两个文件:

[root@master demo]# ls
tls.crt  tls.key

第二步:创建secret secret介绍

[root@master demo]# kubectl create secret tls nginx-test --cert=tls.crt --key=tls.key

[root@master demo]# kubectl get secret
NAME                     TYPE                                  DATA      AGE
nginx-test               kubernetes.io/tls                     2         17s

第三步:修改ingress

vi nginx-ingress-yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: feiutest.cn
    http:
      paths:
      - path:
        backend:
          serviceName: test-ingress
          servicePort: 80
  tls:
  - hosts:
    - feiutest.cn
    secretName: nginx-test

kubectl apply -f nginx-ingress-yaml

测试访问

https://feiutest.cn:30643

ingress-nginx部署及配置https_第1张图片

正常访问,成功了!

参考

https://kubernetes.io/docs/concepts/services-networking/ingress/

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

你可能感兴趣的:(Kubernetes)