了解pod

特点:

  • 最小部署单元

  • 一组容器的集合

  • 一个Pod中的容器共享网络命名空间

  • Pod是短暂的

Pod容器分类:

1:infrastructure container 基础容器:维护整个Pod网络空间

node节点操作

查看容器的网络

[root@localhost ~]# cat /opt/kubernetes/cfg/kubelet
--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"

每次创建Pod时候就会创建,与Pod对应的,对于用户是透明的

[root@localhost ~]# docker ps
registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0   "/pause"

2:initcontainers 初始化容器

  • 先于业务容器开始执行,原先Pod中容器是并行开启,现在进行了改进

3:container 业务容器

  • 并行启动

镜像拉取策略(image PullPolicy)

  • IfNotPresent:默认值,镜像在宿主机上不存在时才拉取

  • Always:每次创建Pod都会重新拉取一次镜像

  • Never:Pod永远不会主动拉取这个镜像

创建pods资源

[root@localhost demo]# vim pod1.yaml

apiVersion: v1

kind: Pod

metadata:

 name: mypod

spec:

 containers:

  - name: nginx

   image: nginx

   imagePullPolicy: Always

   command: [ "echo", "SUCCESS" ]

创建pode1

[root@localhost demo]# kubectl create -f pod1.yaml 

如果发生CrashLoopBackOff:失败的状态的原因是因为命令启动冲突

我们删除SUCCESS

[root@localhost demo]# kubectl get pods
NAME                              READY   STATUS             RESTARTS   AGE
mypod                             0/1     CrashLoopBackOff   2          49s
//失败的状态的原因是因为命令启动冲突
删除 command: [ "echo", "SUCCESS" ]

同时更改一下版本

[root@localhost ~]# vim pod1.yaml
image: nginx:1.14

image-20201013105113493

删除原有的资源

[root@localhost demo]# kubectl delete -f pod1.yaml 

pod "mypod" deleted

------------------------更新资源--------------------------------

[root@localhost demo]# kubectl apply -f pod1.yaml 

[root@localhost demo]# kubectl get pods

NAME                READY  STATUS   RESTARTS  AGE

mypod               1/1   Running  0      34s


--------------------查看分配节点---------------------------
[root@localhost demo]# kubectl get pods -o wide
NAME                              READY   STATUS    RESTARTS   AGE    IP            NODE              NOMINATED NODE
mypod                             1/1     Running   0          118s   

在任意node节点使用curl 查看头部信息

node节点操作
[root@localhost ~]# curl -I 172.17.31.6
HTTP/1.1 200 OK
Server: nginx/1.14.2

image-20201013105805849

k8s和Harbor服务过程

重新开启一个node节点

以下服务都是在有docker的前提下部署的!!!
安装docker-compose

[root@localhost ~]# chmod +x docker-compose 
1

系统可以识别

 mv docker-compose /usr/local/bin/
1

查看是否安装

[root@localhost ~]# docker-compose -v
docker-compose version 1.21.1, build 5a3f1a3
12

安装harbor

[root@localhost ~]# tar zxvf harbor-offline-installer-v1.2.2.tgz -C /usr/local/
1

修改配置文件

[root@localhost ~]# cd /usr/local/harbor/ 
[root@localhost harbor]# vim harbor.cfg 
 5 hostname = 192.168.136.81
123

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vnQmkZcD-1601048267017)(../AppData/Roaming/Typora/typora-user-images/image-20200924120918776.png)]

开启Harbor

[root@localhost harbor]# sh install.sh 
1

输入地址查看

密码Harbor12345默认

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qFCacstW-1601048267021)(../AppData/Roaming/Typora/typora-user-images/image-20200924121312522.png)]

image-20201013112818401

node节点配置连接私有仓库

2个node节点都要配置

(注意后面的逗号要添加)

[root@localhost ~]# vim /etc/docker/daemon.json
{
  "registry-mirrors": ["https://05vz3np5.mirror.aliyuncs.com"],
  "insecure-registries":["192.168.136.50"]
}

image-20201013115244644

开启服务

[root@localhost ~]# systemctl restart docker

登录harbor私有仓库

[root@localhost ~]# docker login 192.168.136.50

Username: admin 

Password:   //输入密码Harbor12345

查看凭证文件

[root@localhost ~]# ls -a
.docker
[root@localhost ~]# cd .docker/ (查看凭证)
[root@localhost .docker]# cat config.json | base64  -w 0
ewoJImF1dGhzIjogewoJCSIxOTIuMTY4LjEzNi41MCI6IHsKCQkJImF1dGgiOiAiWVdSdGFXNDZTR0Z5WW05eU1USXpORFU9IgoJCX0KCX0sCgkiSHR0cEhlYWRlcnMiOiB7CgkJIlVzZXItQWdlbnQiOiAiRG9ja2VyLUNsaWVudC8xOS4wMy4xMyAobGludXgpIgoJfQp9[root@localhost .docker]# 

