Ingress
1、在master上执行
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.20.0/deploy/mandatory.yaml wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/provider/baremetal/service-nodeport.yaml #对外提供服务,如果不需要可以不下载
2、下载替换defaultbackend-amd64和nginx-ingress-controller镜像地址,速度也会更快!
[root@master ingress-nginx]# sed -i 's#k8s.gcr.io/defaultbackend-amd64#registry.cn-qingdao.aliyuncs.com
/kubernetes_xingej/defaultbackend-amd64#g' mandatory.yaml [root@master ingress-nginx]# sed -i 's#quay.io/kubernetes-ingress-controller/nginx-ingress-controller#
registry.cn-qingdao.aliyuncs.com/kubernetes_xingej/nginx-ingress-controller#g' mandatory.yaml
3、修改service-nodeport.yaml文件,添加NodePort端口,默认为随机端口
[root@master ingress-nginx]# cat service-nodeport.yaml apiVersion: v1 kind: Service metadata: name: ingress-nginx namespace: ingress-nginx labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx spec: type: NodePort ports: - name: http port: 80 targetPort: 80 protocol: TCP nodePort: 30080 - name: https port: 443 targetPort: 443 protocol: TCP nodePort: 30443 selector: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx
4、执行service-nodeport.yaml和mandatory.yaml两个文件
[root@k8s-master ingress-nginx]# kubectl apply -f mandatory.yaml [root@k8s-master ingress-nginx]# kubectl apply -f service-nodeport.yaml
5、打开浏览器验证,正常
6、创建后端服务,这里我们已nginx为服务为例,创建一个nginx和跟nginx对应的service,这里要注意metadata.name要
和后面创建的ingress中的serviceName一致,切记!
[root@master myself]# cat mypod.yaml apiVersion: v1 kind: Service metadata: name: service-nginx namespace: default spec: selector: app: mynginx ports: - name: http port: 80 targetPort: 80 --- apiVersion: apps/v1 kind: Deployment metadata: name: mydepoy namespace: default spec: replicas: 5 selector: matchLabels: app: mynginx template: metadata: labels: app: mynginx spec: containers: - name: mycontainer image: lizhaoqwe/nginx:v1 imagePullPolicy: IfNotPresent ports: - name: nginx containerPort: 80
有了前端了,也有后端了,那么接下来就该创建ingress规则了
7、创建ingress规则
[root@master myself]# cat ingress-nginx.yaml apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-mynginx namespace: default annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - host: mynginx.fengzi.com http: paths: - path: backend: serviceName: service-nginx servicePort: 80
8、在本地电脑C:WindowsSystem32driversetchosts添加一条hosts记录(mynginx.fengzi.com 192.168.254.13)然后打开浏览器验证
9、我们可以去查看nginx的配置文件,去查看我们所创建的规则有没有注入到ingress中
#查看ingress-controller中的规则 [root@master myself]# kubectl get pods -n ingress-nginx NAME READY STATUS RESTARTS AGE default-http-backend-7fccc47f44-sgj6g 1/1 Running 0 140m nginx-ingress-controller-d786fc9d4-4vb5z 1/1 Running 0 140m [root@master myself]# kubectl exec -it nginx-ingress-controller-d786fc9d4-4vb5z -n ingress-nginx -- /bin/bash www-data@nginx-ingress-controller-d786fc9d4-4vb5z:/etc/nginx$ cat nginx.conf
我们可以看到nginx配置文件中已经有了我们所定义的反代规则
ok,成功!!!