淘先锋技术网

首页 1 2 3 4 5 6 7

Ingress-nginx:

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

官网地址:https://kubernetes.github.io/ingress-nginx/

github:ingress-nginx/charts/ingress-nginx at main · kubernetes/ingress-nginx · GitHub

ingress-nginx最新版本v1.0; 适用于 Kubernetes  版本 v1.19+ (包括 v1.19 );

Kubernetes-v1.22+ 需要使用 ingress-nginx>=1.0,因为 networking.k8s.io/v1beta 已经移除

Ingress-nginx部署:

 github上下载文件;

wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.0.0/deploy/static/provider/baremetal/deploy.yaml

kubectl apply -f ingress-nginx.yaml

查看ingress-nginx:

Completed 状态的是正常的,可以忽略。

[root@master01 ingress]#  kubectl get po -n ingress-nginx
NAME                                        READY   STATUS      RESTARTS   AGE
ingress-nginx-admission-create--1-99slc     0/1     Completed   0          152m
ingress-nginx-admission-patch--1-ks8nw      0/1     Completed   2          152m
ingress-nginx-controller-6b64bc6f47-z8csf   1/1     Running     0          152m

应用测试: 

1、创建 tomcat.yaml应用

apiVersion: v1
kind: Service
metadata:
  name: tomcat
  namespace: default
spec:
  selector:
    app: tomcat
    release: canary
  ports:
  - name: http
    port: 8080
    targetPort: 8080
  - name: ajp
    port: 8009
    targetPort: 8009

---
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: tomcat-deploy
spec:
  replicas: 2
  selector: 
    matchLabels:
      app: tomcat
      release: canary
  template:
    metadata:
      labels:
        app: tomcat
        release: canary
    spec:
      containers:
      - name: tomcat
        image: tomcat:7-alpine
        ports:
        - name: httpd
          containerPort: 8080
        - name: ajp
          containerPort: 8009
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: tomcat
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: tomcat.com
    http:
      paths:
      - path: "/"
        pathType: Prefix
        backend:
          service:
            name: tomcat
            port:
              number: 8080

[root@master01 /]# kubectl  apply  -f  tomcat.yaml   #部署tomcat应用

[root@master01 /]# kubectl get ing,svc,deploy
NAME                               CLASS    HOSTS        ADDRESS       PORTS   AGE
ingress.networking.k8s.io/tomcat   <none>   tomcat.com   192.168.7.5   80      49m

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)             AGE
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP             5d22h
service/tomcat       ClusterIP   10.110.140.14   <none>        8080/TCP,8009/TCP   155m

NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/tomcat-deploy   2/2     2            2           155m
[root@master01 /]# 

查看Ingress暴露的端口:

[root@master01 /]# kubectl get svc -n ingress-nginx
NAME                                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx-controller             NodePort    10.97.83.93    <none>        80:31720/TCP,443:32606/TCP   3h5m
ingress-nginx-controller-admission   ClusterIP   10.102.15.71   <none>        443/TCP      

在本地主机hosts文件中添加解析条目;

192.168.xx.xx tomcat.com

http://tomcat.com:31720/   #访问项目