您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
36、 Service Ingress
发布时间:2022-12-29 23:11:27编辑:雪饮阅读()
Step1 配置https
接着上篇
首先创建证书
[root@k8s-master01 ingress]# cd /root
[root@k8s-master01 ~]# mkdir https
[root@k8s-master01 ~]# cd https
[root@k8s-master01 https]#
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=nginxsvc/O=nginxsvc"
然后kubectl根据生成的证书创建证书
[root@k8s-master01 https]# kubectl create secret tls tls-secret --key tls.key --cert tls.crt
secret/tls-secret created
然后我们创建https的yaml如:
[root@k8s-master01 https]# cat https.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: deployment3
spec:
replicas: 2
template:
metadata:
labels:
name: nginx3
spec:
containers:
- name: nginx3
image: wangyanglinux/myapp:v3
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: svc-3
spec:
ports:
- port: 80
targetPort : 80
protocol: TCP
selector:
name: nginx3
然后创建
[root@k8s-master01 https]# kubectl apply -f https.yaml
deployment.extensions/deployment3 created
service/svc-3 created
可以看到有svc-3了
[root@k8s-master01 https]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15d
nginx-dm NodePort 10.97.54.233 <none> 80:32033/TCP 47h
svc-3 ClusterIP 10.105.243.106 <none> 80/TCP 61s
并且访问起来没有问题
[root@k8s-master01 https]# curl 10.105.243.106
Hello MyApp | Version: v3 | <a href="hostname.html">Pod Name</a>
然后我们创建https的ingress
[root@k8s-master01 https]# cat https-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: https
spec:
tls:
- hosts:
- www3.atguigu.com
secretName: tls-secret
rules:
- host: www3.atguigu.com
http:
paths:
- path: /
backend:
serviceName: svc-3
servicePort: 80
然后创建
[root@k8s-master01 https]# kubectl apply -f https-ingress.yaml
ingress.extensions/https created
然后这里端口就是30798咯
[root@k8s-master01 https]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx NodePort 10.110.31.115 <none> 80:32265/TCP,443:30798/TCP 18m
然后win11的hosts上面解析www3.atguigu.com到master的192.168.66.10
然后浏览器果然可以https访问
https://www3.atguigu.com:30798
不受信任的证书也正常,自己点击继续即可,毕竟属于是自签证书咯。
Step2
配置basic-auth
下载basic-auth的依赖
yum -y install httpd
然后咱们开始配置
[root@k8s-master01 https]# cd ..
[root@k8s-master01 ~]# mkdir basic-auth
[root@k8s-master01 ~]# cd basic-auth/
[root@k8s-master01 basic-auth]# htpasswd -c auth foo
New password:
Re-type new password:
Adding password for user foo
这里foo是用户名,应该是可以随便设置的,密码的话就是你自己输入的密码,要输入两次
然后执行这一步骤
[root@k8s-master01 basic-auth]# kubectl create secret generic basic-auth --from-file=auth
secret/basic-auth created
这里—from-file=auth
据说这个auth不能变,可能是硬性编码,所以注意下,否则可能出现访问时候变成503错误,或者这一整个步骤忘了执行则访问时候也会出现503错误的。
接下来我们需要用到昨天的svc1,所以也创建下
[root@k8s-master01 basic-auth]# cat /usr/local/install-k8s/plugin/ingress/ingress-vh/ingress.http2.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: deployment1
spec:
replicas: 2
template:
metadata:
labels:
name: nginx
spec:
containers:
- name: nginx
image: wangyanglinux/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: svc-1
spec:
ports:
- port: 80
targetPort : 80
protocol: TCP
selector:
name: nginx
[root@k8s-master01 basic-auth]# kubectl apply -f /usr/local/install-k8s/plugin/ingress/ingress-vh/ingress.http2.yaml
deployment.extensions/deployment1 created
service/svc-1 created
然后咱们创建basic-auth的yaml的模板指向刚才的svc-1上面
[root@k8s-master01 basic-auth]# cat auth.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-with-auth
annotations:
nginx.ingress.kubernetes.io/auth-type: basic
nginx.ingress.kubernetes.io/auth-secret: basic-auth
nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - foo'
spec:
rules:
- host: auth.atguigu.com
http:
paths:
- path: /
backend:
serviceName: svc-1
servicePort: 80
然后创建这个basic-auth
[root@k8s-master01 basic-auth]# kubectl apply -f auth.yaml
ingress.extensions/ingress-with-auth created
由于这里只是basic-auth指向svc-1,没有特别配置证书,所以这里我们是用端口32265咯
[root@k8s-master01 basic-auth]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx NodePort 10.110.31.115 <none> 80:32265/TCP,443:30798/TCP 36m
win11中增加hosts解析
192.168.66.10 auth.atguigu.com
然后浏览器访问就没有问题了
用户名就是刚才的foo,密码就是你刚才设置的密码
其实这里就是关于basic-auth重要的字段就是上面我标记的红色的那三段配置,其他则和普通的ingress配置一样咯,就是说那个ingress需要basic-auth则那个就启用这三段即可咯
Step3
这次的任务就是我们访问一个非https的链接或者说是域名然后自动跳转到一个https的链接或者说是域名中。
我们这里则拟定re.atgui.com跳转到www3.atguigu.com
[root@k8s-master01 basic-auth]# cd ..
[root@k8s-master01 ~]# mkdir re
[root@k8s-master01 ~]# cd re
创建跳转用的ingress模板
[root@k8s-master01 re]# cat re.yaml
apiVersion : extensions/v1beta1
kind: Ingress
metadata:
name: nginx-test
annotations:
nginx.ingress.kubernetes.io/rewrite-target: https://www3.atguigu.com:30798
spec:
rules:
- host: re.atguigu.com
http:
paths:
- path: /
backend:
serviceName: svc-1
servicePort: 80
这里svc据说是随便链接那个都可以。。。甚至可以不用链接
但是我觉得肯定不能随便链接吧,不用链接我则不晓得,现在先链接之前那个svc-1吧。那个也是我们上面用basic-auth的非https的,不过貌似这东西不分是不是https,是不是https则是取决于ingress哈。。。
然后创建这个新的支持http跳转到https的ingress哈
[root@k8s-master01 re]# kubectl apply -f re.yaml
ingress.extensions/nginx-test created
然后win11的hosts中追加解析如
192.168.66.10 re.atguigu.com
然后访问re.atgui.com果然到了https://www3.atguigu.com:30798了哈
关键字词:Service,Ingress