在上篇文章中咱們已經使用 k8s 部署了第一個應用,此時咱們可使用 Ingress
使它能夠在互聯網上能夠被訪問到 (固然你要有本身的域名而且指向正確)node
如下是官網搬用的關於 Ingress
的一幅圖,用以描述 Ingress 的做用。若是你對它一無所知,你能夠把它理解爲傳統的 nginx,用以配置本身網站的域名使之可以經過外網訪問。nginx
internet
|
[ Ingress ]
--|-----|--
[ Services ]
複製代碼
其中,Ingress
包含兩個組件git
Ingress
: 配置轉發規則,相似於 nginx 的配置文件Ingress Controller
: 轉發,相似於 nginx,它會讀取 Ingress
的規則並轉化爲 nginx
的配置文件而 Ingress Controller
除了 nginx
外還有 haproxy
,ingress
等等,咱們選用 nginx
做爲 Ingress Controllergithub
咱們使用 helm 選擇官方的 stable/nginx-ingress chart 進行部署。docker
nginx-ingress
會配置一個 type 爲 LoadBalancer
的 service, 所以須要配置 EXTERNAL-IP
爲k8s集羣節點的 IP。 在這裏 external-ip 會設置爲 [172.17.68.39, 172.17.68.40]
後端
咱們能夠經過 kubectl get nodes
來獲取 IP 地址api
# 獲取node的 INTERNAL-IP,做爲 LoadBalancer 的 EXTERNAL-IP
$ kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
shanyue Ready master 13d v1.16.0 172.17.68.39 <none> CentOS Linux 7 (Core) 3.10.0-957.21.3.el7.x86_64 docker://18.6.2
shuifeng Ready <none> 13d v1.16.0 172.17.68.40 <none> CentOS Linux 7 (Core) 3.10.0-957.21.3.el7.x86_64 docker://18.6.2
複製代碼
在這裏 external-ip 會設置爲 [172.17.68.39, 172.17.68.40]
瀏覽器
controller.service.externalIPs[0]=172.17.68.39
controller.service.externalIPs[1]=172.17.68.40
複製代碼
# 使用 helm v3 部署,若是使用 helm v2 部署的話,把 release-name 使用 --name 指定
$ helm install nginx-ingress stable/nginx-ingress --set "controller.service.externalIPs[0]=172.17.68.39,controller.service.externalIPs[1]=172.17.68.40"
NAME: nginx-ingress
LAST DEPLOYED: 2019-10-18 21:21:44.115902395 +0800 CST m=+1.904554085
NAMESPACE: default
STATUS: deployed
NOTES:
The nginx-ingress controller has been installed.
It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status by running 'kubectl --namespace default get services -o wide -w nginx-ingress-controller'
An example Ingress that makes use of the controller:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
name: example
namespace: foo
spec:
rules:
- host: www.example.com
http:
paths:
- backend:
serviceName: exampleService
servicePort: 80
path: /
# This section is only required if TLS is to be enabled for the Ingress
tls:
- hosts:
- www.example.com
secretName: example-tls
If TLS is enabled for the Ingress, a Secret containing the certificate and key must also be provided:
apiVersion: v1
kind: Secret
metadata:
name: example-tls
namespace: foo
data:
tls.crt: <base64 encoded cert>
tls.key: <base64 encoded key>
type: kubernetes.io/tls
複製代碼
校驗 nginx-ingress 的部署狀況bash
$ helm ls
NAME NAMESPACE REVISION UPDATED STATUS CHART
nginx-ingress default 1 2019-10-18 11:21:44.115902395 +0800 CST deployed nginx-ingress-1.24.0
# 查看 nginx-ingress 全部的 service
$ kubectl get svc -l app=nginx-ingress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-ingress-controller LoadBalancer 10.101.64.64 172.17.68.39,172.17.68.40 80:30285/TCP,443:31094/TCP 7m19s
nginx-ingress-default-backend ClusterIP 10.110.76.15 <none> 80/TCP 7m19s
複製代碼
與已知知識關聯有助於咱們更好地學習新知識,如下是關於 nginx 與 ingress 部署一個博客應用的簡單配置文件app
server {
listen 80
server_name nginx.xiange.tech
location / {
proxy_pass: http://nginx:80
}
}
複製代碼
使用 Ingress
配置路由規則以下
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: nginx-service-ingress
spec:
rules:
- host: nginx.xiange.tech
http:
paths:
- backend:
serviceName: nginx-service
servicePort: 80
path: /
複製代碼
咱們使用 Ingress
把它配置到了 nginx.xiange.tech
該域名下,在公網環境下的瀏覽器中打開域名 nginx.xiange.tech
,能夠看到熟悉的 nginx 配置頁面
部署一個應用從 Deployment
,Service
再到 Ingress
的完整配置文件以下
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: nginx-service-ingress
spec:
rules:
- host: nginx.xiange.tech
http:
paths:
- backend:
serviceName: nginx-service
servicePort: 80
path: /
複製代碼
歡迎關注公衆號山月行,我會按期分享一些先後端以及運維的文章,而且會有技術與生活上的每日回顧與總結,歡迎關注交流