Skip to content

Instantly share code, notes, and snippets.

@dudo
Last active December 5, 2021 17:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dudo/14c2b25bf8f19e5026379fee34147497 to your computer and use it in GitHub Desktop.
Save dudo/14c2b25bf8f19e5026379fee34147497 to your computer and use it in GitHub Desktop.
Tooling for Interacting with Kubernetes

Kubernetes local development

kubectl

Autocomplete

echo "source <(kubectl completion zsh)" >> ~/.zshrc

Ingress Controller

This will allow you to visit localhost, simply, and it will route based on services:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-0.32.0/deploy/static/provider/cloud/deploy.yaml

Verify this is working with kubectl get pods -n ingress-nginx

Private Docker images

Locally, docker login is probabably sufficient. You can also create a secret via yaml:

cat <<EOF | kubectl apply -f -
---
apiVersion: v1
kind: Secret
metadata:
  name: regcred
data:
  .dockerconfigjson: $(echo "{\"auths\": {\"https://index.docker.io/v1/\": {\"auth\": \"$(echo "janedoe:xxxxxxxxxxx" | base64)\"}}}" | base64)
type: kubernetes.io/dockerconfigjson
EOF

Or manually via:

kubectl create secret docker-registry regcred \
  --docker-server=https://index.docker.io/v1/ \
  --docker-username=janedoe \
  --docker-password=xxxxxxxxxxx \
  --docker-email=jdoe@example.com

Using kubectl

Modify the deployment of the repository you're working on to allow live reloading of your code (volumeMounts), then apply the services you need access to.

Helpful commands:

docker help
docker images
docker ps
docker build -t <organization/repo:tag> .
docker run --rm <organization/repo:tag>
docker push <organization/repo:tag>
docker commit <container id> <organization/repo:tag>

kubectl help

kubectl get nodes
kubectl drain <node name>
kubectl delete node <node name>

kubectl get deployments
kubectl edit deploy <deployment name>
kubectl scale deploy <deployment name> --replicas=0
kubectl rollout restart StatefulSet/foo
kubectl rollout restart Deployment/bar

kubectl get pods
kubectl describe pod <pod name>
kubectl attach -it <pod name>
kubectl exec -it <pod name> -- /bin/bash
kubectl exec -it <pod name> -c <sub-name> -- printenv # good for if you want to get into a debug container
kubectl run <pod name>
kubectl logs -f <pod name>
kubectl delete pod <pod name>

kubectl get services
kubectl get ingress
kubectl get ds
kubectl get secrets
kubectl get configmaps

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - sleep
    - "1000000"
EOF

Run application specific scripts

Once you've verified and viewed all your pods from the Dashboard, run some scripts for the various repos:

kubectl get pods | grep my_app

Take note of that pod name, something like my_app-6cd98d5778-ql9xf

kubectl exec <pod name> bundle exec rake db:reset

Dashboard

https://github.com/kubernetes/dashboard

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.1/aio/deploy/recommended.yaml
kubectl create serviceaccount dashboard-admin-sa
kubectl create clusterrolebinding dashboard-admin-sa --clusterrole=cluster-admin --serviceaccount=default:dashboard-admin-sa

kubectl get secrets
kubectl describe secret dashboard-admin-sa-token-#####
kubectl proxy

Visit the Dashboard, and authenticate with the token from above:

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

Monitoring

https://github.com/kubernetes-incubator/metrics-server

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.6/components.yaml

As of 2019-11, a local cluster needs a bit of a security hole. Follow along here

Add the following arguments to the metrics-server container via kubectl edit deploy -n kube-system metrics-server

- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname

fluxctl

https://docs.fluxcd.io/en/stable/tutorials/get-started-kustomize.html https://github.com/fluxcd/flux/blob/master/docs/references/fluxctl.md

Flux is a gitops solution to CD.

Modify the file below before copying and pasting:

brew install fluxctl

cat <<EOF | kubectl apply -f -
---
namespace: flux
bases:
  - github.com/fluxcd/flux/deploy
patchesStrategicMerge:
  - patch.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: flux
spec:
  template:
    spec:
      containers:
        - name: flux
          args:
            - --manifest-generation=true # USED FOR KUSTOMIZE
            - --memcached-hostname=memcached.flux
            - --memcached-service=
            - --ssh-keygen-dir=/var/fluxd/keygen
            - --git-branch=master
            - --git-user=Flux automation
            - --git-email=flux@example.com
            - --git-url=git@github.com:dudo/k8s_colors # YOUR CLUSTER
            - --sync-garbage-collection=true
            - --git-poll-interval=30s
            - --registry-poll-interval=30s
EOF

export FLUX_FORWARD_NAMESPACE=flux
fluxctl identity
fluxctl sync
fluxctl list-workloads
fluxctl list-images

Manage workloads - https://docs.fluxcd.io/en/1.20.2/references/fluxctl/#workloads

linkerd

https://linkerd.io/2/getting-started/

Linkerd is a service mesh. TLDR - The service mesh gives you features that are critical for running modern server-side software in a way that’s uniform across your stack and decoupled from application code.

brew install linkerd

linkerd check --pre
linkerd install | kubectl apply -f -
linkerd check
linkerd dashboard &

Start Reading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment