01-Jul-2021 Kubernetes
01-Jul-2021 Kubernetes
---------
vim pod-definition2.yml
---
apiVersion: v1
kind: Pod
metadata:
name: postgres-pod
labels:
author: sunil
type: database
spec:
containers:
- name: mypostgres
image: postgres
env:
- name: POSTGRES_PASSWORD
value: durgasoft
- name: POSTGRES_USER
value: myuser
- name: POSTGRES_DB
value: mydb
:wq
To get the list of pods along with IP address and which node the pod is running
---------------------------
kubectl get pods -o wide
or
Ex3:
vim pod-definition3.yml
---
apiVersion: v1
kind: Pod
metadata:
name: jenkins-pod
labels:
author: sunil
ci: cd
spec:
containers:
- name: myjenkins
image: jenkins/jenkins
ports:
- containerPort: 8080
hostPort: 8080
:wq
gke-cluster-1-default-pool-9fb99245-q1nm
35.223.183.189:8080
34.68.242.87:8080
++++++++++++++++++++++++
Deployment Object
-------------------------
This is also an high level object which can be used for scalling, load balancing
and perform rolling updates.
vim deployment.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
author: sunil
type: proxyserver
spec:
replicas: 3
selector:
matchLabels:
type: proxyserver
template:
metadata:
name: nginx-pod
labels:
type: proxyserver
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
hostPort: 8888
:wq
We can anyways perform scaling, apart from that we can perform rolling updates.
nginx-deployment-6fdc797dc6-qrlqb
:q
( It will take some time )
++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++
Service Object
-----------------------
This is used for network load balancing and port mapping.
Service Object uses 3 ports
1. Target port - Its is pod or container port
2. port - Refers to service port.
3. hostPort - Refers to host machine port to make it accessable from external
network.
2. nodePort: This is used, if we want to access the pods from an external network
and it also performs network load balancing. ie Even if a pod is running on a
specific slave, we can access it from other slave in the cluster.
3. LoadBalancer: This is similar to nodePort. It is used for external
connectivity of a pod and also network load balancing and it also assigns a public
ip for all the nodes combined together.
+++++++++++++++++++++++
vim pod-definition1.yml
Ex: Create a service definition file for port mapping on nginx pod
vim pod-definition1.yml
---
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
author: sunil
type: reverse-proxy
spec:
containers:
- name: appserver
image: nginx
:wq
vim service1.yml
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
author: sunil
spec:
type: NodePort
ports:
- targetPort: 80
port: 80
nodePort: 30008
selector:
author: sunil
type: reverse-proxy
...
:wq
34.123.230.145:30008
If service object is not created, we used to identify in which node the pod is
running, take that node IP, from that node IP, we used to access that application.
++++++++++++++++++++++
Kubernetes Project
---------------
This is a python based application which is used for accepting a vote ( voting
app ).
This application accepts the vote and passes it to temporary db created using
redis. From redis, the data is passed to worker application created using dotnet.
Dotnet based application analyses the data and stores it in permanant database
created using postgres.
From postgres database, results can be seen on an application created using node
JS.
vim voting-app-pod.yml
---
apiVersion: v1
kind: Pod
metadata:
name: voting-app-pod
labels:
name: voting-app-pod
app: demo-voting-app
spec:
containers:
- name: voting-app
image: dockersamples/examplevotingapp_vote
ports:
- containerPort: 80
...
:wq
vim result-app-pod.yml
apiVersion: v1
kind: Pod
metadata:
name: result-app-pod
labels:
name: result-app-pod
app: demo-voting-app
spec:
containers:
- name: result-app
image: dockersamples/examplevotingapp_result
ports:
- containerPort: 80
...
:wq
vim worker-app-pod.yml
---
apiVersion: v1
kind: Pod
metadata:
name: worker-app-pod
labels:
name: worker-app-pod
app: demo-voting-app
spec:
containers:
- name: worker-app
image: dockersamples/examplevotingapp_worker
...
:wq
vim redis-pod.yml
---
apiVersion: v1
kind: Pod
metadata:
name: redis-pod
labels:
name: redis-pod
app: demo-voting-app
spec:
containers:
- name: redis
image: redis
ports:
- containerPort: 6379
...
:wq
vim postgres-pod.yml
---
apiVersion: v1
kind: Pod
metadata:
name: postgres-pod
labels:
name: postgres-pod
app: demo-voting-app
spec:
containers:
- name: postgres
image: postgres:9.4
ports:
- containerPort: 5432
...
vim redis-service.yml
---
apiVersion: v1
kind: Service
metadata:
name: redis-service
labels:
name: redis-service
app: demo-voting-app
spec:
ports:
- port: 6379
targetPort: 6379
selector:
name: redis-pod
app: demo-voting-app
:wq
Note: As we have not specified the type of service object, by default it creates
service object to type Cluster_IP
vim result-app-service.yml
apiVersion: v1
kind: Service
metadata:
name: result-service
labels:
name: result-service
app: demo-voting-app
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 80
selector:
name: result-app-pod
app: demo-voting-app
:wq
The above two service objects are of type load balancer. we can access it from the
external network.
Open gitbash from the location where all the definition files are saved.
$ git init
$ git add .
$ git commit -m "a"
upload the files from local repository to remote repository using the two commands
We should able to see the definition files in github repository ( Total 9 files )
$ cd kuber_project
$ ls ( we get the files )
++++++++++++++++++++++++++++++++++++++