kubernetes ingress 使用

一、更改NodePort端口范围

kubeadm 更改NodePort端口范围

kubernetes默认端口号范围是 30000-32767 ,如果期望值不是这个区间则需要更改。

vim  /etc/kubernetes/manifests/kube-apiserver.yaml

编辑添加配置 service-node-port-range=1-65535

保存后,稍等10秒左右会自动生效

二、部署ingress插件

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


获取配置文件位置: https://github.com/kubernetes/ingress-nginx/tree/nginx-0.20.0/deploy

1.namespace.yaml 
创建一个独立的命名空间 ingress-nginx
2.configmap.yaml 
ConfigMap是存储通用的配置变量的,类似于配置文件,使用户可以将分布式系统中用于不同模块的环境变量统一到一个对象中管理;而它与配置文件的区别在于它是存在集群的“环境”中的,并且支持K8S集群中所有通用的操作调用方式。
从数据角度来看,ConfigMap的类型只是键值组,用于存储被Pod或者其他资源对象(如RC)访问的信息。这与secret的设计理念有异曲同工之妙,主要区别在于ConfigMap通常不用于存储敏感信息,而只存储简单的文本信息。
ConfigMap可以保存环境变量的属性,也可以保存配置文件。
创建pod时,对configmap进行绑定,pod内的应用可以直接引用ConfigMap的配置。相当于configmap为应用/运行环境封装配置。
pod使用ConfigMap,通常用于:设置环境变量的值、设置命令行参数、创建配置文件。

3.default-backend.yaml 
如果外界访问的域名不存在的话,则默认转发到default-http-backend这个Service,其会直接返回404:

4.rbac.yaml 
负责Ingress的RBAC授权的控制,其创建了Ingress用到的ServiceAccount、ClusterRole、Role、RoleBinding、ClusterRoleBinding

5.with-rbac.yaml 
是Ingress的核心,用于创建ingress-controller。前面提到过,ingress-controller的作用是将新加入的Ingress进行转化为Nginx的配置

更换国内镜像

sed -i 's#k8s.gcr.io/defaultbackend-amd64#registry.cn-beijing.aliyuncs.com/kaikai136/defaultbackend-amd64#g' mandatory.yaml 
sed -i 's#quay.io/kubernetes-ingress-controller/nginx-ingress-controller#registry.cn-beijing.aliyuncs.com/kaikai136/nginx-ingress-controller#g' mandatory.yaml

修改Deployment   apiVersion类型为apps/v1

vim /opt/plugin/ingress/ingress-nginx/deploy/mandatory.yaml

apiVersion: apps/v1
kind: Deployment

修改service-nodeport.yaml文件,添加NodePort端口,默认为随机端口

vim /opt/plugin/ingress/ingress-nginx/deploy/provider/baremetal/service-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  type: NodePort
  ports:
    - name: http
      port: 80
      targetPort: 80
      protocol: TCP
      nodePort: 80  #http
    - name: https
      port: 443
      targetPort: 443
      protocol: TCP
      nodePort: 443  #https
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

部署nginx-ingress

kubectl apply -f  /opt/plugin/ingress/ingress-nginx/deploy/mandatory.yaml

kubectl apply -f  /opt/plugin/ingress/ingress-nginx/deploy/provider/baremetal/service-nodeport.yaml

部署测试

vim vh-ingress.yaml 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment1
spec:
  replicas: 2
  selector:
    matchLabels:
      name: nginx
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
          - containerPort: 80
      imagePullSecrets:
        - name: kaikai.txt-regcred
---
apiVersion: v1
kind: Service
metadata:
  name: svc-1
spec:
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30902
    protocol: TCP
  selector:
    name: nginx
  type: NodePort
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress1
spec:
  rules:
  - host: www1.kaikai.txt.com
    http:
      paths:
      - path: /
        backend:
          serviceName: svc-1
          servicePort: 80

kubectl apply -f vh-ingress.yaml

解析域名

www1.kaikai.txt.com  为k8s任意节点  访问

猜你喜欢

转载自blog.csdn.net/kaikai136412162/article/details/110436050