-
Notifications
You must be signed in to change notification settings - Fork 29
Kubernetes Objects
- Lets deploy a pod using:
kubectl run nginx
This command will only deploy a Pod on your kubernetes worker node without any container inside it
- Now lets deploy a pod with a Nginx container
kubectl run nginx --image nginx
This command will only deploy a Pod on your kubernetes worker node with nginx container inside it
- Get running pod list using
kubectl get pod
- Get running pod details using:
kubectl describe pod <pod-name>
- Delete pod using
kubectl delete pod <pod-name>
- Now lets try to deploy pod with its container using YAML file
Create a pod-definition.yml file
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
name: myapp-pod
type: front-end
spec:
containers:
- name: nginx-container
image: nginxDeploy using following command
kubectl create -f pod-definition.yml
- Lets deploy some pod with replication controller using yml file
Create a rs-definition.yml file
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp-replicaset
labels:
app: myapp
type: front-end
spec:
template:
metadata:
name: myapp-pod
labels:
app: myapp
type: front-end
spec:
containers:
- name: nginx-controller
image: nginx
replicas: 3
selector:
matchLabels:
type: front-endDeploy using following command
kubectl create -f rs-definition.yml
- Get list of replication controller
kubectl get replicationset
- Get list of Pods
kuberctl get pods
- You can increase the number of pods by updating the yml definition file then running:
kubectl replace -f rs-definition.yml
- You can also increase the number of pods by using scale command:
kubectl scale -replicas=6 -f rs-definition.yml
- Delete replication set using:
kubectl delete replication myapp-rs
A Deployment provides declarative updates for Pods and ReplicaSets.
You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets, or to remove existing Deployments and adopt all their resources with new Deployments.
Create a deployment-definition.yml file
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container-pod
image: nginx- Lets deploy some pod with deployment using yml file
kubectl create -f deployment-definition.yml
- Get list of replication controller
kubectl get deployments
- Get list of Pods
kuberctl get pods
In Kubernetes, there are three general approaches to exposing your application.
- Using a Kubernetes service of type NodePort, which exposes the application on a port across each of your nodes
- Use a Kubernetes service of type LoadBalancer, which creates an external load balancer that points to a Kubernetes service in your cluster
- Use a Kubernetes Ingress Resource
Read more on Medium
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: k8s.gcr.io/test-webserver
name: test-container
volumeMounts:
- mountPath: /test-pd
name: test-volume
volumes:
- name: test-volume
hostPath:
# directory location on host
path: /data
# this field is optional
type: Directory
- Get pod details of other namespace using:
kubectl get pods --namespace=dev
- By below command you will get pod details of default namespace
kubectl get pods
- You can change default namespace by using:
kubectl config set-context ${kubectl config current-context} --namespace=dev
- Now for getting pod details of default you need to use
kubectl get pods --namespace=default
- Get all pods details across namespaces using:
kubectl get pods --all-namespaces
- Creating a namespace using
ymldefinition:
First, Create a namespace-dev.yml file
apiVersion: v1
Kind: Namespace
metadata:
name: devNow, run the following command:
kubectl create -f namespace-dev.yml
- Mentioning namespace in pod:
kubectl create -f pod-definition.yml --namespace=dev
- Get list of system pods using:
kubectl get pods --namespace=kube-system
- You can set resource limits on namespaces, here an example yml for same:
apiVersion: 1
kind: ResourceQuota
metadata:
namespace: compute-quota
namespace: dev
spec:
hard:
pods: "10"
requests.cpu: "4"
requests.memory: 5Gi
limits.cpu: "10"
limits.memory: 10Gikubectl create -f compute-quota.yml