1.确保k8s集群已经安装完成(我的k8s集群为3台机器)
2.编写Deployment,代码如下:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment
  labels:
    app: tomcat
spec:
  replicas: 3
  selector:
    matchLabels:
      app: tomcat
  template:
    metadata:
      labels:
        app: tomcat
    spec:
      containers:
      - name: tomcat
        image: tomcat:latest
        ports:
        - containerPort: 8080

具体Deployment编写细节请参考以下地址:
https://kubernetes.io/zh/docs/concepts/workloads/controllers/deployment/

[root@master01 tomcat]# kubectl apply -f tomcat.yaml
deployment.apps/nginx-deployment created

查看当前Deploment创建状态

[root@master01 tomcat]# kubectl get deployments
NAME                READY   UP-TO-DATE   AVAILABLE   AGE
tomcat-deployment   2/3     3            2           2m41s
[root@master01 tomcat]# kubectl get pods --show-labels
NAME                                 READY   STATUS              RESTARTS   AGE     LABELS
tomcat-deployment-77cb595c87-cs79r   0/1     ContainerCreating   0          3m24s   app=tomcat,pod-template-hash=77cb595c87
tomcat-deployment-77cb595c87-tkdfz   1/1     Running             0          3m24s   app=tomcat,pod-template-hash=77cb595c87
tomcat-deployment-77cb595c87-w8sjr   1/1     Running             0          3m24s   app=tomcat,pod-template-hash=77cb595c87

还有个副本正在创建中,耐心等待

[root@master01 tomcat]# kubectl describe deployments
Name:                   tomcat-deployment
Namespace:              default
CreationTimestamp:      Sat, 22 May 2021 17:54:29 -0700
Labels:                 app=tomcat
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=tomcat
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=tomcat
  Containers:
   tomcat:
    Image:        tomcat:latest
    Port:         8080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   tomcat-deployment-77cb595c87 (3/3 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  4m26s  deployment-controller  Scaled up replica set tomcat-deployment-77cb595c87 to 3

已经完成了

[root@master01 tomcat]# kubectl get deployments
NAME                READY   UP-TO-DATE   AVAILABLE   AGE
tomcat-deployment   3/3     3            3           4m43s
[root@master01 tomcat]# kubectl get pods -o wide
NAME                                 READY   STATUS    RESTARTS   AGE     IP               NODE                NOMINATED NODE   READINESS GATES
tomcat-deployment-77cb595c87-cs79r   1/1     Running   0          7m36s   10.122.226.196   master03.paas.com   <none>           <none>
tomcat-deployment-77cb595c87-tkdfz   1/1     Running   0          7m36s   10.122.156.195   master02.paas.com   <none>           <none>
tomcat-deployment-77cb595c87-w8sjr   1/1     Running   0          7m36s   10.122.156.196   master02.paas.com   <none>           <none>

可以运行查看状态

[root@master01 tomcat]# curl 10.122.226.196:8080
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.46</h3></body></html>

已经正常部署了

3.创建service提供外部访问
[root@master01 tomcat]# vim tomcat-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: tomcat-service
spec:
  type: NodePort
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 30080
  selector:
    app: tomcat
[root@master01 tomcat]# kubectl apply -f tomcat-service.yaml
service/tomcat-service created
[root@master01 tomcat]# kubectl get service
NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes       ClusterIP   10.10.0.1       <none>        443/TCP          16h
tomcat-service   NodePort    10.10.122.207   <none>        8080:30080/TCP   30s

30080端口就是提供外部访问的端口

img

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