kubernetes

Kubernetes for developer Create Pods

0
(0)

### Creating pods
– pod core concepts
– creating a pod
– kubectl and pods
– YAML fundamentals
– Define a pod using YAML
– pod health

### Pod
– Basic execution unit of Kubernetes application
– Smallest and simplest unit in Kubernetes object model
– One node has many pods.
– Each pod will have container(s)
– Organize application parts into pods (server, api, caching, database, etc)
– Prefer single process per container, single container per pod
– pod will have ip, memory, volumes . Containers within pod can share these.
– scale ho rinzantally by adding more pod replicas.
– pod live and die, don’t come back to life. Kubernetes kills sick pods and might create new pods.

Master Node 1-n> schedule pods -> Worker Nodes

Pods
Horizontally scale -> replicas
Kubernetes monitors sick pods, kills them, starts new pods.

One Node – N pods
Each pod – unique ip address
Container have unique ports.

Side Car Container
A container besides your container in the same pod.
Then use different ports.

Same pod can’t be on two nodes

### Creating a Pod
kubectl run podname --image=nginx:alpine
# display all resources
kubectl get all

Expose Pod ports
– pods and containers accesible within Kubernetes cluster by default
– Expose container port externally
kubectl port-forward nameofpod 8080:80
8080 – External port – what you are calling from browser
80 – Internal Port – Port on which container is running in pod.

### Delete a pod
kubectl delete pod nameofpod
Even after deleting, kubectl get all, you see the pod come to life
Notice carefully id of new pod and deleted pod are different.
Kubernetes has created a new pod for the pod you have deleted. So ID is changed. Because Kubernetes wants to have your deployment same.

kubenetes delete deployment nameofDeployment
(which has scheduled your pod).
then your new pod won’t be created.

### Pods Practical
kubectl get all
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d1h
kubectl run my-nginx --image=nginx:alpine

pod/my-nginx created

kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx 1/1 Running 0 34s

Above commands are quick and easy way. But in general we use more yamls

kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d1h

clsuterip is internal only. can’t use externally.

kubectl port-forward my-nginx 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
Handling connection for 8080

Now you can access it via localhost:8080
But your console is blocked.

kubectl delete pod my-nginx
pod "my-nginx" deleted
kubectl get pods
No resources found in default namespace.
kubectl run podname--image=imagename
kubectl get pods
kubectl port-forward podname 8080:80
kubectl delete pod pdoname

### YAML Fundamentals
declarative approach
yet another markup language
maps and lists like json, but not clumsy brackets
always use spaces for indentation
Maps – key value pairs, can contain other maps
Lists – sequence of items, maps

YAML Example
## key value pair
key: value

## complex maps
complexMap:
key1: value
key2:
subKey: value

### array like representation
items:
- item1
- item2
- item3
### list that define sequence of maps

itemMap:
- map1: value
- map1Prop: value
- map2: value
- map2Prop: value

### Note
Indentation matters
Use spaces not tabs (Be consistent at how many spaces you use)

### Define pods with YAML

YAML(pod) + kubctl = pod with container

Very simple YAML file for nginx

check https://kubernetes.io/docs for further documentation.

nginx.pod.yml (or yaml)

apiVersion: v1
kind: Pod
metadata:
 name: my-nginx
 labels:
  app: nginx
  rel: stable
spec:
 containers:
 - name: my-nginx
   image: nginx:alpine
   port:
   - containerPort: 80

apiVersion: v1 (this is Kubernetes API version)
kind: Pod (type of Kubernetes resource)
metadata: (metadata about the pod)
name: my-nginx
spec: (blueprint of pod)
containers:
– name: my-nginx
image: nginx:alpine

# performs a trial create and also validate YAML
kubectl create -f file.pod.yml --dry-run --validate=true
## create a pod from YAML, will error if Pod already exists
kubectl create -f file.pod.yml
## another way to create or apply changes to a pod from YAML.
## preferable, create and update resources using Pod.
kubectl apply -f file.pod.yml
### use --save-config when you want to use kubectl apply in future for that pod.
kubectl create -f file.pod.yml --save-config

