Kubernetes 资源定义

Kubernetes 资源定义

1. 滚动更新

//使用dockerfile创建两个版本的镜像
[root@master ~]# mkdir test 
[root@master ~]# cd test/
[root@master test]# ls
[root@master test]# vim Dockerfile 
[root@master test]# cat Dockerfile 
FROM busybox 

RUN mkdir /data &&  echo "11111" > /data/index.html 

ENTRYPOINT ["/bin/httpd","-f","-h","/data"]

//创建第一个
[root@master test]# docker build -t linlusama/httpd:v1 test/ 
unable to prepare context: path "test/" not found
[root@master test]# docker build -t linlusama/httpd:v1 /root/test/ 
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM busybox
latest: Pulling from library/busybox
3cb635b06aa2: Pull complete 
Digest: sha256:b5cfd4befc119a590ca1a81d6bb0fa1fb19f1fbebd0397f25fae164abe1e8a6a
Status: Downloaded newer image for busybox:latest
 ---> ffe9d497c324
Step 2/3 : RUN mkdir /data &&  echo "11111" > /data/index.html
 ---> Running in 1dee83f53069
Removing intermediate container 1dee83f53069
 ---> 0f1556007794
Step 3/3 : ENTRYPOINT ["/bin/httpd","-f","-h","/data"]
 ---> Running in 3de83f7e61fa
Removing intermediate container 3de83f7e61fa
 ---> e612254e7bf6
Successfully built e612254e7bf6
Successfully tagged linlusama/httpd:v1

//创建第二个
[root@master test]# cat Dockerfile 
FROM busybox 

RUN mkdir /data &&  echo "222222" > /data/index.html 

ENTRYPOINT ["/bin/httpd","-f","-h","/data"]

[root@master test]# docker build -t linlusama/httpd:v2 /root/test/ 
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM busybox
 ---> ffe9d497c324
Step 2/3 : RUN mkdir /data &&  echo "222222" > /data/index.html
 ---> Running in 545536c92203
Removing intermediate container 545536c92203
 ---> d538c951e46e
Step 3/3 : ENTRYPOINT ["/bin/httpd","-f","-h","/data"]
 ---> Running in 56c730a2be47
Removing intermediate container 56c730a2be47
 ---> 68cd9bf398ff
Successfully built 68cd9bf398ff
Successfully tagged linlusama/httpd:v2

//传镜像入仓库
[root@master test]# docker login 
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: linlusama
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

[root@master test]# docker push linlusama/httpd:v1
The push refers to repository [docker.io/linlusama/httpd]
554178406fd2: Pushed 
64cac9eaf0da: Mounted from library/busybox 
v1: digest: sha256:91467a9fedd92985bcab6b9a435389719697416a4eccd8aee89786b39c1cf911 size: 734
[root@master test]# docker push linlusama/httpd:v2
The push refers to repository [docker.io/linlusama/httpd]
2d3a672061e5: Pushed 
64cac9eaf0da: Layer already exists 
v2: digest: sha256:4821f788d4203c9034c90475117290b9665b7b869550484ec52c19f2193b57a4 size: 734

[root@master test]# docker push linlusama/httpd:v2
The push refers to repository [docker.io/linlusama/httpd]
2d3a672061e5: Layer already exists 
64cac9eaf0da: Layer already exists 
v2: digest: sha256:4821f788d4203c9034c90475117290b9665b7b869550484ec52c19f2193b57a4 size: 734

//使用v1版本 创建3个容器
[root@master test]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
httpd-567687c96d-2pwlc   1/1     Running   0          3m23s
httpd-567687c96d-mxwl9   1/1     Running   0          3m23s
httpd-567687c96d-sl8n2   1/1     Running   0          3m23s

//暴露端口
[root@master test]# kubectl expose deploy httpd --port 80 --target-port 80 
service/httpd exposed
[root@master test]# kubectl get pods,svc 
NAME                         READY   STATUS    RESTARTS   AGE
pod/httpd-567687c96d-2pwlc   1/1     Running   0          4m24s
pod/httpd-567687c96d-mxwl9   1/1     Running   0          4m24s
pod/httpd-567687c96d-sl8n2   1/1     Running   0          4m24s

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/httpd        ClusterIP   10.96.252.108    <none>        80/TCP         5s
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        3d3h
service/nginx        NodePort    10.109.148.173   <none>        80:30362/TCP   27h

//访问
[root@master test]# while :;do curl 10.96.252.108:80;done 
11111
11111
11111
11111
11111
11111
11111
11111
11111
11111
......

//版本升级
[root@master test]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
httpd-577df9d7c7-fd6cw   1/1     Running   0          4m34s
httpd-577df9d7c7-jmjst   1/1     Running   0          3m54s
httpd-577df9d7c7-xpdrt   1/1     Running   0          3m12s

//访问
[root@master test]# while :;do curl 10.96.252.108:80;done 
222222
222222
222222
222222
222222
222222
222222
222222
222222
......

2. 回滚

//回滚到上个版本
[root@master test]# kubectl rollout undo deploy/httpd 
deployment.apps/httpd rolled back
[root@master test]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
httpd-567687c96d-5jvjh   1/1     Running   0          2m50s
httpd-567687c96d-gcbpz   1/1     Running   0          2m51s
httpd-567687c96d-gd6sd   1/1     Running   0          2m49s

//访问
[root@master test]# while :;do curl 10.96.252.108:80;done 
11111
11111
11111
11111
11111
11111
11111
11111
11111
11111
....

3. 使用yml文件创建,删除容器

内部命令:

命令作用
apiVersionAPI版本
Kind资源类型
metadata资源元数据
spec资源规格
replicas副本数量
selector标签选择器
templatePod模板
metadataPod元数据
specPod规格
containers容器配置
Pod容器的字段拼写

kubectl + explain + 类型 + 对象命令
kubectl + explain + 类型 + 对象命令 + 对象命令分类

// 查看 metadata使用方式
[root@master ~]# kubectl explain deployment.metadata

// 查看 metadata对象的namespace的使用方式
[root@master ~]# kubectl explain deployment.metadata.namespace

// 列表对象开头必须添加 "-" 
<[]Object>

将你需要创建的资源描述到YAML文件中

部署:kubectl apply -f xxx.yaml

卸载:kubectl delete -f xxx.yaml

实例

[root@master ~]# cd /opt/
[root@master opt]# mkdir manifes 
[root@master opt]# cd manifes

//编写deployment类型的yml文件
[root@master manifes]# cat deploy.yml 
---
apiVersion: apps/v1
kind: Deployment 
metadata: 
  name: web 
  namespace: default 
spec: 
  replicas: 3 
  selector: 
    matchLabels: 
      app: amu 
  template: 
    metadata: 
      labels: 
        app: amu 
    spec: 
      containers: 
      - image: linlusama/httpd:v1 
        imagePullPolicy: IfNotPresent 
        name: httpd 


    
[root@master manifes]# cat svc-deploy.yml 
apiVersion: v1 
kind: Service 
metadata: 
  name: web 
  namespace: default 
spec: 
  ports: 
  - port: 8001 
    protocol: TCP 
    targetPort: 80 
  selector: 
    app: amu 
  type: NodePort

//创建容器
[root@master manifes]# kubectl get pods,svc 
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   3d3h

[root@master manifes]# kubectl create -f deploy.yml
deployment.apps/web created
[root@master manifes]# kubectl expose -f svc-deploy.yml
service/web exposed

[root@master manifes]# kubectl get pods,svc 
NAME                       READY   STATUS    RESTARTS   AGE
pod/web-7b965bb7b9-4lgwz   1/1     Running   0          2m24s
pod/web-7b965bb7b9-l76sn   1/1     Running   0          2m24s
pod/web-7b965bb7b9-lktp4   1/1     Running   0          2m24s

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
service/kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP    3d4h
service/web          ClusterIP   10.99.86.224   <none>        8001:32468/TCP   4s

//访问
[root@master manifes]# curl 10.99.86.224:8001 
11111

合并

//合并文件
[root@master manifes]# cat deploy.yml
---
apiVersion: apps/v1
kind: Deployment 
metadata: 
  name: web 
  namespace: default 
spec: 
  replicas: 3 
  selector: 
    matchLabels: 
      app: amu 
  template: 
    metadata: 
      labels: 
        app: amu 
    spec: 
      containers: 
      - image: linlusama/httpd:v1 
        imagePullPolicy: IfNotPresent 
        name: httpd 

---
apiversion: v1
kind: Service
metadata: 
  name: web
  namespace: default
spec:
  ports: 
  - port: 8001
    protocol: TCP					           targetPort: 80
  selector:
    app: amu
  type: NodePort

[root@master manifes]# kubectl apply -f  deploy.yml
deployment.apps/web unchanged
service/web created

[root@master manifes]# kubectl get pods,svc 
NAME                       READY   STATUS    RESTARTS   AGE
pod/web-7b965bb7b9-rckvq   1/1     Running   0          119s
pod/web-7b965bb7b9-whbd7   1/1     Running   0          119s
pod/web-7b965bb7b9-zqjcz   1/1     Running   0          119s

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP          3d4h
service/web          NodePort    10.103.177.184   <none>        8001:31594/TCP   13s

//访问
[root@master manifes]# curl http://10.103.177.184:8001
11111

//更新版本
......
spec:
      containers:
      - image: linlusama/httpd:v2  //修改镜像
        imagePullPolicy: IfNotPresent
        name: httpd
.....

//创建容器
[root@master manifes]# kubectl apply -f  deploy.yml
deployment.apps/web created
service/web created

[root@master manifes]# kubectl get pods,svc 
NAME                       READY   STATUS    RESTARTS   AGE
pod/web-857947b7bd-jqgr8   1/1     Running   0          32s
pod/web-857947b7bd-lqmhn   1/1     Running   0          32s
pod/web-857947b7bd-z8bz2   1/1     Running   0          32s

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP          3d4h
service/web          NodePort    10.111.139.137   <none>        8001:32592/TCP   32s

//访问
[root@master manifes]# curl http://10.111.139.137:8001
222222

4. 命名空间

Kubernetes 支持多个虚拟集群,它们底层依赖于同一个物理集群。 这些虚拟集群被称为命名空间。命名空间为名称提供了一个范围。资源的名称需要在命名空间内是唯一的,但不能跨命名空间。且命名空间不能相互嵌套。

[root@master ~]# kubectl create namespace mysapce     //创建命名空间
namespace/mysapce created

[root@master ~]# kubectl get namespaces               //查看命令空间
NAME              STATUS   AGE
default           Active   2d8h
kube-node-lease   Active   2d8h
kube-public       Active   2d8h
kube-system       Active   2d8h
mysapce           Active   16s
[root@master ~]# kubectl delete namespaces mysapce    //删除命名空间
namespace "mysapce" deleted

[root@master ~]# kubectl get namespaces 
NAME              STATUS   AGE
default           Active   2d8h
kube-node-lease   Active   2d8h
kube-public       Active   2d8h
kube-system       Active   2d8h


# 当然也可以通过yaml 文件进行创建
[root@master ~]# vi my-namespace.yaml 
apiVersion: v1
kind: Namespace
metadata: 
  name: red-test
  labels:
    name: red-test-v1

[root@master ~]# kubectl create -f my-namespace.yaml   //使用kubectl create -f 指定文件镜像创建
namespace/red-test created

[root@master ~]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   2d8h
kube-node-lease   Active   2d8h
kube-public       Active   2d8h
kube-system       Active   2d8h
red-test          Active   14s

5. haproxy负载均衡nginx

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

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

//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: linlusama/haproxy:latest
        imagePullPolicy: Always
        env: 
        - name: RS
          value: "10.96.57.142 10.111.156.39"
        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


//RS1文件
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rs1
  labels:
    app: rs1
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rs1
  template:
    metadata:
      labels:
        app: rs1
    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: linlusama/centos-nginx:v1.20.1
        imagePullPolicy: Always
        name: rs1

---
apiVersion: v1
kind: Service
metadata:
  name: rs1
  labels: 
    app: rs1
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx
  clusterIP: 10.96.57.142


//RS2文件
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rs2
  labels:
    app: rs2
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rs2
  template:
    metadata:
      labels:
        app: rs2
    spec:
      containers:
      - image: linlusama/centos-apache:v1
        imagePullPolicy: Always
        name: rs2

---
apiVersion: v1
kind: Service
metadata:
  name: rs2
  labels: 
    app: rs2
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: httpd1
  clusterIP: 10.111.156.39


//创建
[root@master manifes]# kubectl apply -f  haproxy.yml 
deployment.apps/rs1 created
deployment.apps/rs2 created
deployment.apps/haproxy unchanged
service/rs1 created
service/rs2 created
service/haproxy unchanged

[root@master manifes]# kubectl get pods,svc 
NAME                           READY   STATUS              RESTARTS   AGE
pod/haproxy-694cfb4785-5zr2x   0/1     Completed           4          2m48s
pod/rs1-6c9bc5db4c-z5ljd       0/1     ContainerCreating   0          63s
pod/rs2-8ffd8d4f4-678rp        1/1     Running             0          63s
pod/web-857947b7bd-jqgr8       1/1     Running             0          27m
pod/web-857947b7bd-lqmhn       1/1     Running             0          27m
pod/web-857947b7bd-z8bz2       1/1     Running             0          27m

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
service/haproxy      NodePort    10.98.127.133    <none>        80:30504/TCP     2m48s
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP          3d4h
service/rs1          NodePort    10.96.57.142     <none>        80/TCP   63s
service/rs2          NodePort    10.111.156.39    <none>        80/TCP   63s

//访问
[root@master manifes]# curl 10.98.127.133:80
<html><body><h1>It works!</h1></body></html>

[root@master manifes]# curl 10.98.127.133:80
我是徐猛他爹
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值