中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Kubernetes怎么使用ingress-nginx作為反向代理?

發布時間:2020-05-25 16:56:24 來源:億速云 閱讀:2231 作者:鴿子 欄目:系統運維

Kubernetes系列之Kubernetes使用ingress-nginx作為反向代理

#一、Ingress簡介

在Kubernetes中,服務和Pod的IP地址僅可以在集群網絡內部使用,對于集群外的應用是不可見的。為了使外部的應用能夠訪問集群內的服務,在Kubernetes 目前 提供了以下幾種方案:
NodePort
LoadBalancer
Ingress
###1、Ingress組成
ingress controller
  將新加入的Ingress轉化成Nginx的配置文件并使之生效
ingress服務
  將Nginx的配置抽象成一個Ingress對象,每添加一個新的服務只需寫一個新的Ingress的yaml文件即可
###2、Ingress工作原理
1.ingress controller通過和kubernetes api交互,動態的去感知集群中ingress規則變化,
2.然后讀取它,按照自定義的規則,規則就是寫明了哪個域名對應哪個service,生成一段nginx配置,
3.再寫到nginx-ingress-control的pod里,這個Ingress controller的pod里運行著一個Nginx服務,控制器會把生成的nginx配置寫入/etc/nginx.conf文件中,
4.然后reload一下使配置生效。以此達到域名分配置和動態更新的問題。
###3、Ingress 可以解決什么問題
1.動態配置服務
  如果按照傳統方式, 當新增加一個服務時, 我們可能需要在流量入口加一個反向代理指向我們新的k8s服務. 而如果用了Ingress, 只需要配置好這個服務, 當服務啟動時, 會自動注冊到Ingress的中, 不需要而外的操作.
2.減少不必要的端口暴露
  配置過k8s的都清楚, 第一步是要關閉防火墻的, 主要原因是k8s的很多服務會以NodePort方式映射出去, 這樣就相當于給宿主機打了很多孔, 既不安全也不優雅. 而Ingress可以避免這個問題, 除了Ingress自身服務可能需要映射出去, 其他服務都不要用NodePort方式
#二、部署配置ingress-nginx
1、下載配置文件(下載的整合文件)

# cd /data/kubernetes/ingress-nginx
# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml

2、文件說明

可以分成五個單獨的文件
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的配置

3、選擇要部署的節點

#給master002和master003打上標簽
kubectl label nodes huoban-k8s-master02 kubernetes.io=nginx-ingress
kubectl label nodes huoban-k8s-master03 kubernetes.io=nginx-ingress

4、修改配置文件

vim mandatory.yaml


apiVersion: v1
kind: Namespace
metadata:
name: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx


kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-configuration
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginxs
data:
proxy-body-size: "200m"


kind: ConfigMap
apiVersion: v1
metadata:
name: tcp-services
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx


kind: ConfigMap
apiVersion: v1
metadata:
name: udp-services
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx


apiVersion: v1
kind: ServiceAccount
metadata:
name: nginx-ingress-serviceaccount
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx


apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
name: nginx-ingress-clusterrole
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
rules:

  • apiGroups:
    • ""
      resources:
    • configmaps
    • endpoints
    • nodes
    • pods
    • secrets
      verbs:
    • list
    • watch
  • apiGroups:
    • ""
      resources:
    • nodes
      verbs:
    • get
  • apiGroups:
    • ""
      resources:
    • services
      verbs:
    • get
    • list
    • watch
  • apiGroups:
    • ""
      resources:
    • events
      verbs:
    • create
    • patch
  • apiGroups:
    • "extensions"
    • "networking.k8s.io"
      resources:
    • ingresses
      verbs:
    • get
    • list
    • watch
  • apiGroups:
    • "extensions"
    • "networking.k8s.io"
      resources:
    • ingresses/status
      verbs:
    • update

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:
name: nginx-ingress-role
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
rules:

  • apiGroups:
    • ""
      resources:
    • configmaps
    • pods
    • secrets
    • namespaces
      verbs:
    • get
  • apiGroups:
    • ""
      resources:
    • configmaps
      resourceNames:

      Defaults to "<election-id>-<ingress-class>"

      Here: "<ingress-controller-leader>-<nginx>"

      This has to be adapted if you change either parameter

      when launching the nginx-ingress-controller.

    • "ingress-controller-leader-nginx"
      verbs:
    • get
    • update
  • apiGroups:
    • ""
      resources:
    • configmaps
      verbs:
    • create
  • apiGroups:
    • ""
      resources:
    • endpoints
      verbs:
    • get

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
name: nginx-ingress-role-nisa-binding
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: nginx-ingress-role
subjects:

  • kind: ServiceAccount
    name: nginx-ingress-serviceaccount
    namespace: ingress-nginx

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: nginx-ingress-clusterrole-nisa-binding
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: nginx-ingress-clusterrole
subjects:

  • kind: ServiceAccount
    name: nginx-ingress-serviceaccount
    namespace: ingress-nginx

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-ingress-controller
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
spec:
replicas: 2
selector:
matchLabels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
template:
metadata:
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
annotations:
prometheus.io/port: "10254"
prometheus.io/scrape: "true"
spec:
nodeSelector:
kubernetes.io: nginx-ingress
tolerations:

  • effect: NoSchedule
    operator: Exists
    hostNetwork: true
    serviceAccountName: nginx-ingress-serviceaccount
    containers:
    • name: nginx-ingress-controller
      image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:0.25.1
      imagePullPolicy: IfNotPresent
      args:
      • /nginx-ingress-controller
      • --configmap=$(POD_NAMESPACE)/nginx-configuration
      • --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
      • --udp-services-configmap=$(POD_NAMESPACE)/udp-services
      • --publish-service=$(POD_NAMESPACE)/ingress-nginx
      • --annotations-prefix=nginx.ingress.kubernetes.io
        securityContext:
        allowPrivilegeEscalation: true
        capabilities:
        drop:
        • ALL
          add:
        • NET_BIND_SERVICE

          www-data -> 33

          runAsUser: 33
          env:

      • name: POD_NAME
        valueFrom:
        fieldRef:
        fieldPath: metadata.name
      • name: POD_NAMESPACE
        valueFrom:
        fieldRef:
        fieldPath: metadata.namespace
        ports:
      • name: http
        containerPort: 80
      • name: https
        containerPort: 443
        volumeMounts:
        • name: ssl
          mountPath: /etc/ingress-controller/ssl
          livenessProbe:
          failureThreshold: 3
          httpGet:
          path: /healthz
          port: 10254
          scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 10
          readinessProbe:
          failureThreshold: 3
          httpGet:
          path: /healthz
          port: 10254
          scheme: HTTP
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 10
          volumes:
  • name: ssl
    nfs:
    path: /conf/global_sign_ssl
    server: 0a52248244-vcq8.cn-hangzhou.nas.aliyuncs.com

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:
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
    5、部署

    kubectl apply -f mandatory.yaml

    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
    service/ingress-nginx created

    6、訪問測試

    kubectl get pods -n ingress-nginx -o wide

    NAME                                       READY   STATUS    RESTARTS   AGE   IP              NODE                  NOMINATED NODE   READINESS GATES
    nginx-ingress-controller-b44c4d4d7-9rprz   1/1     Running   0          63s   172.16.17.192   huoban-k8s-master03   <none>           <none>
    nginx-ingress-controller-b44c4d4d7-zfj5n   1/1     Running   0          63s   172.16.17.193   huoban-k8s-master02   <none>           <none>
    [root@HUOBAN-K8S-MASTER01 mq1]# curl 172.16.17.192
    <html>
    <head><title>404 Not Found</title></head>
    <body>
    <center><h2>404 Not Found</h2></center>
    <hr><center>openresty/1.15.8.1</center>
    </body>
    </html>
    [root@HUOBAN-K8S-MASTER01 mq1]# curl 172.16.17.193
    <html>
    <head><title>404 Not Found</title></head>
    <body>
    <center><h2>404 Not Found</h2></center>
    <hr><center>openresty/1.15.8.1</center>
    </body>
    </html>

