ingress controller学习记录

创建ingress-nginx

按文档操作

wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/mandatory.yaml
kubectl apply -f mandatory.yaml
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/provider/baremetal/service-nodeport.yaml
### 在service的spec中将nodePort固定一下。
kubectl apply -f service-nodeport.yaml

deploy-demo.yaml 的内容

apiVersion: v1
kind: Service
metadata:
  name: myapp
  namespace: default
spec:
  selector:
    app: myapp
    release: canary
  ports:
  - name: http
    targetPort: 80
    port: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  namespace: default
spec:
  replicas: 3
  selector: 
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      labels:
        app: myapp
        release: canary
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v2
        ports: 
        - name: http
          containerPort: 80

创建myapp的service和myapp-deploy的Deployment。

kubectl apply -f deploy-demo.yaml

结果

[root@master ingress-nginx]# kubectl get pods -n ingress-nginx
NAME                                        READY     STATUS    RESTARTS   AGE
default-http-backend-6586bc58b6-4f6qm       1/1       Running   0          30m
nginx-ingress-controller-7675fd6cdb-njvdb   1/1       Running   0          30m
[root@master ingress-nginx]# kubectl get pods 
NAME                            READY     STATUS    RESTARTS   AGE
myapp-deploy-67f6f6b4dc-67f7f   1/1       Running   0          7m
myapp-deploy-67f6f6b4dc-hdbht   1/1       Running   0          7m
myapp-deploy-67f6f6b4dc-ktfh6   1/1       Running   0          7m
[root@master ingress-nginx]# kubectl get svc --all-namespaces
NAMESPACE       NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
default         kubernetes             ClusterIP   10.96.0.1                443/TCP                      8d
default         myapp                  ClusterIP   10.103.69.18             80/TCP                       7m
ingress-nginx   default-http-backend   ClusterIP   10.97.71.74              80/TCP                       33m
ingress-nginx   ingress-nginx          NodePort    10.110.227.164           80:30080/TCP,443:30443/TCP   31m
kube-system     kube-dns               ClusterIP   10.96.0.10               53/UDP,53/TCP                8d

此时,访问nodeIP:nodePort已经可以看到输出,如图

ingress controller学习记录_第1张图片

ingress controller学习记录_第2张图片

创建Ingress对象
ingress-myapp.yaml 的内容如下

apiVersion: extensions/v1beta1
kind: Ingress
metadata: 
  name: ingress-myapp
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: myapp.test.com
    http: 
      paths:
      - path:
        backend: 
          serviceName: myapp
          servicePort: 80

创建Ingress

[root@master ingress-nginx]# kubectl apply -f ingress-myapp.yaml 
ingress.extensions/ingress-myapp created
[root@master ingress-nginx]# kubectl get ingress
NAME            HOSTS            ADDRESS   PORTS     AGE
ingress-myapp   myapp.test.com             80        4s

创建成功后,Ingress会将信息注入到ingress-controller里面去,即:会自动转换为nginx的配置文件

[root@master ingress-nginx]# kubectl get pods -n ingress-nginx
NAME                                        READY     STATUS    RESTARTS   AGE
default-http-backend-6586bc58b6-4f6qm       1/1       Running   0          1h
nginx-ingress-controller-7675fd6cdb-njvdb   1/1       Running   0          1h
[root@master ingress-nginx]# kubectl exec -n ingress-nginx -it nginx-ingress-controller-7675fd6cdb-njvdb -- /bin/sh

$ cat nginx.conf|grep -C2 myapp
    ## end server _
    
    ## start server myapp.test.com
    server {
        server_name myapp.test.com ;
        
        listen 80;
--
            
            set $namespace      "default";
            set $ingress_name   "ingress-myapp";
            set $service_name   "myapp";
            set $service_port   "80";
            set $location_path  "/";
--
            port_in_redirect off;
            
            set $proxy_upstream_name "default-myapp-80";
            
            client_max_body_size                    "1m";
--
        
    }
    ## end server myapp.test.com
    
    # default server, used for NGINX healthcheck and access to nginx stats
$ 

可以看到pod内的nginx.conf文件确实被注入了myapp相关的信息。

此时,在本地机器绑定myapp.test.com为两个node的ip,就可以在本地机器的浏览器中访问 http://myapp.test.com:30080/ 网页了。

你可能感兴趣的:(ingress controller学习记录)