Kubernetes资源定义

Kubernetes资源定义

资源定义

重启策略:

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的pod进行负载均衡

nginx

#yml文件
[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:
      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
  
#创建
[root@master haproxy]# kubectl create -f nginx.yml 
deployment.apps/nginx1 created
service/nginx1 created

#查看
[root@master haproxy]# kubectl get pod,svc
NAME                          READY   STATUS    RESTARTS   AGE
pod/nginx1-7cf8bc594f-t5btg   1/1     Running   0          26s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   74m
service/nginx1       ClusterIP   10.97.0.50   <none>        80/TCP    26s

apache

#yml文件
[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
  
#创建
[root@master haproxy]# kubectl create -f apache1.yml 
deployment.apps/httpd1 created
service/httpd1 created

#查看
[root@master haproxy]# kubectl get pod,svc
NAME                          READY   STATUS    RESTARTS   AGE
pod/httpd1-57c7b6f7cb-sk86h   1/1     Running   0          28s
pod/nginx1-7cf8bc594f-t5btg   1/1     Running   0          112s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/httpd1       ClusterIP   10.97.0.10   <none>        80/TCP    28s
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   75m
service/nginx1       ClusterIP   10.97.0.50   <none>        80/TCP    112s

haproxy

#yml文件
[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:
      containers:
      - image: 93quan/haproxy:v1-alpine
        imagePullPolicy: Always
        env: 
        - name: RSIP
          value: "10.97.0.10 10.97.0.50"
        name: haproxy
        ports:
        - containerPort: 80
          hostPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: haproxy
  namespace: default
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: haproxy
  type: NodePort
  
#创建
[root@master haproxy]# kubectl create -f haproxy.yml 
deployment.apps/haproxy created
service/haproxy created

#查看是否创建成功
[root@master haproxy]# kubectl get pod,svc
NAME                           READY   STATUS    RESTARTS   AGE
pod/haproxy-7565dc6587-h8sdg   1/1     Running   0          18s
pod/httpd1-57c7b6f7cb-sk86h    1/1     Running   0          81s
pod/nginx1-7cf8bc594f-t5btg    1/1     Running   0          2m45s

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/haproxy      NodePort    10.99.52.161   <none>        80:31884/TCP   18s
service/httpd1       ClusterIP   10.97.0.10     <none>        80/TCP         81s
service/kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        76m
service/nginx1       ClusterIP   10.97.0.50     <none>        80/TCP         2m45s

访问测试

[root@master haproxy]# curl 192.168.240.30:31884
<html><body><h1>It works!</h1></body></html>

[root@master haproxy]# curl 192.168.240.30:31884
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枯木逢秋࿐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值