/ KUBERNETES

Kubernetes 1.28.2 部署 nginx-Ingress-controller 1.12.0

版本对应

alt

DaemonSet+HostNetwork+nodeSelector 部署

(1)节点标签

# 查看节点标签
kubectl get nodes --show-labels
# 标签添加
kubectl label node k8s-node1 ingress=true
# 标签移除
kubectl label node k8s-node1 ingress-

(2)配置文件修改

# 下载安装yaml
curl https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.12.0-beta.0/deploy/static/provider/cloud/deploy.yaml -O
# 替换镜像地址
sed -i 's/registry.k8s.io/k8s.m.daocloud.io/g' deploy.yaml
# 修改配置文件
vi deploy.yaml
apiVersion: apps/v1
kind: DaemonSet # 修改Deployment为DaemonSet
metadata:
  labels:
    .......
  name: ingress-nginx-controller
  namespace: ingress-nginx
spec:
    .......
# 注释strategy
#  strategy:
#    rollingUpdate:
#      maxUnavailable: 1
#    type: RollingUpdate
  template:
    metadata:
      labels:
      	........
    spec:
      hostNetwork: true # 使用主机网络
      nodeSelector:
        ingress: true # 选择节点运行,节点有标签ingress=true

(3)部署服务

kubectl apply -f deploy.yaml

(4)服务验证

# 创建部署文件
vi nginx-test.yaml
---
apiVersion: v1
kind: Namespace
metadata:
  name: project
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx1
  name: nginx1
  namespace: project
spec:
  ports:
  - name: port-nginx
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx1
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx1
  namespace: project
spec:
   replicas: 1
   selector:
    matchLabels:
      app: nginx1
   template:
     metadata:
      name: nginx1
      creationTimestamp: null
      labels:
        app: nginx1
     spec:
       containers:
       - image: nginx
         name: nginx1
         imagePullPolicy: IfNotPresent
         env:
         - name: APP_ENV
           value: "test"
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx2
  name: nginx2
  namespace: project
spec:
  ports:
  - name: port-nginx
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx2
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx2
  namespace: project
spec:
   replicas: 1
   selector:
    matchLabels:
      app: nginx2
   template:
     metadata:
      name: nginx2
      creationTimestamp: null
      labels:
        app: nginx2
     spec:
       containers:
       - image: nginx
         name: nginx2
         imagePullPolicy: IfNotPresent
         env:
         - name: APP_ENV
           value: "test"
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-http
  namespace: project
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  ingressClassName: nginx
  rules:
    - host: project.test.com
      http:
        paths:
          - path: /(.*)
            pathType: ImplementationSpecific
            backend:
              service:
                name: nginx1
                port:
                  number: 80
          - path: /api/(.*)
            pathType: ImplementationSpecific
            backend:
              service:
                name: nginx2
                port:
                  number: 80
    - host: nginx2.test.com
      http:
        paths:
          - path: /api/(.*)
            pathType: ImplementationSpecific
            backend:
              service:
                name: nginx2
                port:
                  number: 80
# 服务部署
kubectl apply -f nginx-test.yaml

alt

(5)添加虚拟host

192.168.186.129 project.test.com
192.168.186.129 nginx2.test.com

alt alt alt