目的

部署nginx-ingress

说明

文档存在不足,正在完善!

default-http-backend

  1. apiVersion: extensions/v1beta1
  2. kind: Deployment
  3. metadata:
  4. name: default-http-backend
  5. labels:
  6. k8s-app: default-http-backend
  7. namespace: kube-system
  8. spec:
  9. replicas: 1
  10. template:
  11. metadata:
  12. labels:
  13. k8s-app: default-http-backend
  14. spec:
  15. terminationGracePeriodSeconds: 60
  16. containers:
  17. - name: default-http-backend
  18. # Any image is permissable as long as:
  19. # 1. It serves a 404 page at /
  20. # 2. It serves 200 on a /healthz endpoint
  21. image: hub.k8s.com/google-containers/defaultbackend:1.3
  22. livenessProbe:
  23. httpGet:
  24. path: /healthz
  25. port: 8080
  26. scheme: HTTP
  27. initialDelaySeconds: 30
  28. timeoutSeconds: 5
  29. ports:
  30. - containerPort: 8080
  31. resources:
  32. limits:
  33. cpu: 10m
  34. memory: 20Mi
  35. requests:
  36. cpu: 10m
  37. memory: 20Mi
  38. ---
  39. apiVersion: v1
  40. kind: Service
  41. metadata:
  42. name: default-http-backend
  43. namespace: kube-system
  44. labels:
  45. k8s-app: default-http-backend
  46. spec:
  47. ports:
  48. - port: 80
  49. targetPort: 8080
  50. selector:
  51. k8s-app: default-http-backend

安装ningx-ingress-controller

创建secret ,创建kubeconfig

  1. kubectl create secret generic nginx-ingress-certs --from-file=./kubeconfig -n kube-system

导入configmap

  1. kind: ConfigMap
  2. apiVersion: v1
  3. metadata:
  4. name: nginx-configuration
  5. namespace: kube-system
  6. labels:
  7. app: ingress-nginx
  8. ---
  9. kind: ConfigMap
  10. apiVersion: v1
  11. metadata:
  12. name: tcp-services
  13. namespace: kube-system
  14. ---
  15. kind: ConfigMap
  16. apiVersion: v1
  17. metadata:
  18. name: udp-services
  19. namespace: kube-system

创建rbac

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. name: nginx-ingress-serviceaccount
  5. namespace: kube-system
  6. ---
  7. apiVersion: rbac.authorization.k8s.io/v1beta1
  8. kind: ClusterRole
  9. metadata:
  10. name: nginx-ingress-clusterrole
  11. rules:
  12. - apiGroups:
  13. - ""
  14. resources:
  15. - configmaps
  16. - endpoints
  17. - nodes
  18. - pods
  19. - secrets
  20. verbs:
  21. - list
  22. - watch
  23. - apiGroups:
  24. - ""
  25. resources:
  26. - nodes
  27. verbs:
  28. - get
  29. - apiGroups:
  30. - ""
  31. resources:
  32. - services
  33. verbs:
  34. - get
  35. - list
  36. - watch
  37. - apiGroups:
  38. - "extensions"
  39. resources:
  40. - ingresses
  41. verbs:
  42. - get
  43. - list
  44. - watch
  45. - apiGroups:
  46. - ""
  47. resources:
  48. - events
  49. verbs:
  50. - create
  51. - patch
  52. - apiGroups:
  53. - "extensions"
  54. resources:
  55. - ingresses/status
  56. verbs:
  57. - update
  58. ---
  59. apiVersion: rbac.authorization.k8s.io/v1beta1
  60. kind: Role
  61. metadata:
  62. name: nginx-ingress-role
  63. namespace: kube-system
  64. rules:
  65. - apiGroups:
  66. - ""
  67. resources:
  68. - configmaps
  69. - pods
  70. - secrets
  71. - namespaces
  72. verbs:
  73. - get
  74. - apiGroups:
  75. - ""
  76. resources:
  77. - configmaps
  78. resourceNames:
  79. # Defaults to "<election-id>-<ingress-class>"
  80. # Here: "<ingress-controller-leader>-<nginx>"
  81. # This has to be adapted if you change either parameter
  82. # when launching the nginx-ingress-controller.
  83. - "ingress-controller-leader-nginx"
  84. verbs:
  85. - get
  86. - update
  87. - apiGroups:
  88. - ""
  89. resources:
  90. - configmaps
  91. verbs:
  92. - create
  93. - apiGroups:
  94. - ""
  95. resources:
  96. - endpoints
  97. verbs:
  98. - get
  99. ---
  100. apiVersion: rbac.authorization.k8s.io/v1beta1
  101. kind: RoleBinding
  102. metadata:
  103. name: nginx-ingress-role-nisa-binding
  104. namespace: kube-system
  105. roleRef:
  106. apiGroup: rbac.authorization.k8s.io
  107. kind: Role
  108. name: nginx-ingress-role
  109. subjects:
  110. - kind: ServiceAccount
  111. name: nginx-ingress-serviceaccount
  112. namespace: kube-system
  113. ---
  114. apiVersion: rbac.authorization.k8s.io/v1beta1
  115. kind: ClusterRoleBinding
  116. metadata:
  117. name: nginx-ingress-clusterrole-nisa-binding
  118. roleRef:
  119. apiGroup: rbac.authorization.k8s.io
  120. kind: ClusterRole
  121. name: nginx-ingress-clusterrole
  122. subjects:
  123. - kind: ServiceAccount
  124. name: nginx-ingress-serviceaccount
  125. namespace: kube-system

