这篇文章将为大家详细讲解有关Kubernates之如何从Harbor上拉去镜像创建服务,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
1、将Harbor加入k8s宿主机Docker的insecure-registries中
修改k8s的master和俩个node的/etc/docker/daemon.json,将192.168.33.5将入到insecure-registries中,如下所示:
List-1.1
[root@localhost data]# more /etc/docker/daemon.json
{"bip": "172.172.172.1/24","insecure-registries": ["10.221.128.52","192.168.33.5"]}
重启Docker服务,如下List-1.2
List-1.2
[root@localhost data]# systemctl daemon-reload
[root@localhost data]# systemctl restart Docker
在每个节点上试试能否login到192.168.33.5,如下List-1.3
List-1.3
[root@localhost data]# docker login 192.168.33.5
Username (mjduan):
Password:
Login Succeeded
2、用yaml在k8s上创建服务
在master节点执行如下List-2.1,创建secret,其中registry-harbor2是名称,namespace的值使用default,docker-server的值是Harbor的IP,docker-username和docker-password的值是在Harbor上创建的用户。
List-2.1
kubectl create secret docker-registry registry-harbor2 --namespace=default \
--docker-server=http://192.168.33.5 --docker-username=mjduan \
--docker-password=***** --docker-email=dmj1161859184@126.com
List-2.2 查看创建了的secret
[root@localhost data]# kubectl get secret
NAME TYPE DATA AGE
default-token-dqvqc kubernetes.io/service-account-token 3 12h
registry-harbor2 kubernetes.io/dockerconfigjson 1 5h
创建如下List-2.3所示的nginx.yaml,注意imagePullSecrets的值是registry-harbor2
List-2.3
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: 192.168.33.5/project/nginx:1.0
imagePullPolicy: Always
ports:
- containerPort: 80
imagePullSecrets:
- name: registry-harbor2
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
sessionAffinity: ClientIP
selector:
app: nginx
ports:
- port: 80
nodePort: 30180
之后用kubectl创建服务,如下List-2.4
List-2.4
[root@localhost data]# create -f deploy-nginx.yaml
查看结果,如下List-2.5
List-2.5
[root@localhost data]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12h
nginx-service NodePort 10.103.100.169 <none> 80:30180/TCP 5h
[root@localhost data]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-789794d957-m7wjv 1/1 Running 0 5h
[root@localhost data]#
关于“Kubernates之如何从Harbor上拉去镜像创建服务”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。