IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> k8s入门基本操作 -> 正文阅读

[PHP知识库]k8s入门基本操作

1、部署一个Tomcat

kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8


[root@k8s-node1 k8s]# kubectl get all
NAME                           READY   STATUS    RESTARTS   AGE
pod/tomcat6-5f7ccf4cb9-tft59   1/1     Running   0          77m

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   172.96.0.1   <none>        443/TCP   81m

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/tomcat6   1/1     1            1           77m

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/tomcat6-5f7ccf4cb9   1         1         1       77m
[root@k8s-node1 k8s]# kubectl get all -o wide  #可以获取到tomcat信息
NAME                           READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
pod/tomcat6-5f7ccf4cb9-tft59   1/1     Running   0          79m   172.244.2.2   k8s-node3   <none>           <none>

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE   SELECTOR
service/kubernetes   ClusterIP   172.96.0.1   <none>        443/TCP   83m   <none>

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES               SELECTOR
deployment.apps/tomcat6   1/1     1            1           79m   tomcat       tomcat:6.0.53-jre8   app=tomcat6

NAME                                 DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES               SELECTOR
replicaset.apps/tomcat6-5f7ccf4cb9   1         1         1       79m   tomcat       tomcat:6.0.53-jre8   app=tomcat6,pod-template-hash=5f7ccf4cb9
[root@k8s-node1 k8s]# kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
tomcat6-5f7ccf4cb9-tft59   1/1     Running   0          83m
[root@k8s-node1 k8s]# kubectl get pods --all-namespaces
NAMESPACE     NAME                                READY   STATUS    RESTARTS   AGE
default       tomcat6-5f7ccf4cb9-tft59            1/1     Running   0          83m
kube-system   coredns-7f9c544f75-fkxhb            1/1     Running   0          87m
kube-system   coredns-7f9c544f75-j2wfg            1/1     Running   0          87m
kube-system   etcd-k8s-node1                      1/1     Running   0          87m
kube-system   kube-apiserver-k8s-node1            1/1     Running   0          87m
kube-system   kube-controller-manager-k8s-node1   1/1     Running   0          87m
kube-system   kube-flannel-ds-amd64-89kvs         1/1     Running   0          85m
kube-system   kube-flannel-ds-amd64-fh474         1/1     Running   0          86m
kube-system   kube-flannel-ds-amd64-rvscb         1/1     Running   0          87m
kube-system   kube-proxy-h7wfr                    1/1     Running   0          85m
kube-system   kube-proxy-mb4l5                    1/1     Running   0          86m
kube-system   kube-proxy-vsjxb                    1/1     Running   0          87m
kube-system   kube-scheduler-k8s-node1            1/1     Running   0          87m

tomcat在k8s-node3节点

[root@k8s-node3 k8s]# docker images
REPOSITORY                                                       TAG             IMAGE ID       CREATED         SIZE
tomcat                                                           6.0.53-jre8     49ab0583115a   3 years ago     290MB

[root@k8s-node3 k8s]# docker ps
CONTAINER ID   IMAGE                                                            COMMAND                  CREATED             STATUS             PORTS     NAMES
a58c4fc582f6   tomcat                                                           "catalina.sh run"        About an hour ago   Up About an hour             k8s_tomcat_tomcat6-5f7ccf4cb9-tft59_default_dd2d9b08-88f6-494c-b922-161a70c9c38b_0
模拟k8s-node3宕机,会在k8s-node2上启动一个tomcat,容灾恢复
[root@k8s-node1 ~]#  kubectl get pods -o wide

NAME                       READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
tomcat6-5f7ccf4cb9-8nfq5   1/1     Running   0          23m   172.244.1.4   k8s-node2   <none>           <none>

2、暴露nginx访问

kubectl expose deployment tomcat6 --port=80 --target-port=8080 --type=NodePort
service/tomcat6 exposed

Pod的80映射容器的8080;service会代理Pod的80

[root@k8s-node1 ~]# kubectl get service
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   172.96.0.1       <none>        443/TCP        137m
tomcat6      NodePort    172.96.156.221   <none>        80:32759/TCP   22s

[root@k8s-node1 ~]# kubectl get service -o wide
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE    SELECTOR
kubernetes   ClusterIP   172.96.0.1       <none>        443/TCP        139m   <none>
tomcat6      NodePort    172.96.156.221   <none>        80:32759/TCP   98s    app=tomcat6

发现80端口对外暴露了32759端口,浏览器访问http://192.168.54.161:32759/ http://192.168.54.162:32759/ http://192.168.54.163:32759/

均可以访问tomcat主页

3、动态扩容测试

kubectl get deployment

应用升级 kubectl set image (--help 查看帮助)
扩容 kubectl scale --replicas=3 deployment tomcat6
扩容了多份,所有无论访问哪个node的指定端口,都可以访问到tomcat6

?

#在k8s-node2 节点有两个 k8s-node3节点有一个
[root@k8s-node1 ~]# kubectl get pods -o wide
NAME                       READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
tomcat6-5f7ccf4cb9-8nfq5   1/1     Running   0          42m   172.244.1.4   k8s-node2   <none>           <none>
tomcat6-5f7ccf4cb9-t8sd6   1/1     Running   0          27s   172.244.1.5   k8s-node2   <none>           <none>
tomcat6-5f7ccf4cb9-xr56v   1/1     Running   0          27s   172.244.2.5   k8s-node3   <none>           <none>

[root@k8s-node1 ~]# kubectl get service -o wide
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE    SELECTOR
kubernetes   ClusterIP   172.96.0.1       <none>        443/TCP        148m   <none>
tomcat6      NodePort    172.96.156.221   <none>        80:32759/TCP   10m    app=tomcat6
#缩容
kubectl scale --replicas=1 deployment tomcat6
#只有k8s-node3节点有一个
[root@k8s-node1 ~]# kubectl get pods -o wide
NAME                       READY   STATUS    RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
tomcat6-5f7ccf4cb9-xr56v   1/1     Running   0          3m29s   172.244.2.5   k8s-node3   <none>           <none>

删除资源

获取所有资源
[root@k8s-node1 ~]# kubectl get all
NAME                           READY   STATUS    RESTARTS   AGE
pod/tomcat6-5f7ccf4cb9-xr56v   1/1     Running   0          5m59s

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   172.96.0.1       <none>        443/TCP        153m
service/tomcat6      NodePort    172.96.156.221   <none>        80:32759/TCP   15m

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/tomcat6   1/1     1            1           149m

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/tomcat6-5f7ccf4cb9   1         1         1       149m

删除部署信息
[root@k8s-node1 ~]# kubectl delete deployment.apps/tomcat6 
删除service
[root@k8s-node1 ~]# kubectl delete service/tomcat6

查看命令帮助信息 --help

kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8 --help

输出部署命令的yaml配置

kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8 --dry-run -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: tomcat6
  name: tomcat6
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat6
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: tomcat6
    spec:
      containers:
      - image: tomcat:6.0.53-jre8
        name: tomcat
        resources: {}
status: {}

简化部署3分tomcat6,将该配置写在tomcat6.yaml 文件中

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: tomcat6
  name: tomcat6
spec:
  replicas: 3
  selector:
    matchLabels:
      app: tomcat6
  template:
    metadata:
      labels:
        app: tomcat6
    spec:
      containers:
      - image: tomcat:6.0.53-jre8
        name: tomcat

执行,

kubectl apply -f tomcat6.yaml

[root@k8s-master k8s]# kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
tomcat6-5f7ccf4cb9-m2hbz   1/1     Running   0          17s
tomcat6-5f7ccf4cb9-vv248   1/1     Running   0          17s
tomcat6-5f7ccf4cb9-wbhd7   1/1     Running   0          17s
获取pod的完整信息
kubectl get pod tomcat6-5f7ccf4cb9-m2hbz -o yaml

将一下配置写在 mypod.yaml 文件中

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: tomcat6-new
  name: tomcat6-new
  namespace: default
spec:
  containers:
  - image: tomcat:6.0.53-jre8
    imagePullPolicy: IfNotPresent
    name: tomcat6-new
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: nginx
kubectl apply -f mypod.yaml


获取暴露service yaml配置

kubectl expose deployment tomcat6 --port=80 --target-port=8080 --type=NodePort --dry-run -o yaml

?

apiVersion: v1
kind: Service
metadata:
  labels:
    app: tomcat6
  name: tomcat6
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: tomcat6
  type: NodePort
status:
  loadBalancer: {}

将部署与暴露yaml合并 --- 分割

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: tomcat6
  name: tomcat6
spec:
  replicas: 3
  selector:
    matchLabels:
      app: tomcat6
  template:
    metadata:
      labels:
        app: tomcat6
    spec:
      containers:
      - image: tomcat:6.0.53-jre8
        name: tomcat
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: tomcat6
  name: tomcat6
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: tomcat6
  type: NodePort
status:
  loadBalancer: {}

先将以前的部署信息删除

 kubectl delete deployment.apps/tomcat6

重新执行部署与暴露

kubectl apply -f tomcat6-deployment.yaml

  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-09-10 10:38:00  更:2021-09-10 10:38:06 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 0:59:25-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码