Kubernetes Practice

Introduction

I would like to actually run the official tutorial from kubernetes.io

This tutorial uses Katakoba, but this time I and run in on k3s

Environment

What Learn in this ?

  • Deploy containerized applications to a cluster
  • Deployment scaling
  • Update containerized applications with new software version
  • Debugging containerized applications

Create Kubernetes Cluster

Article about this how to Create Kubernetes Cluster You can find in link below, but if You need simple and lightweight kubernetes just use https://k3s.io/ 😀

Create Namespace

  • Namespace configuration
apiVersion: v1
kind: Namespace
metadata:
name: k8s-tutorial
  • Create new namespace
root@vagrant:/home/vagrant# kubectl apply -f ns.yml
namespace/k8s-tutorial created

Deploy Application

  • Application Configuration
apiVersion: apps/v1
kind: Deployment
metadata:
name: kubernetes-bootcamp
namespace: k8s-tutorial
spec:
selector:
matchLabels:
app: kubernetes-bootcamp
template:
metadata:
labels:
app: kubernetes-bootcamp
spec:
containers:
- name: kubernetes-bootcamp
image: gcr.io/google-samples/kubernetes-bootcamp:v1
resources:
limits:
memory: "128Mi"
cpu: "500m"
  • Deploy Application without yml file
root@vagrant:/home/vagrant# kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1##Check deploymentroot@vagrant:/home/vagrant# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 40s
  • Deploy Application with yml file
root@vagrant:/home/vagrant# kubectl apply -f app4test.yml
deployment.apps/kubernetes-bootcamp created
##Check deployment
root@vagrant:/home/vagrant# kubectl get deployments --namespace k8s-tutorial
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 43s

createdIf an error occurs without the message, app4test.ymlcheck.
deploymentLet's check the status of.

root@vagrant:/home/vagrant# kubectl get deploy -n k8s-tutorial -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
kubernetes-bootcamp 1/1 1 1 86s kubernetes-bootcamp gcr.io/google-samples/kubernetes-bootcamp:v1 app=kubernetes-bootcamp

READYThere 1/1has been successfully deployed since has become.

Now let's check it for the time being Pod.

root@vagrant:/home/vagrant# kubectl get po -n k8s-tutorial
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-6b66d5f8fd-j86z8 1/1 Running 0 2m11s

STATUSIsn't RUNNINGit?

This is the end of this chapter, so let’s clean it.

root@vagrant:/home/vagrant# kubectl delete -f app4test.yml
deployment.apps "kubernetes-bootcamp" deleted

Executed to Pod

Set the target pod name in the environment variable

root@vagrant:/home/vagrant# POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')

Now show environment variables in the target pod

root@vagrant:/home/vagrant# kubectl exec $POD_NAME  env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=kubernetes-bootcamp-57978f5f5d-nbrgr
NPM_CONFIG_LOGLEVEL=info
NODE_VERSION=6.3.1
KUBERNETES_SERVICE_HOST=10.43.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.43.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.43.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.43.0.1
HOME=/root

Launch bash in target pod

root@vagrant:/home/vagrant# kubectl exec $POD_NAME -it bash
root@kubernetes-bootcamp-57978f5f5d-nbrgr:/#

Check the files inside the pod

root@kubernetes-bootcamp-57978f5f5d-nbrgr:/# ls -laht
total 460K
dr-xr-xr-x 13 root root 0 Nov 19 19:47 sys
drwxr-xr-x 5 root root 360 Nov 19 19:23 dev
drwxr-xr-x 1 root root 4.0K Nov 19 19:23 run
dr-xr-xr-x 135 root root 0 Nov 19 19:23 proc
drwxr-xr-x 1 root root 4.0K Nov 19 19:23 .
drwxr-xr-x 1 root root 4.0K Nov 19 19:23 ..
-rw-r--r-- 1 root root 742 Jul 29 2016 server.js
drwx------ 1 root root 4.0K Jul 22 2016 root
drwxr-xr-x 1 root root 4.0K Jul 22 2016 usr
drwxr-xr-x 1 root root 4.0K Jul 22 2016 etc
drwxr-xr-x 1 root root 4.0K Jul 22 2016 var
drwxrwxrwt 1 root root 4.0K Jul 22 2016 tmp
-rw------- 1 root root 384K Jun 9 2016 core
drwxr-xr-x 1 root root 4.0K Jun 9 2016 lib
drwxr-xr-x 2 root root 4.0K Jun 8 2016 sbin
drwxr-xr-x 2 root root 4.0K Jun 8 2016 bin
drwxr-xr-x 2 root root 4.0K Jun 8 2016 lib64
drwxr-xr-x 2 root root 4.0K Jun 8 2016 media
drwxr-xr-x 2 root root 4.0K Jun 8 2016 mnt
drwxr-xr-x 2 root root 4.0K Jun 8 2016 opt
drwxr-xr-x 2 root root 4.0K Jun 8 2016 srv
drwxr-xr-x 2 root root 4.0K May 30 2016 boot
drwxr-xr-x 2 root root 4.0K May 30 2016 home
root@kubernetes-bootcamp-57978f5f5d-nbrgr:/#

