Monday, December 14, 2020

Kubernetes - creating Kubernetes objects imperatively

 Practice LAB

Creating Kubernetes objects imperatively.


Kubernetes objects 
Imperative LAB tasks
Here, is a POD definition file.
# cat pod-definition-file.yaml
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
    type: front-end-service
  sepc: 
    containers:
    - name: nginx-container
      image: nginx:1.19
      ports:
        - containerPort: 8080
    nodeName: 
---------------------------------------------------------------

In this lab, you will get hands-on practice with creating Kubernetes objects imperatively.
All the questions in this lab can be done imperatively. However, for some questions, you may need to first create the YAML file using imperative methods. You can then modify the YAML according to the need and create the object using kubectl apply -f command.


1. Deploy a pod named nginx-pod using the nginx:alpine image.
Use imperative commands only.
    Name: nginx-pod
    Image: nginx:alpine 
-> Use the command kubectl run nginx-pod --image=nginx:alpine
# kc run nginx-pod --image=nginx:alpine
# kc get pod


2. Deploy a redis pod using the redis:alpine image with the labels set to tier=db.
-> Either use imperative commands to create the pod with the labels. Or else use imperative commands to generate the pod definition file, then add the labels before creating the pod using the file.
    Pod Name: redis
    Image: redis:alpine
    Labels: tier=db 
-> Use the command kubectl run redis --image=redis:alpine -l tier=db
# kc run redis --image=redis:alpine -l tier=db
3. Create a service redis-service to expose the redis application within the cluster on port 6379.
Use imperative commands
    Service: redis-service
    Port: 6379
    Type: ClusterIp
    Selector: tier=db 
-> Run the command kubectl expose pod redis --port=6379 --name redis-service
# kc expose pod redis --port=6379 --name redis-service
4. Create a deployment named webapp using the image kodekloud/webapp-color with 3 replicas
Try to use imperative commands only. Do not create definition files.
    Name: webapp
    Image: kodekloud/webapp-color
    Replicas: 3 
-> Use the command kubectl create deployment webapp --image=kodekloud/webapp-color. To scale the webapp to 3 using command kubectl scale deployment/webapp --replicas=3
# kc create deployment webapp --image=kodekloud/webapp-color --replicas=3
5. Create a new pod called custom-nginx using the nginx image and expose it on container port 8080
-> Run : kubectl run custom-nginx --image=nginx --port=8080
    Pod created correctly? 
# kc run custom-nginx --image-nginx --port=8080
# kc get pod
6. Create a new namespace called dev-ns.
Use imperative commands.
    Namespace created? 
-> 
# kc create ns dev-ns
# kc get ns
7. Create a new deployment called redis-deploy in the dev-ns namespace with the redis image. It should have 2 replicas.
Use imperative commands.
    redis-deploy created in the dev-ns namespace?
    replicas: 2 
-> 
# kc create deployment redis-deploy --namespace=dev-ns --image=redis --replicas=2
or 
Step 1: Create the deployment YAML file
# kubectl create deployment redis-deploy --image redis --namespace=dev-ns --dry-run=client -o yaml > deploy.yaml
Step 2: Edit the YAML file and add update the replicas to 2
Step 3: Run kubectl apply -f deploy.yaml to create the deployment in the dev-ns namespace.
You can also use kubectl scale deployment or kubectl edit deployment to change the number of replicas once the object has been created.


8. Create a pod called httpd using the image httpd:alpine in the default namespace. Next, create a service of type ClusterIP by the same name (httpd). The target port for the service should be 80.
Try to do this with as few steps as possible.
    `httpd` pod created with the correct image?
    'httpd' service is of type 'clusterIP'?
    'httpd' service uses correct target port 80?
    'httpd' service exposes the 'httpd' pod? 


-> First create a YAML file for both the pod and service like this:
kubectl run httpd --image=httpd:alpine --port=80 --expose




x

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. ...