# 命令启用一个控制器
kubectl run nginx-deploy --image=ikubernetes/nginx:1.7.5 --port=88 --replicas=1
# 命令暴露端口
kubectl expose deployment nginx-deploy --name=nginx --port=88 --target-port=88 --protocol=TCP
# 修改副本数量
kubectl scale --replicas=3 deployment nginx-deploy
# 镜像升级
kubuctl set image deployment myapp myapp=ikubernetes/myapp:v2
# 获取配置清单字段如何配置
kubectl explain pods.spec
# 过滤
kubectl get pods -l run,!release --show-labels
# 等值关系标签选择器
kubectl get pods -l release!=stable,run=nginx-deploy --show-labels
# 集合关系标签选择器
kubectl get pods -l "release notin (stable,canary,beta,alpha)" --show-labels
# 监控pods
kubectl get pods -w
# 查看ip等详细信息
kubectl get pods -o wide
# 显示标签
kubectl get pods -L run,app
# 修改标签
kubectl label pods pod-demo release=stable --overwrite
# 修改升级rs,但修改pod模板,pod在不创建的情况下,不会修改
kubectl edit rs myapp
# 查看滚动历史
kubectl rollout history deployment myapp-deploy
# 镜像回滚到上一版本
kubectl rollout undo deployment myapp
# 镜像回滚到第一个版本
kubectl rollout undo deployment myapp --to-revision=1
# 打补丁
kubectl patch deployment myapp-deploy -p '{"spec":{"replicas":5}}'
# 暂停命令,暂停deployment更新
kubectl rollout pause deployment myapp-deploy
# 取消平暂停命令
kubectl rollout resume deployment myapp-deploy
# 查看更新状态
kubectl status deployment myapp-deploy
# 更新ds的镜像
kubectl set image daemonsets filebeat filebeat=ikubernetes/filebeat:5.6.6-alpine
# 创建configmap
kubectl create configmap nginx-config --from-literal=nginx_port=8080 --from-literal=server_name=myapp.sean.com
# 创建configmap
kubectl create configmap nginx-config2 --from-file=./myconfig2.conf
# 创建secret
kubectl create secret generic mysql-root-password --from-literal=password=My@Pass123
# group/version通过 以下命令获取 $ kubectl api-versions
apiVersion: group/version
kind: #资源类别
metadata: #元数据
name:
namespace:
labels:
# key:字母、数字、_、-、. 长度为64个字符,以字母和数字开头
# value:可以为空,字母、数字、_、-、. 长度为64个字符,以字母和数字开头及结尾
key: value
annotations:
#每个资源的应用PATH
selfLink: /api/GROUP/VERSION/namespaces/NAMESPACE/TYPE/NAME
spec: #期望的状态, disired state
nodeSelector: # 节点标签选择器
nodeName: # 节点名称
restartPolicy: # 重启策略 Always, OnFailure, Never. Default to Always.
containers:
- name:
image:
imagePullPolice: # 镜像拉取策略 Always,Never,IfNotPresent,若image的版本为latest,则使用Always策略
ports: # 端口
- name: #端口名称,svr可引用该名称
containerPort: # 暴露端口
#探测的三种方式 ExecAction:exec; TCPSocketAction:topSocket; HTTPGetAction:httpGet
livenessProbe: # 存活探测器
readinessProbe: # 就绪探测器
lifecycle:
postStart: #启动后执行
preStop: #停止前执行
Volumes: # 数据卷挂载
- name: html
emptyDir:
medium:
sizeLimit:
status: #当前状态,current state 本字段由kubernetes集群维护;
matchLabels: #直接给定键值
matchExpressions: #匹配表达式
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
tier: frontend
annotations:
sean.com/created-by: "cluster admin"
spec:
containers:
- name: myapp
image: ikubernetes/nginx:1.16.0
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html/
- name: busybox
image: ikubernetes/busybox:1.28
imagePullPolicy: IfNotPresent
volumeMounts:
- name: html
mountPath: /data/
command: ["/bin/sh"]
args: ["-c","while true; do echo $(date) >> /data/index.html; sleep 2; done"]
volumes:
- name: html
emptyDir: {}
apiVersion: api/v1
kind: ReplicatSet
metadata:
name: myapp
namespace: default
spec:
replicas: 2
# 两类标签选择器
selector:
matchLabels:
app: myapp
release: canary
matchExpressions:
template:
metadata:
name: myapp-pod
# 此处的标签选择器需要能被上述标签选择
labels:
app: myapp
reslease: canary
environment: qa
spec:
containers:
- name: myapp-container
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
resources:
requests: # 需求,下线
memory: "64Mi"
cpu: "500m"
limits: # 最大
memory: "128Mi"
cpu: "600m"
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
strategy:
type: # 更新策略 recreate:重建更新 RollingUpdate:滚动更新,控制更新粒度
rollingUpdate: # 只有type为RollingUpdate该类型时,才能定义该字段
maxSurge: # 最多能多几个
maxUnavailable: # 最多能少几个
revisionHistoryLimit: # 历史的保存几个
# 其他与rs相同
apiVersion: apps/v1
kind: DemanSet
metadata:
name: filebeat-ds
namespace: default
spec:
selector:
matchLabels:
app: filebeat
release: stable
matchExpressions:
template:
metadata:
# 此处的标签选择器需要能被上述标签选择
labels:
app: filebeat
reslease: stable
spec:
containers:
- name: filebeat
image: ikubernetes/filebeat:5.6.5-alpine
env:
- name: REDIS_HOST
value: redis.default.svc.cluster.local
- name: REDIS_LOG_LEVEL
value: info
工作模式:
类型:ExternamName:集群内部访问集群外部的服务;ClusterIp:集群内部使用; NodePort:节点端口,集群外部访问; LoadBlance:用虚拟机的负载均衡
资源记录:SVC_NAME.NS_NAME.DOMAIN.LTD. redis.default.svc.cluster.local.
apiVersion: v1
kind: Service
metadata:
name: redis
namespace: default
spec:
selector: # 只支持一种标签选择器
app: redis
role: logstor
type: NodePort
port:
- port: # 对外容器的端口
targetPort: # 容器的端口
nodePort: #节点暴露的端口
protocol: #协议 TCP
sessionAffinity: # ClientIp:同一个客户端只会访问统一节点,None则会多个节点都会请求到
ClusterIp: # 可以指定IP,若为None,则为无头Service,会直接绑定到Pod的IP上
apiVersion: v1
kind: Pod
metadata:
name: pod-vol-hostpath
namespace: defalut
spec:
containers:
- name: myapp
image: ikuburntes/myapp:v1
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html/
volumes:
- name: html
hostPath:
path: /data/pod/volume1
type: DirectoryOrCreate
apiVersion: v1
kind: Pod
metadata:
name: pod-vol-nfs
namespace: defalut
spec:
containers:
- name: myapp
image: ikuburntes/myapp:v1
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html/
volumes:
- name: html
nfs:
path: /data/volumes # Nfs服务器挂载路径
server: stor01.sean.com # Nfs地址
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv001
labels:
name: pv001
spec:
nfs:
path: /data/volumes/v1 # Nfs服务器挂载路径
server: stor01.sean.com # Nfs地址
accessModes: ["ReadWriteMony","ReadWriteOnce","ReadOnlyMany"]
capacity:
storage: 2Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv002
labels:
name: pv002
spec:
nfs:
path: /data/volumes/v2 # Nfs服务器挂载路径
server: stor01.sean.com # Nfs地址
accessModes: ["ReadWriteMony","ReadOnlyMany"]
capacity:
storage: 5Gi
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mypvc
namespace: default
spec:
accessModes: ["ReadWriteMony"]
resources:
requests:
storage: 3Gi
---
apiVersion: v1
kind: Pod
metadata:
name: pod-vol-hostpath
namespace: defalut
spec:
containers:
- name: myapp
image: ikuburntes/myapp:v1
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html/
volumes:
- name: html
PersistentVolumeClaim:
cliamName: mypvc
- 自定义命令行参数 args:[]
- 把配置文件直接放进镜像中
apiVersion: v1
kind: Pod
metadata:
name: pod-cm-1
namespace: defalut
spec:
containers:
- name: myapp
image: ikuburntes/myapp:v1
env:
- name: NGINX_SERVER_PORT
valueFrom:
configMapKeyRef:
name: nginx-confg
key: nginx_port
- name: NGINX_SERVER_NAME
valueFrom:
configMapKeyRef:
name: nginx-confg
key: nginx_name
apiVersion: v1
kind: Pod
metadata:
name: pod-cm-2
namespace: defalut
spec:
containers:
- name: myapp
image: ikuburntes/myapp:v1
volumeMounts:
- name: nginxconf
mountPath: /etc/nginx/config.d/
readOnly: true # 容器不能修改其内容
volumes:
- name: nginxconf
configMap:
name: nginx-config
apiVersion: v1
kind: Pod
metadata:
name: pod-cm-1
namespace: defalut
spec:
containers:
- name: myapp
image: ikuburntes/myapp:v1
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-root-password
key: password