John Siu Blog

Tech - Business Tool, Personal Toys

Kubernetes Commands

☰ Table of Content

Some Kubernetes commands.

Node

Restart kubelet (kubernetes agent)

1
sudo systemctl restart kubelet

Get nodes

1
kubectl get nodes -o wide --all-namespaces

Pod

Pod is a collection of container(docker)

Get pods

1
kubectl get pods -o wide --all-namespaces

Create pod

Create pod file: ghost.yml

1
2
3
4
5
6
7
8
apiVersion: v1
kind: Pod
metadata:
  name: ghost
spec:
  containers:
  - name: ghost
    image: ghost

Create ghost pod

1
kubectl create -f ghost.yml

Delete pod

1
kubectl delete pods ghost

Console

1
kubectl exec -it <pod name> bash

Logs

1
2
3
kubectl logs <pod name>
kubectl logs <pod name> -c <container name>
kubectl logs <pod name> --all-containers=true

Label

1
2
kubectl label <kind> <name> <name:value>
kubectl label pod ghost app=v1.8

Service/Expose

Create a service using expose

1
2
3
kubectl expose deployment <name> --type="NodePort" --port 8080
kubectl expose pod <name> --type="NodePort" --port 8080
kubectl expose deployment <name> --type=LoadBalancer

Delete a service

1
2
kubectl delete service <name>
kubectl delete service -l label

Proxy

1
kubectl proxy

Output

1
Starting to serve on 127.0.0.1:8001

Get pod name

1
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')

Curl

1
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/

ReplicataSet

ghostReplicaSet.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: ghost
spec:
  replicas: 5
  selector:
    matchLabels:
      app: ghost
  template:
    metadata:
      name: ghost
      labels:
        app: ghost
    spec:
      containers:
      - name: ghost
        image: ghost

Create replica set

1
kubectl create -f ghostReplicaSet.yml

Scaling - Changing replica number after a set is created. (If a replica set is created by deployment, it need to be scaled using deployment, not replica set directly.)

1
kubectl scale rs ghost --replicas=2

Service

ghostSvc.yml

  • selector is labels
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
apiVersion: v1
kind: Service
metadata:
  name: ghost
spec:
  selector:
    app: ghost
  ports:
  - post: 2368
  type: NodePort
1
kubectl create -f ghostSvc.yml

NodePort will use a random high port of the node. To check what port is being used:

1
kubectl get svc

Output:

1
2
3
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
ghost        NodePort    10.97.85.198   <none>        2368:30218/TCP   1h
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP          2h

2368:30218/TCP means application(ghost) port 2368 inside the pod is mapped to port 30218 of the node.


Generators / Deployments

Deployment will create pod, replica set and service.


Run without YAML

1
kubectl run ghost --image=ghost:1.7 --port 2368 --expose=true

Output:

1
2
service/ghost created
deployment.apps/ghost created

What is created

1
kubectl get all -o wide

Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
NAME                         READY     STATUS    RESTARTS   AGE       IP          NODE      NOMINATED NODE
pod/ghost-7b7d68d64c-xfm95   1/1       Running   0          1m        10.32.0.3   u64s02    <none>

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE       SELECTOR
service/ghost        ClusterIP   10.102.59.219   <none>        2368/TCP   1m        run=ghost
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    3h        <none>

NAME                    DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES    SELECTOR
deployment.apps/ghost   1         1         1            1           1m        ghost        ghost     run=ghost

NAME                               DESIRED   CURRENT   READY     AGE       CONTAINERS   IMAGES    SELECTOR
replicaset.apps/ghost-7b7d68d64c   1         1         1         1m        ghost        ghost     pod-template-hash=3638248207,run=ghost

Edit Manifest(config) on the fly

1
2
kubectl edit <kind> <name>
kubectl edit service ghost

Scale the deployment

1
kubectl scale deployment ghost --replicas=5

Rollout Update

Edit the image version in deployment manifest:

1
kubectl edit deployment ghost

or

1
kubectl set image deployment/ghost ghost=ghost:v1.8

Rollout Status

1
kubectl rollout status deployment <name>

Rollback

1
2
kubectl rollout undo deployment <name>
kubectl rollout undo deployment ghost

John Siu

Update: 2022-05-12
comments powered by Disqus