手写yaml文件创建k8s的deployment和service

编写deployment和service的yaml文件,在kubernates集群中添加服务。

前置条件

1、docker镜像已存在,名为mydemoapp,tag为0.0.1。如果不清楚如何制作自己的docker镜像,可以参考《构建一个Java应用的Docker镜像》
2、k8s集群已搭建成功。
3、应用:端口为999,有个可访问的接口名称为hello

下面就可以开始手写yaml文件 ,向k8s集群中添加自己的服务了。

开始手写yaml文件
# mydemoapp-0.0.1.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mydemoapp
  namespace: default
  labels:
    app: mydemoapp
spec:
  replicas: 2
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  selector:
    matchLabels:
      app: mydemoapp
  template:
    metadata:
      labels:
        app: mydemoapp
    spec:
      containers:
      - name: mydemoapp
        image: mydemoapp:0.0.1
        ports:
        - containerPort: 999
          protocol: TCP
        imagePullPolicy: IfNotPresent
      hostAliases:
      - hostnames:
        - yourDns_mysql_hostName
        ip: 192.168.99.99
      restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: mydemoapp
  namespace: default
  labels:
    app: mydemoapp
spec:
  ports:
  - port: 80
    targetPort: 999
    nodePort: 30001
    protocol: TCP
  selector:
    app: mydemoapp
  type: NodePort
创建k8s服务
[root@dev ~]# kubectl create -f mydemoapp-0.0.1.yaml
deployment.apps/mydemoapp created
service/mydemoapp created
查看k8s状态
[root@dev ~]# kubectl get all
NAME                             READY   STATUS    RESTARTS   AGE
pod/mydemoapp-86c57db685-fpn4g   1/1     Running   0          43s

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
service/mydemoapp    NodePort    10.96.0.1      <none>        80:30001/TCP    43s

NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mydemoapp    1/1     1            1           43s

NAME                        DESIRED   CURRENT   READY   AGE
replicaset.apps/mydemoapp   1         1         1       43s
访问应用
curl http://<yourNodeIp>:30001/hello
Logo

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

更多推荐