利用kubernetes部署docker的镜像文件

系列文章

1. mac上安装docker并运行kubernetes

2. Docker上部署并运行SpringBoot项目

3. 利用kubernetes部署docker的镜像文件

4. Kubernetes之secrets使用

1.准备好docker的镜像文件,比如test-demo1:v1.0

2. 编写部署的deployment.yaml的文件内容

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test123
  labels:
    app: test123
spec:
  replicas: 1
  template:
    metadata:
      name: test123
      labels:
        app: test123
    spec:
      containers:
        - name: test123
          image: test-demo1:v1.0
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8080
          resources: 
            requests: 
              cpu: 100m
              memory: 128Mi
            limits:
              cpu: 250m
              memory: 256Mi
      restartPolicy: Always
  selector:
    matchLabels:
      app: test123
---
apiVersion: v1
kind: Service
metadata:
  name: test123-service
  labels:
    app: test123-service
spec:
  selector:
    app: test123
  ports:
    - port: 8087
      targetPort: 8089
      nodePort: 30082
  type: NodePort

3.  运行下面命令来部署

kubectl apply -f deployment.yml

4. 查看部署的服务

#kubectl get services
NAME              TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes        ClusterIP   10.96.0.1        <none>        443/TCP          42d
test123-service   NodePort    10.106.109.213   <none>        8087:30082/TCP   33d

5. 查看test123-service

6. 在kubernetes dashboard里查看test123-service

7. 浏览器访问来测试

其它

1. port,targetport, nodeport 的区别

  • port 端口:端口是使服务对同一K8群集中运行的其他服务可见的端口号。换句话说,如果服务想要调用在同一Kubernetes集群中运行的另一个服务,它将能够使用服务规范文件中针对“port”指定的端口来执行此操作。
  • targetPort:目标端口是POD上运行服务的端口。
  • nodePort:节点端口是使用Kube-Proxy从外部用户访问服务的端口。
  • Port - exposes the Kubernetes service on the specified port within the cluster. Other pods within the cluster can communicate with this server on the specified port.
  • TargetPort - is the port on which the service will send requests to, that your pod will be listening on. Your application in the container will need to be listening on this port also.
  • NodePort - exposes a service externally to the cluster by means of the target nodes IP address and the NodePort. NodePort is the default setting if the port field is not specified.

Kubernetes 的ipTables 维护 nodePort 与 targetPort 的映射。K8s Kube-Proxy使用ipTables来解析特定nodePort上的请求,并将它们重定向到适当的pod。

2. kubernetes进入pod

进入docker容器 :

docker exec -ti  <your-container-name>   /bin/sh

进入Kubernetes的pod:

kubectl exec -ti <your-pod-name>  -n <your-namespace>  -- /bin/sh

kubectl exec - Execute a command against a container in a pod.

// Get output from running 'date' from pod <pod-name>. By default, output is from the first container.
$ kubectl exec <pod-name> date

// Get output from running 'date' in container <container-name> of pod <pod-name>.
$ kubectl exec <pod-name> -c <container-name> date

// Get an interactive TTY and run /bin/bash from pod <pod-name>. By default, output is from the first container.
$ kubectl exec -ti <pod-name> /bin/bash

What is inside a Docker image? | Bartosz Mikulski

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

茫茫人海一粒沙

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

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

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

打赏作者

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

抵扣说明:

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

余额充值