VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 网络工程 > Linux 教程 >
  • k8s之Service详解-Ingress使用

环境准备

搭建ingress环境

复制代码
#创建文件夹
[root@master ~]# mkdir ingress-controller
[root@master ~]# cd ingress-controller/
[root@master ingress-controller]# 

#获取ingress-nginx,本次案例使用的是0.30版本(不挂代理可能无法访问,这里我把yaml文件内容复制上来,需要的可以自行复制粘贴)
[root@master ingress-controller]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml
[root@master ingress-controller]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml
[root@master ingress-controller]# ls
mandatory.yaml service-nodeport.yaml
复制代码

mandatory.yaml

 View Code

 service-nodeport.yaml

 View Code
复制代码
#创建ingress-nginx
[root@master ingress-controller]# kubectl apply -f ./
namespace/ingress-nginx created
configmap/nginx-configuration created
configmap/tcp-services created
configmap/udp-services created
serviceaccount/nginx-ingress-serviceaccount created
clusterrole.rbac.authorization.k8s.io/nginx-ingress-clusterrole created
role.rbac.authorization.k8s.io/nginx-ingress-role created
rolebinding.rbac.authorization.k8s.io/nginx-ingress-role-nisa-binding created
clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-clusterrole-nisa-binding created
deployment.apps/nginx-ingress-controller created
limitrange/ingress-nginx created
service/ingress-nginx created

#查看ingress-nginx
[root@master ingress-controller]# kubectl get pod -n ingress-nginx
NAME                                        READY   STATUS    RESTARTS   AGE
nginx-ingress-controller-7f74f657bd-kzvrq   1/1     Running   0          2m7s

#查看service
[root@master ingress-controller]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx NodePort 10.106.160.9 <none> 80:32000/TCP,443:32421/TCP 5m48s
复制代码

准备service和pod

为了方便后续的实验,创建下图所示的模型

 

创建tomcat-nginx.yaml

 View Code

使用配置文件

复制代码
[root@master ingress-controller]# cd ..
[root@master ~]# vim tomcat-nginx.yaml
[root@master ~]# kubectl create -f tomcat-nginx.yaml 
deployment.apps/nginx-deployment created
deployment.apps/tomcat-deployment created
service/nginx-service created
service/tomcat-service created
[root@master ~]# kubectl get svc -n dev
NAME             TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
nginx-service    ClusterIP   None         <none>        80/TCP     20s
tomcat-service   ClusterIP   None         <none>        8080/TCP   20s
复制代码

Http代理

创建ingress-http.yaml

复制代码
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-http
  namespace: dev
spec:
  rules:
  - host: nginx.test.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-service
          servicePort: 80
  - host: tomcat.test.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-service
          servicePort: 8080
复制代码

使用配置文件

复制代码
[root@master ~]# vim ingress-http.yaml

[root@master ~]# kubectl create -f ingress-http.yaml 
ingress.extensions/ingress-http created

[root@master ~]# kubectl get ing ingress-http -n dev
NAME           HOSTS                            ADDRESS   PORTS   AGE
ingress-http   nginx.test.com,tomcat.test.com             80      13s

[root@master ~]# kubectl describe ing ingress-http -n dev
Name:             ingress-http
Namespace:        dev
Address:          10.106.160.9
Default backend:  default-http-backend:80 (<none>)
Rules:
  Host             Path  Backends
  ----             ----  --------
  nginx.test.com   
                   /   nginx-service:80 (10.244.1.33:80,10.244.2.13:80,10.244.2.16:80)
  tomcat.test.com  
                   /   tomcat-service:8080 (10.244.2.14:8080,10.244.2.17:8080,10.244.2.18:8080)
Annotations:
  kubectl.kubernetes.io/last-applied-configuration:  {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{},"name":"ingress-http","namespace":"dev"},"spec":{"rules":[{"host":"nginx.test.com","http":{"paths":[{"backend":{"serviceName":"nginx-service","servicePort":80},"path":"/"}]}},{"host":"tomcat.test.com","http":{"paths":[{"backend":{"serviceName":"tomcat-service","servicePort":8080},"path":"/"}]}}]}}

Events:
  Type    Reason  Age                From                      Message
  ----    ------  ----               ----                      -------
  Normal  CREATE  18m                nginx-ingress-controller  Ingress dev/ingress-http
  Normal  UPDATE  38s (x2 over 18m)  nginx-ingress-controller  Ingress dev/ingress-http
复制代码

 

修改本机的hosts文件(位置:C:\Windows\System32\drivers\etc),添加如下内容

master虚拟机的IP地址    nginx.test.com
masete虚拟机的IP地址    tomcat.test.com

查看ingress为service提供的端口号

[root@master ~]# kubectl get svc -n ingress-nginx
NAME            TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx   NodePort   10.106.160.9   <none>        80:32000/TCP,443:32421/TCP   95m

在浏览器中测试访问,发现能够访问通

nginx:

 

 tomcat:

 

Https代理

创建证书

[root@master ~]# openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/C=CN/ST=BJ/L=BJ/O=nginx/CN=test.com"
Generating a 2048 bit RSA private key
............................+++
..............................+++
writing new private key to 'tls.key'
-----

创建密钥

[root@master ~]# kubectl create secret tls tls-secret --key tls.key --cert tls.crt
secret/tls-secret created

创建ingress-https.yaml

复制代码
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-https
  namespace: dev
spec:
  tls:
    - hosts:
      - nginx.test.com
      - tomcat.test.com
      secretName: tls-secret  #指定密钥
  rules:
  - host: nginx.test.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-service
          servicePort: 80
  - host: tomcat.test.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-service
          servicePort: 8080
复制代码

使用配置文件

复制代码
[root@master ~]# vim ingress-https.yaml
[root@master ~]# kubectl create -f ingress-https.yaml 
ingress.extensions/ingress-https created

[root@master ~]# kubectl get ing ingress-https -n dev
NAME            HOSTS                            ADDRESS        PORTS     AGE
ingress-https   nginx.test.com,tomcat.test.com   10.106.160.9   80, 443   52s

[root@master ~]# kubectl describe ing ingress-https -n dev
Name:             ingress-https
Namespace:        dev
Address:          10.106.160.9
Default backend:  default-http-backend:80 (<none>)
TLS:
  tls-secret terminates nginx.test.com,tomcat.test.com
Rules:
  Host             Path  Backends
  ----             ----  --------
  nginx.test.com   
                   /   nginx-service:80 (10.244.1.33:80,10.244.2.13:80,10.244.2.16:80)
  tomcat.test.com  
                   /   tomcat-service:8080 (10.244.2.14:8080,10.244.2.17:8080,10.244.2.18:8080)
Annotations:
Events:
  Type    Reason  Age   From                      Message
  ----    ------  ----  ----                      -------
  Normal  CREATE  79s   nginx-ingress-controller  Ingress dev/ingress-https
  Normal  UPDATE  38s   nginx-ingress-controller  Ingress dev/ingress-https
复制代码

获取端口,左边的是http使用的端口,右边是https使用的端口,因此要使用的端口是32421

[root@master ~]# kubectl get svc -n ingress-nginx
NAME            TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx   NodePort   10.106.160.9   <none>        80:32000/TCP,443:32421/TCP   129m

使用浏览器访问

nginx:

 

tomcat:

 

出处:https://www.cnblogs.com/Ayanamidesu/p/15136379.html

相关教程