[CKA] Examtopics CKA 덤프 topic1 풀이 (#1 - #10)

    728x90
    반응형

    Question #1

    SIMULATION

    #Set configuration context: 
    [student@node-1]$ kubectl config use-context k8s

     

    Context -
    You have been asked to create a new ClusterRole for a deployment pipeline and bind it to a specific ServiceAccount scoped to a specific namespace.

    Task -
    Create a new ClusterRole named deployment-clusterrole, which only allows to create the following resource types:
    ✑ Deployment
    ✑ Stateful Set
    ✑ DaemonSet
    Create a new ServiceAccount named cicd-token in the existing namespace app-team1.
    Bind the new ClusterRole deployment-clusterrole to the new ServiceAccount cicd-token, limited to the namespace app-team1.

     

    풀이

    #Create a new ClusterRole named deployment-clusterrole, which only allows to create the following resource types:
    #✑ Deployment
    #✑ Stateful Set
    #✑ DaemonSet
    
    #Clusterrole 생성 커맨드
    [root@]$ kubectl create clusterrole deployment-clusterrole --verb=create --resource=Deployments,DaemonSets,StatefulSets
    
    [옵션]
    --verb=create:
    권한을 정의한다.
    create는 새 리소스를 생성하는 작업을 허용한다는 의미
    
    --resource=Deployments,DaemonSets,StatefulSets:
    ClusterRole이 어떤 리소스에 대해 권한을 부여하는지를 정의한다.

     

    #Create a new ServiceAccount named cicd-token in the existing namespace app-team1
    
    #ServiceAccount 생성 커맨드
    [root@]$ kubectl create serviceaccount cicd-token -n app-team1
    
    [옵션]
    -n app-team1:
    생성될 리소스가 속할 네임스페이스를 지정한다.

     

    #Bind the new ClusterRole deployment-clusterrole to the new ServiceAccount cicd-token, limited to the namespace app-team1
    
    #주석
    [root@]$ kubectl create rolebinding cr-sa-bind --clusterrole=deployement-clusterrole --serviceaccount=app-team1:cicd-token -n app-team1
    
    [옵션]
    --clusterrole=deployment-clusterrole:
    바인딩할 ClusterRole을 지정한다.
    
    --serviceaccount=app-team1:cicd-token:
    ClusterRole을 바인딩할 ServiceAccount를 지정한다.
    
    -n app-team1:
    app-team1 네임스페이스에 RoleBinding을 생성하여, 바인딩이 이 네임스페이스에 제한되도록 한다.

    Question #2

    SIMULATION

    #Set configuration context
    [root@]$ kubectl config use-context ek8s

     

    Set the node named ek8s-node-0 as unavailable and reschedule all the pods running on it.

     

    풀이

    #Set the node named ek8s-node-0 as unavailable and reschedule all the pods running on it.
    
    #노드를 더 이상 파드가 스케줄되지 않도록 설정한다.
    [root@]$ kubectl cordon ek8s-node-0
    
    #해당 노드에서 실행 중인 파드들을 종료하고 다른 노드에 재배치한다.
    [root@]$ kubectl drain ek8s-node-0 --ignore-daemonsets --delete-local-data
    
    [옵션]
    kubectl cordon: 
    노드에 파드가 더 이상 스케줄되지 않도록 설정
    
    kubectl drain: 
    해당 노드에서 실행 중인 파드들을 종료하고 다른 노드에 재배치
    --ignore-daemonsets: 
    Daemonsets 파드는 제외
    
    --delete-local-data: 
    로컬 데이터를 삭제

    Question #3

    SIMULATION

    #Set configuration context
    [root@]$ kubectl config use-context mk8s

     

    Task -
    Given an existing Kubernetes cluster running version 1.22.1, upgrade all of the Kubernetes control plane and node components on the master node only to version 1.22.2.
    Be sure to drain the master node before upgrading it and uncordon it after the upgrade.

    You can ssh to the master node using:
    [root@]$ | ssh mk8s-master-0

    You can assume elevated privileges on the master node with the following command: 
    [root@]$ | sudo -i

    You are also expected to upgrade kubelet and kubectl on the master node.
    Do not upgrade the worker nodes, etcd, the container manager, the CNI plugin, the DNS service or any other addons.

     

    풀이

    #Be sure to drain the master node before upgrading it and uncordon it after the upgrade.
    #마스터 노드를 drain하여 해당 노드에서 실행 중인 파드를 다른 노드로 이동시킨다.
    
    #마스터 노드를 cordon하여 더 이상 새 파드가 해당 노드에 스케줄되지 않도록 만든다.
    [root@]$ kubectl cordon mk8s-master-0
    
    #마스터 노드에서 실행 중인 모든 파드를 강제로 종료하고 다른 노드로 재배치한다.
    [root@]$ kubectl drain mk8s-master-0 --ignore-daemonsets --force

     

    #upgrade all of the Kubernetes control plane and node components on the master node only to version 1.22.2.
    #마스터 노드의 Kubernetes 컨트롤 플레인 및 노드 구성 요소를 버전 1.22.2로 업그레이드한다.
    
    #SSH를 통해 마스터 노드에 접속한다.
    [root@]$ ssh mk8s-master0
    
    #마스터 노드에 접속한 후 관리자 권한을 얻는다.
    [root@]$ sudo -i
    
    #마스터 노드에서 kubeadm, kubelet, kubectl을 버전 1.22.2로 설치한다.
    [root@]$ apt install kubeadm=1.22.2-00 kubelet=1.22.2-00 kubectl=1.22.2-00
    
    #현재 클러스터의 업그레이드 가능한 버전과 필요한 변경 사항을 미리 확인할 수 있다.
    [root@]$ kubeadm upgrade plan
    
    #kubeadm을 사용하여 Kubernetes 버전 1.22.2로 업그레이드를 적용한다.
    #etcd를 업그레이드하지 않도록 설정하는 옵션을 적용한다.
    [root@]$ kubeadm upgrade apply 1.22.2 --etcd-upgrade=false

     

    #Be sure to drain the master node before upgrading it and uncordon it after the upgrade.
    #업그레이드가 완료된 후에는 마스터 노드를 다시 uncordon하여, 해당 노드가 다시 파드를 스케줄 받을 수 있도록 설정한다.
    
    #kubelet 서비스를 재시작하여 업그레이드한 버전을 반영한다.
    [root@]$ systemctl restart kubelet daemon-reload
    
    #마스터 노드를 다시 uncordon하여 파드를 스케줄받을 수 있도록 설정한다.
    [root@]$ kubectl uncordon mk8s-master-0
    
    #마스터 노드의 상태가 READY인지 확인한다.
    [root@]$ kubectl get nodes

    Question #4

    SIMULATION

    Task -
    First, create a snapshot of the existing etcd instance running at https://127.0.0.1:2379, saving the snapshot to /var/lib/backup/etcd-snapshot.db.

    The following TLS certificates/key are supplied for connecting to the server with etcdctl: 
    ▪ CA certificate: /opt/KUIN00601/ca.crt
    ▪ Client certificate: /opt/KUIN00601/etcd-client.crt
    ▪ Client key: /opt/KUIN00601/etcd-client.key

    Creating a snapshot of the given instance is expected to complete in seconds.
    If the operation seems to hang, something's likely wrong with your command. Use Ctrl+c to cancel the operation and try again.

    Next, restore an existing, previous snapshot located at /var/lib/backup/etcd-snapshot-previous.db.

     

    풀이

    #create a snapshot of the existing etcd instance running at https://127.0.0.1:2379, saving the snapshot to /var/lib/backup/etcd-snapshot.db.
    #https://127.0.0.1:2379에서 실행 중인 etcd 인스턴스의 스냅샷을 생성해 /var/lib/backup/etcd-snapshot.db 위치에 저장한다.
    
    #ETCDCTL_API 환경변수를 지정하지 않으면 v2가 기본으로 지정된다.
    [root@]$ ETCDCTL_API=3
    
    #주석
    [root@]$ etcdctl --endpoints=http://127.0.0.1:2379 
    --cert-file=/opt/KUIN00601/etcd-client.crt 
    --key-file=/opt/KUIN00601/etcd-client.key 
    --ca-file=/opt/KUIN00601/ca.crt snapshot save /var/lib/backup/etcd-snapshot.db
    
    [옵션]
    --endpoints: 
    etcd클러스터의 endpoint를 지정한다.
    
    --cert-file: 
    etcd 클러스터에 보안 연결을 위해 사용할 클라이언트 인증서를 지정한다.
    
    --key-file: 
    etcd 서버와 연결 시 사용할 클라이언트 개인 키 파일을 지정한다.
    
    --ca-file:
    클러스터의 인증 기관(CA) 인증서를 지정한다.

     

    #restore an existing, previous snapshot located at /var/lib/backup/etcd-snapshot-previous.db.
    #이전에 생성한 스냅샷 파일(/var/lib/backup/etcd-snapshot-previous.db)을 사용하여 etcd 인스턴스를 복원한다.
    
    #ETCDCTL_API 환경변수를 지정하지 않으면 v2가 기본으로 지정된다.
    [root@]$ ETCDCTL_API=3
    
    #주석
    [root@]$ etcdctl --endpoints=http://127.0.0.1:2379 
    --cert-file=/opt/KUIN00601/etcd-client.crt 
    --key-file=/opt/KUIN00601/etcd-client.key 
    --ca-file=/opt/KUIN00601/ca.crt snapshot restore /var/lib/backup/etcd-snapshot-previous.db.

    Question #5

    SIMULATION

    #Set configutaion context
    [root@]$ kubectl config use-context hk8s

     

    Task -
    Create a new NetworkPolicy named allow-port-from-namespace in the existing namespace fubar.
    Ensure that the new NetworkPolicy allows Pods in namespace internal to connect to port 9000 of Pods in namespace fubar.
    Further ensure that the new NetworkPolicy:
    ✑ does not allow access to Pods, which don't listen on port 9000
    ✑ does not allow access from Pods, which are not in namespace internal

     

    풀이

    #주석[root@]$ CMD

    Question #6

    SIMULATION

    #Set configuration context
    [root@]$ kubectl config use-context k8s

     

    Task -
    Reconfigure the existing deployment front-end and add a port specification named http exposing port 80/tcp of the existing container nginx.
    Create a new service named front-end-svc exposing the container port http.
    Configure the new service to also expose the individual Pods via a NodePort on the nodes on which they are scheduled.

     

    풀이

    #Reconfigure the existing deployment front-end and add a port specification named http exposing port 80/tcp of the existing container nginx.
    #front-end deployment를 수정해 nginx 컨테이너에 포트 80/tcp를 노출하는 설정을 추가한다.
    
    #주석
    [root@]$ kubectl edit deployment front-end
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    ...
        spec:
          containers:
          - image: nginx:latest
            imagePullPolicy: Always
            name: nginx
            ports:
            - containerPort: xxxxx
              protocol: TCP
            // 추가 //
            - name: http
              containerPort: 80
              protocol: TCP

     

    #Configure the new service to also expose the individual Pods via a NodePort on the nodes on which they are scheduled.
    #front-end-svc라는 이름으로 새 서비스를 생성하고, NodePort를 사용하여 노드에서 개별 Pod에 접근할 수 있도록 설정한다.
    
    #
    [root@]$ kubectl expose deployment front-end --name=front-end-svc --target-port=http --type=NodePort
    
    [옵션]
    kubectl expose:
    Service를 생성할 때 사용된다.
    Deployment, Pod, ReplicaSet 등 다양한 리소스를 외부에 접근할 수 있도록 서비스로 노출한다.
    
    deployment front-end:
    front-end라는 Deployment에 기반하여 새로운 Service를 생성한다.
    
    --target-port=http:
    Service가 연결할 대상 포트
    http는 nginx 컨테이너에서 열려 있는 포트 이름을 의미한다.
    
    --type=NodePort:
    서비스 타입 중 하나

    Question #7

    SIMULATION

    #Set configuration context
    [root@]$ kubectl config use-context k8s

     

    Task -
    Scale the deployment presentation to 3 pods.

     

    풀이

    #Scale the deployment presentation to 3 pods.
    #presentation deployment의 replicas를 3개로 스케일 업 한다.
    
    [root@]$ kubectl scale --replicas=3 deployment/presentation

    Question #8

    SIMULATION

    #Set configuration context
    [root@]$ kubectl config use-context k8s

     

    Task -
    Schedule a pod as follows:
    ✑ Name: nginx-kusc00401
    ✑ Image: nginx
    ✑ Node selector: disk=ssd

     

    풀이

    #yaml파일 생성
    [root@]$ kubectl run nginx-kusc00401 --image=nginx --dry-run=client -oyaml >> abc.yaml
    
    #nodeSelector 추가
    [root@]$ vi abc.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      creationTimestamp: null
      labels:
        run: nginx-kusc00401
      name: nginx-kusc00401
    spec:
      containers:
      - image: nginx
        name: nginx-kusc00401
        resources: {}
      // 추가 //
      nodeSelector: 
        disk: ssd
      // 추가 //
      dnsPolicy: ClusterFirst
      restartPolicy: Always
    status: {}
    
    #파드 실행
    [root@]$ kubectl apply -f abc.yaml

    Question #9

    SIMULATION

    #Set configuration context
    [root@]$ kubectl config use-context k8s

     

    Task -
    Check to see how many nodes are ready (not including nodes tainted NoSchedule) and write the number to /opt/KUSC00402/kusc00402.txt.

     

    풀이

    #NoSchedule으로 taint가 설정된 노드를 제외한 Ready상태의 노드의 수를 확인
    [root@]$ kubectl describe node <<node-name>> | grep taint

    Question #10

    SIMULATION

    Task -
    Schedule a Pod as follows:
    ✑ Name: kucc8
    ✑ App Containers: 2
    ✑ Container Name/Images:
    - nginx
    - consul

     

    풀이

    #nginx 이미지의 kucc8 파드 yaml 생성
    [root@]$ kubectl run kucc8 --image=nginx --dry-run=client -oyaml >> abc.yaml
    
    #consul 컨테이너 추가
    [root@]$ vi abc.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      creationTimestamp: null
      labels:
        run: kucc8
      name: kucc8
    spec:
      containers:
      - image: nginx
        name: nginx
      // 추가 //
      - image: hashicorp/consul
        name: consul
      // 추가 //
        resources: {}
      dnsPolicy: ClusterFirst
      restartPolicy: Always
    status: {}
    
    #파드 실행
    [root@]$ kubectl apply -f abc.yaml

    728x90
    반응형

    댓글