So always use –save-config if you want to use create command.
It is better though just directly use apply. Much better.

kubectl edit (edit a pod right in console)
kubectl patch (patch a prticular property)
Deleting a pod
kubectl delete pod podname
Another way of deleting
kubectl delete -f file.pod.yml

Getting the POD YAML in kubectl cluster

labels
deployments and services can link to pods via labels.

kubectl create -f nginx.pod.yml --save-config
pod/my-nginx created
kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx 1/1 Running 0 117s
kubectl get pods my-nginx -o yaml
gives you a long output, look at annotations. --savconfig saved current config there.

See the events log for troubleshooting.

kubectl apply -f nginx.pod.yml
pod/my-nginx unchanged

When you have some changes in yaml file, then you will see below message as configured.

kubectl apply -f nginx.pod.yml
pod/my-nginx configured

apply command creates resource if it is not there, and if it is there, then it’s applies the changes.

Let’s try to get into container of that pod.

>kubectl exec my-nginx -it sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ # ls
bin etc mnt run tmp
dev home opt sbin usr
docker-entrypoint.d lib proc srv var
docker-entrypoint.sh media root sys
/ # cd usr
/usr # ls
bin lib local sbin share
/usr # cd share/
/usr/share # ls
GeoIP ca-certificates licenses misc udhcpc
apk doc man nginx zoneinfo
/usr/share # cd nginx/
/usr/share/nginx # ls
html
/usr/share/nginx # cd html/
/usr/share/nginx/html # ls
50x.html index.html
/usr/share/nginx/html # exit
kubectl edit -f nginx.pod.yml
pod/my-nginx edited
You can edit the pod this way.
kubectl delete -f nginx.pod.yml
pod "my-nginx" deleted
Since the pod is not running, it is deleted
kubectl get all
You just see only the service running.

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d22h

So in a nutshell useful pod commands

kubectl create -f nginx.pod.yml --save-config
kubectl describe pod podname
kubectl apply -f nginx.pod.yml
kubectl exec podname -it sh
kubectl edit -f nginx.pod.yml
kubectl delete -f nginx.pod.yml

### Pod Health
probes determine health
it is a diagnostic report
2 types of probes
liveness probe – pod is healthy and running as expected
readiness probe – can be used to find if pod should receive requests.

Failed Pod Containers – recreated by default (restart policy defaults to always)

### Probe Types
ExecAction – executes an action inside a container

TCPSocketAction – TCP check against the containers’ IP address on a specified port.

HTTPGetAction – HTTP GET request against container

Probes can have only following results
– success
– failure
– unknown

Define an HTTP Liveness Probe (When should a container restart)

livenessProbe: (Define a liveness probe)
httpGet:
path: /index.html (Check index.html at port 80)
port: 80
initialDelaySeconds: 15 (Wait for 15 seconds)
timeoutSeconds: 2
periodSeconds: 5
failureThreshold: 1

Defines a readiness probe (Determine when should a container start receiving traffic)
Because if we start sending traffic too soon, it could be an issue
readinessProbe: (Define readiness probe)
httpGet: (Check /index.html on port 80)
path: /index.html
port: 80
initialDelaySeconds: 2 (Wait for 2 seconds)
periodSeconds: 5 (Check every 5 seconds till this pod is alive)

Example of liveness probe and readiness probe defined in YAML.

apiVersion: v1
kind: Pod
metadata:
 name: my-nginx
 labels:
  app: nginxapp
  rel: stable
spec:
 containers:
 - name: my-nginx
   image: nginx:alpine
   livenessProbe:
    httpGet: 
     path: /index.html
     port: 80
     initialDelaySeconds: 15
     timeoutSeconds: 2
     periodSeconds: 5
     failureThreshold: 1
   readinessProbe:
    httpGet: 
     path: /index.html
     port: 80
     initialDelaySeconds: 2
     periodSeconds: 5
   ports:
   - containerPort: 80

 

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Tagged ,
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments