系列文章
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