Skip to content

Latest commit

 

History

History
284 lines (232 loc) · 8.38 KB

03.md

File metadata and controls

284 lines (232 loc) · 8.38 KB
  1. Taints & Tolerations, NodeAffinity/pod-nodeaffinity.yaml

apiVersion: v1 kind: Pod metadata: name: with-node-affinity spec: containers:

  • name: myapp image: nginx:1.20 affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/os operator: In values: - linux preferredDuringSchedulingIgnoredDuringExecution:
    • weight: 1 preference: matchExpressions:
      • key: type operator: In values:
        • cpu

---===--- 11. Taints & Tolerations, NodeAffinity/pod-podaffinity.yaml

apiVersion: apps/v1 kind: Deployment metadata: name: myapp-deployment spec: selector: matchLabels: app: myapp replicas: 5 template: metadata: labels: app: myapp spec: affinity: podAntiAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: app operator: In values: - myapp topologyKey: "kubernetes.io/hostname" podAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: app operator: In values: - etcd topologyKey: "kubernetes.io/hostname" containers: - name: myapp-container image: nginx:1.20 nodeSelector: type: master---===--- 11. Taints & Tolerations, NodeAffinity/pod-with-node-name.yaml

apiVersion: v1 kind: Pod metadata: name: nginx spec: containers:

  • name: nginx image: nginx:1.20 nodeName: worker1---===---
  1. Taints & Tolerations, NodeAffinity/pod-with-node-selector.yaml

apiVersion: v1 kind: Pod metadata: name: nginx spec: containers:

  • name: nginx image: nginx:1.20 nodeSelector: type: cpu---===---
  1. Taints & Tolerations, NodeAffinity/pod-with-tolerations.yaml

apiVersion: v1 kind: Pod metadata: name: pod-with-toleration labels: env: test spec: containers:

  • name: nginx image: nginx:1.20 tolerations:
  • effect: NoExecute operator: Exists nodeName: master ---===---
  1. Taints & Tolerations, NodeAffinity/useful-links.md
assigning pods to nodes
nodeAffinity
interPodAffinity and antiAffinity
taints & tolerations

---===--- 12. Readiness & Liveness Probes/pod-health-probes.yaml

apiVersion: v1 kind: Pod metadata: name: myapp-health-probes spec: containers:

  • image: nginx:1.20 name: myapp-container ports:
    • containerPort: 80 readinessProbe: tcpSocket: port: 80 initialDelaySeconds: 10 periodSeconds: 5 livenessProbe: tcpSocket: port: 80 initialDelaySeconds: 5 periodSeconds: 15

---===--- 12. Readiness & Liveness Probes/useful-links.md

health probes
  1. Rolling Updates/commands.md
rollout commands
kubectl rollout history deployment/{depl-name}
kubectl rollout undo deployment/{depl-name}
kubectl rollout status deployment/{depl-name}

---===--- 13. Rolling Updates/useful-links.md

replicaset
deployment upgrade strategies

---===--- 14. Etcd Backup & Restore/commands.md

Install ectdctl

sudo apt  install etcd-client

Backup

snapshot backup with authentication
ETCDCTL_API=3 etcdctl snapshot save /tmp/etcd-backup.db \
--cacert /etc/kubernetes/pki/etcd/ca.crt \
--cert /etc/kubernetes/pki/etcd/server.crt \
--key /etc/kubernetes/pki/etcd/server.key
check snapshot status
ETCDCTL_API=3 etcdctl --write-out=table snapshot status snapshotdb

Restore

create restore point from the backup
ETCDCTL_API=3 etcdctl snapshot restore /tmp/etcd-backup.db --data-dir /var/lib/etcd-backup
the restored files are located at the new folder /var/lib/etcd-backup, so now configure etcd to use that directory:
vim /etc/kubernetes/manifests/etcd.yaml

---===--- 14. Etcd Backup & Restore/useful-links.md

etcd backup

Back up etcd store: https://kubernetes.io/docs/tasks/administer-cluster/configure-upgrade-etcd/#backing-up-an-etcd-cluster

etcd restore

Restore etcd backup: https://kubernetes.io/docs/tasks/administer-cluster/configure-upgrade-etcd/#restoring-an-etcd-cluster ---===--- 15. K8s Rest API/commands.md

Access API through proxy

kubectl proxy --port=8081 &
curl http://localhost:8081/api/

Access without kubectl proxy

create serviceaccount for myscript usage
kubectl create serviceaccount myscript
create role with Deployment, Pod, Service permissions
kubectl apply -f myscript-role.yml
add Binding for serviceaccount
kubectl create rolebinding script-role-binding --role=script-role --serviceaccount=default:myscript
get config info from kubectl
kubectl config view
set cluster location var
APISERVER=https://172.31.44.88:6443
set token var from default token
kubectl get serviceaccount myscript -o yaml
kubectl get secret xxxxx -o yaml

TOKEN=$(echo "token" | base64 --decode | tr -d "\n")
if we don't have the ca cert for curl, we can accept insecure, without providing curl client with ca certificate
curl -X GET $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
if we don't want insecure connection, we can specify ca cert for curl providing curl with k8s ca certificate
curl -X GET $APISERVER/api --header "Authorization: Bearer $TOKEN" --cacert /etc/kubernetes/pki/ca.crt

Get data

main endpoint /api
curl -X GET $APISERVER/api --header "Authorization: Bearer $TOKEN" --cacert /etc/kubernetes/pki/ca.crt
list all deployments
curl -X GET $APISERVER/apis/apps/v1/namespaces/default/deployments --header "Authorization: Bearer $TOKEN" --cacert /etc/kubernetes/pki/ca.crt
list all services
curl -X GET $APISERVER/api/v1/namespaces/default/services --header "Authorization: Bearer $TOKEN" --cacert /etc/kubernetes/pki/ca.crt
get a specific service or deployment
curl -X GET $APISERVER/api/v1/namespaces/default/services/nginx-service --header "Authorization: Bearer $TOKEN" --cacert /etc/kubernetes/pki/ca.crt
get all pod names
curl -X GET $APISERVER/api/v1/namespaces/default/pods/pod-name/logs --header "Authorization: Bearer $TOKEN" --cacert /etc/kubernetes/pki/ca.crt

---===--- 15. K8s Rest API/myscript-role.yaml

apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: script-role rules:

  • apiGroups: [""] resources: ["pods", "services"] verbs: ["get", "list", "delete"]
  • apiGroups: ["apps"] resources: ["deployments"] verbs: ["get", "list", "delete"]---===---
  1. K8s Rest API/useful-links.md
K8s rest API
programmatic access