k8s的ingress安装和简单使用

/ k8s / 没有评论 / 127浏览

官方文档:

https://kubernetes.github.io/ingress-nginx/

image

quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.25.0

创建基础配置

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml

查看ingress-controller运行

[root@k8s-master ingress]#  kubectl get pods -n ingress-nginx
NAME                                        READY   STATUS    RESTARTS   AGE
nginx-ingress-controller-7995bd9c47-csx2g   1/1     Running   0          21h

pod状态

[root@k8s-master ingress]# kubectl get pods --all-namespaces -o wide |grep ingress
default                nginx-ingress-controller-65c48679d6-rjrr8       1/1     Running   0          21h    10.244.0.44     k8s-master   <none>           <none>
default                nginx-ingress-default-backend-b7b99f759-qkfjn   1/1     Running   0          21h    10.244.0.45     k8s-master   <none>           <none>

查看service

[root@k8s-master ingress]# kubectl get svc -n ingress-nginx
NAME            TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx   LoadBalancer   10.96.188.209   <pending>     80:32698/TCP,443:31402/TCP   22h

 至此ingress-controller安装完成

demo

Nginx demo,使用ingress的域名访问

[root@k8s-master nginx]# cat nginx-http.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-test-project
  namespace: nginx-test
  labels:
    app: nginx-test-project
spec:
  replicas: 5
  template:
    metadata:
      name: nginx-test-project
      labels:
        app: nginx-test-project
    spec:
      imagePullSecrets:
        - name: esxi-harbor
      containers:
        - name: nginx-test-project
          image: 192.168.0.241/nginx/nginx:v1
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
      restartPolicy: Always
  selector:
    matchLabels:
      app: nginx-test-project
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-test-project
  name: nginx-test-project
  namespace: nginx-test
spec:
  ports:
    - name: http
      port: 80
  selector:
    app: nginx-test-project
  sessionAffinity: None
  type: NodePort

apply yaml

[root@k8s-master nginx]# kubectl apply -f nginx-http.yaml

service list

这里要注意的一点是,service所在的ns 和 端口

[root@k8s-master nginx]# kubectl get service --all-namespaces
NAMESPACE              NAME                            TYPE           CLUSTER-IP       EXTERNAL-IP    PORT(S)                      AGE
default                demo                            NodePort       10.104.222.97    <none>         80:30518/TCP                 2d12h
default                kubernetes                      ClusterIP      10.96.0.1        <none>         443/TCP                      23d
default                nginx-ingress-controller        LoadBalancer   10.102.42.22     192.168.0.89   80:32411/TCP,443:30410/TCP   2d12h
default                nginx-ingress-default-backend   ClusterIP      10.102.90.7      <none>         80/TCP                       2d12h
esxi-test-http-py      lijinghua-test-project          NodePort       10.111.212.234   <none>         8000:31643/TCP               6d23h
ingress-nginx          ingress-nginx                   LoadBalancer   10.96.188.209    <pending>      80:32698/TCP,443:31402/TCP   2d13h
kube-system            kube-dns                        ClusterIP      10.96.0.10       <none>         53/UDP,53/TCP,9153/TCP       23d
kube-system            tiller-deploy                   ClusterIP      10.109.176.162   <none>         44134/TCP                    14d
kubernetes-dashboard   dashboard-metrics-scraper       ClusterIP      10.107.174.25    <none>         8000/TCP                     23d
kubernetes-dashboard   kubernetes-dashboard            NodePort       10.100.164.118   <none>         443:32357/TCP                23d
nginx-test             nginx-test-project              NodePort       10.100.18.172    <none>         80:30938/TCP                 40h

ingress apply yaml

[root@k8s-master ingress]# cat nginx-http-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-test-project
  namespace: nginx-test
spec:
  rules:
  - host: nginx-http.k8s.ingress
    http:
      paths:
      - backend:
          serviceName: nginx-test-project
          servicePort: 80
[root@k8s-master ingress]# kubectl apply -f nginx-http-ingress.yaml

ingress list

记住 namespace 的区别,得到域名是 nginx-http.k8s.ingress

[root@k8s-master ingress]# kubectl get ingress  -n nginx-test
NAME                 HOSTS                    ADDRESS   PORTS   AGE
nginx-test-project   nginx-http.k8s.ingress             80      40h

Local access

本季解析

➜  ~ cat /etc/hosts |grep ingress
192.168.0.89 nginx-http.k8s.ingress

demo