Access to kubernetes-bootcamp

Just to service.ymlhave deployed in the kubernetes-bootcampteeth 8080/tcpbecause you have received, corresponding to it Serviceto create.

  • Application Configuration
apiVersion: apps/v1
kind: Deployment
metadata:
name: kubernetes-bootcamp
namespace: k8s-tutorial
spec:
selector:
matchLabels:
app: kubernetes-bootcamp
template:
metadata:
labels:
app: kubernetes-bootcamp
spec:
containers:
- name: kubernetes-bootcamp
image: gcr.io/google-samples/kubernetes-bootcamp:v1
resources:
limits:
memory: "128Mi"
cpu: "500m"
  • Now deploy this application
root@vagrant:/home/vagrant# kubectl apply -f app4test.yml
deployment.apps/kubernetes-bootcamp created
  • Service configuration
apiVersion: v1
kind: Service
metadata:
name: kubernetes-bootcamp
namespace: k8s-tutorial
spec:
selector:
app: kubernetes-bootcamp
type: NodePort
ports:
- port: 80
targetPort: 8080
  • Deploy service
root@vagrant:/home/vagrant# kubectl apply -f service.yml
service/kubernetes-bootcamp created
  • Check Service status
root@vagrant:/home/vagrant# kubectl get svc -n k8s-tutorial
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-bootcamp NodePort 10.43.228.38 <none> 80:32315/TCP 24s
  • Run simple curl to check our deployed application
root@vagrant:/home/vagrant# curl 10.43.110.214
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6b66d5f8fd-2smt4 | v=1

Scale In/Out

The current kubernetes-bootcampnumber of pods is 1, but you can increase or decrease this.

root@vagrant:/home/vagrant# kubectl get po -n k8s-tutorial
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-6b66d5f8fd-2smt4 1/1 Running 0 4m41s
  • Increase number of pods
root@vagrant:/home/vagrant# kubectl scale -n k8s-tutorial deploy kubernetes-bootcamp --replicas 15
deployment.apps/kubernetes-bootcamp scaled

Now kubernetes-bootcampthe Pod number of has been increased to 15, bu let’s check it :)

root@vagrant:/home/vagrant# kubectl get deploy -n k8s-tutorial kubernetes-bootcamp
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 15/15 15 15 6m38s
root@vagrant:/home/vagrant# kubectl get po -n k8s-tutorial
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-6b66d5f8fd-2smt4 1/1 Running 0 8m4s
kubernetes-bootcamp-6b66d5f8fd-crqdt 1/1 Running 0 2m31s
kubernetes-bootcamp-6b66d5f8fd-6l8d4 1/1 Running 0 2m31s
kubernetes-bootcamp-6b66d5f8fd-l27vn 1/1 Running 0 2m31s
kubernetes-bootcamp-6b66d5f8fd-djct7 1/1 Running 0 2m31s
kubernetes-bootcamp-6b66d5f8fd-mpqjc 1/1 Running 0 2m31s
kubernetes-bootcamp-6b66d5f8fd-5xlr4 1/1 Running 0 2m31s
kubernetes-bootcamp-6b66d5f8fd-49vbq 1/1 Running 0 2m31s
kubernetes-bootcamp-6b66d5f8fd-hxj8v 1/1 Running 0 2m31s
kubernetes-bootcamp-6b66d5f8fd-9hvms 1/1 Running 0 2m31s
kubernetes-bootcamp-6b66d5f8fd-5tnqz 1/1 Running 0 2m5s
kubernetes-bootcamp-6b66d5f8fd-hsgq6 1/1 Running 0 2m5s
kubernetes-bootcamp-6b66d5f8fd-6rbsq 1/1 Running 0 2m5s
kubernetes-bootcamp-6b66d5f8fd-2qj5f 1/1 Running 0 2m5s
kubernetes-bootcamp-6b66d5f8fd-bw85j 1/1 Running 0 2m5s

