How to use kubernetes

qomarullah
4 min readOct 23, 2020

simple tutorial

This tutorial use kubernetes in digital ocean.

Create k8s with minimum resource 2 nodes : 2CPU, 4GB Memmory, it takes $20/month

Install kubectl

https://kubernetes.io/docs/tasks/tools/install-kubectl/

Configuration

  • download cluster config from DO and put inside ~/.kube
$ cd ~/.kube && kubectl --kubeconfig="k8s-01-kubeconfig.yaml" get nodes

if you have one config, you can copy k8s cluster config as default, so your command is only kubectl

$cp k8s-01-kubeconfig.yaml config
$kubectl get nodes

now we have two pool nodes ready.

Deployment

Before you go to deployment and dont know anything about docker, you can check this first tutorial.

We can use this sample deployment config

$ kubectl apply -f deployment.yml
deployment.apps/go-hello-world created
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
go-hello-world 3/3 3 3 107s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
go-hello-world-fbfc48747-g9d8g 1/1 Running 0 2m32s
go-hello-world-fbfc48747-jk6vb 1/1 Running 0 2m32s
go-hello-world-fbfc48747-tngx9 1/1 Running 0 2m32s

Service

After deployment pods, you need access from outside cluster using services.

  • Test port forward kube to one pod
$ kubectl port-forward go-hello-world-fbfc48747-g9d8g 8080:8080
Forwarding from 127.0.0.1:8080 -> 8080
Forwarding from [::1]:8080 -> 8080
$ curl localhost:8080
Hello, Guest
  • Creating service act as proxy to multiple pods inside cluster
$ kubectl apply -f deployment.yml
deployment.apps/go-hello-world unchanged
service/go-hello-world-service created
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
go-hello-world-service NodePort 10.245.139.86 <none> 9090:31447/TCP 6m56s
kubernetes ClusterIP 10.245.0.1 <none> 443/TCP 27h

we can see there is cluster IP type nodeport 10.245.139.86:9090, this is still private IP so then we need to additional IP public for i, it can be exposed using LB

Ingress with LB

update services to use type LoadBalancer instead NodePort

After re-apply deployment with Load Balancer

$ kubectl apply -f deployment-with-lb.yml 
deployment.apps/go-hello-world unchanged
service/go-hello-world-service configured
$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
go-hello-world-service LoadBalancer 10.245.139.86 165.227.241.128 80:31365/TCP 91m
kubernetes ClusterIP 10.245.0.1 <none> 443/TCP 28h

Now we have ip public load balancer, and we also have new bill for load balancer with two droplet. It takes arround $10/month

$kubectl describe service go-hello-world-service
Name: go-hello-world-service
Namespace: default
Labels: <none>
Annotations: kubernetes.digitalocean.com/load-balancer-id: de209ac6-ea0f-46ea-a13c-066d84d24425
Selector: app=go-hello-world
Type: LoadBalancer
IP: 10.245.199.8
LoadBalancer Ingress: 165.227.241.128
Port: tcp 80/TCP
TargetPort: 8080/TCP
NodePort: tcp 30431/TCP
Endpoints: 10.244.0.172:8080,10.244.0.224:8080,10.244.0.44:8080
Session Affinity: None
External Traffic Policy: Cluster
Events:

Now we can curl to load balancer

curl http://165.227.241.128
Hello, Guest

Dashboard

$ kubectl cluster-info
Kubernetes master is running at https://30543e5d-3037-4c59-9fe8-922c70bf303a.k8s.ondigitalocean.com
CoreDNS is running at https://30543e5d-3037-4c59-9fe8-922c70bf303a.k8s.ondigitalocean.com/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
$ kubectl -n default describe deployments
Name: go-hello-world
Namespace: default
CreationTimestamp: Sat, 17 Oct 2020 21:47:50 +0700
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=go-hello-world
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=go-hello-world
Containers:
go-hello-world:
Image: qomarullah/go-hello-world:1.0.0
Port: 8080/TCP
Host Port: 0/TCP
Liveness: http-get http://:8080/health delay=5s timeout=5s period=15s #success=1 #failure=3
Readiness: http-get http://:8080/readiness delay=5s timeout=1s period=10s #success=1 #failure=3
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: go-hello-world-fbfc48747 (3/3 replicas created)
Events: <none>

We can check dashboard kubernetes via digitalocean dashboard link

How to Scale

kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
go-hello-world 3/3 3 3 74m
$ kubectl scale --replicas=4 deployment/go-hello-world
deployment.apps/go-hello-world scaled
$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
go-hello-world 4/4 4 4 76m

How to Delete

kubectl delete pod {pod} 
kubectl delete service {service}
kubectl delete deployment {deployment}

Reference

--

--