CKA (Certified Kubernetes Administrator)/Kode Kloud

06.Security - KubeConfig

seulseul 2022. 1. 25. 16:55
Security

1) View Certificate Details
2) Certificates API
3) KubeConfig
4) Role Based Access Controls
5) Cluster Roles
6) Service Accounts
7) Image Security
8) Security Contexts
9) Network Policies

 

01. Where is the default kubeconfig file located in the current environment?

 

Find the current home directory by looking at the HOME environment variable.

1) /root/.kube/config

2) /root/,kube/kubeconfig

3) /root/kubeconfig

4) /home/packer/.kube/config

Use the command ls -a and look for the kube config file under /root/.kube.

02. How many clusters are defined in the default kubeconfig file?

 

ask : 1

 

root@controlplane:~/.kube# cat config
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM1ekNDQWMrZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJeU1ERXlOVEEzTVRjeU5sb1hEVE15TURFeU16QTNNVGN5Tmxvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTjlXCnM3UC9KWE5hMDVEeDBwMlZWUHppcm1raW1GK3BCVVNYUmNQd2E1RVQ2WlJZcjdnb2ZPeHNaL0s3cEZNeWdyV1IKNllCdHFJMkN0VmhkODlUVVJQZDdLeS82WGxXUW4vUjVBOWxKM0R4OVViZi9DNk03TUpxVTg5Nm5qR1ZuaXVKYQphZVF1Nlh2cmtJRUEvbW9kd21pbXdIUGNHRTlMS3UxT01CWWJ4aGdRakgrZlZtTGdjeVEzWVBPNUlsOEpLd1h4ClBsU0tvNFowTGtkTVBmTnpYa2R1eHlxdjAreThqVjNzVER1MEt4WVVLMkRqSnc2aW1EYVRMZnlmTDlFVjBhK3YKY0hSM2g5cXBHYWc5dTdyVXNuR3V4TVRNa1B2MXJiZ2Y4QXBuY0xNTzhCSmxjcWQ1NU9PbWQ5TmpodHdCVUgvWApOanhDQUp2VTRNSjRNMDlidTBzQ0F3RUFBYU5DTUVBd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0hRWURWUjBPQkJZRUZDNUtGazNIcmJEcW16S3BZWXorL1B0YjNCVHdNQTBHQ1NxR1NJYjMKRFFFQkN3VUFBNElCQVFCV0F3Z1pBNldXRWVWRXdYU2xHZ0E4Y05xaVZweXZIQzlRUkJWZk45aEc0cWlKT2p0eAo5WUx1T2dKV1p0QWEwWWxFSlF6SkFvWHloRHVPdXo5TkFIWkFZOThkNUJaSEFWcFZtNDFtb0hkSHN2c2R5TzU3CmNsVUxpZ1dVbE9wYW1NWVF5eE5aejNVQTA5cTlEcVZOclROdGNqRUZjVysyREZoWXFrMERrSWRlRFNIUkl1U3AKeUFRSnA3NUMvdnBCeEV5SGZUVFZYYVZNV3E2bTVMSC81OXBRQ09CRW5xYmZnZVpHYmpvS2ZheVJucGhyS1RuNwpENkVqcVZOanRIZkQ3Z1lhYXRIYm44d0gwVXlWT3pmRDNpTkJIbWVZV2RxcjdxcjVFb08xT3VHeEdrZFE5cjM4CkxQVzFiRHVMZlZ0cm9FZHBhUEs2QXJzSDh0RGlDdk1JL2txSwotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
    server: https://controlplane:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURFekNDQWZ1Z0F3SUJBZ0lJZEo5djJHYnUxRTB3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB5TWpBeE1qVXdOekUzTWpaYUZ3MHl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1B0TDlpZXpaa1Bxa0VGNFNsSHl4WmxuMUJZZmhZUi8wbmI1dG02VlgvQU16ZFplTUJuM1BPMDM3VnhHCjNrcGNKSHNOQW9HQkFMTSs2WU81RkFSTmJLZ0xxTmdmbEVHWVlOZHZLM1U5ckU2ZGZEU0JuaWtUem1tS0VlNFUKOHVFNzFGMGxyWDJXenJDWUFNMTA5SVFjRExwLzRsaDlQK2trUWhLMlhETTc4WUZ6eTFDSTJpNUt2REtKNjFpOQo0akdEZ2xXcjAwZzFBbGpTZGUvcUQ0MFoxWXZjZTBNSzZRK21ZRTAvNmUwZmxiM3pKU0RWUFd0WAotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=

03. How many Users are defined in the default kubeconfig file?

ask : 1

 

04. How many contexts are defined in the default kubeconfig file?

ask : 1

 

05. What is the user configured in the current context?

 

ask : kubernetes-admin

 

06. What is the name of the cluster configured in the default kubeconfig file?

 

ask : kubernetes

 

07. A new kubeconfig file named my-kube-config is created.

It is placed in the /root directory. How many clusters are defined in that kubeconfig file?

 

ask : 4

 

root@controlplane:~# cat my-kube-config 
apiVersion: v1
kind: Config

clusters:
- name: production
  cluster:
    certificate-authority: /etc/kubernetes/pki/ca.crt
    server: https://controlplane:6443

- name: development
  cluster:
    certificate-authority: /etc/kubernetes/pki/ca.crt
    server: https://controlplane:6443

- name: kubernetes-on-aws
  cluster:
    certificate-authority: /etc/kubernetes/pki/ca.crt
    server: https://controlplane:6443

- name: test-cluster-1
  cluster:
    certificate-authority: /etc/kubernetes/pki/ca.crt
    server: https://controlplane:6443

contexts:
- name: test-user@development
  context:
    cluster: development
    user: test-user

- name: aws-user@kubernetes-on-aws
  context:
    cluster: kubernetes-on-aws
    user: aws-user

- name: test-user@production
  context:
    cluster: production
    user: test-user

- name: research
  context:
    cluster: test-cluster-1
    user: dev-user

users:
- name: test-user
  user:
    client-certificate: /etc/kubernetes/pki/users/test-user/test-user.crt
    client-key: /etc/kubernetes/pki/users/test-user/test-user.key
- name: dev-user
  user:
    client-certificate: /etc/kubernetes/pki/users/dev-user/developer-user.crt
    client-key: /etc/kubernetes/pki/users/dev-user/dev-user.key
- name: aws-user
  user:
    client-certificate: /etc/kubernetes/pki/users/aws-user/aws-user.crt
    client-key: /etc/kubernetes/pki/users/aws-user/aws-user.key

current-context: test-user@development
preferences: {}

 

08. How many contexts are configured in the my-kube-config file?

 

ask : 4

 

09. What user is configured in the research context?

 

ask : dev-user

 

10. What is the name of the client-certificate file configured for the aws-user?

 

ask : aws-user.crt

 

11. What is the current context set to in the my-kube-config file?

my-kube-config 파일에 설정된 현재 컨텍스트는 무엇입니까?

 

ask : test-user@development

 

12. I would like to use the dev-user to access test-cluster-1.

dev-user를 사용하여 test-cluster-1에 액세스하고 싶습니다.

 

Set the current context to the right one so I can do that.

내가 할 수 있도록 현재 컨텍스트를 올바른 컨텍스트로 설정하십시오.


Once the right context is identified, use the kubectl config use-context command.

 
  • Current context set
To use that context, run the command: 

kubectl config --kubeconfig=/root/my-kube-config use-context research

To know the current context, run the command: 

kubectl config --kubeconfig=/root/my-kube-config current-context
root@controlplane:~# kubectl config --kubeconfig=/root/my-kube-config use-context research
Switched to context "research".
root@controlplane:~# kubectl config --kubeconfig=/root/my-kube-config current-context
research

 

13. We don't want to have to specify the kubeconfig file option on each command.

각 명령에 kubeconfig 파일 옵션을 지정하고 싶지 않습니다.

 

Make the my-kube-config file the default kubeconfig.


 

 

  • Default kubeconfig file configured
Replace the contents in the default kubeconfig file with the content from my-kube-config file.
   39  cd .kube/
   40  ls
   41  mv config config_bak
   42  cd ..
   43  ls
   44  cp my-kube-config ~/.kube/config
   45  kubectl config view

 

 

14. With the current-context set toresearch, we are trying to access the cluster.

However something seems to be wrong. Identify and fix the issue.


Try running the kubectl get pods command and look for the error.

All users certificates are stored at /etc/kubernetes/pki/users.

 
  • Issue fixed
root@controlplane:~# k get pod
error: unable to read client-cert /etc/kubernetes/pki/users/dev-user/developer-user.crt for dev-user due to open /etc/kubernetes/pki/users/dev-user/developer-user.crt: no such file or directory

 

The path to certificate is incorrect in the kubeconfig file.

Correct the certificate name which is available at /etc/kubernetes/pki/users/.

 

cd /root/.kube

vi config

# - name: dev-user
#  user:
#    client-certificate: /etc/kubernetes/pki/users/dev-user/developer-user.crt

# developer-user.crt > dev-user.crt

Bookmark

 

https://kubernetes.io/ko/docs/concepts/configuration/organize-cluster-access-kubeconfig/

 

kubeconfig 파일을 사용하여 클러스터 접근 구성하기

kubeconfig 파일들을 사용하여 클러스터, 사용자, 네임스페이스 및 인증 메커니즘에 대한 정보를 관리하자. kubectl 커맨드라인 툴은 kubeconfig 파일을 사용하여 클러스터의 선택과 클러스터의 API 서버

kubernetes.io