k8s-nginx
根据ip访问首先创建nginx的docker-compose.ymlversion: '3.1'services:nginx:restart: alwaysimage: nginx:1.17container_name: nginxports:- 80:80- 8080:8080volumes:...
·
根据ip访问
首先创建nginx的docker-compose.yml
version: '3.1'
services:
nginx:
restart: always
image: nginx:1.17
container_name: nginx
ports:
- 80:80
- 8080:8080
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./html:/usr/share/nginx/html
接着创建nginx.conf
# 启动进程 通常设置成和CPU的数量相等
worker_processes 2;
events {
#epoll 是多路复用IO(I/O Multiplexing)中的一种方式
#但是仅用于linux2.6 以上内核 可以大大提高nginx的性能
use epoll;
#单个后台worker porcess 进程的最大并发链接数
worker_connections 1024;
}
http {
# 设定 mime 类型 类型有mime.type 文件定义
include mime.types;
default_type application/octet-stream;
# sendfile 指令指定nginx是否调用sendfile 函数(zero copy 方式)来输出文件, 对于普通应用,
# 必须设为 on 如果用来进行下载等应用磁盘IO重负载应用,可设置为off 以平衡磁盘与网络I/O处理速度,降低系统的uptime
sendfile on;
#连接超时时间
keepalive_timeout 65;
#设置请求缓冲
client_header_buffer_size 2k;
#配置虚拟主机 192.168.113.60
server {
#监听的IP 和端口 配置 192.168.113.60:80
listen 80;
# 虚拟主机名称这里配置 IP 地址
server_name 192.168.113.60;
#所有的请求都以 / 开始 所有的请求都可以匹配此location
location / {
#使用 root 指令指定虚拟主机目录即网页存放目录
#比如访问 http://ip/index.html 将找到 /usr/local/docker/nginx/html/html80/index.html
#比如访问 http://ip/item/index.html 将找到 /usr/local/docekr/nginx/html/html80/item/index.html
root /usr/share/nginx/html/html80;
# 指定欢迎页面,按照从左到右顺序查找
index index.html index.htm
}
}
server {
listen 8080;
server_name 192.168.113.60;
location / {
root /usr/share/nginx/html/html8080;
index index.html index.htm
}
}}
在相对应界面配置html(数据卷)
接着在docker-compose.yml下
docker-compose up -d
接着就可以去192.168.113.60:8080 或者80进行访问了
根据域名访问
worker_processes 2;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
client_header_buffer_size 2k;
server {
listen 80;
server_name service1.zql.com;
location / {
root /usr/share/nginx/html/html80;
index index.html index.htm
}
}
server {
listen 80;
server_name service2.zql.com;
location / {
root /usr/share/nginx/html/html8080;
index index.html index.htm
}
}}
然后用SwitchHosts 里面添加hosts
然后根据域名进行访问
反向代理
将docker-compose.yml 增加配置
version: '3.1'
services:
nginx:
restart: always
image: nginx:1.17
container_name: nginx
ports:
- 80:80
- 8080:8080
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./html:/usr/share/nginx/html
# 配置了两个tomcat服务
tomcat1:
image: tomcat
container_name: tomcat1
tomcat2:
image: tomcat
container_name: tomcat2
修改nginx.yml配置文件
worker_processes 2;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
client_header_buffer_size 2k;
upstream tomcatServer1{
server tomcat1:8080;
}
upstream tomcatServer1{
server tomcat2:8080;
}
server {
listen 80;
server_name service1.zql.com;
location / {
proxy_pass http://tomcatServer2;
index index.html index.htm;
}
}
server {
listen 80;
server_name service2.zql.com;
location / {
proxy_pass http://tomcatServer2;
index index.html index.htm;
}
}
}
接着运行docker-compose.yml
用service1.zql.com或者service2.zql.com访问
负载均衡
修改 nginx.conf 文件
worker_processes 2;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
client_header_buffer_size 2k;
upstream myapp1{
server tomcat1:8080 weight=10;
server tomcat2:8080 weight=10;
}
server {
listen 80;
server_name service1.zql.com;
location / {
proxy_pass http://myapp1;
index index.html index.htm;
}
}
}
然后交互式进入tomcat去修改index.jsp文件
docker exec -it tomcat1 /bin/bash
交互式页面内查看文件用
ls -al
然后刷新 service1.zql.com
Nginx Ingress Controller
在kubernetes文件夹下创建文件夹 ingress
安装 Ingress
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml
修改配置文件
部署
kubectl apply -f mandatory.yaml
查看
kubectl get pods -n ingress-nginx -o wide
如果没有启动成功 删除重新部署
kubectl delete -f <YAML>
接着创建ingress.yml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: nginx-web
annotations:
# 指定 Ingress Controller 的类型
kubernetes.io/ingress.class: "nginx"
# 指定我们的 rules 的 path 可以使用正则表达式
nginx.ingress.kubernetes.io/use-regex: "true"
# 连接超时时间,默认为 5s
nginx.ingress.kubernetes.io/proxy-connect-timeout: "600"
# 后端服务器回转数据超时时间,默认为 60s
nginx.ingress.kubernetes.io/proxy-send-timeout: "600"
# 后端服务器响应超时时间,默认为 60s
nginx.ingress.kubernetes.io/proxy-read-timeout: "600"
# 客户端上传文件,最大大小,默认为 20m
nginx.ingress.kubernetes.io/proxy-body-size: "10m"
# URL 重写
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
# 路由规则
rules:
# 主机名,只能是域名,修改为你自己的
- host: k8s.test.com
http:
paths:
- path:
backend:
# 后台部署的 Service Name,与上面部署的 Tomcat 对应
serviceName: tomcat-http
# 后台部署的 Service Port,与上面部署的 Tomcat 对应
servicePort: 8080
创建tomcat.yml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: tomcat-app
spec:
replicas: 2
template:
metadata:
labels:
name: tomcat
spec:
containers:
- name: tomcat
image: tomcat:8.5.43
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: tomcat-http
spec:
ports:
- port: 8080
targetPort: 8080
# ClusterIP, NodePort, LoadBalancer
type: ClusterIP
selector:
name: tomcat
启动
kubectl apply -f tomcat.yml
查看容器
kubectl get pods
查看部署
kubectl get deployment
查看服务
kubectl get service
接着去SwitchHosts
添加
然后k8s.test.com进行访问
更多推荐
已为社区贡献3条内容
所有评论(0)