Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run command after initialization

Tags:

kubernetes

I would like to run specific command after initialization of deployment is successful.

This is my yaml file:

apiVersion: extensions/v1beta1 kind: Deployment metadata:   name: auth spec:   replicas: 1   template:     metadata:         labels:           app: auth     spec:       containers:         - name: auth           image: {{my-service-image}}           env:           - name: NODE_ENV             value: "docker-dev"           resources:             requests:               cpu: 100m               memory: 100Mi           ports:             - containerPort: 3000 

However, I would like to run command for db migration after (not before) deployment is successfully initialized and pods are running.

I can do it manually for every pod (with kubectl exec), but this is not very scalable.

like image 698
Peter Parada Avatar asked May 23 '17 16:05

Peter Parada


People also ask

How do you use commands and arguments in Kubernetes?

Define a command and arguments when you create a PodTo define a command, include the command field in the configuration file. To define arguments for the command, include the args field in the configuration file. The command and arguments that you define cannot be changed after the Pod is created.

What is the difference between command and args in Kubernetes?

If you supply a command but no args for a Container, only the supplied command is used. The default EntryPoint and the default Cmd defined in the Docker image are ignored. If you supply only args for a Container, the default Entrypoint defined in the Docker image is run with the args that you supplied.


1 Answers

I resolved it using lifecycles:

apiVersion: extensions/v1beta1 kind: Deployment metadata:   name: auth spec:   replicas: 1   template:     metadata:         labels:           app: auth     spec:       containers:         - name: auth           image: {{my-service-image}}           env:           - name: NODE_ENV             value: "docker-dev"           resources:             requests:               cpu: 100m               memory: 100Mi           ports:             - containerPort: 3000           lifecycle:             postStart:               exec:                 command: ["/bin/sh", "-c", {{cmd}}] 
like image 131
Peter Parada Avatar answered Oct 24 '22 07:10

Peter Parada