k8s Basics

This post demonstrate some Kubernetes basic commends.

Server Overview

We have setup 3 virtual machines,each has 1 cpu and 1GB memory. Details:

Server OS IP Address Node Type CPU Memory Hostname
ubuntu16.04 192.168.56.103 master 1 1G server01
ubuntu16.04 192.168.56.104 slave1 1 1G server02
ubuntu16.04 192.168.56.105 slave2 1 1G server03

To follow this demo, root privilege is required, ask system admin (Todd) for root access.

Startup all nodes

Login to the system then start virtualBox and each virtual machine.

Launch VirtualBox

1
2
3
4
# get root privilege
$ sudo -s
# Then start master, slave1 and slave2 virtual machines
$ virtualbox

Login to each node

1
2
3
4
5
$ Login as: main
# passwd: 000000

# And get root priviledge on each node
$ sudo -s

Check system information on each node

1
2
3
4
5
6
# check ip address
$ ifconfig
# check running containers
$ docker ps
# check services & ports
$ netstat -nltp

On master node, one should see services and their port numbers: kube-apiserver : 6443/8080, etcd : 2379/2380, kube-scheduler : 10251, kube-controller : 10252, calico-felix : 9099

On worker nodes, one should see services and their port numbers: kubelet : 4194/10248/10250/10255, kube-proxy:10249/10256, calico-felix : 9099

Commonly used commands

Use calico to check the network status on each node

1
$ calicoctl node status

At each node, it should be able to see the other two nodes’ ip addresses in the cluster.

Use kubectl on master node to verify the cluster resources (deloyment, nodes, pods, services, etc.)

1
2
3
4
5
6
7
8
9
10
# check server/client version
$ kubectl version
# get workers
$ kubectl get node
# get pods
$ kubectl get pods
# get deployment
$ kubectl get deploy
# get services
$ kubectl get svc

More kubectl commands

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$ kubectl run kubernetes-bootcamp --image=jocatalin/kubernetes-bootcamp:v1 --port=8080
# check deploy/pods again
$ kubectl get deploy
$ kubectl get pods
# i.e. NAME: kubernetes-bootcamp-6b7849c495-p7dsw

# Then check log of the pod
$ kubectl logs kubernetes-bootcamp-6b7849c495-p7dsw -f
# (ctrl-c out the following log)

# describe pod
$ kubectl describe pods kubernetes-bootcamp-6b7849c495-p7dsw
# (Find the Mounts:/var/run/secrets/kubernetes.io/serviceaccount in the description)

# Enter the running pod and verify the above path
$ kubectl exec -it kubernetes-bootcamp-6b7849c495-p7dsw bash
# Find out the certificate files in that path
$ ls -l /var/run/secrets/kubernetes.io/serviceaccount
# Exit the pod
$ exit
# These ca files are actually associate with the ca account, check that info by:
$ kubectl get sa -o yaml
# can also output other pattern, e.g. json

# check the "secret"
$ kubectl get secrets -o yaml
# (The content of this secret has 3 sections that are mounted as three files in each pod as we see in above)

The “secret” are mounted to each created pod as files located in /var/run/secrets/… so that each pod can connect with api-server with https requests.

Use ‘apply’ or ‘create’ with yaml files

Kubectl ‘apply’ command is similar to ‘create’ command, but has rich properties.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
$ cd services
# Create a nginx pod and verify
$ kubectl apply -f nginx-pod.yaml
$ kubectl describe pod nginx
# (version here used is 1.7.9)

# vi nginx-pod.yaml and change image:nginx:1.7.9 -> image:nginx:1.13, re-apply the yaml file
$ kubectl apply -f nginx-pod.yaml
# (version here is now 1.13)

# Another way to change version of running image is to use 'set' command
$ kubectl set image pods nginx nginx=nginx:1.7.9
# (reset the version to 1.7.9)

# 'apply' command can be also used to create other resources
$ kubectl apply -f nginx-deployment.yaml
$ kubectl apply -f nginx-service.yaml

# Check the service is runing
$ curl 192.168.56.104:20000
$ curl 192.168.56.105:20000
# (Nginx welcome page should be displayed)

# Verify the service
$ kubectl get svc
# (copy the CLUSTER-IP for nginx-service here, e.g. 10.68.33.239)
# Use busybox image (sandbox within the cluster) for testing
$ kubectl delete pod busybox
$ kubectl run busybox --rm=true --image=busybox --restart=Never --tty -i
# In busybox container access nginx service with kube-proxy
$ wget -qO - 10.68.33.239:8080
# One can also access the service directly through service name
$ wget -qO - nginx-service:8080
# exit
$ exit

# clear-out the cluster after this demo session
$ kubectl delete -f nginx-pod.yaml
$ kubectl delete -f nginx-deployment.yaml
$ kubectl delete -f nginx-service.yaml
$ kubectl delete deploy kubernetes-bootcamp

Following YouTube links also provide some examples worth trying out:

[1] https://www.youtube.com/watch?v=K1HuOLzPSpU

[2] https://www.youtube.com/watch?v=yu3HlOXoEKk

[3] https://www.youtube.com/watch?v=kvQ3VT_wH98

ㄟ(●′ω`●)ㄏ
0%