学习kubernetes——部署Ingress

一、安装Ingress

  官方文档:https://kubernetes.github.io/ingress-nginx/deploy/

  由于证书问题,我们可以把ingress安装在主节点

  下载:https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml

  修改下面的配置:

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:
      # wait up to five minutes for the drain of connections
      terminationGracePeriodSeconds: 300
      serviceAccountName: nginx-ingress-serviceaccount
      nodeSelector:
        kubernetes.io/os: linux
    # 安装在主节点
nodeName: kubenetes-node1
# 增加 hostNetwork: true,意思是开启主机网络模式,暴露 Nginx 服务端口 80

    hostNetwork: true containers:
- name: nginx-ingress-controller image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.26.2 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

  然后执行

kubectl apply -f mandatory.yaml

  查看pods

  

NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
ingress-nginx          nginx-ingress-controller-65c9f74bcf-xp988    1/1     Running   0          15m

  

二、测试

  1、部署测试服务

 

  这里我们部署nginx作为测试服务

  新建 test_nginx.yaml

  

apiVersion: v1
kind: Service
metadata:
  name: service-test-nginx
  namespace: default
spec:
  selector:
    app: test-nginx
  ports:
  - name: http
    port: 80
    targetPort: 80

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-nginx
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: test-nginx
  template:
    metadata:
      labels:
        app: test-nginx
    spec:
      containers:
      - name: test-nginx
        image: 192.168.56.104:5000/nginx:v1
        imagePullPolicy: IfNotPresent
        ports:
        - name: nginx
          containerPort: 80

  运行 kubectl apply -f test_nginx.yaml

  查看是否部署成功

  

[root@kubernetes-node1 kubernetes]# kubectl get pods -n default
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
default                test-nginx-595ff469f4-28wg7                  1/1     Running   0          20s
default                test-nginx-595ff469f4-zjrbg                  1/1     Running   0          20s

 

  2、部署对应的ingress

  新建 test_nginx_ingress.yaml 内容如下:

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

 

  然后添加hosts

  ip 为主节点的ip,域名为ingress中配置的域名

192.168.56.104 nginx.test.com

  打开浏览器输入 nginx.test.com

 

posted @ 2019-12-29 18:27  lwli  阅读(278)  评论(0编辑  收藏  举报