资源定义haproxy

资源定义haproxy

重启策略:

Always:当容器终止退出后,总是重启容器,默认策略。

OnFailure:当容器异常退出(退出状态码非0)时,才重启容器。

Never:当容器终止退出,从不重启容器。

健康检查类型:
livenessProbe (存活检查)∶如果检查失败,将杀死容器,根据Pod的restartPolicy来操作。

readinessProbe (就绪检查)︰如果检查失败,Kubernetes会把Podservice endpoints中剔除。

#端口探测
apiVersion: v1
kind: Pod
metadata:
  name: probe-demo
  namespace: demo
spec:
containers:
- name: web
  image: nginx
  ports:
  - containerPort: 8o
  livenessProbe:
    tcpSocket:
      port: 80
    initialDelaySeconds: 30#启动容器后多少秒健康检查
    periodSeconds: 10#以后间隔多少秒检查一次
  readinessProbe:
    tcpSocket:
      port: 80
    initialDelaySeconds: 30
    periodSeconds: 10

示例:执行Shell命令
livenessProbe:
  exec:
    command:
    - cat
    - /tmp/healthy
    
示例:HTTP请求
livenessProbe:httpGet:
  httpGet:
    path: /healthz
    port: 8080
    httpHeaders:
    - name: Custom-Header
      value: Awesome
    

支持的检查方法:

  • httpGet:发送HTTP请求,返回200-400范围状态码为成功。
  • exec:执行Shell命令返回状态码是0为成功。
  • tcpSocket:发起TCP Socket建立成功。

初始化容器

InitContainer:顾名思义,用于初始化工作,执行完就结束,可以理解为一次性任务

  • 支持大部分应用容器配置,但不支持健康检查
  • 优先应用容器执行

应用场景:
环境检查:例如确保应用容器依赖的服务启动后再启动应用容器。初始化配置:例如给应用容器准备配置文件

haproxy

[root@master haproxy]# cat haproxy.yml 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: haproxy
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: haproxy
  template:
    metadata:
      labels:
        app: haproxy
    spec:
      initContainers:
      - name: cp
        volumeMounts:
        - name: haproxy-cfg
          mountPath: /tmp/
      restartPolicy: Always		#健康检查出问题就重启容器
      containers:
      - image: best2001/haproxy:v3.0
        imagePullPolicy: Always
        env: 
        - name: RSs
          value: "10.97.0.10 10.97.0.50"
        name: haproxy
        ports:
        - containerPort: 80
          hostPort: 80
        livenessProbe:	#检查80端口是否存在
          tcpSocket:
            port: 80
      volumes:
      - name: haproxy-cfg
        emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: haproxy
  namespace: default
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: haproxy
  type: NodePort

nginx

[root@master haproxy]# cat nginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx1
  labels:
    app: nginx1
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx1
  template:
    metadata:
      labels:
        app: nginx1 
    spec:
      initContainers:
      - name: in
        command:
        - "wget"
        - "-O"
        - "/usr/local/nginx/html"
        - "http://www.baidu.com"
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - mountPath: "/usr/local/nginx/html"
          name: document-root
      containers:
      - image: best2001/nginx:v0.3 
        imagePullPolicy: Always
        name: nginx1

---
apiVersion: v1
kind: Service
metadata:
  name: nginx1
  labels: 
    app: nginx1
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx1
  clusterIP: 10.97.0.50

apache

[root@master haproxy]# cat apache1.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd1
  labels:
    app: httpd1
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: httpd1
  template:
    metadata:
      labels:
        app: httpd1 
    spec:
      containers:
      - image: best2001/httpd
        imagePullPolicy: Always
        name: httpd1

---
apiVersion: v1
kind: Service
metadata:
  name: httpd1
  labels: 
    app: httpd1
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: httpd1
  clusterIP: 10.97.0.10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值