ingress(SVC)

把端口对外暴露,通过ip+端口号进行访问(nodeport)

但是nodeport有一定的缺陷

1. 在每个节点上都会启动一个端口,访问时通过任何节点,通过节点ip+暴露端口号
2. 意味着每个端口只能使用一次,一个端口对应一个应用

ingress 能够弥补nodeport的缺陷

 

一. ingress代理访问流程

1. 客户端访问ingress代理的域名  
2. 根据访问的域名会访问到service
3. 再由SVC 然后在调度到节点(容器)

 

#项目地址

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

1. 创建服务pod并对外暴露端口

kubectl create deployment web --image=nginx:1.15
kubectl expose deployment web --port=80 --target-port=80 --type=NodePort

#查看
[root@k8s-master01 cfg]# kubectl get pod,svc
NAME                       READY   STATUS    RESTARTS   AGE
pod/web-7d9697b7f8-2bj6z   1/1     Running   0          8s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP        49d
service/web          NodePort    10.0.0.209   <none>        80:32461/TCP   8s

现在我们通过节点ip:32461访问,但是我们这里只是为了ingress做准备

 

2. 部署ingress-nginx

#deployment
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml

#获取svc
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml

添加配置

212     spec:
213       # wait up to five minutes for the drain of connections
213     hostNetwork: true       #对外暴露host网络  添加配置
214     terminationGracePeriodSeconds: 300

部署

#为了防止出错先下载镜像
docker pull quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0
#或者将镜像换成
#lizhenliang/nginx-ingress-controller:0.30.0


#部署
kubectl create -f mandatory.yaml
kubectl create -f service-nodeport.yaml



[root@k8s-master01 ~]# kubectl get pod,svc -n ingress-nginx
NAME                                           READY   STATUS    RESTARTS   AGE
pod/nginx-ingress-controller-766fb9f77-b9nsh   1/1     Running   0          5m1s

NAME                    TYPE       CLUSTER-IP   EXTERNAL-IP   PORT(S)                      AGE
service/ingress-nginx   NodePort   10.0.0.156   <none>        80:30211/TCP,443:31787/TCP   4m54s

 

 

3. 创建ingress规则

用于找到你要访问的pod

cat > ingress-http.yaml <<EOF
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: example-ingress
spec:
  rules:
  - host: www.test.com
    http:
      paths:      #可以匹配多个
      - path: /        
        backend:
          serviceName: web  #svc的名称
          servicePort: 80   #监听的端口号port
EOF


#部署
kubectl create -f ingress-http.yaml

查看运行节点

[root@k8s-master01 ~]# kubectl get pod -n ingress-nginx -o wide
NAME                                       READY   STATUS    RESTARTS   AGE   IP             NODE           NOMINATED NODE   READINESS GATES
nginx-ingress-controller-766fb9f77-b9nsh   1/1     Running   0          11m   192.168.1.20   192.168.1.20   <none>           <none>

#他作用到了192.168.1.20节点上

 

4.添加本地主机名解析

cat > /etc/hosts <<EOF
192.168.1.20 www.test.com
EOF

5. 访问测试

[root@k8s-master01 ~]# kubectl get ingress
NAME              CLASS    HOSTS          ADDRESS      PORTS   AGE
example-ingress   <none>   www.test.com   10.0.0.156   80      4m59s


[root@k8s-master01 ~]# curl www.test.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

 

我们访问成功了,但是我们要还检查一下他的代理区域是否正确,而不是其他服务影响

进入ingress-nginx容器检查配置,可以看到自动生成的反向代理区域

#查看配置
kubectl exec -it nginx-ingress-controller-766fb9f77-b9nsh cat -n ingress-nginx cat nginx.conf

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