利用yaml文件管理资源
利用yaml配置文件管理资源
[root@master ~]# cat nginx-deployment.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.10
ports:
- containerPort: 80
[root@master ~]# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
ports:
- port: 88
targetPort: 80
selector:
app: nginx
执行如下命令即可发布服务
kubectl create -f nginx-deployment.yaml
kubectl create -f nginx-service.yaml
查看发布的服务
[root@master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-66d64dcdf8-89tfc 1/1 Running 0 5h21m
pod/nginx-66d64dcdf8-hwfbm 1/1 Running 0 5h21m
pod/nginx-66d64dcdf8-m8jfs 1/1 Running 0 5m49s
pod/nginx-deployment-58d6d6ccb8-5hnzm 1/1 Running 0 10m
pod/nginx-deployment-58d6d6ccb8-6wn9w 1/1 Running 0 10m
pod/nginx-deployment-58d6d6ccb8-mh48n 1/1 Running 0 10m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.10.10.1 <none> 443/TCP 46h
service/nginx-service ClusterIP 10.10.10.135 <none> 88/TCP 4m7s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.extensions/nginx 3/3 3 3 5h21m
deployment.extensions/nginx-deployment 3/3 3 3 10m
NAME DESIRED CURRENT READY AGE
replicaset.extensions/nginx-66d64dcdf8 3 3 3 5h21m
replicaset.extensions/nginx-deployment-58d6d6ccb8 3 3 3 10m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 3/3 3 3 5h21m
deployment.apps/nginx-deployment 3/3 3 3 10m
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-66d64dcdf8 3 3 3 5h21m
replicaset.apps/nginx-deployment-58d6d6ccb8 3 3 3 10m
Pod基本管理
创建一个pod:
[root@master ~]# cat pod-nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
查看pod
[root@master ~]# kubectl describe pod nginx-pod
Name: nginx-pod
Namespace: default
Node: 172.16.163.130/172.16.163.130
Start Time: Sat, 31 Aug 2019 09:58:34 -0400
Labels: app=nginx
Annotations: <none>
Status: Running
IP: 172.17.76.3
Containers:
nginx:
Container ID: docker://248a01e3010c97ea16f9bf964472109d6050557fcbb437925b538228135525aa
Image: nginx
Image ID: docker-pullable://nginx@sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9
Port: <none>
Host Port: <none>
State: Running
Started: Sat, 31 Aug 2019 09:58:54 -0400
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-rmchc (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-rmchc:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-rmchc
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 75s default-scheduler Successfully assigned nginx-pod to 172.16.163.130
Normal SuccessfulMountVolume 75s kubelet, 172.16.163.130 MountVolume.SetUp succeeded for volume "default-token-rmchc"
Normal Pulling 74s kubelet, 172.16.163.130 pulling image "nginx"
Normal Pulled 56s kubelet, 172.16.163.130 Successfully pulled image "nginx"
Normal Created 56s kubelet, 172.16.163.130 Created container
Normal Started 55s kubelet, 172.16.163.130 Started container
pod的管理
基本管理:
# 创建pod资源
kubectl create -f pod.yaml
# 查看pods
kubectl get pods nginx-pod
# 查看pod描述
kubectl describe pod nginx-pod
# 更新资源
kubectl apply -f pod.yaml
# 删除资源
kubectl delete pod nginx-pod
Pod资源限制
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Pod调度约束与重启策略
调度约束就是让某个应用运行在指定的节点上
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
# nodeName: node01 //这个是指定到某个node节点上,填写node的ip地址
nodeSelector:
env_role: dev //通过标签来进行指定匹配
containers:
- name: nginx
image: nginx
//为node节点设置标签:
kubectl label nodes 172.16.163.130 env_role=dev
Pod的重启策略
三种重启策略:
Always:当容器停止,总是重建容器,默认策略。
OnFailure:当容器异常退出(退出状态码非0)时,才重启容器。
Never:当容器终止退出,从不重启容器。
例如:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
# nodeName: node01
nodeSelector:
env_role: dev //指定调度到某个节点上
containers:
- name: nginx
image: nginx
restartPolicy: OnFailure //只有当容器异常退出时才重启
Pod的健康检查
提供Probe机制,有以下两种类型:
-
livenessProbe: 探测应用是否处于健康状态,如果不健康则删除重建该容器
-
readinessProbe:探测应用是否启动完成并且处于正常服务状态,如果不正常则更新容器的状态
Probe支持以下三种检查方法:
-
httpGet:发送HTTP请求,返回200-400范围状态码为成功。
-
exec :执行Shell命令返回状态码是0为成功。
-
tcpSocket :发起TCP Socket建立成功。
例如:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /index.html
port: 80
Pod问题定位
kubectl describe TYPE NAME_PREFIX
kubectl logs nginx-xxx
kubectl exec –it nginx-xxx bash