Kubernetes
Kubernetes¶
Install/Startup¶
MicroK8s¶
Kubectl¶
Kubernetes Info:
#Kubernetes Information
>>> kubectl cluster-info
Kubernetes control plane is running at https://192.168.49.2:8443
CoreDNS is running at https://192.168.49.2:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
#Minicube Info
>>> minikube profile list
|----------|-----------|---------|--------------|------|---------|---------|-------|----------------|--------------------|
| Profile | VM Driver | Runtime | IP | Port | Version | Status | Nodes | Active Profile | Active Kubecontext |
|----------|-----------|---------|--------------|------|---------|---------|-------|----------------|--------------------|
| minikube | docker | docker | 192.168.49.2 | 8443 | v1.30.0 | Running | 1 | * | * |
|----------|-----------|---------|--------------|------|---------|---------|-------|----------------|--------------------|
#Service Info
>>> kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapp LoadBalancer 10.110.151.13 <pending> 8080:30306/TCP 3h43m
Pod Information:
#Pod Information
>>> kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
hello-world-pod 1/1 Running 0 169m <none>
myapp-865648495b-gsb9z 0/1 CrashLoopBackOff 37 (64s ago) 165m app=myapp,pod-template-hash=865648495b
myapp-865648495b-j7hqg 0/1 CrashLoopBackOff 37 (33s ago) 165m app=myapp,pod-template-hash=865648495b
#Get Single Pod Info
>>> kubectl describe pod hello-world-pod
Name: hello-world-pod
Namespace: myspace
Priority: 0
Service Account: default
Node: minikube/192.168.49.2
Start Time: Sat, 07 Sep 2024 00:05:05 +0000
Labels: <none>
Annotations: <none>
Status: Running
IP: 10.244.0.8
IPs:
IP: 10.244.0.8
Containers:
hello-world-container:
Container ID: docker://a5b41e190a297e935a4553ef1f47e7ae7744923eb922588cd5e6576947cc4e03
Image: nginx:latest
Image ID: docker-pullable://nginx@sha256:04ba374043ccd2fc5c593885c0eacddebabd5ca375f9323666f28dfd5a9710e3
Port: <none>
Host Port: <none>
State: Running
Started: Sat, 07 Sep 2024 00:05:11 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-bz7gf (ro)
Conditions:
Type Status
PodReadyToStartContainers True
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-bz7gf:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>
#Get Single Service
>>> kubectl describe service myapp
Name: myapp
Namespace: myspace
Labels: app=myapp
Annotations: <none>
Selector: app=myapp
Type: LoadBalancer
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.110.151.13
IPs: 10.110.151.13
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30306/TCP
Endpoints:
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
#Delete Pod
>>> kubectl delete pod myapp-865648495b-jbgg9 myapp-865648495b-ks9ks
pod "myapp-865648495b-jbgg9" deleted
pod "myapp-865648495b-ks9ks" deleted
#Delete all bods by label
>>> kubectl delete pod -l app=myapp
pod "myapp-865648495b-gsb9z" deleted
pod "myapp-865648495b-j7hqg" deleted
Namespaces:
#Create New Namespace
>>> kubectl create namespace myspace
namespace/myspace created
#Enter new namespace
>>> kubectl config set-context --current --namespace=myspace
Context "minikube" modified.
Deploy new Containers:
#Create New Container
>>> kubectl create deployment myapp --image=busybox:latest
deployment.apps/myapp created
#Scale Container
>>> kubectl scale deployment myapp --replicas=2
deployment.apps/myapp scaled
#Open Conter Ports to Server
>>> kubectl expose deployment myapp --port 8080 --type=LoadBalancer
service/myapp exposed
Using YAML Files¶
Sample config:
apiVersion: v1
kind: Pod
metadata:
name: hello-world-pod
spec:
containers:
- name: hello-world-container
image: nginx:latest
# command: ["echo"]
# args: ["Hello World!"]
Run Config:
kubectl apply -f pod.yml
Secrets¶
Secrets are Mounted as Volumes
Secret Management:
#Add new Secret
>>> kubectl create secret generic mysecret --from-literal=user="MyUserName" --from-literal=password="examplepassword"
secret/mysecret created
#List Secrets
>>> kubectl get secrets
NAME TYPE DATA AGE
mysecret Opaque 2 11s
#Get Secret Info
>>> kubectl get secret mysecret -o yaml
apiVersion: v1
data:
password: ZXhhbXBsZXBhc3N3b3Jk
user: TXlVc2VyTmFtZQ==
kind: Secret
metadata:
creationTimestamp: "2024-09-07T15:50:40Z"
name: mysecret
namespace: myspace
resourceVersion: "102386"
uid: b1563310-d425-49fe-8427-32a3fa41018d
type: Opaque
Jobs¶
Cron Jobs
Stern¶
Best way to View containers logs for all of the running instance of the same type.
All Containers with the same Pod name's logs:
>>> stern hello-world
+ hello-world-pod › hello-world-container
hello-world-pod hello-world-container /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
hello-world-pod hello-world-container /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
hello-world-pod hello-world-container /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
hello-world-pod hello-world-container 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
hello-world-pod hello-world-container 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
hello-world-pod hello-world-container /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
hello-world-pod hello-world-container /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
hello-world-pod hello-world-container /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
hello-world-pod hello-world-container /docker-entrypoint.sh: Configuration complete; ready for start up
hello-world-pod hello-world-container 2024/09/07 00:05:11 [notice] 1#1: using the "epoll" event method
hello-world-pod hello-world-container 2024/09/07 00:05:11 [notice] 1#1: nginx/1.27.1
hello-world-pod hello-world-container 2024/09/07 00:05:11 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
hello-world-pod hello-world-container 2024/09/07 00:05:11 [notice] 1#1: OS: Linux 6.6.49-1-lts
hello-world-pod hello-world-container 2024/09/07 00:05:11 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
hello-world-pod hello-world-container 2024/09/07 00:05:11 [notice] 1#1: start worker processes
hello-world-pod hello-world-container 2024/09/07 00:05:11 [notice] 1#1: start worker process 29
hello-world-pod hello-world-container 2024/09/07 00:05:11 [notice] 1#1: start worker process 30
hello-world-pod hello-world-container 2024/09/07 00:05:11 [notice] 1#1: start worker process 31
hello-world-pod hello-world-container 2024/09/07 00:05:11 [notice] 1#1: start worker process 32
hello-world-pod hello-world-container 2024/09/07 00:05:11 [notice] 1#1: start worker process 33
hello-world-pod hello-world-container 2024/09/07 00:05:11 [notice] 1#1: start worker process 34
hello-world-pod hello-world-container 2024/09/07 00:05:11 [notice] 1#1: start worker process 35
hello-world-pod hello-world-container 2024/09/07 00:05:11 [notice] 1#1: start worker process 36
Tilt¶
Launch with tilt:
tilt up --file Tiltfile.full-stack