How to Deploy a Docker Container to Kubernetes

Once you have a Docker container, it is really simple to deploy it to Kubernetes.

Generate a Deployment YAML

kubectl provides a fantastic way to help generate deployment yamls.

kubectl create deployment <app_name> --image=<some/image> --dry-run=client -o yaml > <deployment_file_name>.yaml
Code language: Bash (bash)

Perform the Deployment

Now that we have a deployment yaml, we can simply run:

kubectl apply -f <deployment_file_name>.yaml
Code language: Bash (bash)

What next?

Next you might want to add a Service so that the deployment is available to the world.

Open up the deployment yaml file generated and add the following:

--- apiVersion: v1 kind: Service metadata: name: lb-service labels: app: lb-service spec: type: LoadBalancer ports: - port: 80 targetPort: 5000 selector: app: <app_name>
Code language: YAML (yaml)

Note that the above Service uses a LoadBalancer type, listens for requests on port 80, forwards them onto port 5000 to the app specified in the selector label.

We called this <app_name> above, which you should swap out with whatever appname you chose before. This is specified by the label used in the original yaml.

Notify of
Inline Feedbacks
View all comments