圖中所出現的內容,在配置文件中均有體現
html
流量基本的流向圖所示,LoadBalancer能夠是雲服務所提供,也能夠是一個Nginx/HAProxy/istio,甚至「它」不存在均可以。前端
糙一點能夠直接讓IngressService拿外網IP提供服務,本文就這麼幹的!vue
術語 | 做用 | 關聯 |
---|---|---|
IngressController | Ingress控制器,內置的是ingress-nginx, 能夠認爲是真正運行流量分發的地方。k8s系統 自帶的是Nginx |
Ingress |
IngressService | 就是個Service,但它是IngressController的前端 | IngressController |
Ingress | 至關於Nginx的主機配置文件 | IngressController |
ingress annotations | 經過註解詳細配置主機 | Github Ingress Doc |
準備ingress-nginx的鏡像地址nginx
registry.cn-beijing.aliyuncs.com/smokelee/nginx-ingress-controller:0.30.0
下載部署用的yamlgit
地址會由於項目不停的開發而變化,到Ingress-Nginx Github去看一眼會拿到新連接github
$wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml
編輯mandatory.yamlweb
鏡像地址調整爲registry.cn-beijing.aliyuncs.com/smokelee/nginx-ingress-controller:0.30.0
私有倉庫有密碼的狀況,在ingress-nginx的命名空間中加一個secret,並在yaml中增長 imagePullSecrets配置docker
準備上面須要的Service來公佈服務api
kind: Service apiVersion: v1 metadata: name: ingress-nginx namespace: ingress-nginx labels: <===這兩個Selector會被ingress-nginx-controller拿到,並讀取數據。找不到這個Service,ingress-nginx會不停輸出錯誤。但狀態時Running和Ready app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx spec: selector: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx externalIPs: 這裏直接指定IP了,條件容許應該用LB。NodePort也行,我嫌麻煩 - 192.168.56.5 ports: - name: http port: 80 protocol: TCP targetPort: http - name: https port: 443 protocol: TCP targetPort: https
坑!bash
就算不部署上面的Service,ingress-controller安裝完成後也會提示Running/Ready。而且只有到Node節點打印log才發現,它須要一個Service。
kubectl get all -n ingress-nginx NAME READY STATUS RESTARTS AGE pod/nginx-ingress-controller-648f9697ff-rwxxr 1/1 Running 0 24h NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/nginx-ingress-controller 1/1 1 1 24h NAME DESIRED CURRENT READY AGE replicaset.apps/nginx-ingress-controller-648f9697ff 1 1 1 24h
用Vue腳手架搭建一個vue並編譯
編譯完成後目錄中有一個dist目錄
編寫Docker文件
以爲上兩步麻煩,直接弄個目錄,寫一個html隨便寫點啥都行!和Dockerfile在一個目錄便可。換成COPY index.html .就行啦
FROM nginx:latest ENV NODE_ENV production WORKDIR /usr/share/nginx/html COPY dist .
編譯並推送到倉庫
$docker build -t <鏡像地址>:latest $docker push <鏡像地址>:latest 這裏假設地址是:registry.cn-beijing.aliyuncs.com/smokelee/app/vue-demo:latest
編寫APP的部署文件
apiVersion: apps/v1 kind: Deployment metadata: name: vue-demo-deployment spec: selector: matchLabels: app: vue-demo-pod template: metadata: labels: app: vue-demo-pod spec: imagePullSecrets: - name: registry-secret-smokelee.com containers: - name: vue-demo image: registry.i.smokelee.com/app/vue-demo:latest imagePullPolicy: IfNotPresent ports: - containerPort: 80 name: web protocol: TCP --- apiVersion: v1 kind: Service metadata: name: vue-demo-svc labels: app: vue-demo-svc spec: selector: app: vue-demo-pod ports: - name: web port: 80 targetPort: web
編寫app的Ingress
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: simple-nginx-example annotations: nginx.ingress.kubernetes.io/rewrite-target: / 關於ingress-nginx的註解,在1.3中的表格有描述 spec: rules: - host: vue.smokelee.com 定義域名 http: paths: - path: / backend: serviceName: vue-demo-svc 內部DNS找到主機,對應見app的部署內容 servicePort: 80
kubectl 建立上述兩個資源
建立完成後,經過命令找到ingress-nginx service的IP地址,並在/etc/hosts中增長記錄
kubectl get svc -n ingress-nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ingress-nginx ClusterIP 10.254.7.143 192.168.56.5 80/TCP,443/TCP 15s
經過curl來測試訪問結果
$curl http://vue.smokelee.com 返回HTML
去看看2.2.7的註解吧,比較粗的發佈都能作到!網上有阿里雲的示例,並不適用默認的nginx-ingress-controller,人家本身開發了一些。