kubectl create namespace simple-microservice
apiVersion: apps/v1
kind: Deployment
metadata:
name: ruoyi-server
namespace: simple-microservice
spec:
replicas: 2
selector:
matchLabels:
project: simple-microservice
app: ruoyi-server
template:
metadata:
labels:
project: simple-microservice
app: ruoyi-server
spec:
imagePullSecrets:
- name: registry-pull-secert
containers:
- name: ruoyi-server
image: 192.168.17.83:5000/ruoyi-server
imagePullPolicy: Always
env:
- name: TZ
value: Asia/Shanghai
ports:
- protocol: TCP
containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: ruoyi-service
namespace: simple-microservice
spec:
type: ClusterIP
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: ruoyi-server
project: simple-microservice
执行创建pod和service
kubectl apply -f ruoyi-server.yaml
查看创建的service:
kubectl get service -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
simple-microservice ruoyi-service ClusterIP 10.10.93.11 80/TCP 21m
执行访问测试:
curl http://10.10.93.11
或者
wget http://10.10.93.11
k8s集群以外访问k8s集群可以借助于ingress进行。
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.35.0/deploy/static/provider/baremetal/deploy.yaml
# vim ingress-deploy.yaml
......
spec:
hostNetwork: true #本地网络访问
dnsPolicy: ClusterFirst
containers:
- name: controller
image: registry.aliyuncs.com/google_containers/nginx-ingress-controller:0.30.0
imagePullPolicy: IfNotPresent
......
# kubectl apply -f ingress-deploy.yaml
# kubectl get pod -o wide -n ingress-nginx
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
ingress-nginx-admission-create-zcd8m 0/1 Completed 0 111m 10.11.2.28 k8s-node83
ingress-nginx-admission-patch-v6rkx 0/1 Completed 0 111m 10.11.2.29 k8s-node83
ingress-nginx-controller-5d8b79898-nlv8q 1/1 Running 0 111m 192.168.17.83 k8s-node83
# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.10.145.224 80:32458/TCP,443:31810/TCP 27h
ingress-nginx-controller-admission ClusterIP 10.10.247.171 443/TCP 27h
出现上面信息,说明ingress部署成功了。
查看ingress-nginx-controller内部状态
# kubectl exec -it -n ingress-nginx ingress-nginx-controller-5d8b79898-nlv8q sh
cat nginx.conf
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: web-ingress
namespace: simple-microservice
spec:
rules:
- host: www.ruoyi.demo
http:
paths:
- path: /
backend:
serviceName: ruoyi-service
servicePort: 80
创建ingress转发
kubectl apply -f ingress-ruoyi.yaml
查看对应的规则
# kubectl describe ing web-ingress -n simple-microservice
Name: web-ingress
Namespace: simple-microservice
Address: 192.168.17.83
Default backend: default-http-backend:80 ()
Rules:
Host Path Backends
---- ---- --------
www.ruoyi.demo
/ ruoyi-service:80 (10.11.2.44:80)
在C:\Windows\System32\drivers\etc的hosts中,添加
192.168.17.83 www.ruoyi.demo
http://www.ruoyi.demo/
可以看到登录页显示如下:
至此,基于k8s部署服务,并通过ingress实现外部访问。
开始的时候,service的selector设置如下:
selector:
name: ruoyi-server
namespace: simple-microservice
这样导致service起来以后,无法找到匹配的pod,所以无法访问。
开始的时候,pod的副本数设置如下:
spec:
replicas: 2
这样会启动两个副本,service转发到后端的时候,默认采用轮询的算法,导致每次登录的时候都提示验证码过期,实际上是请求验证码在一个节点上,同时验证码在节点本地做了缓存,验证码的认证请求发送到另外一个节点,结果导致总是提示验证码过期。将副本设置为1个就可以正常登录了。
更合理的方式是设置合适的转发策略,确保相同来源的请求能够总是发送到相同的节点上。