1. k8s的yaml文件到底有多復雜
Kubernetes創建、更新、刪除資源等操作時均可以使用json或yaml文件進行操作,更新和刪除可以依賴之前的文件進行更改,但是創建具有多變形,往往編輯起來比較復雜,容器出錯,而且k8s的配置項實在太多,稍微不注意就會犯錯。要寫好一個yaml文件,你需要了解yaml的語法,需要掌握k8s的各種配置,對於一個k8s的初學者而言,這將是一件很難的事情。
前沿技術,快人一步,點我了解~
比如我們看一個同時創建一個Deployment、Service、Ingress的yaml文件內容:
---
apiVersion: v1
kind: Service
metadata:
labels:
app: test-yaml
name: test-yaml
namespace: freeswitch
spec:
ports:
- name: container-1-web-1
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: test-yaml
sessionAffinity: None
type: ClusterIP
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
creationTimestamp: null
name: test-yaml
spec:
rules:
- host: test.com
http:
paths:
- backend:
serviceName: test-yaml
servicePort: 8080
path: /
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: test-yaml
name: test-yaml
namespace: freeswitch
spec:
replicas: 3
selector:
matchLabels:
app: test-yaml
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
annotations:
info: test for yaml
labels:
app: test-yaml
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- test-yaml
topologyKey: kubernetes.io/hostname
weight: 100
containers:
- env:
- name: TZ
value: Asia/Shanghai
- name: LANG
value: C.UTF-8
image: nginx
imagePullPolicy: Always
lifecycle: {}
livenessProbe:
failureThreshold: 2
initialDelaySeconds: 30
periodSeconds: 10
successThreshold: 1
tcpSocket:
port: 8080
timeoutSeconds: 2
name: test-yaml
ports:
- containerPort: 8080
name: web
protocol: TCP
readinessProbe:
failureThreshold: 2
initialDelaySeconds: 30
periodSeconds: 10
successThreshold: 1
tcpSocket:
port: 8080
timeoutSeconds: 2
resources:
limits:
cpu: 195m
memory: 375Mi
requests:
cpu: 10m
memory: 10Mi
securityContext:
allowPrivilegeEscalation: false
capabilities: {}
privileged: false
procMount: Default
readOnlyRootFilesystem: false
runAsNonRoot: false
volumeMounts:
- mountPath: /usr/share/zoneinfo/Asia/Shanghai
name: tz-config
- mountPath: /etc/localtime
name: tz-config
- mountPath: /etc/timezone
name: timezone
dnsPolicy: ClusterFirst
hostAliases:
- hostnames:
- www.baidu.com
ip: 114.114.114.114
imagePullSecrets:
- name: myregistrykey
- name: myregistrykey2
restartPolicy: Always
securityContext: {}
volumes:
- hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
type: ""
name: tz-config
- hostPath:
path: /etc/timezone
type: ""
name: timezone
這是一個包含了Service、Ingress、Deployment比較常用並且沒有用到高級功能的yaml配置,就已經有上百行,如果是在添加了一些高級配置或者是Deployment中的容器不止一個,這個yaml會更大,就會造成一種視覺上疲勞,更改起來也比較麻煩而且非常容易出錯。
2. 基於圖形化的方式自動生成yaml
2.1 k8s圖形化管理工具Ratel安裝
本次采用Ratel自動生成yaml文件,Ratel安裝文檔:https://github.com/dotbalo/ratel-doc/blob/master/cluster/Install.md
2.2 使用Ratel創建生成yaml文件
2.2.1 基本配置
安裝完成后,可以生成、創建管理常用的k8s核心資源,比如創建一個Deployment:
點擊Deployment -- 創建如圖所示:

之后可以填寫一些基本的配置信息,比如Deployment名稱、副本數、標簽信息等,當然也可以點擊必須/盡量部署至不同宿主機進行Pod親和力的配置
同時也可添加一些復雜的配置,比如內核配置、容忍配置、節點親和力快捷配置:

2.2.2 親和力配置
基本配置編譯完成以后,點擊NEXT,下一個配置親和力配置,如果上一頁使用了親和力快捷鍵,這邊會自動生成親和力配置,你可以再次編輯或者添加、刪除:

2.2.3 存儲配置
親和力配置完成以后,可以點擊NEXT進行存儲配置,目前支持volume和projectedVolume配置,volume支持configMap、Secret、HostPath、PVC、NFS、Empty等常用類型的配置:

2.2.4 容器配置
接下來是容器配置,支持常用的容器配置,當然也可以添加多個容器:

