Yaml use file management resources

Yaml use profile management resources

[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

Run the following command to publish services

kubectl create -f nginx-deployment.yaml
kubectl create -f nginx-service.yaml 

View published service

[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 basic management

Creating a pod:

[root@master ~]# cat pod-nginx.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx

View 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 management

基本管理:
# 创建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 resource constraints

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 scheduling constraints and restart strategy

Scheduling constraints is to allow an application to run on the specified node

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 restart strategy

三种重启策略:
Always:当容器停止,总是重建容器,默认策略。
OnFailure:当容器异常退出(退出状态码非0)时,才重启容器。
Never:当容器终止退出,从不重启容器。

E.g:

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 health check

Probe provides the mechanism, there are two types:

  • livenessProbe: detecting whether the application is in a healthy state, if not healthy then delete the reconstruction of the container

  • readinessProbe: detecting whether the application is complete and start the service in normal state, if not properly updating the status of the vessel

    Probe supports the following three methods:

  • httpGet: HTTP request, returns a successful status code range 200-400.

  • exec: execute Shell command returns a status code of 0 is successful.

  • tcpSocket: initiate TCP Socket successfully established.

E.g:

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 locate the problem

kubectl describe TYPE NAME_PREFIX
kubectl logs nginx-xxx
kubectl exec –it nginx-xxx bash

Guess you like

Origin www.cnblogs.com/jasonboren/p/11493268.html