DeploymentIn 15/15has become, Podin fifteen of the Pod has been output.

Ok next, let’s reduce what we have increased to 4

root@vagrant:/home/vagrant# kubectl scale -n k8s-tutorial deploy kubernetes-bootcamp --replicas 4
deployment.apps/kubernetes-bootcamp scaled
root@vagrant:/home/vagrant# kubectl get po -n k8s-tutorial
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-6b66d5f8fd-2smt4 1/1 Running 0 11m
kubernetes-bootcamp-6b66d5f8fd-l27vn 1/1 Running 0 6m
kubernetes-bootcamp-6b66d5f8fd-mpqjc 1/1 Running 0 6m
kubernetes-bootcamp-6b66d5f8fd-5xlr4 1/1 Running 0 6m

Load Balancing

I just accessed one pod. But what if you have multiple pods and access them in the same way?

root@vagrant:/home/vagrant# kubectl get po -n k8s-tutorial
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-6b66d5f8fd-2smt4 1/1 Running 0 11m
kubernetes-bootcamp-6b66d5f8fd-l27vn 1/1 Running 0 6m
kubernetes-bootcamp-6b66d5f8fd-mpqjc 1/1 Running 0 6m
kubernetes-bootcamp-6b66d5f8fd-5xlr4 1/1 Running 0 6m

There are four pods. Let’s actually access it.

root@vagrant:/home/vagrant# curl 10.43.110.214
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6b66d5f8fd-mpqjc | v=1
root@vagrant:/home/vagrant# curl 10.43.110.214
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6b66d5f8fd-2smt4 | v=1
root@vagrant:/home/vagrant# curl 10.43.110.214
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6b66d5f8fd-5xlr4 | v=1
root@vagrant:/home/vagrant# curl 10.43.110.214
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6b66d5f8fd-l27vn | v=1

I’m accessing the Master, but it’s divided into four pods.

Rolling Update

We’ve used in this tutorial kubernetes-bootcamp:v1, now we will update the image v1from v10.

root@vagrant:/home/vagrant# kubectl set image deploy kubernetes-bootcamp kubernetes-bootcamp=google-samples/kubernetes-bootcamp:v10 -n k8s-tutoria
deployment.apps/kubernetes-bootcamp image updated

Let’s actually Podcheck.

root@vagrant:/home/vagrant# kubectl get po -n k8s-tutorial
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-6b66d5f8fd-6xlzn 1/1 Running 0 113s
kubernetes-bootcamp-6dd7d67b89-cjdfp 1/1 Running 0 81s
kubernetes-bootcamp-6b66d5f8fd-nr68t 1/1 Running 0 3s
kubernetes-bootcamp-6dd7d67b89-ds98b 1/1 Running 0 3s

As we can see pod has been recreated due to an updated image and is also a new time. To see the image that is being used is --output wide

root@vagrant:/home/vagrant# kubectl get deploy -n k8s-tutorial -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
kubernetes-bootcamp 4/4 4 4 5m21s kubernetes-bootcamp google-samples/kubernetes-bootcamp:v10 app=kubernetes-bootcamp

If you update the image , and you want to return to the original image due to a problem etc., you can return it with one command.

root@vagrant:/home/vagrant# kubectl rollout undo deploy kubernetes-bootcamp -n k8s-tutorial
deployment.apps/kubernetes-bootcamp rolled back

Let’s actually check results

NAME                                   READY   STATUS      RESTARTS   AGE
kubernetes-bootcamp-6b66d5f8fd-6xlzn 1/1 Running 0 13s
kubernetes-bootcamp-6dd7d67b89-cjdfp 1/1 Running 0 10s
kubernetes-bootcamp-6b66d5f8fd-nr68t 1/1 Running 0 3s
kubernetes-bootcamp-6dd7d67b89-ds98b 1/1 Running 0 3s
kubernetes-bootcamp-6b66d5f8fd-4bl2s 1/1 Terminating 0 14m
kubernetes-bootcamp-6dd7d67b89-5wggs 1/1 Terminating 0 14ms
kubernetes-bootcamp-6b66d5f8fd-ng69z 1/1 Terminating 0 14m
kubernetes-bootcamp-6dd7d67b89-bsxbb 1/1 Terminating 0 14m

So far, you’ve found that Kubernetes makes it easy to deploy, publish, update, and rollback.

 by the author.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Maciej

Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.