创建nginx-ingress-controller

  1. apiVersion: extensions/v1beta1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-ingress-controller
  5. namespace: kube-system
  6. spec:
  7. replicas: 1
  8. selector:
  9. matchLabels:
  10. app: ingress-nginx
  11. template:
  12. metadata:
  13. labels:
  14. app: ingress-nginx
  15. annotations:
  16. prometheus.io/port: '10254'
  17. prometheus.io/scrape: 'true'
  18. spec:
  19. serviceAccountName: nginx-ingress-serviceaccount
  20. dnsPolicy: ClusterFirst
  21. containers:
  22. - name: nginx-ingress-controller
  23. image: hub.k8s.com/google-containers/nginx-ingress-controller:0.9.0-beta.15
  24. args:
  25. - /nginx-ingress-controller
  26. - --default-backend-service=$(POD_NAMESPACE)/default-http-backend
  27. - --configmap=$(POD_NAMESPACE)/nginx-configuration
  28. - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
  29. - --udp-services-configmap=$(POD_NAMESPACE)/udp-services
  30. - --kubeconfig=/kubeconfig/kubeconfig
  31. env:
  32. - name: POD_NAME
  33. valueFrom:
  34. fieldRef:
  35. fieldPath: metadata.name
  36. - name: POD_NAMESPACE
  37. valueFrom:
  38. fieldRef:
  39. fieldPath: metadata.namespace
  40. volumeMounts:
  41. - name: kubeconfig
  42. mountPath: /kubeconfig
  43. ports:
  44. - name: http
  45. containerPort: 80
  46. hostPort: 80
  47. - name: https
  48. containerPort: 443
  49. hostPort: 443
  50. livenessProbe:
  51. failureThreshold: 3
  52. httpGet:
  53. path: /healthz
  54. port: 10254
  55. scheme: HTTP
  56. initialDelaySeconds: 10
  57. periodSeconds: 10
  58. successThreshold: 1
  59. timeoutSeconds: 1
  60. readinessProbe:
  61. failureThreshold: 3
  62. httpGet:
  63. path: /healthz
  64. port: 10254
  65. scheme: HTTP
  66. periodSeconds: 10
  67. successThreshold: 1
  68. timeoutSeconds: 1
  69. volumes:
  70. - name: kubeconfig
  71. secret:
  72. secretName: nginx-ingress-certs

验证

创建一个示例

  1. apiVersion: v1
  2. kind: Namespace
  3. metadata:
  4. name: web-test
  5. ---
  6. apiVersion: v1
  7. kind: ReplicationController
  8. metadata:
  9. name: web
  10. namespace: web-test
  11. spec:
  12. replicas: 1
  13. selector:
  14. name: web
  15. template:
  16. metadata:
  17. labels:
  18. name: web
  19. spec:
  20. containers:
  21. - name: web
  22. image: hub.k8s.com/apps/php:7.1
  23. ports:
  24. - name: http
  25. containerPort: 80
  26. ---
  27. apiVersion: v1
  28. kind: Service
  29. metadata:
  30. name: wordpress-svc
  31. namespace: web-test
  32. labels:
  33. name: web-svc
  34. spec:
  35. clusterIP: 10.254.0.201
  36. ports:
  37. - port: 80
  38. selector:
  39. name: web
  40. ---
  41. apiVersion: extensions/v1beta1
  42. kind: Ingress
  43. metadata:
  44. name: web-ingress
  45. namespace: web-test
  46. spec:
  47. rules:
  48. - host: www.k8s.com
  49. http:
  50. paths:
  51. - path: /
  52. backend:
  53. serviceName: wordpress-svc
  54. servicePort: 80

访问http://www.k8s.com