【问题标题】:How to deploy nginx.config file in kubernetes如何在 Kubernetes 中部署 nginx.config 文件
【发布时间】:2019-10-16 07:01:24
【问题描述】:

最近我开始研究 Kubernetes,现在我可以使用默认选项部署 nginx。 但是我如何在 Kubernetes 中部署我的 nginx.conf 呢? 也许有人有一个简单的例子?

【问题讨论】:

  • 您可以使用 helm 图表来部署您的 Nginx 或任何其他容器/服务。维护、更新或回滚到以前的版本要容易得多。
  • Kubernetes Configuring Redis using a ConfigMap 教程演示了这些原则。

标签: linux docker kubernetes


【解决方案1】:

为 nginx 部署创建 yaml:

kubectl run --image=nginx nginx -oyaml --dry-run
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

使用 nginx 配置创建配置 ConfigMap

kubectl create configmap nginx-conf --from-file=./nginx.conf

挂载文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf
          name: nginx-conf
          subPath: nginx.conf
      volumes:
      - configMap:
          name: nginx-conf
        name: nginx-conf

【讨论】:

    【解决方案2】:

    您可以在 nginx 默认镜像之上构建您自己的镜像,然后将您自己的 nginx.conf 复制到该镜像中。 创建镜像后,您可以将其推送到 Dockerhub/您自己的私有存储库,然后使用该镜像代替 kubernetes 中默认的 nginx 镜像。

    This answer 涵盖了创建自定义 nginx 映像的过程。

    一旦可以将映像拉入您的 kubernetes 集群,您就可以像这样将其部署到您的集群 -
    kubectl run nginx --image=<your-docker-hub-username>/<customr-nginx>

    【讨论】:

    • 不应锁定图像
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 2017-07-22
    • 1970-01-01
    相关资源
    最近更新 更多