Kubernetes: kubectl

kubectl lets you control the Kubernetes Cluster Manager.

https://kubernetes.io/docs/reference/kubectl/kubectl/

 

Quick tips

create – creates resources

get – get resources

apply – applies resources

 

Common Commands

See also the kubectl Cheat Sheet: https://kubernetes.io/docs/reference/kubectl/cheatsheet/

 

Show config: k config view

 

To switch to a non-VT cluster (e.g. minikube), need to:

kubectl config get-contexts

k config use-context minikube

 

And view cluster-info or nodes (e.g. to check) with:

Cluster info: k cluster-info

List nodes: k get nodes

Show deployments: k get deployments

List contexts: k config get-contexts

 

 

Pods

Get all pods: k get pods

Describe all pods: k describe pods

Describe pod: k describe pods <pod name>

Listing all pods:

export POD_NAME=$(kubectl get pods -o go-template –template ‘{{range .items}}{{.metadata.name}}{{“\n”}}{{end}}’)

echo Name of the Pod: $POD_NAME

E.g. get output from pod using proxy

k proxy

curl http://localhost:8001/api/v1/namespaces/default/pods/<name of pod>/proxy/

Logs from Pod: k logs $POD_NAME

 

Services

List services: k get services

Describe service: k describe services/kubernetes-bootcamp

Docs

See also this great tutorial: https://kubernetes.io/docs/tutorials/kubernetes-basics/explore/explore-interactive/

and Cheat Sheet: https://kubernetes.io/docs/tasks/debug-application-cluster/get-shell-running-container/

 

Command Summary

If you run kubectl without any arguments you’ll get some information about it. E.g. some commands:

 

Following examples assume we’ve got the name of the Pod in $POD_NAME, e.g. using:

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

(for more on Go templates see Go: templates

 

Commands – details

apply

Apply additional changes.

Note: this warning can be ignore:

Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply

config view

Shows kubeconfig settings which you can query. E.g.

kubectl config view -o jsonpath='{.users[?(@.name == "minikube")].user.client-certificate}'

https://kubernetes.io/docs/reference/kubectl/cheatsheet/#kubectl-context-and-configuration

create

Create resources

delete

Deletes resources.

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#delete

Note that deleting a service does not stop an app running inside a container.

describe

Shows details of a resource.

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#describe

E.g.

kubectl describe deploy <deployment name>

exec

Run commands on the container.

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#exec

E.g. to see environment variables:

kubectl exec $POD_NAME env

Or to get a terminal:

kubectl exec -it $POD_NAME bash

https://kubernetes.io/docs/tasks/debug-application-cluster/get-shell-running-container/

expose

Creates and exposes a new service to traffic.

Note: --type can be:

  • ClusterIP
  • NodePort
  • LoadBalancer
  • ExternalName.

Default is ClusterIP.

E.g. kubectl expose deployment/kubernetes-bootcamp –type=”NodePort” –port 8080

Running kubectl get services shows the new service.

List the port with describe services/kubernetes-bootcamp.

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#expose

get

Gets resources. E.g. for deployments:

get deployments

Services:

get services

Or pods:

get pods

Note: for a wider format use -o wide.

And for much, much more detail use: -o yaml. e.g.

kubectl get pods -o yaml.

To narrow it down you probably want to use the name of your pod as well. E.g.

kubectl get pods emoji-6fc8785888-gjd8r -o yaml

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get

Note: you can specify the output as ‘template’ and provide a Go template as the value of the --template flag, to filter the attributes of the fetched resources. E.g.

kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'

Or

kubectl get pods emoji-6fc8785888-gjd8r -o jsonpath --template={.status.podIP}

for the IP address of a Pod.

For more on Go Templates see: https://golang.org/pkg/text/template/

 

 

Use --no-headers for no headers. E.g. if you want to pipe into awk.

logs

Prints logs for a container in a pod.

Anything the application sends to STDOUT becomes the logs for the container.

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#logs

proxy

Creates a proxy (note: this is blocking so need to run it in a new terminal) that forwards communications into the cluster-wide private network which we can query directly using the API. E.g.

curl http://localhost:8001/version

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#proxy

rollout

Manage the rollout of a resource. E.g.

kubectl rollout status deployments/kubernetes-bootcamp

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#rollout

Or

kubectl rollout status deployment <name of deployment>

 

And view the history with:

kubectl rollout history deployment <name of deployment>

Note: to make sure rollouts are recorded use --record=true. E.g.

kubectl apply --filename=deploy.yml --record=true

run

runNAME –image=… –port=…

This:

  • searches for a node
  • schedules the application (i.e. the image) to run on that node
  • configures the cluster to the instance on a new node when needed

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#run

Notes: this is the iterative method.

scale

Sets a new size. E.g. number of pods in a deployment.

Trying this on kubernetes.io I scaled beyond capacity and got:

Unable to connect to the server: net/http: TLS handshake timeout

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#scale

set

Configure application resources – e.g. to update a deployment

See also Kubernetes: update a deployed application and rollback

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#set

 

Errors / Problems

The connection to the server 192.168.99.100:8443 was refused – did you specify the right host or port?

Did you start your Kubernetes cluster? E.g. if you’re using minikube then:

minikube start

 

 

Primer on Go

Install

https://stackoverflow.com/questions/12843063/install-go-with-brew-and-running-the-gotour

https://golang.org/doc/install#install

Test

cd $HOME/go/src/hello

go build

./hello

Declaration

Basic

and more idiomatically:

Pointers

*string => pointer to a string

&message => use the memory location of the variable message

so this returns

Hello all%!(EXTRA *string=0xc42007a1c0)

whereas this:

returns the value. i.e.

Hello all%!(EXTRA string=Hello all)

Basic User Types

type Salutation string

or a more complex example:

type Salutation struct {

name string

greeting string

}

func main() {

var a = Salutation{name: “Bob”, greeting: “Hi” }

fmt.Println(s.greeting, s.name)

}

Getters / Setters and Scope

There are no Getters / Setters.

A capitalised variable is publicly visible from a package. E.g. Salutation.

Lowercase is not visible.

Constants

Outputs:

Note on iota. This is used in const delarations to increment numbers.

https://github.com/golang/go/wiki/Iota

A better example of using iotais probably this:

Note the implicit iotain the const for subsequent Weekdays.

Functions

  • Multiple return values
  • Use like any other type
  • Function literals

Note: function literals aka lambdas or closures are functions without a name.

https://programming.guide/go/anonymous-function-literal-lambda-closure.html

E.g. of function declaration:


package main

import "fmt"

type Salutation struct {
    name string
    greeting string
}

func Greet(salutation Salutation) {
    fmt.Println(salutation.greeting)
    fmt.Println(salutation.name)
}

func main() {
    var s = Salutation{name: "Bob", greeting: "Hi" }

    Greet(s)

}

Order of type and variable name

Note the typecomes after the variable name (i.e. opposite to C).

E.g.

This is different to C . For why see https://blog.golang.org/gos-declaration-syntax

Also, if function parameters share a type you can leave the type to the end.

E.g.

Multiple return values

E.g.

 

Exported names

Names are exported if they begin with a capital letter.

i.e. Pi would be exported but pi would not.

Variable declarations

Multiple declarations:

var i, j int = 1, 2

 

Here k has an implicit type of int.

k := 3

Zero values:

0 for numeric

false for Boolean

"" for strings

Type conversions

E.g.

i := 42

f := float64(i)

u := uint(f)

REPL

Handy online REPL here:

https://tour.golang.org/basics/8