杭州微網(wǎng)站開發(fā)先做后付費(fèi)的代運(yùn)營(yíng)
目錄
ingress的證書訪問
traefik
traefik的部署方式:
deamonset
deployment
nginx-ingress與traefix-ingress相比較
nginx-ingress-controller
ui訪問
deployment部署
ingress的證書訪問
ingress實(shí)現(xiàn)https代理訪問:
需要證書和密鑰
創(chuàng)建證書 密鑰
secrets 保存密鑰信息,部署pod時(shí)把secrets掛載到pod
創(chuàng)建密鑰和證書
#創(chuàng)建密鑰和證書
openssl req -x509 -sha256 -nodes -days 356 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=nginxsvc/O=nginxsvc"
req: 生成證書文件的
x509: 生成x.509自簽名的證書
-sha256:表示使用sha-256的散列算法
-nodes:表示生成的密鑰不加密
-days 365: 證書有效期是365天
-newkey rsa:2048: RSA的密鑰對(duì),長(zhǎng)度2048位
-keyout tls.key -out tls.crt: 密鑰文件 key 證書文件 crt
-subj"/CN=nginxsvc/O=nginxsvc”: 主題,CN common name O : organization#用secret保存密鑰和證書
kubectl create secret tls tls-secret --key tls.key --cert tls.crt
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-httpslabels:app: https
spec:replicas: 3selector:matchLabels:app: httpstemplate:metadata:labels:app: httpsspec:containers:- name: nginximage: nginx:1.22
---
apiVersion: v1
kind: Service
metadata:name: nginx-svc
spec:ports:- port: 80targetPort: 80protocol: TCPselector:app: https
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: nginx-ingress-https
spec:tls:- hosts:- www.123ccc.comsecretName: tls-secret
#加密的配置保存在ingress,請(qǐng)求---ingress-controller--ingress---轉(zhuǎn)發(fā)到service
#在代理進(jìn)行時(shí),就要先驗(yàn)證密鑰對(duì),然后再把請(qǐng)求轉(zhuǎn)發(fā)service到相應(yīng)的podrules:- host: www.123ccc.comhttp:paths:- path: /pathType: Prefixbackend:service:name: nginx-svcport:number: 80
訪問
https://www.123ccc.com:31505/
或
curl -k https://www.123ccc.com:31505
nginx的登錄賬戶認(rèn)證
yum -y install httpd
#借用httpd的htpasswd
htpasswd -c auth zyg
New password: 123456
Re-type new password: 123456#生成加密
kubectl create secret generic basic-auth --from-file=auth
basic-auth.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: ingress-authannotations:
#開啟認(rèn)證模塊的配置nginx.ingress.kubernetes.io/auth-type: basic
#設(shè)置認(rèn)證類型為basic,這是k8s自帶的認(rèn)證加密的模塊nginx.ingress.kubernetes.io/auth-secret: basic-auth
#把認(rèn)證的加密模塊導(dǎo)入到ingress當(dāng)中nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required -lyw'
#設(shè)置認(rèn)證窗口的提示信息。
spec:rules:- host: www.zyg1.comhttp:paths:- path: /pathType: Prefixbackend:service:name: nginx-svcport:number: 80
?訪問
https://www.zyg1.com:31505
重定向
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: nginx-rewriteannotations:nginx.ingress.kubernetes.io/rewrite-target: https://www.zyg1.com:31505
#訪問頁面會(huì)跳轉(zhuǎn)到指定的頁面。
spec:rules:- host: www.liukgc.comhttp:paths:- path: /pathType: Prefixbackend:service:name: nginx-svcport:number: 80
kubectl apply -f ingress-rewirte.yaml
訪問
??https://www.liukgc.com:31505
traefik
traefik ingress-controller--deployment
traefik是一個(gè)為了讓部署微服務(wù)更加快捷而誕生的一個(gè)http方向代理,負(fù)載均衡,
traefix設(shè)計(jì)時(shí)就能夠?qū)崟r(shí)的和k8s api交互,感知后端口service以及pod的變化,可以自動(dòng)更新配置和重載。
可以自帶感知后端變化
traefik的部署方式:
deamonset
優(yōu)點(diǎn)-特點(diǎn):每個(gè)節(jié)點(diǎn)都會(huì)部署一個(gè)traefik,節(jié)點(diǎn)感知可以自動(dòng)發(fā)現(xiàn),更新容器的配置。不需要手動(dòng)重載
缺點(diǎn):占用資源大,大型集群中,deamonset可能會(huì)運(yùn)行多個(gè)traefik實(shí)例,尤其是節(jié)點(diǎn)上不需要大量容器運(yùn)行的情況下,無法擴(kuò)縮容
部署對(duì)外集群,對(duì)外的業(yè)務(wù)會(huì)經(jīng)常變更,deamonset可以更好的發(fā)現(xiàn)服務(wù)配置變更
deployment
優(yōu)點(diǎn):集中辦公控制,可以使用少量的實(shí)例來運(yùn)行處理整個(gè)集群的流量。更容易升級(jí)和維護(hù)。
缺點(diǎn):deployment的負(fù)載均衡不會(huì)均分到每個(gè)節(jié)點(diǎn)。需要手動(dòng)更新。他無法感知容器內(nèi)部配置的變化。
部署對(duì)內(nèi)集群:對(duì)內(nèi)的相對(duì)穩(wěn)定,更新和變化也比較少,適合deployment.
traffic-type:internal 對(duì)內(nèi)服務(wù)
traffic-type:external 對(duì)外服務(wù)
nginx-ingress與traefix-ingress相比較
nginx-ingress 相對(duì)較慢
traefix-ingress 自帶更新的的重載快,更方便
工作原理都一樣,都是7層代理,都可以動(dòng)態(tài)的更新配置,都可以自動(dòng)發(fā)現(xiàn)服務(wù)
traefik的并發(fā)能力只有nginx-ingress的6成 60%
nginx-ingress-controller
ui訪問
#權(quán)限
wget https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-rbac.yaml#deamonset的
wget https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-ds.yaml#deployment的
wget https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-deployment.yaml#ui的
wget https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/ui.yaml
kubectl apply -f traefik-rbac.yaml
kubectl apply -f traefik-deployment.yaml
kubectl apply -f ui.yaml
訪問ui
http://192.168.10.10:30488/dashboard/
deployment部署
123.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-traefixlabels:nginx: traefik
spec:replicas: 3selector:matchLabels:nginx: traefiktemplate:metadata:labels:nginx: traefikspec:containers:- name: nginximage: nginx:1.22
---
apiVersion: v1
kind: Service
metadata:name: nginx-traefix-svc1
spec:ports:- port: 80targetPort: 80protocol: TCPselector:nginx: traefik
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: nginx-traefix-test1
spec:rules:- host: www.yyw.comhttp:paths:- path: /pathType: Prefixbackend:service:name: nginx-traefix-svc1port:number: 80
做映射
訪問
http://www.yyw.com:30227/
總結(jié)
nginx-ingress-controller
deployment+loadbalancer:要公有云提供的負(fù)載均衡的公網(wǎng)地址
daemonset+hostbnetwork+nodeselector: 和節(jié)點(diǎn)服務(wù)共享網(wǎng)絡(luò),一個(gè)節(jié)點(diǎn)只能部署一個(gè)controller pod,使用宿主機(jī)的端口性能最好,適合大并發(fā)
deployment+NodePort:最常見、最常用,最簡(jiǎn)單的方法。但行呢個(gè)不太好,多了一層nat地址轉(zhuǎn)發(fā)
適用于大并發(fā)
traefik-ingress-controller:
daemonset 對(duì)外 可以自動(dòng)更新容器的配置 host節(jié)點(diǎn)網(wǎng)絡(luò) deployment 對(duì)內(nèi) 無法自動(dòng)自動(dòng)更新配置 Nodeport
適用于小的集群,并發(fā)是ingress的
https: 1.生成證書密鑰 2.創(chuàng)建secret,保存證書和密鑰
3.創(chuàng)建ingress把secret導(dǎo)入
加密認(rèn)證: 1、htpasswd -c auth 認(rèn)證文件只能是auth 2、 創(chuàng)建ingress:
nginx.ingress.kubernets.io/auth-type: basic
#聲明認(rèn)證類型
nginx.ingress.kubernets.io/auth-secret: basic-auth
#導(dǎo)入認(rèn)證的密鑰文件,sercet的方式存儲(chǔ)集群當(dāng)中
重定向:
nginx.ingress.kubernetes.io/rewrite-target: https://123ccc.com:31505
在ingress文件當(dāng)中聲明的URI都會(huì)跳轉(zhuǎn)到這個(gè)地址