Deploying a Docker Container to a K8s Cluster

Maximus Powers
2 min readApr 24, 2023

Notes for myself fyi

Deployment Files:

You’ll need three primary .yaml files to deploy your container as a pod:

  1. Load Balancer: Distributes the workload (? idek)
  2. Ingress: The client’s entry point to the cluster
  3. Deployment: Pulls an image from dockerhub and spins it up

Repo with templates

Load Balancer: name-lb.yaml

Find and replace project_name with your project’s name. Change targetPort to the port that the app runs on (found in the app). Change the port to the port you want it to run on.

apiVersion: v1
kind: Service
metadata:
name: project_name
spec:
selector:
app: project_name
ports:
- protocol: TCP
port: 5005 # the port you want to access it on
targetPort: 5000 # the port that app runs on
type: LoadBalancer

Ingress: name-ing.yaml

Find and replace project_name with your project’s name. Change the port you want to access it on.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: project_name-ing
spec:
rules:
- http:
paths:
- path: /project_name # this isn't working
pathType: Prefix
backend:
service:
name: project_name # refs the project_name-lb.yaml service name (in metadata)
port:
number: 5005 #the port you want to access it on

Deployment: name-deployment.yaml

Find and replace project_name with your project’s name. Make sure your container is referencing the right image on your dockerhub.

apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: project_name
name: project_name
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: project_name
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: project_name
spec:
containers:
- image: maximuspowers/project_name # change dockerhub username if needed
imagePullPolicy: Always
name: project_name
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30

Finally Deploying It:

Run these commands to apply all your yaml config files to the Kubernetes cluster:

kubectl apply -f /path/project_name-deployment.yaml
kubectl apply -f /path/project_name-ing.yaml
kubectl apply -f /path/project_name-lb.yaml

Cheat Sheet:

///////////////// K8s Commands ///////////////////
kubectl apply -f filename.yaml // applies the config file

kubectl get pods // shows all running pods

--

--

Maximus Powers

Building my Web3 stack everyday. Software engineer, background in data science.