View on GitHub

notes

kubernetes

tips

installing

installing kubeadm, kubelet, kubectl

apt-get update
apt-cache madison kubeadm # for to see kubeadm versions
apt-get install kubelet=<version> kubeadm=<version> kubectl=<version>

installing container runtime

control groups (cgroup)

initializing control-plane

installing network addon (cni)

cluster upgrade

kubeadm - upgrade

metada

apiVersion: v1
kind: Pod
metadata:
  name: webapp-color
  labels:
    name: webapp-color

commands and arguments

spec:
  containers:
  - name: ubuntu
    image: ubuntu
    command:
    - "sleep"
    args:
    - "1200"

environment variables

spec:
  containers:
  - name: ubuntu
    image: ubuntu
    env:
    - name: "APP_COLOR"
      value: "GREEN"
spec:
  containers:
  - name: ubuntu
    image: ubuntu
    env:
    - name: APP_COLOR
      valueFrom:
        configMapKeyRef:
          name: webapp-config-map
          key: APP_COLOR

configmap

emre@home ~ → kubectl create configmap \
  app-config --from-literal=APP_COLOR=blue \
  --from-literal=APP_MODE=test
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  APP_COLOR: blue
  APP_MODE: test
spec:
  containers:
  - name: ubuntu
    image: ubuntu
    envFrom:
    - configMapRef:
        name: app-config

secrets

emre@home ~ → kubectl create secret generic <secret-name> --from-literal=<key>=<value>

emre@home ~ → kubectl create secret generic app-secret --from-file=app_secret.properties

apiVersion: v1
kind: Secret
metadata:
  name: app-secret
data:
  DB_Host: mysql
  #DB_HOST: bXlzcWw=
  DB_User: root
  DB_Password: password
spec:
  containers:
  - name: ubuntu
    image: ubuntu
    envFrom:
    - secretRef:
        name: app-secret

init containers

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']

cluster maintenance

etcd

backup & restore

security

secure hosts

authentication

tls in kubernetes

kubernetes server components

kubernetes client components

api groups

/metrics /healthz /version /api /apis /logs

authorization mechanisms

rbac

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: developer
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["list", "get", "create", "update", "delete"]
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metada:
  name: devuser-developer-binding
subjects:
- kind: User
  name: dev-user
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: developer
  apiGroup: rbac.authorization.k8s.io

cluster roles

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: cluster-administrator
rules:
- apiGroups: [""]
  resources: [“nodes"]
  verbs: ["list“, "get", “create“, “delete"]
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: cluster-admin-role-binding
subjects:
- kind: User
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: cluster-administrator
  apiGroup: rbac.authorization.k8s.io

service accounts

apiVersion: v1
kind: Secret
type: kubernetes.io/service-account-token
metadata:
  name: mysecretname
  annotations:
    kubernetes.io/service-account.name: myserviceaccount

image security

kubectl create secret docker-registry regcred --docker-server= --docker-username --docker-password --docker-email

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
  - name: nginx
    image: private-registry.io/apps/internap-app-name
  imagePullSecrets:
  - name: regrecd

security context

apiVersion: v1
kind: Pod
metadata:
  name: web-pod
spec:
  securityContext:
   runAsUser: 1000
  containers:
  - name: ubuntu
    image: ubuntu
    command: ["sleep", "3600"]
apiVersion: v1
kind: Pod
metadata:
  name: web-pod
spec:
  containers:
  - name: ubuntu
    image: ubuntu
    command: ["sleep", "3600"]
    securityContext:
      runAsUser: 1000
      capabilities:
        add: ["MAC_ADMIN"]

network

network policy

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: db-policy
spec:
  podSelector:
    matchLabels:
      role: db
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          name: api-pod
      namespaceSelector:
        matchLabels:
          name: prod
    ports:
    - protocol: TCP
      port: 3306
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: db-policy
spec:
  podSelector:
    matchLabels:
      role: db
  policyTypes:
  - Ingress
  ingress:
  - from:
    - ipBlock:
        cidr: 192.168.5.10/32
    ports:
    - protocol: TCP
      port: 3306
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: db-policy
spec:
  podSelector:
    matchLabels:
      role: db
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - ipBlock:
        cidr: 192.168.5.10/32
    ports:
    - protocol: TCP
      port: 3306
  egress:
  - to:
    - ipBlock:
        cidr: 192.168.5.10/32
    ports:
    - protocol: TCP
      port: 80

certificates api

apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
  name: Jane
spec:
  expirationSeconds: 600
  usages:
  - digital signature
  - key encipherment
  - server auth
  request:
  - <base64 csr>

kube config

apiVersion: v1
kind: Config
current-context: my-kube-admin@my-kube-playground
clusters:
- name:  my-kube-playground
  cluster:
    certificate-authority: ca.crt
    server: https://my-kube-playground:6443
context:
- name: my-kube-admin@my-kube-playground
  context:
    cluster: my-kube-playground
    user: my-kube-admin
    namespace: finance
users:
- name: my-kube-admin
  user:
    client-certificate: admin.crt
    client-key: admin.key

volumes

apiVersion: v1
kind: Pod
metadata:
  name: random-number-generator
spec:
  containers:
  - image: alpine
    name: alpine
    volumeMounts:
    - mountPath: /opt
      name: data-volume
  volumes:
  - name: data-volume
    hostPath:
      path: /data
      type: Directory

persistent volumes

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-vol1
spec:
  accessModes: # defines how a volume should be mounted on host
  - ReadWriteOnce
  capacity:
    storage: 1Gi
  hostPath: # defines volume type do not use this on production replace this with storage solutions
    path: /tmp/data
apiVersion: v1
kind: PersistenctVolumeClaim
metadata:
  name: myclaim
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 505Mi
spec:
  volumes:
  - name: mypd
    persistentVolumeClaim:
      claimName: myclaim

storage class

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: google-storage
provisioner: kubernetes.io/gce-pd
spec:
  storageClassName: google-storage

network

cni plugin responsibilities

service network

dns

ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-example
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx-example
  rules:
  - http:
      paths:
      - path: /testpath
        pathType: Prefix
        backend:
          service:
            name: test
            port:
              number: 80

gateway api

apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
  name: example-class
spec:
  controllerName: example.com/gateway-controller
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: example-gateway
spec:
  gatewayClassName: example-class
  listeners:
  - name: http
    protocol: HTTP
    port: 80
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metada:
  name: example-httproute
spec:
  parentRefs:
  - name: example-gateway
    namespace: example-namespace
  hostname:
  - "www.example.com"
  rules:
  - matches:
    - path:
        type: Prefix
        value: /login
    backendRefs:
    - name: example-svc
      port: 8080