docker - 在Kubernetes中传递Docker容器的运行参数

标签 docker kubernetes rancher

我在RancherOS v1.0.3上运行了两个容器(GitLab和PostgreSQL)。我想使它们成为Kubernetes集群的一部分。

[rancher@rancher-agent-1 ~]$ cat postgresql.sh
docker run --name gitlab-postgresql -d \
    --env 'POSTGRES_DB=gitlabhq_production' \
    --env 'POSTGRES_USER=gitlab' --env 'POSTGRES_PASSWORD=password' \
    --volume /srv/docker/gitlab/postgresql:/var/lib/postgresql \
    postgres:9.6-2


[rancher@rancher-agent-1 ~]$ cat gitlab.sh
docker run --name gitlab -d \
    --link gitlab-postgresql:postgresql \
    --publish 443:443 --publish 80:80 \
    --env 'GITLAB_PORT=80' --env 'GITLAB_SSH_PORT=10022' \
    --env 'GITLAB_SECRETS_DB_KEY_BASE=64-char-key-A' \
    --env 'GITLAB_SECRETS_SECRET_KEY_BASE=64-char-key-B' \
    --env 'GITLAB_SECRETS_OTP_KEY_BASE=64-char-key-C' \
    --volume /srv/docker/gitlab/gitlab:/home/git/data \
    sameersbn/gitlab:9.4.5

查询:
1)我对如何使用YAML文件配置Pod,复制 Controller 等有一些想法,但是我不确定如何将上述docker run参数传递给Kubernetes,以便它可以将其正确地应用于镜像。

2)我不确定在Kubernetes中是否也需要传递--link参数(在上面的gitlab.sh中使用)。尽管我目前正在单个主机上部署这两个容器,但是稍后将创建每个容器的集群(PostgreSQL和GitLab),所以只想确认Kubernetes是否会自动处理主机间的通信。如果没有,那么可以探索哪些选择?

最佳答案

您应该首先尝试将运行语句表示为docker-compose.yml文件。这很容易,它将变成下面的样子

version: '3'

services:
  postgresql:
    image: postgres:9.6-2
    environment:
      - "POSTGRES_DB=gitlabhq_production"
      - "POSTGRES_USER=gitlab"
      - "POSTGRES_PASSWORD=password"
    volumes:
      - /srv/docker/gitlab/postgresql:/var/lib/postgresql
  gitlab:
    image: sameersbn/gitlab:9.4.5
    ports:
      - "443:443"
      - "80:80"
    environment:
      - "GITLAB_PORT=80"
      - "GITLAB_SSH_PORT=10022"
      - "GITLAB_SECRETS_DB_KEY_BASE=64-char-key-A"
      - "GITLAB_SECRETS_SECRET_KEY_BASE=64-char-key-B"
      - "GITLAB_SECRETS_OTP_KEY_BASE=64-char-key-C"
    volumes:
      - /srv/docker/gitlab/gitlab:/home/git/data

现在,kompose.io提供了一个了不起的工具名称kompose,它可以为您完成转换。如果您进行上述转换,您将获得相关文件
$ kompose convert -f docker-compose.yml
WARN Volume mount on the host "/srv/docker/gitlab/gitlab" isn't supported - ignoring path on the host
WARN Volume mount on the host "/srv/docker/gitlab/postgresql" isn't supported - ignoring path on the host
INFO Kubernetes file "gitlab-service.yaml" created
INFO Kubernetes file "postgresql-service.yaml" created
INFO Kubernetes file "gitlab-deployment.yaml" created
INFO Kubernetes file "gitlab-claim0-persistentvolumeclaim.yaml" created
INFO Kubernetes file "postgresql-deployment.yaml" created
INFO Kubernetes file "postgresql-claim0-persistentvolumeclaim.yaml" created

现在,您必须根据kubernetes修复卷安装部分。这样就完成了80%的工作,您只需要弄清楚其余的20%

这是所有生成文件的一部分,因此您可以仅查看生成了哪种文件
==> gitlab-claim0-persistentvolumeclaim.yaml <==
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: gitlab-claim0
  name: gitlab-claim0
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
status: {}

==> gitlab-deployment.yaml <==
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: gitlab
  name: gitlab
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: gitlab
    spec:
      containers:
      - env:
        - name: GITLAB_PORT
          value: "80"
        - name: GITLAB_SECRETS_DB_KEY_BASE
          value: 64-char-key-A
        - name: GITLAB_SECRETS_OTP_KEY_BASE
          value: 64-char-key-C
        - name: GITLAB_SECRETS_SECRET_KEY_BASE
          value: 64-char-key-B
        - name: GITLAB_SSH_PORT
          value: "10022"
        image: sameersbn/gitlab:9.4.5
        name: gitlab
        ports:
        - containerPort: 443
        - containerPort: 80
        resources: {}
        volumeMounts:
        - mountPath: /home/git/data
          name: gitlab-claim0
      restartPolicy: Always
      volumes:
      - name: gitlab-claim0
        persistentVolumeClaim:
          claimName: gitlab-claim0
status: {}

==> gitlab-service.yaml <==
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: gitlab
  name: gitlab
spec:
  ports:
  - name: "443"
    port: 443
    targetPort: 443
  - name: "80"
    port: 80
    targetPort: 80
  selector:
    io.kompose.service: gitlab
status:
  loadBalancer: {}

==> postgresql-claim0-persistentvolumeclaim.yaml <==
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: postgresql-claim0
  name: postgresql-claim0
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
status: {}

==> postgresql-deployment.yaml <==
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: postgresql
  name: postgresql
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: postgresql
    spec:
      containers:
      - env:
        - name: POSTGRES_DB
          value: gitlabhq_production
        - name: POSTGRES_PASSWORD
          value: password
        - name: POSTGRES_USER
          value: gitlab
        image: postgres:9.6-2
        name: postgresql
        resources: {}
        volumeMounts:
        - mountPath: /var/lib/postgresql
          name: postgresql-claim0
      restartPolicy: Always
      volumes:
      - name: postgresql-claim0
        persistentVolumeClaim:
          claimName: postgresql-claim0
status: {}

==> postgresql-service.yaml <==
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: postgresql
  name: postgresql
spec:
  clusterIP: None
  ports:
  - name: headless
    port: 55555
    targetPort: 0
  selector:
    io.kompose.service: postgresql
status:
  loadBalancer: {}

关于docker - 在Kubernetes中传递Docker容器的运行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45845044/

相关文章:

node.js - GKE kubernetes 上的 Node 大小

docker - 容器之间的链接在我的 Rancher 1.6.21 堆栈中不起作用

authentication - 扩展 Rancher 的授权服务以使用其他 (OAuth) 提供商

docker - redis master 看不到 slave

docker - 如何启用Docker守护程序的 “debug”日志记录? (Ubuntu 16.04)

docker - 使用Kubernetes的SolrCloud持久卷权限问题

kubernetes - GKE Kubernetes Autoscaler - 最大集群 cpu,达到内存限制

linux - Fabric Docker 镜像版本 1.1.0-alpha 与此较新版本的 BYFN 不匹配,不受支持

docker - 在不同的用户下启动docker容器

Nginx 入口资源 - 从 www 重定向(SSL 不起作用)