kubernetes ingress网站发布

ingress网站发布 单域名

# 1.创建nginx pod 名称: nginx-nodeport.yaml
cat nginx-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
    - name: https
      port: 443
      targetPort: 443
      protocol: TCP
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx




# 2.创建nginx svc和deploy   
# cat nginx-nodeport-service.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
 name: nginx-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-app-svc
spec:
  type: ClusterIP
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  selector:
    app: nginx




# 1.创建nginx ingress

cat ingress-app.yaml
 
# 内容
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: "nginx"
  name: nginx-app-ingress
spec:
  rules:
  - host: www.chenleilei.net
    http:
      paths:
      - backend:
          serviceName: nginx-app-svc
          servicePort: 80
        path: /



## 2. 在已安装的ingress服务器上发布ingress网站
kubectl create -f nginx-nodeport.yaml 
kubectl create -f nginx-nodeport-service.yaml
kubectl create -f ingress-app.yaml



## 3. 删除ingress网站:
kubectl delete -f nginx-nodeport.yaml 
kubectl delete -f nginx-nodeport-service.yaml
kubectl delete -f ingress-app.yaml

多域名配置

# 0. 创建svc
cat ingress-app-duoyuming-svc01.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-app-svc-01
spec:
  type: ClusterIP
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  selector:
    app: nginx
    

cat ingress-app-duoyuming-svc02.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-app-svc-02
spec:
  type: ClusterIP
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  selector:
    app: nginx


#1. 创建 ingress
cat ingress-app-duoyuming.yaml:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: "nginx"
  name: nginx-app-ingress-1
spec:
  rules:         #一个ingress可以配置多个规则
  - host: www.chenleilei01.net    #域名配置
    http:
      paths:     #相当于nginx中的location,同一个host可以配置多个 路径
      - backend:
          serviceName: nginx-app-svc-01
          servicePort: 80
        path: /
  - host: www.chenleilei02.net
    http:
      paths:
      - backend:
          serviceName: nginx-app-svc-02
          servicePort: 80
        path: /




#执行:
kubectl create -f ingress-app-duoyuming.yaml


# 2. 创建 nodeport
cat nginx-nodeport01.yaml 

apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx-01
  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
    - name: https
      port: 443
      targetPort: 443
      protocol: TCP
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    
    
    
cat nginx-nodeport02.yaml 

apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx-02
  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
    - name: https
      port: 443
      targetPort: 443
      protocol: TCP
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

检查状态:

[root@k8s-master01 duoyuming]# kubectl get ingress
NAME                  CLASS    HOSTS                                       ADDRESS   PORTS   AGE
nginx-app-ingress-1   <none>   www.chenleilei01.net,www.chenleilei02.net             80      18m




#本地host文件配置:

192.168.3.85 chenleilei01.net chenleilei02.net www.chenleilei01.net www.chenleilei02.net 

微信赞赏

支付宝赞赏

原文地址:https://www.cnblogs.com/superlinux/p/15048912.html