Using Docker and Kubernetes Together

Learn how to use Docker and Kubernetes together in this hands-on tutorial. We'll guide you through deploying the classic 2048 game, from local development to scalable production. Perfect for developers ready to level up their containerization and orchestration skills.

Docker and Kubernetes are often considered separate tools, but using them together forms a powerful combination for developing, deploying, and scaling applications. Docker simplifies container creation and management, while Kubernetes automates the orchestration of these containers across distributed systems. In this tutorial, we’ll explore leveraging Docker and Kubernetes to deploy the classic 2048 game. By the end, you’ll understand how to use Docker for local development and Kubernetes to manage and scale your applications in production.

Step-by-Step Guide: Deploying the 2048 Game with Docker and Kubernetes

Now that we understand the power of Docker and Kubernetes working together, let’s dive into the actual process of deploying the 2048 game. We’ll begin by pulling the game’s Docker image, testing it locally, and then setting up a Kubernetes deployment to bring it to life in a scalable environment. Follow the steps below to experience firsthand how these tools come together to manage and deploy your application efficiently.

Step 1: Pull the Game

First, let’s pull the 2048 game from Docker Hub:

docker pull alexwhen/docker-2048

Think of it as downloading the game cartridge for your Kubernetes arcade machine!

Step 2: Testing the Game Locally

Let’s test-drive the game before we deploy it. This will make sure everything’s in top shape before we publish it to the world:

docker run -d -p 8080:80 --name 2048-game alexwhen/docker-2048

Now, fire up your browser and head over to http://localhost:8080 to get your game running!

Step 3: Creating the Kubernetes Deployment

It’s time to create a Kubernetes deployment! First, we need a 2048-deployment.yaml file to tell Kubernetes how to deploy our game.

  • Choose a working directory: On your local machine, create or navigate to a directory where you’ll store the Kubernetes deployment files:
mkdir 2048-game-kubernetes
cd 2048-game-kubernetes
  • Create the YAML file: In this directory, create the file 2048-deployment.yaml. You can use any text editor:
vim 2048-deployment.yaml
  • Paste the Deployment Configuration: Copy the following YAML configuration into the file. This configuration defines how Kubernetes will deploy your game, including details like the number of replicas and the container image to use.
apiVersion: apps/v1
kind: Deployment
metadata:
name: game-2048
spec:
replicas: 3
selector:
matchLabels:
app: game-2048
template:
metadata:
labels:
app: game-2048
spec:
containers:
- name: game-2048
image: alexwhen/docker-2048
ports:
- containerPort: 80
  • Save and exit.

Step 4: Create the Kubernetes Service

In this step, you’ll create a Kubernetes service to expose the deployed 2048 game.

  • Create a new file: Similar to Step 3, create a new file named 2048-service.yaml in the same working directory.

  • Paste the service configuration: Copy and paste this YAML configuration:

apiVersion: v1
kind: Service
metadata:
name: game-2048-service
spec:
selector:
app: game-2048
type: LoadBalancer
ports:
- protocol: TCP
port: 80
targetPort: 80
  • Save and exit.

Step 5: Deploy the Game to Kubernetes

Now, let’s deploy the game to Kubernetes:

  • Apply the deployment:
kubectl apply -f 2048-deployment.yaml
  • Apply the service YAML:
kubectl apply -f 2048-service.yaml

Step 6: Verify the Deployment

Check if the deployment was successful:

  • Check the deployment status:
kubectl get deployments
  • Check the pods:
kubectl get pods
  • Check the service:
kubectl get services

At this point, you’ve successfully deployed the game! Kubernetes is now running your game in multiple pods, and the service is set up to make it accessible to users.

Step 7: Access the Game

Now for the exciting part—playing the game! How you access the game depends on whether you’re using Minikube or a cloud provider.

For Minikube users

Use this command to directly access the game service through Minikube:

minikube service game-2048-service

This will open your default browser and take you to the 2048 game running on your local Kubernetes cluster.

For cloud provider users (like AWS, GCP, or Azure):

  • Run this command to get the external IP address of the game-2048-service:
kubectl get services game-2048-service
  • Copy the external IP address and open it in your browser:
http://<external-ip>

Step 8: Improve Your Game (Scaling)

To scale up the number of game instances, follow these steps:

  • Scale the deployment:
kubectl scale deployment game-2048 --replicas=5
  • Verify the new pod count:
kubectl get pods

Conclusion

Congratulations! You have successfully deployed the classic 2048 game by leveraging Docker and Kubernetes together. Throughout this tutorial, you learned how to pull the game image, run it locally, create the necessary Kubernetes deployment and service, and even scale the game to handle more players. By mastering these skills, you can now apply your knowledge to deploy and manage other applications in a cloud-native environment, harnessing the power of containerization and orchestration for real-world projects. Keep experimenting and building on your Kubernetes expertise!

Author

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team