简体   繁体   English

如何使用动态 url 启动 docker 容器

[英]How to start docker container with dynamic url

My requirement is as follows:我的要求如下:

  1. Developer creates a branch in Jenkins.开发人员在 Jenkins 中创建一个分支。 Lets say branch name is "mystory-101"假设分支名称是“mystory-101”
  2. Now developer push the code to this branch现在开发者将代码推送到这个分支
  3. Jenkins job starts as soon as commit is pushed to the branch "mystory-101" and create a new docker image for this branch if not created already Jenkins 作业在提交被推送到分支“mystory-101”后立即开始,并为此分支创建一个新的 docker 映像(如果尚未创建)
  4. My application is Node.js based app, so docker container starts with node.js and deployes the code from the branch "mystory-101"我的应用程序是基于 Node.js 的应用程序,因此 docker 容器以 node.js 开头并部署来自分支“mystory-101”的代码
  5. After the code is deployed and node.js is running, then I would also like this node.js app to be accessible via the URL: https://mystory-101.mycompany.com After the code is deployed and node.js is running, then I would also like this node.js app to be accessible via the URL: https://mystory-101.mycompany.com

For this purpose I was reading this https://medium.com/swlh/ci-cd-pipeline-using-jenkins-dynamic-nodes-86ea854ff7a7 but I am not sure how to achive step #5.为此,我正在阅读此https://medium.com/swlh/ci-cd-pipeline-using-jenkins-dynamic-nodes-86ea854ff7a7但我不确定如何完成步骤 5。 Can you please advice how to achive this autometically?您能否建议如何自动实现这一目标?

Reformatting answers from commentaries, having a Jenkins installation and Kubernetes cluster, you may automate your deployments using a Jenkins plugin such as oc or kubernetes , or you could prefer using the kubectl client directly, assuming your agents do have that binary. Reformatting answers from commentaries, having a Jenkins installation and Kubernetes cluster, you may automate your deployments using a Jenkins plugin such as oc or kubernetes , or you could prefer using the kubectl client directly, assuming your agents do have that binary.

Not going through the RBAC specifics, you would probably need a ServiceAccount for Jenkins, and use a token (can be found in a Secret named after your ServiceAccount).不了解 RBAC 细节,您可能需要 Jenkins 的 ServiceAccount,并使用令牌(可以在以您的 ServiceAccount 命名的 Secret 中找到)。 That ServiceAccount should have enough privileges to create resources in the namespaces you intend to deploy stuff into -- usually the edit ClusterRole, with a namespace-scoped RoleBinding:该 ServiceAccount 应该有足够的权限在您打算将内容部署到的命名空间中创建资源——通常是edit ClusterRole,具有命名空间范围的 RoleBinding:

kubectl create sa jenkins -n my-namespace
kubectl create rolebinding jenkins-edit \
  --clusterrole=edit \
  --serviceaccount=my-namespace:jenkins-edit \
  --namespace=my-namespace

Once Jenkins is done building your image, you would deploy it to Kubernetes, most likely creating a Deployment, a Service, and an Ingress, substituting resource names, namespaces and your ingress requested FQDN to match your requirements.一旦 Jenkins 完成构建您的映像,您将其部署到 Kubernetes,最有可能创建部署、服务和入口,替换资源名称、命名空间和入口请求的 FQDN 以匹配您的要求。

Prepare your deployment yaml, something like:准备部署 yaml,类似于:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-BRANCH
spec:
  selector:
    matchLabels:
      name: app-BRANCH
  template:
     spec:
       containers:
       - image: my-registry/path/to/image:BRANCH
[...]
---
apiVersion: v1
kind: Service
metadata:
  name: app-BRANCH
spec:
  selector:
    name: app-BRANCH
  ports:
[...]
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: app-BRANCH
spec:
  rules:
  - host: app-BRANCH.my-base-domain.com
    http:
      paths:
      - backend:
          serviceName: app-BRANCH

Then, have your Jenkins agent apply that configuration, substituting values properly:然后,让您的 Jenkins 代理应用该配置,并正确替换值:

sed "s|BRANCH|$BRANCH|" deploy.yaml | kubectl apply -n my-namespace -f-
kubectl wait -n my-namespace deploy/app-$BRANCH --for=condition=Available
kubectl logs -n my-namespace deploy/app-$BRANCH --tail=200

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM