假设我们不使用镜像仓库的情况下,在A机器上打包镜像在B机器中部署
在本地开发完成后,把应用程序打包成 Docker镜像 ,随后我们将一起一步步将应用部署到k8s集群。
vi Dockerfile
FROM busybox:latest
LABEL wangligang test@email.com>
RUN echo 'hello docker'
docker build -t image_name -f Dockerfile
-t指定镜像名称
-f指定Dockerfile位置
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
image_name latest 1548739e1734 5s ago 20KB
docker save image_name > image_name.tar
#拷贝镜像到B机器
scp image_name.tar root@B:/share/
brew install minikube
minikube start
测试环境下minikube里的内容会因每次重启而丢失,故启用NFS
在MacOs中
vi /etc/exports
写入
/share/dir -network 192.168.0.0 -mask 255.255.0.0 (rw,sync,no_root_squash)
启用nfs
sudo nfsd enable
sudo nfsd start
minikube ssh
su -
mount -t nfs hostname:/share/dir /export/servers
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: demo
name: demo
spec:
replicas: 1
selector:
matchLabels:
app: demo
strategy:
type: Recreate
template:
metadata:
labels:
app: demo
spec:
containers:
- image: image_name
imagePullPolicy: "Never"
name: demo
ports:
- containerPort: 80
resources: {}
restartPolicy: Always
serviceAccountName: ""
status: {}
apiVersion: v1
kind: Service
metadata:
labels:
app: demo-service
name: demo-service
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
app: demo
status:
loadBalancer: {}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: demo-ingress
spec:
rules:
- host: test.demo.com
http:
paths:
- backend:
serviceName: demo-service
servicePort: 80
apiVersion: v1
kind: PersistentVolume
metadata:
name: "demo-data-pv"
labels:
name: demo-data-pv
release: stable
spec:
storageClassName: local-storage
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/export/servers"
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: demo-pvc
namespace: default
spec:
storageClassName: local-storage
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Mi
selector:
matchLabels:
name: demo-data-pv
release: stable
status: {}
docker load --input /share/image_name.tar
kubectl apply -y pv.yaml
kubectl apply -y pvc.yaml
kubectl apply -y deployment.yaml
kubectl apply -y service.yaml
kubectl apply -y ingress.yaml
kubectl get pods
kubectl get deployments
kubectl get services
#查看日志
kubectl logs -f --tail=10 container_namexxxxxx
#登录查看
kubectl exec -it container_namexxxxxx -- /bin/bash
helm create demo-helm
cd demo-helm/template
rm -rf *
cp /share/yamls/deployment.yaml .
cp /share/yamls/service.yaml .
cp /share/yamls/pv.yaml .
cp /share/yamls/pvc.yaml .
cp /share/yamls/ingress.yaml .
清除上一步使用kubectl部署的资源
kubectl delete deployment --all
kubectl delete service --all
kubectl delete ingress --all
kubectl delete pvc --all
kubectl delete pv --all
一键上线
helm install demo-helm demo-helm
如果有一些项目的配置文件需要写入configmap,可以在demo-helm目录下放一个配置文件如conf.properties
在template
下写一个configmap.yaml
如下
apiVersion: v1
kind: ConfigMap
metadata:
name: demo-config
data:
{
{range .Files.Lines "conf.properties"}}
{
{.}}{
{ end }}
应用中就可以使用configmap中的变量了
eval $(minikube docker-env)
invalid type for io.k8s.api.core.v1.ConfigMap.data: got “string”, expected “map”;
使用kubectl create configmap coding-config --from-env-file=coding-config.properties
或者如下方式,注意数字需要用引号引起来
apiVersion: v1
kind: ConfigMap
metadata:
name: coding-config
data:
db_host: 192.168.0.45
db_port: "3306"
db_name: test
使用 sysctl vm.overcommit_memory=1
修复
minikube如果跑在docker上是不支持ingress,需要指定: minikube start --vm=true --driver=hyperkit --memory=4g
无法启用ingress,是因为默认minikube不支持,使用minikube addons enable ingress
开启
远程访问mysql时开启访问别忘记指定密码grant all on *.* to 'root'@'%' identified by '123456';