Deploy your first Kubernetes application through CLI

Deploy your first Kubernetes app with ease using kubectl. This step-by-step guide provides a smooth introduction to cluster configuration, resource definition, deployment, and testing.

3 minutes reading time

Written by

Jubril Oyetunji
Jubril Oyetunji

Technical Writer at Civo

You're ready to deploy your first application to Kubernetes through the command line. This post will walk you through how to do exactly that, the tools you'll need, and a bit of the theory behind what is going on.

Prerequisites

  • A verified Civo account
  • The Civo CLI installed and authenticated (civo apikey save <name> <key>)
  • kubectl installed
  • Basic familiarity with containers

Creating a Civo Kubernetes cluster from the CLI

Since this is a command line guide, we'll create the cluster from the command line too:

civo kubernetes create hello-demo \
--nodes 2 \
--size g4s.kube.small \
--create-firewall \
--save --switch --wait

The --save --switch flags merge the new cluster's kubeconfig into your local config and make it the active context, so there is nothing to download by hand.

Confirm kubectl is talking to the new cluster:

kubectl get nodes

Two nodes should report Ready within a minute or two of the cluster coming up.

One more thing to note down. Every Civo cluster gets a DNS name of the form <cluster-id>.k8s.civo.com, and we'll use it to expose the application later. You can see it with:

civo kubernetes show hello-demo

Look for the DNS entry in the output and store the hostname we'll serve the app on:

HOST=hello-world.<your-cluster-id>.k8s.civo.com

The definition files

Kubernetes is declarative. You describe the state you want in YAML definition files, and the cluster works to make reality match. We'll declare three objects: a Deployment that runs the application, a Service that gives it a stable address inside the cluster, and an Ingress that exposes it to the outside world.

First, the Deployment. It runs two replicas of Civo's hello-world image:

cat > deployment.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
spec:
replicas: 2
selector:
matchLabels:
app: hello-world
template:
metadata:
labels:
app: hello-world
spec:
containers:
- image: civo/docker-hello-world:latest
name: hello-world
EOF

Next, the Service. It selects the pods by their app: hello-world label and gives them one stable port:

cat > service.yaml <<'EOF'
apiVersion: v1
kind: Service
metadata:
name: hello-world
labels:
app: hello-world
spec:
ports:
- name: "hello-world"
port: 5000
selector:
app: hello-world
EOF

Finally, the Ingress. Civo Kubernetes ships with the Traefik ingress controller, and we select it with ingressClassName. Older guides used the kubernetes.io/ingress.class annotation for this, but that annotation is deprecated, spec.ingressClassName is the current field.

This block uses the HOST variable from earlier, so the hostname is filled in for you:

cat > ingress.yaml <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
labels:
app: hello-world
name: hello-world-ingress
spec:
ingressClassName: traefik
rules:
- host: ${HOST}
http:
paths:
- backend:
service:
name: hello-world
port:
number: 5000
path: /
pathType: "Prefix"
EOF

Deploying the application

Apply all three files:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml
deployment.apps/hello-world created
service/hello-world created
ingress.networking.k8s.io/hello-world-ingress created

Watch the pods come up:

kubectl get pods

Both replicas should reach Running. You can also confirm the Service and Ingress landed:

kubectl get svc,ingress

Testing the application

Point your browser at the hostname you stored earlier, or check from the terminal:

curl "http://${HOST}/"

You should see the application respond with a greeting.

Cleaning up

A running cluster costs money, so remove it when you are done experimenting:

civo kubernetes remove hello-demo

Conclusion

By following this guide, you have successfully deployed a simple application to Kubernetes by declaring the configuration you have specified in the deployment, service, and ingress files. This application runs on a Civo Kubernetes cluster, and is accessible via the web on its own subdomain.

From here, try changing the number of replicas in the Deployment and re-applying it, or deploy an image of your own in place of the hello-world container.

Jubril Oyetunji
Jubril Oyetunji

Technical Writer at Civo

Jubril Oyetunji is a DevOps engineer and technical writer with a strong focus on cloud-native technologies and open-source tools. His work centers on creating practical tutorials that help developers better understand platforms such as Kubernetes, NGINX, Rust, and Go.

As a contract technical writer, Jubril authored an extensive library of technical guides covering cloud-native infrastructure and modern development workflows. Many of his tutorials achieved strong search rankings, helping developers around the world learn and adopt emerging technologies.

View author profile

Further Reading

Slide 1 of 2