k8s 1.23.0 安装使用ingress 1.1.1
1、部署ingress controller下载yaml文件,要指定版本
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.1/deploy/static/provider/cloud/deploy.yaml
2、修改配置文件中的镜像下载地址,总共3处修改
image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.1.1 imagePullPolicy: IfNotPresent image: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1 imagePullPolicy: IfNotPresent image: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1 imagePullPolicy: IfNotPresent
3、将deploy配置文件修改type为NodePort,如果不修改在查看svc时,会看到type字段显示为LoadBalancer
4、配置文件修改完成后,直接apply就可以,镜像会自动拉取
kubectl apply -f deploy.yaml
5、采用nodePort的方式进行暴露,编写service-nodeport.yaml,然后apply
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: 80 - name: https port: 443 targetPort: 443 protocol: TCP selector: app.kubernetes.io/name: ingress-nginx #app.kubernetes.io/part-of: ingress-nginx
6、查看service-nodeport.yaml生成的ingress-nginx对应的pod
7、创建nginx和tomcat的pod,编写nginx-tomcat-deployment.yaml,然后apply
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment namespace: ingress-nginx spec: replicas: 3 selector: matchLabels: app: nginx-pod template: metadata: labels: app: nginx-pod spec: containers: - name: nginx-container image: nginx:latest ports: - name: nginx-port containerPort: 80 protocol: TCP --- apiVersion: apps/v1 kind: Deployment metadata: name: tomcat-deployment namespace: ingress-nginx spec: replicas: 3 selector: matchLabels: app: tomcat-pod template: metadata: labels: app: tomcat-pod spec: containers: - name: tomcat-container image: tomcat:8.5-jre10-slim ports: - name: tomcat-port containerPort: 8080 protocol: TCP
8、创建nginx和tomcat的service,编写nginx-tomcat-service.yaml,然后apply
apiVersion: v1 kind: Service metadata: name: nginx-service namespace: ingress-nginx spec: selector: app: nginx-pod type: ClusterIP ports: - protocol: TCP port: 80 targetPort: 80 --- apiVersion: v1 kind: Service metadata: name: tomcat-service namespace: ingress-nginx spec: selector: app: tomcat-pod type: ClusterIP ports: - protocol: TCP port: 8080 targetPort: 8080
9、上述pod跟svc创建完成后,再创建对应的ingress,ingress要在svc创建完成后再创建
vim ingress-http.yaml
annotations:
kubernetes.io/ingress.class: "nginx"
注意:如果不加这一条,在外网访问的时候,可能出现404
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: ingress-http namespace: ingress-nginx annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - host: nginx.bulut.com http: paths: - path: / pathType: Prefix backend: service: name: nginx-service port: number: 80 - host: tomcat.bulut.com http: paths: - path: / pathType: Prefix backend: service: name: tomcat-service port: number: 8080
kubectl apply -f ingress.yaml
10、在win10主机添加hosts记录,192.168.18.11是k8s的master
192.168.18.11 nginx.bulut.com 192.168.18.11 tomcat.bulut.com
11、在浏览器分别访问http://nginx.bulut.com、http://tomcat.bulut.com可以看到