1、infrastructure container:基础容器
维护整个pod网络空间:可以在node节点操作查看容器的网络
[root@localhost ~]# cat /opt/kubernetes/cfg/kubelet
3、container:业务容器
IfNotPresent:默认值,镜像在宿主机上不存在时才拉取。
Always:每次创建Pod都会重新拉取一次镜像,可以保证都是最新版本镜像。
Never:Pod 永远不会主动拉取这个镜像
查看镜像拉取策略(master节点查看):
[root@master1 demo]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-d55b94fd-hbnkr 0/1 Pending 0 2m52s
nginx-deployment-d55b94fd-qdj27 0/1 Pending 0 2m51s
nginx-deployment-d55b94fd-x5zd7 0/1 Pending 0 2m52s
[root@master1 demo]# kubectl edit deploy/nginx-deployment #编辑Pod资源查看策略
[root@localhost ~]# cd demo/
[root@localhost demo]# vim pod1.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: nginx
image: nginx:1.14
imagePullPolicy: Always
[root@master1 demo]# kubectl create -f pod1.yaml
pod/mypod created
[root@master1 demo]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 52s
查看容器详细信息:kubectl describe pod 名称
//查看分配节点
[root@localhost demo]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
mypod 1/1 Running 0 118s 172.17.31.6 192.168.195.150
//在任意node节点使用 curl 查看头部信息
[root@node2 ~]# curl -I 172.17.57.3
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Wed, 13 May 2020 02:12:07 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
1、开局优化,修改主机名(harbor),关闭防火墙,上传docker-compose和harbor的软件包(操作不在赘述),私有仓库的IP地址为:192.168.100.134
2、docker 和 docker-compose 安装,可以看我之前的博客。
博客链接:Docker之入门初了解、部署与镜像加速、网络优化
3、 安装harbor
[root@harbor ~]# tar zxf harbor-offline-installer-v1.2.2.tgz -C /usr/local/
[root@harbor ~]# cd /usr/local/harbor/
[root@harbor harbor]# ls
[root@harbor harbor]# vi harbor.cfg
hostname = 192.168.100.134 ##修改为监听本地地址,不可以使用localhost或者127.0.0.1
[root@harbor harbor]# sh install.sh
4、登录harbor界面,在浏览器输入192.168.100.134,新建私有项目project
5、所有node节点都要修改daemon-json文件,指定harbor仓库地址,修改完文件后记得重启Docker
[root@node1 ~]# vim /etc/docker/daemon.json
[root@node1 ~]# systemctl daemon-reload
[root@node1 ~]# systemctl restart docker
注意:在使用harbor下载镜像创建资源时候,要保证node处于harbor登录状态
//其中一个node节点登录harbor私有仓库
[root@node1 ~]# docker login 192.168.100.134
Username: admin
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
//下载Tomcat镜像进行推送
[root@node1 ~]# docker pull tomcat:8.0.52
//打标签
[root@node1 ~]# docker tag tomcat:8.0.52 192.168.100.134/project/tomcat
此处IP地址是harbor地址
//上传镜像到仓库
[root@node1 ~]# docker push 192.168.100.134/project/tomcat
6、指定node节点从私有仓库下载
[root@node1 ~]# cat .docker/config.json |base64 -w 0
ewoJImF1dGhzIjogewoJCSIxOTIuMTY4LjEwMC4xMzQiOiB7CgkJCSJhdXRoIjogIllXUnRhVzQ2U0dGeVltOXlNVEl6TkRVPSIKCQl9Cgl9LAoJIkh0dHBIZWFkZXJzIjogewoJCSJVc2VyLUFnZW50IjogIkRvY2tlci1DbGllbnQvMTkuMDMuOCAobGludXgpIgoJfQp9
[root@master1 demo]# vim registry-pull-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: registry-pull-secret
data:
.dockerconfigjson: ewoJImF1dGhzIjogewoJCSIxOTIuMTY4LjEwMC4xMzQiOiB7CgkJCSJhdXRoIjogIllXUnRhVzQ2U0dGeVltOXlNVEl6TkRVPSIKCQl9Cgl9LAoJIkh0dHBIZWFkZXJzIjogewoJCSJVc2VyLUFnZW50IjogIkRvY2tlci1DbGllbnQvMTkuMDMuOCAobGludXgpIgoJfQp9
type: kubernetes.io/dockerconfigjson
//创建secret资源
[root@master test]# kubectl create -f registry-pull-secret.yaml
secret/registry-pull-secret created
//查看secret资源
[root@master1 demo]# kubectl get secret
NAME TYPE DATA AGE
default-token-9qgnr kubernetes.io/service-account-token 3 13d
registry-pull-secret kubernetes.io/dockerconfigjson 1 50s
在master节点上创建资源从harbor中下载镜像
[root@localhost demo]# vim tomcat-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-tomcat
spec:
replicas: 2
template:
metadata:
labels:
app: my-tomcat
spec:
imagePullSecrets:
- name: registry-pull-secret
containers:
- name: my-tomcat
image: 192.168.195.80/project/tomcat
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: my-tomcat
spec:
type: NodePort
ports:
- port: 8080
targetPort: 8080
nodePort: 31111
selector:
app: my-tomcat
创建资源并查看
[root@master1 demo]# kubectl create -f tomcat-deployment.yaml
[root@master1 demo]# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-tomcat-7d697d459b-5g6zw 1/1 Running 0 33s
my-tomcat-7d697d459b-9wgd2 1/1 Running 0 33s
mypod 1/1 Running 1 92m