-w 0 禁用转行

在node节点下载tomcat

下载Tomcat镜像进行推送

[root@localhost ~]# docker pull tomcat
[root@localhost ~]# docker images  查看tomcat

web复制推送镜像

image-20201013121454472

打标签

复制粘贴到node节点

[root@localhost ~]# docker tag tomcat 192.168.136.50/project/tomcat

推送成功

[root@localhost ~]# docker tag tomcat 192.168.136.50/project/tomcat

查看web游览器

image-20201013122324862

基于本地镜像创建tomcat

在matser节点下载tomcat镜像

[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:
      containers:
      - name: my-tomcat
        image: docker.io/tomcat:8.0.52
        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

node节点部署tomcat

[root@localhost ~]# docker pull tomcat:8.0.52

在master上发布服务

[root@localhost demo]# kubectl apply -f tomcat-deployment.yaml 

查看端口

nginx-service   NodePort    10.0.0.193   <none>        80:41108/TCP     68m
[root@localhost ~]# kubectl get pods -o wide
NAME                         READY   STATUS    RESTARTS   AGE    IP            NODE             NOMINATED NODE
my-tomcat-7cd4fdbb5b-8b6bx   1/1     Running   0          3m1s   172.17.38.5   192.168.136.40   <none>
my-tomcat-7cd4fdbb5b-ss8nc   1/1     Running   0          3m1s   172.17.27.4   192.168.136.30   <none>

查看tomcat服务端口号


[root@localhost ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP          4d13h
my-tomcat    NodePort    10.0.0.216   <none>        8080:31111/TCP   71m

游览器192.168.136.40:31111

image-20201013134253111

如果遇到处于Terminating状态的无法删除的资源如何处理

[root@localhost demo]# kubectl get pods

NAME                READY  STATUS     RESTARTS  AGE

my-tomcat-57667b9d9-nklvj     1/1   Terminating  0      10h

my-tomcat-57667b9d9-wllnp     1/1   Terminating  0      10h

//这种情况下可以使用强制删除命令:
kubectl delete pod [pod name] --force --grace-period=0 -n [namespace]

上传镜像到harbor

node01上操作(之前登陆过harbor仓库的节点)

镜像打标签

[root@localhost ~]# docker tag tomcat:8.0.52 192.168.136.50/project/tomcat

删除之前的镜像

image-20201014120216547

上传镜像到harbor

[root@localhost ~]# docker push 192.168.136.50/project/tomcat8

查看登陆凭据

[root@localhost ~]# cat .docker/config.json |base64 -w 0
ewoJImF1dGhzIjogewoJCSIxOTIuMTY4LjEzNi41MCI6IHsKCQkJImF1dGgiOiAiWVdSdGFXNDZTR0Z5WW05eU1USXpORFU9IgoJCX0KCX0sCgkiSHR0cEhlYWRlcnMiOiB7CgkJIlVzZXItQWdlbnQiOiAiRG9ja2VyLUNsaWVudC8xOS4wMy4xMyAobGludXgpIgoJfQp9

master节点创建

[root@localhost demo]# vim registry-pull-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: registry-pull-secret
data:
  .dockerconfigjson:ewoJImF1dGhzIjogewoJCSIxOTIuMTY4LjEzNi41MCI6IHsKCQkJImF1dGgiOiAiWVdSdGFXNDZTR0Z5WW05eU1USXpORFU9IgoJCX0KCX0sCgkiSHR0cEhlYWRlcnMiOiB7CgkJIlVzZXItQWdlbnQiOiAiRG9ja2VyLUNsaWVudC8xOS4wMy4xMyAobGludXgpIgoJfQp9
type: kubernetes.io/dockerconfigjson

创建secret资源

[root@localhost ~]# kubectl create -f registry-pull-secret.yaml

查看secret资源

[root@localhost ~]# kubectl get secret
NAME                   TYPE                                  DATA   AGE
default-token-w6xph    kubernetes.io/service-account-token   3      4d22h
registry-pull-secret   kubernetes.io/dockerconfigjson        1      40s

创建资源从harbor中下载镜像

[root@localhost ~]# kubectl create -f registry-pull-secret.yaml  删除原有的镜像
[root@localhost demo]# vim tomcat-deployment.yaml
spec:
      imagePullSecrets:  下面添加
      - name: registry-pull-secret  
      containers:
    
        image: 192.168.195.80/project/tomcat 
        ports:

image-20201013155932044

创建资源

kubectl create -f tomcat-deployment.yaml 

私有仓库中的镜像被下载了2次

image-20201013163822873

Logo

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

更多推荐