前言:自定义项目参考【Docker】Dockerfile构建自定义进阶的helloworld镜像-2【Docker】将自定义的镜像上传至dockerhub或阿里云私有仓库,并在其他节点进行拉取

0 将集群启动

# 关闭防火墙
root@master:/home/hqc/k8s_test/array-plus# ufw disable
	防火墙在系统启动时自动禁用

# 禁用分区
root@master:/home/hqc/k8s_test/array-plus# swapoff -a

# 查看状态
root@master:/home/hqc/k8s_test/array-plus# kubectl get nodes
	NAME     STATUS     ROLES    AGE   VERSION
	master   Ready      master   25d   v1.18.0
	node01   Ready      <none>   20d   v1.18.0

1 yaml文件创建deployment

尝试自动创建:报错

root@master:/home/hqc/k8s_test/array-plus# kubectl create deployment array-plus-deployment --image=registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-array-plus:v2.5  --dry-run -o yml > array-plus-deployment.ymal
	W1213 20:50:23.489941    1289 helpers.go:535] --dry-run is deprecated and can be replaced with --dry-run=client.
	error: unable to match a printer suitable for the output format "yml", allowed formats are: go-template,go-template-file,json,jsonpath,jsonpath-file,name,template,templatefile,yaml
# 自动创建的方法不对,启用

直接创建:

# 编写
root@master:/home/hqc/k8s_test/array-plus# vim array-plus-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: array-plus-deployment
  name: array-plus-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: array-plus-deployment
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: array-plus-deployment
    spec:
      containers:
      - image: registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-array-plus:v2.5
        name: ali-array-plus
        resources: {}
        ports:
        - containerPort: 5000
        imagePullPolicy: IfNotPresent
status: {}

# 创建
root@master:/home/hqc/k8s_test/array-plus# kubectl create -f array-plus-deployment.yaml 
deployment.apps/array-plus-deployment created
# 成功

2 yaml文件创建service

# 编写
root@master:/home/hqc/k8s_test/array-plus# vim array-plus-service.yaml

apiVersion: v1 # 注意此处不能和deployment一样为‘apps/v1’
kind: Service
metadata:
  name: array-plus-deployment
  labels:
    app: array-plus-deployment
spec:
  ports:
  - port: 80
    targetPort: 5000
    nodePort: 30000
    protocol: TCP
  selector:
    app: array-plus-deployment
  type: NodePort

# 创建
root@master:/home/hqc/k8s_test/array-plus# kubectl create -f array-plus-service.yaml 
	service/array-plus-deployment created
# 成功

3 查看

root@master:/home/hqc/k8s_test/array-plus# kubectl get all
	NAME                                         READY   STATUS         RESTARTS   AGE
	pod/array-plus-deployment-55657b7c55-wfjtx   0/1     ErrImagePull   0          6m33s
	pod/array-plus-deployment-55657b7c55-z8kx2   0/1     ErrImagePull   0          6m33s
	pod/helloworld-deployment-79cbf4dcbb-5x6b2   1/1     Running        1          12d
	pod/helloworld-deployment-79cbf4dcbb-n5sps   1/1     Running        1          12d
	pod/helloworld-deployment-79cbf4dcbb-sr8xf   1/1     Running        1          12d
	pod/helloworld-deployment-79cbf4dcbb-wfh5w   1/1     Terminating    0          12d
	pod/nginx-f89759699-ksgcb                    1/1     Running        2          13d
	
	NAME                            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
	service/array-plus-deployment   NodePort    10.102.249.251   <none>        80:30000/TCP   93s
	service/helloworld-deployment   NodePort    10.106.97.144    <none>        80:30002/TCP   13d
	service/kubernetes              ClusterIP   10.96.0.1        <none>        443/TCP        25d
	service/nginx                   NodePort    10.109.178.122   <none>        80:30650/TCP   13d
	
	NAME                                    READY   UP-TO-DATE   AVAILABLE   AGE
	deployment.apps/array-plus-deployment   0/2     2            0           6m33s
	deployment.apps/helloworld-deployment   3/3     3            3           12d
	deployment.apps/nginx                   1/1     1            1           13d
	
	NAME                                               DESIRED   CURRENT   READY   AGE
	replicaset.apps/array-plus-deployment-55657b7c55   2         2         0       6m33s
	replicaset.apps/helloworld-deployment-79cbf4dcbb   3         3         3       12d
	replicaset.apps/nginx-f89759699                    1         1         1       13d
