案例說明:靜态頁面直接通路nginx, 動态請求通過tomcat
架構圖如下:
nginx服務的配置檔案nginx.yaml如下:
kind: Deployment
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
metadata:
labels:
app: linux66-nginx-deployment-label
name: linux66-nginx-deployment
namespace: linux66
spec:
replicas: 1
selector:
matchLabels:
app: linux66-nginx-selector
template:
metadata:
labels:
app: linux66-nginx-selector
spec:
containers:
- name: linux66-nginx-container
image: nginx
#command: ["/apps/tomcat/bin/run_tomcat.sh"]
imagePullPolicy: IfNotPresent
#imagePullPolicy: Always
ports:
- containerPort: 80
protocol: TCP
name: http
- containerPort: 443
protocol: TCP
name: https
env:
- name: "password"
value: "123456"
- name: "age"
value: "18"
# resources:
# limits:
# cpu: 2
# memory: 2Gi
# requests:
# cpu: 500m
# memory: 1Gi
---
kind: Service
apiVersion: v1
metadata:
labels:
app: linux66-nginx-service-label
name: linux66-nginx-service
namespace: linux66
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
nodePort: 30006
- name: https
port: 443
protocol: TCP
targetPort: 443
nodePort: 30443
selector:
app: linux66-nginx-selector
tomcat服務的配置檔案tomcat.yaml如下:
root@k8s-master:~/yaml/1202# cat tomcat.yaml
kind: Deployment
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
metadata:
labels:
app: linux66-tomcat-app1-deployment-label
name: linux66-tomcat-app1-deployment
namespace: linux66
spec:
replicas: 1
selector:
matchLabels:
app: linux66-tomcat-app1-selector
template:
metadata:
labels:
app: linux66-tomcat-app1-selector
spec:
containers:
- name: linux66-tomcat-app1-container
image: tomcat:7.0.94-alpine
#command: ["/apps/tomcat/bin/run_tomcat.sh"]
imagePullPolicy: IfNotPresent
#imagePullPolicy: Always
ports:
- containerPort: 8080
protocol: TCP
name: http
env:
- name: "password"
value: "123456"
- name: "age"
value: "18"
resources:
limits:
cpu: 1
memory: "512Mi"
requests:
cpu: 500m
memory: "512Mi"
---
kind: Service
apiVersion: v1
metadata:
labels:
app: linux66-tomcat-app1-service-label
name: linux66-tomcat-app1-service
namespace: linux66
spec:
#type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
#nodePort: 40003
selector:
app: linux66-tomcat-app1-selector
建立namespace
kubectl create ns linux66
添加tomcat頁面
root@k8s-master:~/yaml/1202# kubectl exec -it -n linux66 linux66-tomcat-app1-deployment-7946f87f8f-28r7b bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
bash-4.4# pwd
/usr/local/tomcat
bash-4.4# ls
BUILDING.txt NOTICE RUNNING.txt include native-jni-lib work
CONTRIBUTING.md README.md bin lib temp
LICENSE RELEASE-NOTES conf logs webapps
bash-4.4# cd webapps/
bash-4.4# ls
ROOT docs examples host-manager manager
bash-4.4# mkdir login
bash-4.4# ls
ROOT docs examples host-manager login manager
bash-4.4# echo "<h1>Login Web Page</h1>" >login/index.jsp
bash-4.4# exit
修改haproxy配置
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
listen k8s-cluster-01-6443
bind 172.31.7.188:6443
mode tcp
server k8s-master.host.com 172.31.7.2:6443 check inter 3s fall 3 rise 1
#增加如下配置,通路80的時候通過vip轉到主控端的80端口
listen k8s-linux66-nginx-80
bind 172.31.7.189:80
mode tcp
server k8s-master.host.com 172.31.7.2:30006 check inter 3s fall 3 rise 1
驗證結果
修改nginx配置,通路login時跳轉到tomcat服務,一般是把這部配置設定置打到鏡像裡面。
root@linux66-nginx-deployment-6bb84c7d7b-jxfnc:/# cat /etc/nginx/conf.d/default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#增加如下配置,通路login頁面跳車到tomcat,位址通tomcat的svc位址,最好寫全稱
location /login{
proxy_pass http://linux66-tomcat-app1-service.linux66.svc.cluster.local;
}
#nginx -s reload
靜态頁面直接通過nginx通路