生成 Kubernetes 清单以部署裸 nginx 容器的脚本

Script to produces Kubernetes manifests to deploy bare nginx container

我想要一个生成 Kubernetes 清单的脚本,以部署一个带有服务端口 80 和主机入口的裸 nginx 容器 example.nginx.com。我会将其部署到 EKS 集群中。有人可以给我线索吗?

您的群集必须 deployed nginx-ingress。然后 运行 在您的命令提示符处使用以下脚本部署 a bare nginx container with service port 80 and ingress for host as example.nginx.com:

cat << EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: example.nginx.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 80
EOF

最近我开始在 EKS 上使用 Nginx 控制器配置 NLB。所以用你需要的脚本记录完整的流程。 我尝试了其他方法,如基于云提供商的 Nginx 部署,但它没有按预期工作(而不是 ELB,它正在创建经典 LB)。 参考- https://github.com/kubernetes/ingress-nginx/issues/6292

简而言之,下面的方法是迄今为止最好的。

  1. 安装 Nginx 控制器 - 这将为 HTTP 创建部署和 NodePort 服务,比如端口 - 31848,HTTPS- 30099

#kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.0.1/deploy/static/provider/baremetal/deploy.yaml

  1. 创建生产部署、服务和入口资源。
apiVersion: apps/v1
kind: Deployment
metadata:
  name: production
  labels:
    app: production
  namespace: app 
spec:
  replicas: 1
  selector:
    matchLabels:
      app: production
  template:
    metadata:
      labels:
        app: production
    spec:
      containers:
      - name: production
        image: mirrorgooglecontainers/echoserver:1.10
        ports:
        - containerPort: 8080
        env:
          - name: NODE_NAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP

---

apiVersion: v1
kind: Service
metadata:
  name: production
  labels:
    app: production
  namespace: app
spec:
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
    name: http
  selector:
    app: production
---

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: production
  annotations:
    kubernetes.io/ingress.class: nginx
  namespace: app
spec:
  rules:
  - http:
      paths:
        - path: /
          pathType: Prefix
          backend:
            serviceName: production
            servicePort: 80
  1. 创建 Canary 部署。
apiVersion: apps/v1
kind: Deployment
metadata:
  name: canary
  labels:
    app: canary
  namespace: app 
spec:
  replicas: 1
  selector:
    matchLabels:
      app: canary
  template:
    metadata:
      labels:
        app: canary
    spec:
      containers:
      - name: canary
        image: mirrorgooglecontainers/echoserver:1.10
        ports:
        - containerPort: 8080
        env:
          - name: NODE_NAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
---

apiVersion: v1
kind: Service
metadata:
  name: canary
  labels:
    app: canary
  namespace: app
spec:
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
    name: http
  selector:
    app: canary
---

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: canary
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-weight: "30"
  namespace: app
spec:
  rules:
  - http:
      paths:
        - path: /
          pathType: Prefix
          backend:
            serviceName: canary
            servicePort: 80
  1. 在 EKS 上创建一个 NLB 类型的负载均衡器。如果您选择“面向互联网”。

  2. 创建一个以“目标类型”作为实例的目标组并Port/Health检查端口- 31848 (HTTP)。

  3. 将目标组附加到自动缩放组。

  4. 在 NLB(TLS-安全 TCP)上创建一个侦听器并将其转发到目标组。

  5. 虽然我们将在私有子网上启动工作节点,但我们需要为所有 IP 打开端口“31848”。这就是 EC2 能够与 NLB 通信的方式。

希望我能为您提供清晰的思路。如果您遇到任何问题,请告诉我。