1、Create a new pod with the nginx image.
kubectl run nginx --image nginx
2、查看pod详细信息
kubectl describe pod newpods-cnf9h
3、查看pod的node信息
kubectl describe pod newpods-cnf9h
kubectl get pods -o wide
4、What does the READY column in the output of the kubectl get pods command indicate?
5、Delete the webapp Pod.
kubectl delete pod webapp
6、Create a new pod with the name redis and with the image redis123.
Use a pod-definition YAML file. And yes the image name is wrong!
kubectl run redis --image=redis123 --dry-run=client -o yaml > redis-definition.yaml
自动生成yaml文件
controlplane ~ ➜ ls
redis-definition.yaml
controlplane ~ ➜ cat redis-definition.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: redis
name: redis
spec:
containers:
- image: redis123
name: redis
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
运行yaml文件
kubectl apply -f redis-definition.yaml
查看是否创建
kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 45m
newpods-mrnw7 1/1 Running 2 (6m55s ago) 40m
newpods-cnf9h 1/1 Running 2 (6m55s ago) 40m
newpods-m42gx 1/1 Running 2 (6m55s ago) 40m
redis 0/1 ErrImagePull 0 13s
注意:
yaml文件自动生成,格式:
kubectl run 自定义pod名称 --image=自定义镜像名称 --dry-run=client(这里还可以定义为server但是推荐client) -o yaml > 自定义名称.yaml
7、Now change the image on this pod to redis.
kubectl edit pod redis
8、Create a new Deployment with the below attributes using your own deployment definition file.
Name: httpd-frontend;
Replicas: 3;
Image: httpd:2.4-alpine
vim httpd.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd-frontend
spec:
replicas: 3
selector:
matchLabels:
name: httpd-frontend
template:
metadata:
labels:
name: httpd-frontend
spec:
containers:
- name: httpd-frontend
image: httpd:2.4-alpine
controlplane ~ ✖ kubectl create -f httpd.yaml
deployment.apps/httpd-frontend created
controlplane ~ ➜ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
frontend-deployment 0/4 4 0 23m
deployment-1 0/2 2 0 14m
httpd-frontend 3/3 3 3 2m33s
9、What is the targetPort configured on the kubernetes service?
10、Create a new service to access the web application using the service-definition-1.yaml file
tip:
Name: webapp-service
Type: NodePort
targetPort: 8080
port: 8080
nodePort: 30080
selector:
name: simple-webapp
controlplane ~ ➜ vim service-definition-1.yaml
---
apiVersion: v1
kind: Service
metadata:
name: webapp-service
spec:
type: NodePort
ports:
- targetPort: 8080
port: 8080
nodePort: 30080
selector:
name: simple-webapp
controlplane ~ ➜ kubectl create -f service-definition-1.yaml
service/webapp-service created
controlplane ~ ➜ kubectl get service # kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 23m
webapp-service NodePort 10.43.52.247 <none> 8080:30080/TCP 68s
11、How many pods exist in the research namespace?
12、Create a POD in the finance namespace.
Use the spec given below.
Name: redis
Image Name: redis
13、Which namespace has the blue pod in it?
controlplane ~ ➜ kubectl get pod --all-namespaces | grep blue
marketing blue 1/1 Running 0 3m40s
14、查看namespace
kubectl get namespace # kubectl get ns
15、Deploy a pod named nginx-pod using the nginx:alpine image.
kubectl run nginx-pod --image=nginx:alpine
16、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.
or
kubectl run redis --image=redis:alpine --dry-run=client -oyaml > redis-pod.yaml
Add given labels tier=db under the metadata section.
---
apiVersion: v1
kind: Pod
metadata:
labels:
tier: db
name: redis
spec:
containers:
- image: redis:alpine
name: redis
dnsPolicy: ClusterFirst
restartPolicy: Always
Then run the command: kubectl create -f redis-pod.yaml to create the pod from the definition file.
16、Create a service redis-service to expose the redis application within the cluster on port 6379.
Use imperative commands.
controlplane ~ ✖ kubectl expose pod redis --port=6379 --type=ClusterIP --name=redis-service
service/redis-service exposed
controlplane ~ ➜ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 58m
redis-service ClusterIP 10.43.84.58 <none> 6379/TCP 27m
17、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.
controlplane ~ ➜ kubectl create deployment webapp --image=kodekloud/webapp-color --replicas=3
deployment.apps/webapp created
kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
webapp 3/3 3 3 42s
18、Create a new pod called custom-nginx using the nginx image and expose it on container port 8080.
19、Create a new namespace called dev-ns.
Use imperative commands.
controlplane ~ ➜ kubectl create namespace dev-ns
namespace/dev-ns created
20、Create a new deployment called redis-deploy in the dev-ns namespace with the redis image. It should have 2 replicas.
Use imperative commands.
controlplane ~ ➜ kubectl create deployment redis-deploy --image=redis --namespace=dev-ns --replicas=2
deployment.apps/redis-deploy created
controlplane ~ ➜ kubectl get deployment -n dev-ns
NAME READY UP-TO-DATE AVAILABLE AGE
redis-deploy 2/2 2 2 30s
21、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.
kubectl run httpd --image=httpd:alpine --port=80 --expose
service/httpd created
pod/httpd created