Scaling Pods in Kubernetes

1.5 hours
  • 5 Learning Objectives

About this Hands-on Lab

For the last six months, the Acme Anvil Corporation has been migrating their bare metal infrastructure to Docker containers. A schism has developed between the members of your team on whether to use Docker Swarm or Kubernetes. To settle the dispute, your manager has decided to create a series of challenges. You have been tasked with setting up a Kubernetes cluster with 3 nodes: 1 master and 2 workers. First, you will create a deployment with 3 replicas using the `httpd` image. Next, you will create a service that will make the pod publicly accessible. Finally, you will scale the pod up to 5 replicas and then down to 2.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Complete the Kubernetes Install

Initialize the cluster:

kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.11.3

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Install Flannel on the master:

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml
Create the Deployment
vi deployment.yml

Add the following to deployment.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-deployment
  labels:
    app: httpd
spec:
  replicas: 3
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
      - name: httpd
        image: httpd:latest
        ports:
        - containerPort: 80

Spin up the deployment:

kubectl create -f deployment.yml
Create the Service
vi service.yml

Add the following to service.yml:

kind: Service
apiVersion: v1
metadata:
  name: service-deployment
spec:
  selector:
    app: httpd
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: NodePort

Create the service:

kubectl create -f service.yml
Scale the Deployment Up to 5 Replicas
vi deployment.yml

Change the number of replicas to 5:

spec:
  replicas: 5

Apply the changes:

kubectl apply -f deployment.yml
Scale the Deployment Down to 2 Replicas
vi deployment.yml

Change the number of replicas to 2:

spec:
  replicas: 2

Apply the changes:

kubectl apply -f deployment.yml

Additional Resources

In this learning activity, you are tasked with scaling up a Kubernetes pod.

  1. Create a Kubernetes cluster with 1 master and 2 worker nodes.
    Install Flannel using the following URL:
    https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml

Create a Deployment

  1. Set the API version to app/v1.
  2. The kind should be set to Deployment.
  3. Name the deployment httpd-deployment.
  4. Create a label called app and set it to httpd.
  5. Create a ReplicaSet that has the following:
    • 3 replicas
    • A match label with a value of httpd
  6. Create a template for the ReplicaSet.
  7. The app label should have a value of httpd.
  8. The container specification should reflect the following:
    • The name should be set to httpd.
    • The image should be httpd with the latest tag.
    • The container port should be set to 80.
  9. Create the deployment.

Create a Service

  1. Set the API version to v1.
  2. The kind should be set to Service.
  3. Name the service service-deployment.
  4. Create a spec for the service.
  5. Make sure there is a selector called app with a value of httpd.
  6. Create a port using the TCP protocol.
  7. Set the port to 80.
  8. Set the type to NodePort.
  9. Create the service.
  10. Test the service to make sure you can access the pod.

Scale the Pod

  1. After you verify that the pod is accessible, scale the pod up to 5 replicas.
  2. Verify that there are 5 replicas.
  3. Scale the pod down to 2 replicas.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?