k8s-nginx-ingress-1

启动ingress

1.官网下载yaml,官网地址:https://kubernetes.github.io/ 也就是去官网复制下面两句话,并在主节点上运行:
复制yaml(应该算是deployment部分):

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

复制yaml(svc部分)

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/baremetal/service-nodeport.yaml

查看pod

[root@apiserver ingerss]# kubectl get pod -n ingress-nginx 
NAME                                       READY   STATUS    RESTARTS   AGE
nginx-ingress-controller-94cd6cc55-tj9n5   1/1     Running   0          23m

查看svc(可以看到容器暴露在外部的端口是80:31866/TCP,443:30271/):

[root@apiserver ingerss]# kubectl get svc -n ingress-nginx 
NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx   NodePort   10.110.60.132   <none>        80:31866/TCP,443:30271/TCP   4m9s

ingress HTTP代理访问

创建并运行yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-dm
spec:
  replicas: 2
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:v1
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
  selector:
    name: nginx
 kubectl apply -f ingress-http.yaml 

查看svc:

[root@apiserver ingerss]# kubectl get svc
NAME             TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
nginx-svc        ClusterIP      10.104.89.251   <none>        80/TCP         16m

这时直接访问svc的地址是没有问题的:

[root@apiserver ingerss]# curl 10.104.89.251
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

下面我们想通过nginx的ingress方案把svc 的IP暴露出去,实现根据域名访问的结构:
创建yaml并运行:

apiVersion: extensions/v1beta1
kind: Ingress
metadata: 
 name: nginx-test
spec: 
  rules:
   - host: www1.kk.com
     http:
      paths:
       - path: /
         backend:
           serviceName: nginx-svc
           servicePort: 80
kubectl apply -f ingress1.yaml

查看svc

[root@apiserver etc]# kubectl get svc -n ingress-nginx 
NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx   NodePort   10.110.60.132   <none>        80:31866/TCP,443:30271/TCP   84m

上面的yaml文件中host域名修改为:www1.kk.com,为了在我主机上能访问,所以在/etc/hosts文件中添加:192.168.10.167(master IP)www1.kk.com
在网页上浏览:http://www1.kk.com:31866/(此端口为上面查到的svc的端口)
在这里插入图片描述

发布了44 篇原创文章 · 获赞 0 · 访问量 962

猜你喜欢

转载自blog.csdn.net/qq_39122146/article/details/103816415