Kubernetes实现资源限制,调度约束,服务发现以及健康检查

①Kubernetes实现资源限制,对每一个Pod设置资源限制html

[root@node-1 k8s-yaml] cat nginx-resource.yaml
node

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginxnginx

spec:
  containers:
  - name: nginx
     image: nginx
     resources:
       requests:
         memory: "64Mi"
         cpu: "250m"
       limits:
         memory: "128Mi"
         cpu: "500m"后端

②Kubernetes调度约束,将Pod调度到某一固定的后端node节点上api

[root@node-1 k8s-yaml]# cat nginx-node-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deploymeng-node
spec:
  replicas: 3
  template:
    metadata:
      name: nginx-pod
      labels:
        app: nginx
    spec:
      nodeName: 192.168.175.131   ##将3个pod部署在192.168.175.130节点上
      containers:
      - name: test
        image: nginx:1.10
        ports:
        - containerPort: 80app

建立,并查看deploment部署状况,成功部署
tcp

图片.png

查看pod节点分布ide

图片.png

③Kubernetes服务发现,使得Deployment以及单个Pod节点在建立的时候可以自动发现Servicespa

注意:想要让Pod,Deplyment在建立的时候自动加入到Service中,必需要先启动Service,而后再建立Pod,Deployment命令行

建立Service:

[root@node-1 k8s-yaml]# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx


ports上下文:

  port: 80表示服务对外开启的接口能够经过80端口进行访问。这个对外仅限于node节点。

  targetPort: 80表示经过port的端口能够去访问到pod的80端口。

selector上下文:

  app: nginx表示标签,当Pod节点,Deployment的标签为Service相同标签的话,就能够加入到Service中。

图片.png

启动deployment,参考②进行建立,label要一致,启动以后进入pod中查看容器变量,如图,成功加入到Service中

图片.png

④Kubernetes健康检查,在Pod启动后进行检测

livenessProbe
若是检查失败,将杀死容器,根据Pod的restartPolicy来操做。
  readinessProbe
若是检查失败,Kubernetes会把Pod从service endpoints中剔除。
Probe支持如下三种检查方法:
httpGet
  发送HTTP请求,返回200-400范围状态码为成功。
exec
  执行Shell命令返回状态码是0为成功。
tcpSocket
  发起TCP Socket创建成功。


注意:默认的Pod的重启规则restartPolicy为Always


httpGet,访问固定页面,若可以访问则判断改Pod为健康,不然重启,对Pod节点中的index.html文件进行健康监测。

启动该yaml文件,注意,Service服务一直开启着,要不没法访问该Pod。

[root@node-1 k8s-yaml]# cat nginx-health.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-health
  labels:
    app: nginx
spec:
  containers:
  - name: nginx-health
    image: nginx:1.10
  #  volumeMounts:
  #  - name: nginx-mount
  #    mountPath: /tmp/k8s-yaml
    livenessProbe:
      #tcpSocket:
      httpGet:
        path: /index.html
        port: 80
      initialDelaySeconds: 10
      periodSeconds: 5
      timeoutSeconds: 3
    ports:
    - containerPort: 80
  #volumes:
  #- name: nginx-mount
  #  hostPath:
  #    path: /tmp/k8s-yaml


initialDelaySeconds: 10 表示在Pod启动10秒后进行检测。
periodSeconds: 5 表示进行健康监测的频率为5秒1次。
timeoutSeconds: 3 表示健康监测失败后的超时时长。

能够试着将index.html删除,或换一个文件路径,这样使用kubectl describe pod nginx-health就会显示unhealthy


tcpSocket,经过检测80,或其余端口是否正常来判断Pod的健康状态。

[root@node-1 k8s-yaml]# cat nginx-health.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-health
  labels:
    app: nginx
spec:
  containers:
  - name: nginx-health
    image: nginx:1.10
  #  volumeMounts:
  #  - name: nginx-mount
  #    mountPath: /tmp/k8s-yaml
    livenessProbe:
      tcpSocket:
      #httpGet:
        #path: /index.html
        port: 80
      initialDelaySeconds: 10
      periodSeconds: 5
      timeoutSeconds: 3
    ports:
    - containerPort: 80
  #volumes:
  #- name: nginx-mount
  #  hostPath:
  #    path: /tmp/k8s-yaml


exec,经过命令行的形式进行健康状态检测。

[root@node-1 k8s-yaml]# cat nginx-health-exec.yaml apiVersion: v1kind: Podmetadata:  labels:    test: liveness  name: liveness-execspec:  containers:  - name: liveness    image: nginx:1.10    livenessProbe:      exec:        command:        - cat        - /usr/share/nginx/html/index.html      initialDelaySeconds: 10      periodSeconds: 5      timeoutSeconds: 3

相关文章
相关标签/搜索