2. 2
Kubernetes Resources
4
[ Pod ]
• 컨테이너를 담고 있는 그릇 (여러개의 컨테이너가 포함될 수 있음)
• 같은 Pods 안에서의 여러 컨테이너가 같은 네트워크 네임스페이스와 ip 를 가짐
(Apache -> (localhost, port) -> Tomcat)
• 같은 Pods 안에서의 여러 컨테이너가 같은 볼륨을 볼 수 있음
[ ReplicaSet ]
• Pod 개수를 관리
[ Deployment ]
• Pod 와 Replica Set 을 통합하여 배포할 수 있는 단위
• 배포 히스토리를 버전별로 관리
[ Service ]
• Route to pod (using labels) – 내부 IP로 Pod 에 대한 Load Balancing (기본기능)
• 외부에서 접근할려면 아래 두 타입을 활용하여 가능
• 타입 : Load balancer (GCE), NodePort (iptables)
[ ConfigMap and Secret ]
• ConfigMap : Application 의 Configuration, 혹은 shell script
• Secret : 보안 값
3. 3
Pod
3
• 컨테이너 배포 단위로 컨테이너를 담고 있는 그릇
• 여러 개의 컨테이너가 포함될 수 있음
• 하나의 Pod 안에서의 여러 컨테이너는 같은 docker ip 를
가짐
• pause 컨테이너가 하나씩 생김
• Pod 내부에서 컨테이너간 통신은 localhost & 포트로 통신
• Docker Networking 의 Mapped Container Mode
docker run -d --name pause pause_image
docker run -d --name web -net=container:pause
web_image
• 하나의 Pods 안에서의 여러 컨테이너는 같은 볼륨을 볼 수
있다.
4. 4
ReplicaSet
44
• Pod 의 개수를 지정해서 실행
• 실행 중인 Pod 의 수를 항상 보장
• Pod 를 명령어로 삭제해도 ReplicaSet 에 의해 자동 복구됨
• Horizontal Pod Autoscaler 가 autoscale 을 할 때 ReplicaSet 활용
• ReplicaSet = Pod + replicas 수 (Pod 개수)
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: frontend-scaler
spec:
scaleTargetRef:
apiVersion: apps/v1beta1
kind: Deployment
name: frontend
minReplicas: 3
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 50
5. Deployment
7
• Deployment = ReplicaSet + History (Revision)
• Pod 배포에 대한 버전 관리가 가능
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: default
spec:
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
$ kubectl create -f nginx.yaml
$ kubectl rollout history deployment/nginx-
deployment
$ kubectl rollout history deployment/nginx-
deployment --revision=2
$ kubectl rollout undo deployment/nginx-
deployment --to-revision=2
6. Service
8
• Type: ClusterIP(default), LoadBalancer, NodePort, ExternalName
• Pod 를 대표하는 DNS 이름
• ClusterIP 가 할당됨 (Virtual IP)
• kube-proxy 가 iptables 에 Cluster IP 세팅
• Simple Load Balance (default : Round Robin)
• selector 를 지정하면 Endpoint 가 생김
Service
Pod B-1 Pod B-2
Pod A-1
serivce명 & port
Pod IP & port (Endpoint)
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
type: NodePort
ports:
- port: 80
nodePort: 31000
selector:
app: guestbook
tier: frontend
11. Helm Architecture
Helm Client
• a command-line client for end users.
• Managing repositories
• Interacting with the Tiller server
• Sending charts to be installed
• Asking for information about releases
• Requesting upgrading or uninstalling of existing releases
Tiller Server
• an in-cluster server that interacts with the Helm client, and interfaces
with the Kubernetes API server.
• Listening for incoming requests from the Helm client
• Combining a chart and configuration to build a release
• Installing charts into Kubernetes, and then tracking the
subsequent release
• Upgrading and uninstalling charts by interacting with
Kubernetes
12. Simple chart 생성
$ helm create mychart
mychart/
├── charts/
├── Chart.yaml
├── templates/
│ ├── deployment.yaml
│ ├── _helpers.tpl
│ ├── ingress.yaml
│ ├── NOTES.txt
│ └── service.yaml
└── values.yaml
helm install 시에 디스플레이 되는 문장
kubernetes 기본 manifest 파일
kubernetes service manifest 파일
template helper, utililty function 처럼
동작
tiller 가 templates rendering engine 을 통해 generation
chart version 등 chart 에 대한 설명을 담고 있음
subcharts 라 부르는 다른 chart를 포함할 수 있는 디렉토리
kubernetes ingress manifest 파일
chart에 대한 기본 값을 저장, 변수처럼 활용
helm install 이나 helm upgrade 시에 override 가능
13. Template directive
$ vi mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
- Template 지시자는 {{ }} 로 표시
- Value 가 template 안에서 치환되며 dot( . ) 으로 시작하여 namespace 를 넣을 수 있다.
- 맨처음의 dot 은 최상의 네임스페이스를 의미하며 Release 는 내장 (built-in) 객체로서 Name
객체를 가지고 있다.
$ helm install ./mychart
$ kubectl get configmap
NAME DATA AGE
iced-bear-configmap 1 19s
$ helm get manifest iced-bear
$ helm delete iced-bear
15. Values File
$ vi mychart/values.yaml
favoriteDrink: coffee
$ vi mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
drink: {{ .Values.favoriteDrink }}
$ helm install ./mychart
NAME: needled-rottweiler
LAST DEPLOYED: Fri Feb 23 04:57:15 2018
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/ConfigMap
NAME DATA AGE
needled-rottweiler-configmap 2 0s
Ø values.yaml 파일 위치는 chart 최상위 디렉토리에 위치 (e.g. ./mychart/values.yaml)
Ø helm install 혹은 helm upgrade 시에 -f 로 override 가능
Ø --set key=value 로 value override 가능
16. Template
"{{23 -}} < {{- 45}}" 는 "23<45" 와 같이 공백없이 출력
Ø "{{-" 은 text 앞의 공백을 없앤다.
Ø "-}}" 은 text 뒤의 공백을 없앤다.
Ø {{/* a comment */}} : 주석 처리
Ø {{pipeline}} : 값 print
Ø {{if pipeline}} T1 {{end}}
pipeline 값이 empty가 아니면 T1 수행
empty 정의 : false, 0, nil 그리고 array, slice, map, string 의 길이가 0
Ø {{if pipeline}} T1 {{else}} T0 {{end}} : pipeline 값이 empty면 T0 수행
Ø {{if pipeline}} T1 {{else if pipeline}} T0 {{end}}
Ø {{range pipeline}} T1 {{end}}
pipeline 은 array, slice, map, channel 중 하나, pipeline 길이가 0이 아니면 수행
dot 으로 지정된 pipeline 의 값으로 수행
Ø {{range pipeline}} T1 {{else}} T0 {{end}}
dot 으로 지정된 pipeline 의 값으로 수행
Ø {{template "name"}} : "name" template 을 nil data 로 전달하여 수행
Ø {{template "name" pipeline}}
"name" template 을 dot 으로 지정된 pipeline 의 값을 전달하여 수행
Ø {{block "name" pipeline}} T1 {{end}}
block 은 template을 간단히 정의 : {{define "name""}} T1 {{end}}
그리고 나서 template 을 dot 지정 값으로 수행 : {{template "name" .}}
19. Template Functions
- b64enc: Base 64 encode a string.
- b64dec: Base 64 decode a string.
Ø Encoding
- tuple
- list
- dict
Ø Data Structures
- first, last
- rest : 첫번째 아이템 제외한 나머지 리턴
- initial : 마지막 아이템 제외한 나머지 리턴
- append : 마지막에 추가
- prepend: 처음에 추가
- reverse
- uniq
- has
ex) '{{ list 1 2 3 | reverse | first }}'
Ø List Functions
20. Template Functions
- add1 : 1만 더하기
- add
- sub
- div
- mod
- mul
- max
- min
Ø Math Functions
- set
- unset
- hasKey
- keys
- pick : 주어진 key에 대한 새로운 dick 이 리턴
- omit : 주어진 key만 생략하고 나머지 dick 을 리턴
ex) '{{set $dick $key, $value }}'
Ø Dick Functions
22. with 로 scope 변경
{{ with PIPELINE }}
# restricted scope
{{ end }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: "Hello World"
{{- with .Values.favorite }}
drink: {{ .drink | default "tea" | quote }}
food: {{ .food | upper | quote }}
{{- end }}
Ø . 은 current scope 으로 .Values values.yaml 의 Values 객체를 나타냄
Ø {{ with }} {{ end }} 로 scope 을 변경할 수 있음