# 发现并未成功

4 问题解决

结合之前的实践经验,问题应该出在节点上没法实时进行拉取,需要在各节点上事先拉取好才行。

# 登录镜像仓库
root@nodroot@node01:/hroot@nrootrootrorororoot@node01:/home/user# docker login --username=errorhqc兮 registry.cn-beijing.aliyuncs.com
	Password: 
	WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
	Configure a credential helper to remove this warning. See
	https://docs.docker.com/engine/reference/commandline/login/#credentials-store
	
	Login Succeeded

# node本地拉取镜像
root@node01:/home/user# docker pull registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-array-plus:v2.5
	v2.5: Pulling from hqc-k8s/ali-array-plus
	647acf3d48c2: Already exists 
	b02967ef0034: Already exists 
	e1ad2231829e: Already exists 
	5576ce26bf1d: Already exists 
	a66b7f31b095: Already exists 
	05189b5b2762: Already exists 
	af08e8fda0d6: Already exists 
	287d56f7527b: Already exists 
	dc0580965fb6: Already exists 
	1cb8e8d308bd: Pull complete 
	c68499244bc4: Pull complete 
	1dafd85198a4: Pull complete 
	Digest: sha256:1dd85ae174eeedbaa5d370e625c6db439c4188634e58d648a6b5d30e28b9aa86
	Status: Downloaded newer image for registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-array-plus:v2.5
	registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-array-plus:v2.5

# 查看镜像
root@node01:/home/user# docker images
	REPOSITORY                                                       TAG       IMAGE ID       CREATED         SIZE
	registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-array-plus          v2.5      7f4f56bbf3c8   2 hours ago     928MB

master中再次查看:发现包括service全部正常

root@master:/home/hqc/k8s_test/array-plus# kubectl get all
	NAME                                         READY   STATUS        RESTARTS   AGE
	pod/array-plus-deployment-55657b7c55-wfjtx   1/1     Running       0          10m
	pod/array-plus-deployment-55657b7c55-z8kx2   1/1     Running       0          10m
	pod/helloworld-deployment-79cbf4dcbb-5x6b2   1/1     Running       1          12d
	pod/helloworld-deployment-79cbf4dcbb-n5sps   1/1     Running       1          12d
	pod/helloworld-deployment-79cbf4dcbb-sr8xf   1/1     Running       1          12d
	pod/helloworld-deployment-79cbf4dcbb-wfh5w   1/1     Terminating   0          12d
	pod/nginx-f89759699-ksgcb                    1/1     Running       2          13d
	
	NAME                            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
	service/array-plus-deployment   NodePort    10.102.249.251   <none>        80:30000/TCP   5m59s
	service/helloworld-deployment   NodePort    10.106.97.144    <none>        80:30002/TCP   13d
	service/kubernetes              ClusterIP   10.96.0.1        <none>        443/TCP        25d
	service/nginx                   NodePort    10.109.178.122   <none>        80:30650/TCP   13d
	
	NAME                                    READY   UP-TO-DATE   AVAILABLE   AGE
	deployment.apps/array-plus-deployment   2/2     2            2           10m
	deployment.apps/helloworld-deployment   3/3     3            3           12d
	deployment.apps/nginx                   1/1     1            1           13d
	
	NAME                                               DESIRED   CURRENT   READY   AGE
	replicaset.apps/array-plus-deployment-55657b7c55   2         2         2       10m
	replicaset.apps/helloworld-deployment-79cbf4dcbb   3         3         3       12d
	replicaset.apps/nginx-f89759699                    1         1         1       13d

注意:无需将deployment和service删除重建,直接查看即可

5 运行验证

  1. master IP + port访问成功
    在这里插入图片描述
  2. node IP + port访问成功在这里插入图片描述
  3. dashboard中查看running成功
    在这里插入图片描述

到此为止,完全成功!!!

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