前言
在已有的集群环境下进行实际应用部署
期间会用到的部分基本命令
kubectl run
kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...]
如启动nginx实例:
kubectl run nginx --image=nginx
附带端口:
kubectl run nginx --image=nginx --port=18080
kubectl run testapp --image=ccr.ccs.tencentyun.com/k8s-tutorial/test-k8s:v1
创建pod
kubetcl apply -f testpod.yaml
创建成功如下
创建deployment
pod常用命令
部署应用
kubetcl apply -f pod.yaml
查看全部
kubectl get all
查看deployment pod
kubectl get deployment
kubectl get pod -o wide
查看所有&指定namespace下的pod
kubectl get pods --all-namespaces
kubectl get pod -n default
查看describe
kubectl describe pod podname
kubectl describe pod tomcat-deployment-2212675085-g5rkj
查看pod log
kubectl logs tomcat-deployment-2212675085-g5rkj
进入pod
kubectl exec -it tomcat-deployment-2212675085-g5rkj bash
复制文件
kubectl cp pod-name:容器内文件路径 /root/主机目录文件 -n namespace
kubectl -n default cp /root/1.war tomcat-deployment-2212675085-g5rkj:/usr/local/tomcat/1.war
pod端口映射
kubectl port-forward tomcat-deployment-2212675085-g5rkj 8090:8081
删除deployment pod
kubectl delete deployment tomcat-deployment
kubectl delete pod tomcat-deployment-2212675085-4z1c2
强制删除
kubectl delete pod tomcat-deployment-2212675085-4z1c2 --grace-period=0 --force
实际部署
此处以tomcat为案例
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: tomcat-deployment
labels:
app: tomcat
spec:
replicas: 2
selector:
matchLabels:
app: tomcat
template:
metadata:
labels:
app: tomcat
spec:
containers:
- name: tomcat
image: tomcat:latest
ports:
- containerPort: 8080
编写完后
kubectl apply -f tomcat.yaml
确认状态
kubectl get deployments
kubectl get pods --show-labels
状态等待变成 running即可
添加外部访问方式的服务
用上面提及的进容器内的方式,对相关镜像拉下来的tomcat文件进行修改即可,改下web目录就行
最终效果如下