练习题

重要知识点练习笔记

练习一

Set confifiguration context $kubectl confifig use-context k8s.
Monitor the logs of Pod foobar and Extract log lines corresponding to error unable-to-access-website . Write them to /opt/KULM00201/foobar.

kubectl config use-context k8s
mkdir /opt/KULM00201/
kubectl logs foobar | grep 'unable-to-access-website' > /opt/KULM00201/foobar

练习二

Set configuration context $kubectl config use-context k8s. List all PVs sorted by name, saving the full
kubectl output to /opt/KUCC0010/my_volumes. Use kubectl own functionally for sorting the output, and
do not manipulate it any further.

mkdir /opt/KUCC0010
kubectl get pv --all-namespaces --sort-by={.metadata.name} > /opt/KUCC0010/my_volumes

练习三

Set configuration context $kubectl config use-context k8s. Ensure a single instance of Pod nginx is
running on each node of the Kubernetes cluster where nginx also represents the image name which has
to be used. Do no override any taints currently in place. Use Daemonset to complete this task and use
ds.kusc00201 as Daemonset name.

#从官网拿一个Daemonset例子,修改yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: ds.kusc00201
  namespace: kube-system
  labels:
    k8s-app: nginx
spec:
  selector:
    matchLabels:
      name: nginx
    metadata:
      labels:
        name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
#创建        
kubectl create -f ds.yaml    
kubectl get ds 
kubectl get pods -o wide

练习四

Set configuration context $kubectl config use-context k8s Perform the following tasks:

Add an init container to lumpy-koala(which has been defined in spec file /opt/kucc00100/pod-specKUCC00100.yaml).

The init container should create an empty file named /workdir/calm.txt. If /workdir/calm.txt is not detected, the Pod should exit. Once the spec file has been updated with the init container definition, the Pod should be created.

https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/

#init container 已经定义在/opt/kucc00100/pod-specKUCC00100.yaml
apiVersion: v1
kind: Pod
metadata:
  name: lumpy-koala
spec:
  containers:
  - name: checker
    image: nginx
    volumeMounts:
    - mountPath: /workdir
      name: workdir
    livenessProbe:
      exec:
        command:
        - cat
        - /workdir/calm.txt
  initContainers:
  - name: install
    image: busybox
    command: 
    - touch
    - /workdir/calm.txt
    volumeMounts:
    - mountPath: /workdir
      name: workdir
  volumes:
  - name: workdir
    emptyDir: {}
#    
vi /opt/kucc00100/pod-spec-KUCC00100.yaml
kubectl create -f /opt/kucc00100/pod-spec-KUCC00100.yaml
kubectl exec lumpy-koala ls workdir

练习五

Set configuration context $kubectl config use-context k8s. Create a pod named kucc4 with a single
container for each of the following images running inside(there may be between 1 and 4 images
specified):nginx +redis+Memcached+consul.

kubectl run  kucc4 --image=nginx --dry-run -o yaml > 4pod.yaml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: kucc4
  name: kucc4
spec:
  containers:
  - image: nginx
    name: nginx
  - image: redis
    name: redis
  - image: Memcached
    name: Memcached
  - image: consul
    name: consul
kubectl create -f 4pod.yaml

练习六

Set configuration context $kubectl config use-context k8s Schedule a Pod as follows: Name: nginxkusc00101 Image: nginx Node selector: disk=ssd

kubectl get nodes --show-labels
#
apiVersion: v1
kind: Pod
metadata:
  name: nginxkusc00101
  labels:
    env: test
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  nodeSelector:
    disk: ssd
#    
kubectl create -f nginx-kusc00101.yaml

练习七

Set configuration context $kubectl config use-context k8s. Create a deployment as follows: Name: nginx-app Using container nginx with version 1.11.9-alpine. The deployment should contain 3 replicas. Next,
deploy the app with new version 1.12.0-alpine by performing a rolling update and record that update.
Finally,rollback that update to the previous version 1.11.9-alpine.

#官方文档获取nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-app
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.11.9-alpine
        ports:
        - containerPort: 80
#
kubectl create -f nginx-deploy.yaml
#更新和record
kubectl set image deployment/nginx-deployment nginx=nginx:1.12.0-alpine --record
#查看历史版本
kubectl rollout history deployment.v1.apps/nginx-deployment
#撤消当前上线并回滚到以前的修订版本
kubectl rollout undo deployment.v1.apps/nginx-deployment --record

练习八

Set configuration context $kubectl config use-context k8s Create and configure the service front-end-service so it’s accessible through NodePort/ClusterIp and routes to the existing pod named front-end.

#kubectl create -f front-end-service.yaml
kubectl expose pod front-end --name=front-end-service --type="NodePort" --port=80

练习九

