• K8S集群实现外部访问(INGRESS)


    环境:

    masternode01node02
    192.168.1.40192.168.1.41192.168.1.42

    INGRESS

    作用:Ingress 是对集群中服务的外部访问进行管理的 API 对象。

    1.创建DEPLOYMENT

    PS:创建nginx和httpd

    1. [root@master yaml]# vim nginx.yaml
    2. kind: Deployment
    3. apiVersion: extensions/v1beta1
    4. metadata:
    5. name: nginx
    6. spec:
    7. replicas: 3
    8. template:
    9. metadata:
    10. labels:
    11. app: nginx
    12. spec:
    13. containers:
    14. - name: nginx
    15. image: nginx
    16. ---
    17. kind: Service
    18. apiVersion: v1
    19. metadata:
    20. name: nginx-svc
    21. spec:
    22. selector:
    23. app: nginx
    24. ports:
    25. - protocol: TCP
    26. port: 80
    27. targetPort: 80
    28. [root@master yaml]# kubectl apply -f nginx.yaml
    29. deployment.extensions/nginx created
    30. service/nginx-svc created
    31. [root@master yaml]# vim httpd.yaml
    32. kind: Deployment
    33. apiVersion: extensions/v1beta1
    34. metadata:
    35. name: httpd
    36. spec:
    37. replicas: 3
    38. template:
    39. metadata:
    40. labels:
    41. app: httpd
    42. spec:
    43. containers:
    44. - name: httpd
    45. image: httpd
    46. ---
    47. kind: Service
    48. apiVersion: v1
    49. metadata:
    50. name: httpd-svc
    51. spec:
    52. selector:
    53. app: httpd
    54. ports:
    55. - protocol: TCP
    56. port: 80
    57. targetPort: 80
    58. [root@master yaml]# kubectl apply -f httpd.yaml
    59. deployment.extensions/httpd created
    60. service/httpd-svc created
    61. [root@master yaml]# kubectl get deployments.
    62. NAME READY UP-TO-DATE AVAILABLE AGE
    63. httpd 3/3 3 3 117s
    64. nginx 3/3 3 3 3m10s
    65. [root@master yaml]# kubectl get svc
    66. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    67. httpd-svc ClusterIP 10.97.134.80 80/TCP 2m1s
    68. nginx-svc ClusterIP 10.96.37.85 80/TCP 3m14s

    2.部署INGRESS

    2.1 下载INGRESS文件

    [root@master yaml]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.35.0/deploy/static/provider/baremetal/deploy.yaml
    

    2.2 修改YAML文件

    1. [root@master yaml]# vim deploy.yaml
    2. ......
    3. spec:
    4. hostNetwork: true #本地网络访问
    5. dnsPolicy: ClusterFirst
    6. containers:
    7. - name: controller
    8. image: registry.aliyuncs.com/google_containers/nginx-ingress-controller:0.30.0
    9. imagePullPolicy: IfNotPresent
    10. ......
    11. [root@master yaml]# kubectl apply -f deploy.yaml

    2.3 查看

    PS:namespace为ingress-nginx

    1. [root@master yaml]# kubectl get pod -o wide -n ingress-nginx
    2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
    3. ingress-nginx-admission-create-qqjz2 0/1 Completed 0 3m30s 10.244.1.9 node02 <none> <none>
    4. ingress-nginx-admission-patch-7xkk8 0/1 Completed 0 3m30s 10.244.2.7 node01 <none> <none>
    5. ingress-nginx-controller-6584bf6bc8-hj9zk 1/1 Running 0 3m30s 192.168.1.41 node01 <none> <none>
    6. [root@master yaml]# kubectl get svc -n ingress-nginx
    7. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    8. ingress-nginx-controller NodePort 10.97.134.37 <none> 80:31126/TCP,443:31537/TCP 3m23s
    9. ingress-nginx-controller-admission ClusterIP 10.96.170.183 <none> 443/TCP 3m23s

    2.4 查看INGRESS-NGINX-CONTROLLER容器内部详情

    PS:它现在已经有一个模板,用来描述Ingress资源能够收集到的信息了

    1. [root@master yaml]# kubectl exec -it -n ingress-nginx ingress-nginx-controller-6584bf6bc8-hj9zk sh
    2. /etc/nginx $ cat nginx.conf
    3. ......
    4. location / {
    5. set $namespace "";
    6. set $ingress_name "";
    7. set $service_name "";
    8. set $service_port "";
    9. set $location_path "/";
    10. ......

    3.基于HTTPD的访问

    3.1 创建对应的INGRESS规则

    1. [root@master yaml]# vim ingress-httpd.yaml
    2. kind: Ingress
    3. apiVersion: extensions/v1beta1
    4. metadata:
    5. name: web-ingress
    6. annotations:
    7. nginx.ingress.kubernetes.io/rewrite-target: /
    8. spec:
    9. rules:
    10. - host: wwww.ingress.com
    11. http:
    12. paths:
    13. - path: /nginx
    14. backend:
    15. serviceName: nginx-svc
    16. servicePort: 80
    17. - path: /httpd
    18. backend:
    19. serviceName: httpd-svc
    20. servicePort: 80
    21. [root@master yaml]# kubectl apply -f ingress-httpd.yaml
    22. ingress.extensions/web-ingress created

    3.2 查看对应规则的详细信息

    1. [root@master yaml]# kubectl describe ingresses. web-ingress
    2. Name: web-ingress
    3. Namespace: default
    4. Address: 192.168.1.41
    5. Default backend: default-http-backend:80 (<none>)
    6. Rules:
    7. Host Path Backends
    8. ---- ---- --------
    9. web.ingress.com
    10. /nginx nginx-svc:80 (10.244.1.2:80,10.244.2.2:80,10.244.2.3:80)
    11. /httpd httpd-svc:80 (10.244.1.3:80,10.244.1.4:80,10.244.2.4:80)
    12. Annotations:
    13. kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"nginx.ingress.kubernetes.io/rewrite-target":"/"},"name":"web-ingress","namespace":"default"},"spec":{"rules":[{"host":"web.ingress.com","http":{"paths":[{"backend":{"serviceName":"nginx-svc","servicePort":80},"path":"/nginx"},{"backend":{"serviceName":"httpd-svc","servicePort":80},"path":"/httpd"}]}}]}}
    14. nginx.ingress.kubernetes.io/rewrite-target: /
    15. Events:
    16. Type Reason Age From Message
    17. ---- ------ ---- ---- -------
    18. Normal CREATE 50s nginx-ingress-controller Ingress default/web-ingress
    19. Normal UPDATE 3s nginx-ingress-controller Ingress default/web-ingress

    3.3 查看INGRESS-NGINX-CONTROLLER容器内部详情

    1. [root@master yaml]# kubectl exec -it -n ingress-nginx ingress-nginx-controller-6584bf6bc8-hj9zk sh
    2. /etc/nginx $ cat nginx.conf
    3. ......
    4. location ~* "^/nginx" {
    5. set $namespace "default";
    6. set $ingress_name "web-ingress";
    7. set $service_name "nginx-svc";
    8. set $service_port "80";
    9. set $location_path "/nginx";
    10. ......
    11. location ~* "^/httpd" {
    12. set $namespace "default";
    13. set $ingress_name "web-ingress";
    14. set $service_name "httpd-svc";
    15. set $service_port "80";
    16. set $location_path "/httpd";
    17. ......

    3.4 访问

    PS:有DNS的话可以设置解析,没有的话必须在host文件下添加域名解析才可访问

    winows:C:\Windows\System32\drivers\etc\

    linux:/etc/hosts

    1. [root@client ~]# vim /etc/hosts
    2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    4. 192.168.1.41 wwww.ingress.com

    4.基于HTTP实现虚拟机主机的访问

    4.1 创建INGRESS规则

    1. [root@master yaml]# vim ingress.yaml
    2. kind: Ingress
    3. apiVersion: extensions/v1beta1
    4. metadata:
    5. name: ingress1
    6. annotations:
    7. nginx.ingress.kubernetes.io/rewrite-target: /
    8. spec:
    9. rules:
    10. - host: ingress1.web.io
    11. http:
    12. paths:
    13. - path: /nginx
    14. backend:
    15. serviceName: nginx-svc
    16. servicePort: 80
    17. ---
    18. kind: Ingress
    19. apiVersion: extensions/v1beta1
    20. metadata:
    21. name: ingress2
    22. annotations:
    23. nginx.ingress.kubernetes.io/rewrite-target: /
    24. spec:
    25. rules:
    26. - host: ingress2.web.io
    27. http:
    28. paths:
    29. - path: /httpd
    30. backend:
    31. serviceName: httpd-svc
    32. servicePort: 80
    33. [root@master yaml]# kubectl apply -f ingress.yaml
    34. ingress.extensions/ingress1 created
    35. ingress.extensions/ingress2 created

    4.2 查看对应的INGRESS规则

    1. [root@master yaml]# kubectl describe ingresses. ingress1
    2. Name: ingress1
    3. Namespace: default
    4. Address: 192.168.1.41
    5. Default backend: default-http-backend:80 (<none>)
    6. Rules:
    7. Host Path Backends
    8. ---- ---- --------
    9. ingress1.web.io
    10. /nginx nginx-svc:80 (10.244.1.2:80,10.244.2.2:80,10.244.2.3:80)
    11. Annotations:
    12. kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"nginx.ingress.kubernetes.io/rewrite-target":"/"},"name":"ingress1","namespace":"default"},"spec":{"rules":[{"host":"ingress1.web.io","http":{"paths":[{"backend":{"serviceName":"nginx-svc","servicePort":80},"path":"/nginx"}]}}]}}
    13. nginx.ingress.kubernetes.io/rewrite-target: /
    14. Events:
    15. Type Reason Age From Message
    16. ---- ------ ---- ---- -------
    17. Normal CREATE 71s nginx-ingress-controller Ingress default/ingress1
    18. Normal UPDATE 39s nginx-ingress-controller Ingress default/ingress1
    19. [root@master yaml]# kubectl describe ingresses. ingress2
    20. Name: ingress2
    21. Namespace: default
    22. Address: 192.168.1.41
    23. Default backend: default-http-backend:80 (<none>)
    24. Rules:
    25. Host Path Backends
    26. ---- ---- --------
    27. ingress2.web.io
    28. /httpd httpd-svc:80 (10.244.1.3:80,10.244.1.4:80,10.244.2.4:80)
    29. Annotations:
    30. kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"nginx.ingress.kubernetes.io/rewrite-target":"/"},"name":"ingress2","namespace":"default"},"spec":{"rules":[{"host":"ingress2.web.io","http":{"paths":[{"backend":{"serviceName":"httpd-svc","servicePort":80},"path":"/httpd"}]}}]}}
    31. nginx.ingress.kubernetes.io/rewrite-target: /
    32. Events:
    33. Type Reason Age From Message
    34. ---- ------ ---- ---- -------
    35. Normal CREATE 73s nginx-ingress-controller Ingress default/ingress2
    36. Normal UPDATE 41s nginx-ingress-controller Ingress default/ingress2

    4.3 访问

    1. [root@node02 ~]# vim /etc/hosts
    2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    4. 192.168.1.41 ingress1.web.io ingress2.web.io

    5.基于HTTPS的访问

    5.1 创建证书

    1. [root@master yaml]# mkdir https
    2. [root@master yaml]# cd https/
    3. [root@master https]# openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=nginxsvc/O=nginxsvc"
    4. Generating a 2048 bit RSA private key
    5. .......................................+++
    6. .....................................................+++
    7. writing new private key to 'tls.key'
    8. -----
    9. [root@master https]# ls
    10. tls.crt tls.key

    5.2 用SECRET创建资源,将证书保存到K8S集群中

    1. [root@master https]# kubectl create secret tls tls-secret --key=tls.key --cert tls.crt
    2. secret/tls-secret created

    5.3 创建DEPLOYMENT和对应INGRESS规则

    1. [root@master https]# vim deploy.yaml
    2. apiVersion: extensions/v1beta1
    3. kind: Deployment
    4. metadata:
    5. name: httpds
    6. spec:
    7. replicas: 2
    8. template:
    9. metadata:
    10. labels:
    11. app: httpd
    12. spec:
    13. containers:
    14. - name: httpd
    15. image: httpd
    16. ---
    17. kind: Service
    18. apiVersion: v1
    19. metadata:
    20. name: httpdsvc-1
    21. spec:
    22. selector:
    23. app: httpd
    24. ports:
    25. - protocol: TCP
    26. port: 80
    27. targetPort: 80
    28. [root@master https]# kubectl apply -f deploy.yaml
    29. deployment.extensions/httpds created
    30. service/httpdsvc-1 created
    31. [root@master https]# vim ingress.yaml
    32. kind: Ingress
    33. apiVersion: extensions/v1beta1
    34. metadata:
    35. name: https
    36. spec:
    37. tls:
    38. - hosts:
    39. - ingress.httpd.com
    40. secretName: tls-secret
    41. rules:
    42. - host: ingress.httpd.com
    43. http:
    44. paths:
    45. - path: /
    46. backend:
    47. serviceName: httpdsvc-1
    48. servicePort: 80
    49. [root@master https]# kubectl apply -f ingress.yaml
    50. ingress.extensions/https created

    5.4 访问

    1. [root@node02 ~]# vim /etc/hosts
    2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    4. 192.168.1.41 ingress1.web.io ingress2.web.io ingress.httpd.com

  • 相关阅读:
    基于spring boot开发的快递管理系统开题报告
    如何在PDF文件中提取图片?PDF图片提取教程
    AI 一键去背景
    Java实验(头歌) -Java继承和多态接口
    2199. Finding the Topic of Each Post
    spring注解使用习惯-Control层前后端交互
    2015年蓝桥杯省赛C/C++ A组 灾后重建题解(100分)
    洛谷P4061 大吉大利,晚上吃鸡
    oracle灾备切换和回切步骤以及sql执行语句
    Spring事件监听机制使用和原理解析
  • 原文地址:https://blog.csdn.net/zfw_666666/article/details/126287385