kubectl get svc -n ingress-nginx -o wide

NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE    SELECTOR
ingress-nginx   ClusterIP   10.100.243.171   <none>        80/TCP,443/TCP   112s   app.kubernetes.io/name=ingress-nginx,app.kubernetes.io/part-of=ingress-nginx

curl http://10.100.243.171

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h2>404 Not Found</h2></center>
<hr><center>openresty/1.15.8.1</center>
</body>
</html>

7、部署一個應用測試一下

1、創建一個nginx應用

vim app-nginx.yaml


apiVersion: v1
kind: Service
metadata:
name: app-nginx
labels:
app: app-nginx
spec:
ports:

  • port: 80
    selector:
    app: app-nginx
    tier: production

    apiVersion: autoscaling/v1
    kind: HorizontalPodAutoscaler
    metadata:
    name: app-nginx
    spec:
    maxReplicas: 3
    minReplicas: 1
    scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: app-nginx
    targetCPUUtilizationPercentage: 80

    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: app-nginx
    labels:
    app: app-nginx
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: app-nginx
    tier: production
    template:
    metadata:
    labels:
    app: app-nginx
    tier: production
    spec:
    containers:

    • name: app-nginx
      image: harbor.huoban.com/open/huoban-nginx:v1.1
      imagePullPolicy: IfNotPresent
      resources:
      requests:
      memory: "50Mi"
      cpu: "25m"
      ports:

      • containerPort: 80
        name: nginx
        volumeMounts:
      • name: html
        mountPath: /usr/share/nginx/html

      • name: conf
        mountPath: /etc/nginx/conf.d
        volumes:
    • name: html
      nfs:
      path: /open/web/app
      server: 192.168.101.11

    • name: conf
      nfs:
      path: /open/conf/app/nginx
      server: 192.168.101.11

2、創建TLS證書
kubectl create secret tls bjwf-ingress-secret --cert=server.crt --key=server.key --dry-run -o yaml > bjwf-ingress-secret.yaml

3、創建應用的ingress

vim app-nginx-ingress.yaml


apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: app-ingress
namespace: default
spec:
tls:

  • hosts:
    • www.bjwf125.com
      secretName: bjwf-ingress-secret
      rules:
    • host: www.bjwf125.com
      http:
      paths:
      • path: /
        backend:
        serviceName: app-nginx
        servicePort: 80
        8、訪問服務(這塊就不截圖了。已經能正常跳轉至443)

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

潢川县| 四平市| 准格尔旗| 乐山市| 绥德县| 前郭尔| 麻栗坡县| 乐至县| 增城市| 洪江市| 巴彦淖尔市| 青铜峡市| 武隆县| 杭锦后旗| 南澳县| 景洪市| 东乌| 江阴市| 江口县| 伊金霍洛旗| 韶关市| 兴国县| 廉江市| 郯城县| 辉南县| 介休市| 酒泉市| 宜君县| 三台县| 沾化县| 山西省| 咸阳市| 德安县| 彭阳县| 龙胜| 卢龙县| 台前县| 海林市| 师宗县| 大余县| 铜梁县|