Set configuration context $kubectl config use-context k8s Create a Pod as follows: Name: jenkins Using
image: jenkins In a new Kubernetes namespace named website-frontend

kubectl run jenikns --image=jenkins -o yaml -n website-frontend --dry-run=client >nginx-666.yaml
kubectl create -f nginx-666.yaml

练习十

Set configuration context $kubectl config use-context k8s

Create a deployment spec file that will: Launch 7 replicas of the redis image with the label : app_enb_stage=dev Deployment name: kual00201 Save a copy of this spec file to /opt/KUAL00201/deploy_spec.yaml (or .json) When you are done,clean up(delete) any new k8s API objects that you produced during this task.

mkdir /opt/KUAL00201
vi deploy_spec.yaml
#从官网获取基础yaml文件进行修改deploy_spec.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: kual00201
  labels:
    app_enb_stage: dev 
spec:
  replicas: 7
  selector:
    matchLabels:
      app_enb_stage: dev 
  template:
    metadata:
      labels:
        app_enb_stage: dev 
    spec:
      containers:
      - name: kual00201
        image: redis
#create
kubectl create -f /opt/KUAL00201/deploy_spec.yaml
#delete        
kubectl delete -f /opt/KUAL00201/deploy_spec.yaml

练习十一

Set configuration context $kubectl config use-context k8s

Create a file /opt/KUCC00302/kucc00302.txt that lists all pods that implement Service foo in Namespace production. The format of the file should be one pod name per line.

kubectl get svc --show-labels -n production
kubectl get pods -l app=foo -n production
kubectl get pods -l app=foo -n production | grep -v NAME | awk '{ print $1 }' >> /opt/KUCC00302/kucc00302.txt

练习十二

Set confifiguration context $kubectl confifig use-context wk8s

A Kubernetes worker node,labelled with name=wk8s-node-0 is in state NotReady. Investigate why this is the case, and perform any appropriate steps to bring the node to a Ready state, Ensuring that any changes are made permanent. Hints: You can ssh to the failed node using $ssh wk8s-node-0. You can assume elevated privileges on the node with the following command sudo -i.

kubectl config use-context wk8s # 切换集群环境 
kubectl get nodes ssh wk8s-node-0 # 登录NotReady节点
systemctl status kubelet # 发现没有启动

systemctl daemon-reload && systemctl restart kubelet
systemctl enable kubelet # 重启配置保存-非常重要

练习十三

Set configuration context $kubectl config use-context k8s

Create a pod as follows: Name: non-persistent-redis Container image: redis Named-volume with name: cache-control Mount path :/data/redis It should launch in the pre-prod namespace and the volume MUST NOT be persistent.

kubectl create ns pre-prod
vi non-persistent-redis.yaml
apiVersion: v1
kind: Pod
metadata:
  name: non-persistent-redis
  namespace: pre-prod
spec:
  containers:
  - image: redis
    name: non-persistent-redis
    volumeMounts:
    - mountPath: /data/redis
      name: cache-control
  volumes:
  - name: cache-control
    emptyDir: {}
kubectl create -f non-persistent-redis.yaml -n pre-prod
kubectl get pods -n pre-pod

练习十四

Set configuration context $kubectl config use-context k8s

Scale the deployment webserver to 6 pods

kubectl scale deployment.v1.apps/webserver --replicas=6

练习十五

Set configuration context $kubectl config use-context k8s

Check to see how many nodes are ready (not including nodes tainted NoSchedule) and write the number to /opt/nodenum.

kubectl get nodes | grep -i ready | wc -l #4,-i为不区分大小写 
kubectl describe nodes | grep Taints | grep -i noschedule | wc -l #1 
vi /opt/nodenum # 写入4-1=3

练习十六

Set configuration context $kubectl config use-context k8s

From the Pod label name=cpu-utilizer, find pods running high CPU workloads and write the name of the Pod consuming most CPU to the file /opt/cpu.txt (which already exists).

kubectl get top pod -l name=cpu-utilizer | grep -v NAME | awk '{print $2}' >> /opt/cpu.txt 

练习十七

Set configuration context $kubectl config use-context ek8s

Set the node labelled with name=ek8s-node-1 as unavailable and reschedule all the pods running on it.

#根据标签查询node
kubectl get node --show-labels | grep name=ek8s-node-1 
#标记一个节点为不可调度,如果标记节点为不可调度(unschedulable),将阻止新 Pod 调度到该节点之上,但不会 影响任何已经在其上的 Pod。 这是重启节点或者执行其他维护操作之前的一个有用的准备步骤
kubectl cordon node1
#从节点安全地逐出所有 Pods。 安全的驱逐过程允许 Pod 的容器 体面地终止, 并确保满足指定的 PodDisruptionBudgets
kubectl drain node1 --delete-local-data --force --ignore-daemonsets

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