Skip to content

Instantly share code, notes, and snippets.

@constantlycoding
Last active July 25, 2022 08:04
Show Gist options
  • Save constantlycoding/220f0dcb4aef8b0a14d4f336f0b102b1 to your computer and use it in GitHub Desktop.
Save constantlycoding/220f0dcb4aef8b0a14d4f336f0b102b1 to your computer and use it in GitHub Desktop.
My CKA tips

Autocomplete

alias k=kubectl
complete -F __start_kubectl k

Namespace

# Create
k create ns dev

# Change
k config set-context --current --namespace=dev

# Check
k get sa default -ojsonpath='{.metadata.namespace}{"\n"}'

Pod

k run --generator=run-pod/v1 nginx --image=nginx

Deployment

Run

k run --generator=deployment/v1beta1 nginx --image=nginx
k run --generator=deployment/v1beta1 nginx --image=nginx --replicas=4

Create

k create deploy --image=nginx nginx
# Create has no --replicas option
k scale deploy nginx --replicas=4

Service

Expose (use pod's labels as selectors)

# ClusterIP
k expose pod redis --port=6379 --name redis-svc

# NodePort
k expose pod nginx --port=80 --name nginx-svc --dry-run -oyaml > nginx-svc.yaml
# Cannot specify nodePort
vim nginx-svc.yaml
# Add:
# spec:
#   type: NodePort
#   ports:
#   - port: 80
#     nodePort: 30008
k create -f nginx-svc.yaml

Create (assumes selectors as app=<svc_name>)

# ClusterIP
k create svc clusterip redis --tcp=6379:6379

# NodePort
k create svc nodeport nginx --tcp=80:80 --node-port=30080

Select

# Show all labels
k get po --show-labels

# Show specific labels
k get po -Lrun,app

# Select labels
k get po -Lrun -lrun=nginx,app=app1

Label

k label po nginx app=app1

Annotate

k annotate po nginx description='frontend'

Secret

k create secret generic app-secret --from-literal=DB_HOST=mysql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment