[k8s] 명령어 Tip By starseat 2022-07-05 00:25:12 server/oss Post Tags # Certification Tips - Imperative Commands with Kubectl kubectl 을 사용한 필수 명령어 - `--dry-run`: 명령이 실행되는 즉시 리소스 생성 - `--dry-run=client`: 단순 명령 테스트. 리소스 생성되지 않고, 리소스 생성 가능 여부 및 올바른 명령인지 알려줌. - `-o yaml`: 리소스 정의 yaml 형식으로 출력 ## POD ### Create an NGINX Pod ``` kubectl run nginx --image=nginx ``` ### Generate POD Manifest YAML file (-o yaml). Don't create it(--dry-run) ``` kubectl run nginx --image=nginx --dry-run=client -o yaml ``` ## Deployment ### Create a deployment ``` kubectl create deployment nginx --image=nginx ``` ### Generate Deployment YAML file (-o yaml). Don't create it(--dry-run) ``` kubectl create deployment nginx --image=nginx --dry-run=client -o yaml ``` ### Generate Deployment with 4 Replicas ``` kubectl create deployment nginx --image=nginx --replicas=4 ``` - You can also scale a deployment using the `kubectl scale` command. ``` kubectl scale deployment nginx --replicas=4 ``` ### Another way to do this is to save the YAML definition to a file and modify ``` kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > nginx-deployment.yaml ``` ## Service ### Create a Service named redis-service of type ClusterIP to expose pod redis on port 6379 - This will automatically use the pod's labels as selectors ``` kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml ``` - another way (This will not use the pods labels as selectors, instead it will assume selectors as `app=redis`. [You cannot pass in selectors as an option](https://github.com/kubernetes/kubernetes/issues/46191). So it does not work very well if your pod has a different label set. So generate the file and modify the selectors before creating the service) ``` kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml ``` ### Create a Service named nginx of type NodePort to expose pod nginx's port 80 on port 30080 on the nodes: - (This will automatically use the pod's labels as selectors, [but you cannot specify the node port](https://github.com/kubernetes/kubernetes/issues/46191). You have to generate a definition file and then add the node port in manually before creating the service with the pod.) ``` kubectl expose pod nginx --name=nginx-service --type=NodePort --port=80 --dry-run=client -o yaml ``` - another way (This will not use the pods labels as selectors) ``` kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml ``` Both the above commands have their own challenges. While one of it cannot accept a selector the other cannot accept a node port. I would recommend going with the `kubectl expose` command. If you need to specify a node port, generate a definition file using the same command and manually input the nodeport before creating the service. ## Reference: - (Udemy 강의) Certification Tips - Imperative Commands with Kubectl - [https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands) - [https://kubernetes.io/docs/reference/kubectl/conventions/](https://kubernetes.io/docs/reference/kubectl/conventions/) # Kubectl Apply Command - 구성 파일인 생명 객체 정의 고려 - 변경 사항을 결정하기 전 `Kubernetes` 에 마지막으로 적용된 구성 확인 - `kubectl apply` 명령이 실행되기 전 `Object` 가 존재하지 않으면 새로운 `Object` 생성 - `Object` 가 생성될 때 저장된 파일(`yaml` 파일)과 유사한 `Object` 구성이 `Kubernetes` 메모리내에 생성되고, 이는 `Object` 의 상태 및 수명, 구성 정보 등이 저장됨. - 저장된 파일(`yaml`) 에서 `Kubernetes` 내의 `Object` 정보로 저장되기 전 `JSON` 타입으로 변환되어 체크함. - 세가지 모두를 비교하여 라이브 개체에 적용할 변경 사항 식별 - 저장된 파일(`yaml` 파일) ```yaml // nginx.yaml apiVersion: v1 kind: Pod metadata: name: myapp-pod labels: app: myapp type: front-end-service spec: containers: - name: nginx-container image: nginx:1.18 ``` - 중간 변환(`JSON`) `Live object configuration` 에 `JSON` 인 이 데이터가 저장됨. ```json // Last applied Configuration { "apiVersion": "v1", "kind": "Pod", "metadata": { "annotations:": {}, "labels": { "run": "myapp-pod", "type": "front-end-service" }, "name": "myapp-pod", }, "spec": { "containers": [ { "image": "nginx:1.18", "name": "nginx-container" } ] } } ``` - `Kubernetes` 내에 생성(저장)된 정보(`Kubernetes` 관리 설정 파일) `Kubernetes` 메모리 내에 저장됨. ```yaml // Live object configuration apiVersion: v1 kind: Pod metadata: name: myapp-pod kubectl.kubernetes.io/last-applied-configuration: {두번쨰 JSON data} # Last applied Configuration labels: app: myapp type: front-end-service spec: containers: - name: nginx-container image: nginx:1.18 status: conditions: - lastProbeTime: null status: "True" type: Initialized ``` Previous Post [apache] httpd yum 설치 후 일반계정 실행 Next Post [nginx] 직접 설치