# 20210923 二次搭建ingress
详细版本创建ingress:
# ---------------- 一 开篇测试安装nginx-ingress 和 测试
# 环境介绍
系统centos7.6 阿里云 kubeadm方式安装 kubelet版本-1.19.6
# 提前准备 负载均衡 DNS服务器 搭建
亲测可用 centos keepalive版-DNS服务器(dnsmasq+nginx)负载均衡服务器_yuezhilangniao的博客-CSDN博客
# 安装kubard 1.19版本对应的 dashboard 和 nginx-ingress
http://press.demo.kuboard.cn/install/history-k8s/install-k8s-1.19.x.html
# 开始测试nginx
https://kuboard.cn/learning/k8s-intermediate/service/ingress.html#ingress
# 定义我自己的 nginx 配置和页面
cat > nginx/conf.d/default.conf << EOF
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /demo {
alias /usr/share/nginx/html/demo;
index demo.html index.htm;
}
location /abc {
alias /usr/share/nginx/html/abc;
index abc.html index.htm;
}
}
EOF
# 静态页面目录树结构
tree /usr/share/nginx/html/
# /usr/share/nginx/html/
# ├── 50x.html
# ├── abc
# │ └── abc.html
# ├── abc.html
# ├── demo
# │ └── demo.html
# ├── index.html
# 构建镜像
docker pull nginx
ls . # 拷贝 /usr/shart/nginx/html /etc/nginx dockerfile所在目录
dockerfile html nginx
# 编写dockerfile
cat > dockerfile << EOF
FROM nginx
ADD html /usr/share/nginx/html
ADD nginx /etc/nginx
EOF
# 打包镜像 scp到k8s-master node节点 或上传仓库
docker build -t nginx-diy:v1 .
docker save -o nginx-diy.tar nginx-diy:v1
# k8s-maser主机操作 导入镜像 修改yaml文件image镜像
docker load < nginx-diy.tar
kubectl apply -f nginx-deployment.yaml # 还有一个set 直接升级的操作 下次再说
# 最后配合 location-ingress 后缀是容器自己实现的 我啥都没干。。。
# ---------------- 二 继续 研究原始nginx location rewrite
# location推荐文章:nginx配置location及rewrite规则重写_一路向前ylc-CSDN博客
location = / {
# 精确匹配 / ,主机名后面不能带任何字符串
[ configuration A ]
}
location / {
# 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
# 但是正则和最长字符串会优先匹配
[ configuration B ]
}
location /documents/ {
# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
[ configuration C ]
}
location ~ /documents/Abc {
# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
[ configuration CC ]
}
location ^~ /images/ {
# 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配所有以 gif,jpg或jpeg 结尾的请求
# 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则
[ configuration E ]
}
location /images/ {
# 字符匹配到 /images/,继续往下,会发现 ^~ 存在
[ configuration F ]
}
location /images/abc {
# 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
# F与G的放置顺序是没有关系的
[ configuration G ]
}
location ~ /images/abc/ {
# 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用
[ configuration H ]
}
location ~* /js/.*/\.js
例子一(域名跳转):
server {
listen 80;
server_name abc.com;
rewrite ^/(.*) http://www.ab c.com/$1 permanent; # 跳转到www.abc.com网址上
}
例子二:
server {
listen 80;
server_name www.myweb.com www.web.info
if($host ~ myweb\.info){ #"."需要使用“\”转义,这里是匹配到www.web.info时
rewrite ^(.*) http://www.myweb.com/&1 permanent; #永久重定向到http://www.myweb.com网址上&1是匹配的uri
}
}
例子三(防盗链):
location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked www.vison.com www.wsvison.com; #这里表示Referer头域中的值是none或者blocked或者后面这些网址才会返回去正常的gif|jpg|png|swf|flv文件,否则执行下面if块代码
if ($invalid_referer) { #上面没有匹配成功,$invalid_referer值为1,否则为0
return 404;
} //防盗链
}
其他例子:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
} //如果UA包含"MSIE",rewrite请求到/msid/目录下
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
set $id $1;
} //如果cookie匹配正则,设置变量$id等于正则引用部分
if ($request_method = POST) {
return 405;
} //如果提交方法为POST,则返回状态405(Method not allowed)。return不能返回301,302
if ($slow) {
limit_rate 10k;
} //限速,$slow可以通过 set 指令设置
if (!-f $request_filename){
break;
proxy_pass http://127.0.0.1;
} //如果请求的文件名不存在,则反向代理到localhost 。这里的break也是停止rewrite检查
if ($args ~ post=140){
rewrite ^ http://example.com/ permanent;
} //如果query string中包含"post=140",永久重定向到example.com