K8S-ingress
ingress参考文档:https://segmentfault.com/a/1190000019908991
在华为云买一块硬盘,挂载到服务器并初始化:https://support.huaweicloud.com/qs-ecs/zh-cn_topic_0085634797.html
搭建nfs:
先跑起来,不然会很快失去兴趣
部署一个ingress控制器,资源清单
apiVersion: v1 kind: Namespace metadata: name: ingress-nginx labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx --- kind: ConfigMap apiVersion: v1 metadata: name: nginx-configuration namespace: ingress-nginx labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx --- kind: ConfigMap apiVersion: v1 metadata: name: tcp-services namespace: ingress-nginx labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx --- kind: ConfigMap apiVersion: v1 metadata: name: udp-services namespace: ingress-nginx labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx --- apiVersion: v1 kind: ServiceAccount metadata: name: nginx-ingress-serviceaccount namespace: ingress-nginx labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx --- apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRole metadata: name: nginx-ingress-clusterrole labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx rules: - apiGroups: - "" resources: - configmaps - endpoints - nodes - pods - secrets verbs: - list - watch - apiGroups: - "" resources: - nodes verbs: - get - apiGroups: - "" resources: - services verbs: - get - list - watch - apiGroups: - "" resources: - events verbs: - create - patch - apiGroups: - "extensions" - "networking.k8s.io" resources: - ingresses verbs: - get - list - watch - apiGroups: - "extensions" - "networking.k8s.io" resources: - ingresses/status verbs: - update --- apiVersion: rbac.authorization.k8s.io/v1beta1 kind: Role metadata: name: nginx-ingress-role namespace: ingress-nginx labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx rules: - apiGroups: - "" resources: - configmaps - pods - secrets - namespaces verbs: - get - apiGroups: - "" resources: - configmaps resourceNames: # Defaults to "- " # Here: "- " # This has to be adapted if you change either parameter # when launching the nginx-ingress-controller. - "ingress-controller-leader-nginx" verbs: - get - update - apiGroups: - "" resources: - configmaps verbs: - create - apiGroups: - "" resources: - endpoints verbs: - get --- apiVersion: rbac.authorization.k8s.io/v1beta1 kind: RoleBinding metadata: name: nginx-ingress-role-nisa-binding namespace: ingress-nginx labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: nginx-ingress-role subjects: - kind: ServiceAccount name: nginx-ingress-serviceaccount namespace: ingress-nginx --- apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRoleBinding metadata: name: nginx-ingress-clusterrole-nisa-binding labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: nginx-ingress-clusterrole subjects: - kind: ServiceAccount name: nginx-ingress-serviceaccount namespace: ingress-nginx --- apiVersion: apps/v1 kind: Deployment metadata: name: nginx-ingress-controller namespace: ingress-nginx labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx spec: replicas: 1 selector: matchLabels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx template: metadata: labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx annotations: prometheus.io/port: "10254" prometheus.io/scrape: "true" spec: serviceAccountName: nginx-ingress-serviceaccount containers: - name: nginx-ingress-controller image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.25.0 imagePullPolicy: IfNotPresent args: - /nginx-ingress-controller - --configmap=$(POD_NAMESPACE)/nginx-configuration - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services - --udp-services-configmap=$(POD_NAMESPACE)/udp-services - --publish-service=$(POD_NAMESPACE)/ingress-nginx - --annotations-prefix=nginx.ingress.kubernetes.io securityContext: allowPrivilegeEscalation: true capabilities: drop: - ALL add: - NET_BIND_SERVICE # www-data -> 33 runAsUser: 33 env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace ports: - name: http containerPort: 80 - name: https containerPort: 443 livenessProbe: failureThreshold: 3 httpGet: path: /healthz port: 10254 scheme: HTTP initialDelaySeconds: 10 periodSeconds: 10 successThreshold: 1 timeoutSeconds: 10 readinessProbe: failureThreshold: 3 httpGet: path: /healthz port: 10254 scheme: HTTP periodSeconds: 10 successThreshold: 1 timeoutSeconds: 10 ---
运行
]# kubectl apply -f mandatory.yaml namespace/ingress-nginx created configmap/nginx-configuration created configmap/tcp-services created configmap/udp-services created serviceaccount/nginx-ingress-serviceaccount created clusterrole.rbac.authorization.k8s.io/nginx-ingress-clusterrole created role.rbac.authorization.k8s.io/nginx-ingress-role created rolebinding.rbac.authorization.k8s.io/nginx-ingress-role-nisa-binding created clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-clusterrole-nisa-binding created deployment.apps/nginx-ingress-controller created
制作两个service后端服务
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } location /webapp { root html; index index.html index.htm; }
修改测试网页
cat index.html
www.magedu.com
www.rtnb.com
制作两个不同业务的镜像:搭建后端pod服务器:
后端服务资源清单
kind: Deployment apiVersion: apps/v1 metadata: labels: app: magedu-nginx-deployment-label name: magedu-nginx-deployment namespace: linux40 spec: replicas: 1 selector: matchLabels: app: magedu-nginx-selector template: metadata: labels: app: magedu-nginx-selector spec: containers: - name: magedu-nginx-container image: nginx-web1-magedu:v1 imagePullPolicy: IfNotPresent ports: - containerPort: 80 protocol: TCP name: http - containerPort: 443 protocol: TCP name: https env: - name: "password" value: "123456" - name: "age" value: "20" resources: limits: cpu: 1 memory: 512Mi requests: cpu: 200m memory: 246Mi volumeMounts: - name: magedu-images mountPath: /usr/local/nginx/html/webapp/images readOnly: false - name: magedu-static mountPath: /usr/local/nginx/html/webapp/static readOnly: false volumes: - name: magedu-images nfs: server: 192.168.0.58 path: /root/data/nginx/nfs1 - name: magedu-static nfs: server: 192.168.0.104 path: /root/data/nginx/nfs2 --- kind: Service apiVersion: v1 metadata: labels: app: magedu-nginx-service-label name: magedu-nginx-service namespace: linux40 spec: type: ClusterIP ports: - name: http port: 80 protocol: TCP targetPort: 80 selector: app: magedu-nginx-selector --- apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata: namespace: linux40 name: linux40-nginx-web1-podautoscaler labels: app: magedu-nginx-selector version: v2beta1 spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: magedu-nginx-deployment minReplicas: 2 maxReplicas: 5 targetCPUUtilizationPercentage: 60
启动服务
[root@master project]# kubectl get pods -A NAMESPACE NAME READY STATUS RESTARTS AGE ingress-nginx nginx-ingress-controller-67cff8fb65-z4cn8 1/1 Running 1 17h linux40 magedu-nginx-deployment-8589cb6dc7-mvtv6 1/1 Running 0 9s linux40 magedu-nginx-deployment-8589cb6dc7-s2tlr 1/1 Running 0 24s linux40 rtnb-nginx-deployment-7c668f7c6-jq8tg 1/1 Running 0 19s linux40 rtnb-nginx-deployment-7c668f7c6-xz7fl 1/1 Running 0 4s
查看svc
[root@master nginx]# kubectl get svc -A NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE linux40 magedu-nginx-service ClusterIP 10.98.112.18380/TCP 19m
测试
[root@master nginx]# curl 10.98.112.183 www.magedu.com test page
部署ingress,转发规则
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-test namespace: linux40 //注意,非同一个namespace,无法转发 annotations: kubernetes.io/ingress.class: "nginx" nginx.ingress.kubernetes.io/use-regex: "true" spec: rules: - host: www.magedu.com http: paths: - path: / backend: serviceName: magedu-nginx-service servicePort: 80 - path: /webapp backend: serviceName: magedu-nginx-service servicePort: 80
查看
[root@master nginx]# kubectl get ingress -n linux40 NAME HOSTS ADDRESS PORTS AGE ingress-test www.magedu.com 80 33m
查看端口
[root@master nginx]# kubectl get svc -A NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ingress-nginx ingress-nginx NodePort 10.98.53.3180:32280/TCP,443:30022/TCP 22h
测试访问,用浏览器访问
http://www.magedu.com:32280/webapp/ linux36 web1 v1 linux36 web1 v2 linux36 web1 v3
http://www.magedu.com:32280/ www.magedu.com test page
二、Ingress HTTPS 代理访问
2.1 制作证书以及secret
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=magedu-nginx-service/O=magedu-nginx-service" kubectl create secret tls tls-secret --key tls.key --cert tls.crt
2.2 ssl域名类型的域名
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress_magedu-nginx-service spec: tls: - hosts: - www.magedu.com secretName: tls-secret rules: - host: www.magedu.com http: paths: - path: / backend: serviceName: magedu-nginx-service servicePort: 80
查看端口
]# kubectl get svc -A NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE default kubernetes ClusterIP 10.96.0.1443/TCP 4d18h ingress-nginx ingress-nginx NodePort 10.98.53.31 80:32280/TCP,443:30022/TCP 2d16h
测试访问
三、Nginx 进行 BasicAuth
3.1 下载tools工具包
[root@master ingress]# yum -y install httpd-tools htpasswd -c auth foo //创建foo用户 kubectl create secret generic basic-auth --from-file=auth //创建secret
3.2 创建ingress
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 //secret 名称 nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - foo' //欢迎信息 spec: rules: - host: foo2.bar.com http: paths: - path: / backend: serviceName: nginx-svc servicePort: 80
四、Nginx 重写
资源清单
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-nginx-rewrite annotations: nginx.ingress.kubernetes.io/rewrite-target: http://www.baidu.com spec: rules: - host: rewrite.magedu.com http: //下面这块可以不用写了 paths: - path: / backend: serviceName: rtnb-nginx-service servicePort: 80
测试,可以看到跳转到了指定网页