Convert Pod to Deployment in Kubernetes
Make your application more reliable
A deployment allows you to run more than 1 replica of the same pod thus, giving you more reliability. You can scale up a Deployment to facilitate and handle more load.
In this brief write-up, I demonstrated how to convert your existing pod to a deployment.
Getting Started
Below is an API service running in the Kubernetes cluster which runs as a single pod.
k8s@terminal:~$ k get pods -n reliable
NAME READY STATUS RESTARTS AGE
med-api 1/1 Running 0 2m9s
Get the current Pod’s YAML
k8s@terminal:~$ k get pods med-api $do -oyaml -n reliable > single-pod.yamlvim single-pod.yaml
....apiVersion: v1
kind: Pod
metadata:
labels:
run: med-api
name: med-api
namespace: reliable
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: med-api
resources: {}
There are multiple ways to do this, one is to copy a Deployment example from https://kubernetes.io/docs and then merge it with the existing Pod YAML.
Let’s demonstrate this: Download a Deployment example manifest from Kubernetes Doc and edit it to match the desired outcome.
Now copy/use a Deployment example YAML and put the Pod’s metadata: and spec: from single-pod.yaml into the Deployment’s template: section:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment # <--- change this
labels:
app: nginx # <--- change this to match label on the pod
spec:
replicas: 3
selector:
matchLabels:
app: nginx # <--- change this to match label on the pod
template: # => from here down copy and paste the pods metadata: and spec: sections
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
The final Deployment Manifest looks like this….
apiVersion: apps/v1
kind: Deployment
metadata:
name: med-api
namespace: reliable
labels:
run: med-api
spec:
replicas: 3
selector:
matchLabels:
run: med-api
template:
# => from here down its the same as the pods metadata: and spec: sections
metadata:
labels:
run: med-api
name: med-api
namespace: reliable
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: med-api
resources: {}
Now, let’s deploy the merged deployment manifest.
k8s@terminal:~$ k apply -f single-pod.yaml
deployment.apps/med-api created
k8s@terminal:~$ k get pods -n reliable
NAME READY STATUS RESTARTS AGE
med-api 1/1 Running 0 21m
med-api-67bb7c7577-chng5 1/1 Running 0 8s
med-api-67bb7c7577-khhcx 1/1 Running 0 8s
med-api-67bb7c7577-tccn2 1/1 Running 0 8sk8s@terminal:~$ k delete pod med-api -n reliable
pod "med-api" deleted
k8s@terminal:~$ k get pods -n reliable
NAME READY STATUS RESTARTS AGE
med-api-67bb7c7577-chng5 1/1 Running 0 55s
med-api-67bb7c7577-khhcx 1/1 Running 0 55s
med-api-67bb7c7577-tccn2 1/1 Running 0 55s
Notice that the Deployment has created all three replicas, and all replicas are up-to-date (they contain the latest Pod template) and available.
Reliable k8s workloads make a happy Engineer!!!