Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
98 views
in Technique[技术] by (71.8m points)

node.js - How to start docker container with dynamic url

My requirement is as follows:

  1. Developer creates a branch in Jenkins. Lets say branch name is "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
  4. My application is Node.js based app, so docker container starts with node.js and deployes the code from the branch "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

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. Can you please advice how to achive this autometically?

question from:https://stackoverflow.com/questions/65904891/how-to-start-docker-container-with-dynamic-url

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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). 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:

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.

Prepare your deployment yaml, something like:

---
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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...