Wednesday, February 9, 2022

Day9 - kubernetes pod, configmap, statelful

 2/09/2022 - Class Notes
ReCap from last class
Configmap
Probes
PV
PVC
Storage class
----------------
Todays agent
create container
 - statefulset
 - helm
 - cronjobs
 - Stateful set
AWs authentication
- EBS volume
- PVC
read about 
job
initcontainer
daemonset

===============
nebulawsworks.com/insights/posts/leaveraging-aws-ens-for-kubernetes-persistent-volumes
- try to use ansible/terraform to recreate your cluster.
- using the tool, you can create and destroy rather then  manual tasks. 
---------------------------
Job and service
job - executes command/script one time. its an one time job. How do we run a particular command in job?
what is job in k8s?
A Job creates one or more Pods and will continue to retry execution of the Pods until a specified number of them successfully terminate. As pods successfully complete, the Job tracks the successful completions.

pod
images
-------
marvel
perl
ruby
python
How to run the image one time?
$ docker run python python abc.py
$ docker run <python-image> python abc.py
https://kubernetes.io/docs/concepts/workloads/controllers/job/
Read this link line by line
$ vi job.yml
apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
  backoffLimit: 4
$ kc apply -f job.yml
$ kc describe
$ kc logs $pods
$ kubectl apply -f job.yml
$ kc get pod --watch
$ kc get pods
pods are created
$ kc logs pi-rdf5t
CLean up finished jobs automatically
- cronjobs
TTL mechanism for finished jobs
https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/
crontab.guru
$ cat cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure
$ kc apply -f cronjob.yaml
$ kc get svc -n kubernetes-dashboard
31617
httpS://get-the-nodeIP:port
[00:27:00]

$ kc get cronjob
$ kc delete confjob hello
check on k8s dashboard

cleanup all the executed/completed jobs for every 12 hours.
$ kc delete jobs name
initcontainer
-------------
sidecar
configmap -> we are injecting data to the pod using config object. But the data is static. 
static vs dynamic
static -> manual -> persistance volume
dynamic -> wihtout your intervention.
we want dynamic data or dependensive data.
https://kubernetes.io/docs/concepts/workloads/pods/init-containers/
Init containers are exactly like regular containers, except:
Init containers always run to completion.
Each init container must complete successfully before the next one starts.
If a Pod's init container fails, the kubelet repeatedly restarts that init container until it succeeds. However, if the Pod has a restartPolicy of Never, and an init container fails during startup of that Pod, Kubernetes treats the overall Pod as failed.

google "ansible awx docker compose"
https://github.com/geerlingguy/awx-container/blob/master/docker-compose.yml
$ cat initcontainer.yml
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.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]

- resource limit
  - cup/mem you can specify to container definition parameter...

baackground pods
- I don't want to access pod, but want them to be running like agent.
backup, log collector, garbage collector, metrics collectors
lets say we have log generated and need to forward to some other location. 
this kind of service, we call it daemonset in k8s.
logs - any issue, any transaction failed.
metrics -> resource utilization

initcontainer
- dynamically give data to your container ics init container while booting
 sidecar docker, 
A sidecar is a utility container in a pod that's loosely coupled to the main application container. 

Deploy jenkins as a pod?
What is disadvantage to run as a replica?
- replica can't work on this jenkins schenario because jenkins is going to use the filesystem.
- jenkins does not support active/active sesssion
lets take a database.
mysql pod -> deploy
as usual it usage pv, if you want to create a replica, how it is going to do?
if it was web appliation, its going to use multiple replicas.
if its a stateless application, we could use. even it shuffel between the nodes, we can still use. most frontend application, we can use it. it does not keep any state. you won't loose anything but in case of jenkins, you loose data. 
in case of data say mysql, if you want to create a multiple instance of mysql, you can't do it.
database if you want to use it, do "write once, use one."
you can have read replicas.
one primary and other are going to be readreplicas.
How can you create this kind of architecture. 
- you have to maintain the state. 
thats why, k8s came up with concept for database pod. 
they came up with idea stateful sets.
- how it is maintaining?
as usual, how are you going to create deployment?

read replicas going to be sync with primary pod automatically.
rds mysql cluster
kind: StatefulSet
https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/
https://bmc.com/blogs/kuberneste-postgresql

static IPaddress
application loadbalancer
create ipaddress and attach it.
type: loadbalancer (application load balancer)
service:
route: pathbased routing
url/admin
url/user
in k8s - assign certificate, dns
ingress object,
how to use it?
https://kubernetes.io/docs/concepts/services-networking/ingress/
package managers
----------------
file will be extrated and stored in a relevent location.
pip
gem
yum
apt
npm
rpm
dpkg
get file from repo, extract and distribute among different directories.
kubernetes
helm 
 packager manager
package
yml
 - dev
 - test
 - prod
helm packages
$ helm install
$ helm get -h
https://helm.sh/docs/intro/using_helm/
install helm on your client side
- install nginx
$ helm install nginx
look for syntax for directory structure.
------------------------
tomorrow
- ansible/terraform





No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...