欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

k8s学习(三) k8s集群内部署一个docker应用

程序员文章站 2022-03-12 10:19:42
...

1、获取一个springboot 应用jar包

2、将jar包打成docker镜像

Dockerfile如下:

FROM java:8-alpine
ADD docker-hello-world-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar", "/app.jar"]

打镜像:

docker build -t hellword:0.0.4 .

3、准备docker 镜像私服
参考https://blog.csdn.net/u011943534/article/details/81331231

4、配置私服地址

编辑/etc/docker/daemon.json,添加nexus仓库地址

{ 
	"exec-opts": ["native.cgroupdriver=systemd"],
	"insecure-registries":["192.168.100.89:80", "172.16.10.190:8008", "quay.io", "k8s.gcr.io", "gcr.io"] 
}

重启docker

systemctl restart docker

5、将镜像推送到远程私服

docker tag helloworld:0.0.4 172.16.10.190:8008/helloworld:0.0.4

登陆nexus

docker login --username=admin 172.16.10.190:8008
输入密码登陆
docker push 172.16.10.190:8008/helloworld:0.0.4

6、配置secret拉取私有镜像仓库内容

kubectl create secret docker-registry myregistrykey --docker-server=172.16.10.190:8008 --docker-username=admin --docker-password=admin123 [email protected]
获取创建的secret
kubectl get secrets

NAME                                     TYPE                                  DATA   AGE
cluster-admin-dashboard-sa-token-hld8l   kubernetes.io/service-account-token   3      3d19h
default-token-7c9dp                      kubernetes.io/service-account-token   3      4d
myregistrykey                            kubernetes.io/dockerconfigjson        1      3d16h
myregistrykey2                           kubernetes.io/dockerconfigjson        1      3d16h
myregistrykey3                           kubernetes.io/dockerconfigjson        1      3d16h
myregistrykey4                           kubernetes.io/dockerconfigjson        1      2d22h
myregistrykey5                           kubernetes.io/dockerconfigjson        1      2d22h
myregistrykey6                           kubernetes.io/dockerconfigjson        1 

7、创建deployment.yaml
imagePullSecrets处配置上一步获得的secret

---
 
apiVersion: extensions/v1beta1
 
kind: Deployment
 
metadata:
 
  name: kube-node
 
spec:
 
  replicas: 2
 
  template:
 
    metadata:
 
      labels:
 
        app: web
 
    spec:
 
      containers:
 
        - name: kube-node-demo-instance
 
          image: kube-node-demo1
 
          ports:
 
            - containerPort: 8081
      imagePullSecrets:
       - name: myregistrykey

8、部署

kubectl create -f deployment.yaml

9、查看部署状态
如果状态为running部署成功

[[email protected] k8s]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
kube-node-87dcfc9cf-9kj8j   1/1     Running   0          25m
kube-node-87dcfc9cf-l2ppz   1/1     Running   0          25m
  

10、测试应用

[[email protected] k8s]# kubectl get pods -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
kube-node-87dcfc9cf-9kj8j   1/1     Running   0          26m   10.244.1.23   k8s-node3   <none>           <none>
kube-node-87dcfc9cf-l2ppz   1/1     Running   0          26m   10.244.2.24   k8s-node2   <none>           <none>


[[email protected] k8s]# curl 10.244.2.24:8080/index
hello,world

11、如果要删除应用,可以删除deployment

kubectl delete deployment/kube-node