稍微復制一點的配置:

2.2.4 初始化容器配置
初始化容器和容器配置類似
2.2.5 Service和Ingress配置
創建Deployment時可以一鍵添加Service和Ingress,添加Service時會自動讀取容器的端口配置,添加Ingress時會自動讀取Service配置


2.2.6 創建資源或生成yaml文件
上述配置完成以后,可以選擇創建資源或生成yaml文件,假如點擊生成yaml文件,會自動生成Service、Ingress、Deployment的yaml文件,可以直接拿着使用:

生成的內容如下:
---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: test-yaml
name: test-yaml
namespace: default
spec:
ports:
- name: container-1-web-1
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: test-yaml
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
creationTimestamp: null
name: test-yaml
spec:
rules:
- host: test.com
http:
paths:
- backend:
serviceName: test-yaml
servicePort: 8080
path: /
status:
loadBalancer: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: test-yaml
name: test-yaml
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: test-yaml
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: test-yaml
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- preference:
matchExpressions:
- key: loki
operator: In
values:
- "true"
weight: 100
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: master
operator: NotIn
values:
- "true"
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- test-yaml
topologyKey: kubernetes.io/hostname
containers:
- args:
- '*.jar --server.port=80'
command:
- java -jar
env:
- name: TZ
value: Asia/Shanghai
- name: LANG
value: C.UTF-8
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
envFrom:
- configMapRef:
name: testcm
image: nginx
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command:
- echo "start"
preStop:
exec:
command:
- sleep 30
livenessProbe:
failureThreshold: 2
initialDelaySeconds: 30
periodSeconds: 10
successThreshold: 1
tcpSocket:
port: 8080
timeoutSeconds: 2
name: test-yaml
ports:
- containerPort: 8080
name: web
protocol: TCP
readinessProbe:
failureThreshold: 2
httpGet:
httpHeaders:
- name: a
value: b
path: /
port: 8080
scheme: HTTP
initialDelaySeconds: 30
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 2
resources:
limits:
cpu: 493m
memory: 622Mi
requests:
cpu: 10m
memory: 10Mi
securityContext:
allowPrivilegeEscalation: false
capabilities: {}
privileged: false
procMount: Default
readOnlyRootFilesystem: false
runAsNonRoot: false
volumeMounts:
- mountPath: /usr/share/zoneinfo/Asia/Shanghai
name: tz-config
- mountPath: /etc/localtime
name: tz-config
- mountPath: /etc/timezone
name: timezone
- mountPath: /mnt
name: nfs-test
dnsPolicy: ClusterFirst
initContainers:
- args:
- init
command:
- echo
env:
- name: TZ
value: Asia/Shanghai
- name: LANG
value: C.UTF-8
image: nignx-init
imagePullPolicy: Always
name: init
resources:
limits:
cpu: 351m
memory: 258Mi
requests:
cpu: 10m
memory: 10Mi
securityContext:
allowPrivilegeEscalation: false
capabilities: {}
privileged: false
procMount: Default
readOnlyRootFilesystem: false
runAsNonRoot: false
volumeMounts:
- mountPath: /usr/share/zoneinfo/Asia/Shanghai
name: tz-config
- mountPath: /etc/localtime
name: tz-config
- mountPath: /etc/timezone
name: timezone
nodeSelector:
ratel: "true"
restartPolicy: Always
securityContext:
sysctls:
- name: net.core.somaxconn
value: "16384"
- name: net.ipv4.tcp_max_syn_backlog
value: "16384"
tolerations:
- effect: NoSchedule
key: node-role.kubernetes.io/master
operator: Exists
volumes:
- name: projected-test
projected:
defaultMode: 420
sources:
- downwardAPI:
items:
- fieldRef:
fieldPath: metadata.name
path: /opt/x
- hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
type: ""
name: tz-config
- hostPath:
path: /etc/timezone
type: ""
name: timezone
- name: nfs-test
nfs:
path: /data/nfs
server: 1.1.1.1
status: {}
這個yaml比之前的稍復雜,並且添加了一些高級配置,手動編寫的還是比較麻煩的,所以用Ratel自動生成還是比較方便的,並且不會出錯。
3. 其他資源文件自動生成
目前支持了很多資源文件的自動生成,比如:Deployment、StatefulSet、DaemonSet、Service、Ingress、CronJob、Secret、ConfigMap、PV、PVC等,可以大大減少我們的工作量和k8s的復雜度。
如果想要系統的學習k8s,可以關注下k8s的課程:
Kubernetes全棧架構師:基於世界500強的k8s實戰課程
