仓库地址为https://at.harbor.com
该仓库是我在本地搭建的一个私有仓库,配置了域名,配置了证书

登录本地仓库

[root@k8s-master01 ~]# docker login https://at.harbor.com
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

在nginx官网拉取一个镜像

[root@k8s-master01 ~]# docker pull nginx:stable-alpine-perl

把镜像推送到本地仓库

推送镜像先要修改标签

docker push at.harbor.com/library/IMAGE[:TAG]
[root@k8s-master01 ~]# docker tag nginx:stable-alpine-perl at.harbor.com/library/nginx:dev

推送到本地仓库

[root@k8s-master01 ~]# docker push at.harbor.com/library/nginx:dev

在浏览器查看

在这里插入图片描述

用k8s启动,启动之前先将这些镜像删除

查看

[root@k8s-master01 ~]# docker images
REPOSITORY                           TAG                  IMAGE ID       CREATED         SIZE
quay.io/coreos/flannel               v0.14.0-rc1          0a1a2818ce59   2 days ago      67.9MB
nginx                                stable-alpine-perl   8dbe4574e65a   3 days ago      56.1MB
at.harbor.com/library/nginx          dev                  8dbe4574e65a   3 days ago      56.1MB
k8s.gcr.io/kube-scheduler            v1.15.1              b0b3c4c404da   21 months ago   81.1MB
k8s.gcr.io/kube-proxy                v1.15.1              89a062da739d   21 months ago   82.4MB
k8s.gcr.io/kube-apiserver            v1.15.1              68c3eb07bfc3   21 months ago   207MB
k8s.gcr.io/kube-controller-manager   v1.15.1              d75082f1d121   21 months ago   159MB
k8s.gcr.io/coredns                   1.3.1                eb516548c180   2 years ago     40.3MB
k8s.gcr.io/etcd                      3.3.10               2c4adeb21b4f   2 years ago     258MB
k8s.gcr.io/pause                     3.1                  da86e6ba6ca1   3 years ago     742kB

删除

[root@k8s-master01 ~]# docker rmi -f at.harbor.com/library/nginx:dev
Untagged: at.harbor.com/library/nginx:dev
Untagged: at.harbor.com/library/nginx@sha256:37f3dc933d680b9346124f69638b5b358374b5f1d7163a150d72650c2f2f50fd
[root@k8s-master01 ~]# docker images
REPOSITORY                           TAG                  IMAGE ID       CREATED         SIZE
quay.io/coreos/flannel               v0.14.0-rc1          0a1a2818ce59   2 days ago      67.9MB
nginx                                stable-alpine-perl   8dbe4574e65a   3 days ago      56.1MB
k8s.gcr.io/kube-proxy                v1.15.1              89a062da739d   21 months ago   82.4MB
k8s.gcr.io/kube-controller-manager   v1.15.1              d75082f1d121   21 months ago   159MB
k8s.gcr.io/kube-scheduler            v1.15.1              b0b3c4c404da   21 months ago   81.1MB
k8s.gcr.io/kube-apiserver            v1.15.1              68c3eb07bfc3   21 months ago   207MB
k8s.gcr.io/coredns                   1.3.1                eb516548c180   2 years ago     40.3MB
k8s.gcr.io/etcd                      3.3.10               2c4adeb21b4f   2 years ago     258MB
k8s.gcr.io/pause                     3.1                  da86e6ba6ca1   3 years ago     742kB
[root@k8s-master01 ~]# docker rmi -f nginx:stable-alpine-perl
Untagged: nginx:stable-alpine-perl
Untagged: nginx@sha256:1235b9c9c7f8a96263aef548e6c274480bb8defc373ba98d8777fb0a46a3b303
Deleted: sha256:8dbe4574e65a7cd77c75f86871aab3be54c80681243ac99e84b1f6092abb5b96
Deleted: sha256:239bbc11c7e99b4fdf668b2e30d4d187a2afff956719ce43835bf6b8eb07d2d8
Deleted: sha256:1b4db689fea723dc3cb721062a54e0b3c4334aecdb07379661b4b98bb702db87
Deleted: sha256:4af0ff524649656749a6b3b86285c94dd2944c6c6eea161800c2c799437904c0
Deleted: sha256:09e0cc51287dd363a0ff0f134ac8e93f6413f50700fe04206700109826996cdf
Deleted: sha256:9a5d14f9f5503e55088666beef7e85a8d9625d4fa7418e2fe269e9c54bcb853c

测试k8s集群的可以用性

[root@k8s-master01 ~]# kubectl run nginx-deployment --image=at.harbor.com/library/nginx:dev --port=80 --replicas=1
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx-deployment created


[root@k8s-master01 ~]# kubectl  get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   1/1     1            1           45s
[root@k8s-master01 ~]# kubectl  get rs
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-66d47c769d   1         1         1       82s
查看pod
[root@k8s-master01 ~]# kubectl  get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-66d47c769d-q42n7   1/1     Running   0          88s
查看详细信息
[root@k8s-master01 ~]# kubectl  get  pod -o wide
NAME                                READY   STATUS    RESTARTS   AGE     IP           NODE         NOMINATED NODE   READINESS GATES
nginx-deployment-66d47c769d-q42n7   1/1     Running   0          2m11s   10.244.1.2   k8s-node01   <none>           <none>

在node01查看进程

[root@k8s-node01 ~]# docker ps -a |grep nginx
979a7b62a9de   8dbe4574e65a           "/docker-entrypoint.…"   3 minutes ago    Up 3 minutes                          k8s_nginx-deployment_nginx-deployment-66d47c769d-q42n7_default_9c86a4c7-8fc9-4c7f-ba6c-c22083f2a204_0
bb90c90bd40d   k8s.gcr.io/pause:3.1   "/pause"                 3 minutes ago    Up 3 minutes                          k8s_POD_nginx-deployment-66d47c769d-q42n7_default_9c86a4c7-8fc9-4c7f-ba6c-c22083f2a204_0

测试访问

[root@k8s-master01 ~]# curl 10.244.1.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

删除一个pod

[root@k8s-master01 ~]# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-66d47c769d-q42n7   1/1     Running   0          6m57s
[root@k8s-master01 ~]# kubectl  delete pod nginx-deployment-66d47c769d-q42n7
pod "nginx-deployment-66d47c769d-q42n7" deleted

发现又重新创建一个pod

[root@k8s-master01 ~]# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-66d47c769d-dzgg2   1/1     Running   0          31s

原因是pod的副本数我们设定的是1 --replicas=1
所已删除,也会自动创建一个,来满足这个副本数的要求

扩容副本数

查看副本名

[root@k8s-master01 ~]# kubectl  get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   1/1     1            1           12m

副本数修改为3
[root@k8s-master01 ~]# kubectl  scale --replicas=3 deployment/nginx-deployment
deployment.extensions/nginx-deployment scaled
查看pod
[root@k8s-master01 ~]# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-66d47c769d-dzgg2   1/1     Running   0          5m34s
nginx-deployment-66d47c769d-kffbh   1/1     Running   0          10s
nginx-deployment-66d47c769d-mqqkd   1/1     Running   0          10s

node02运行了两个,node01运行一个
[root@k8s-master01 ~]# kubectl  get pod  -o wide
NAME                                READY   STATUS    RESTARTS   AGE     IP           NODE         NOMINATED NODE   READINESS GATES
nginx-deployment-66d47c769d-dzgg2   1/1     Running   0          7m15s   10.244.2.2   k8s-node02   <none>           <none>
nginx-deployment-66d47c769d-kffbh   1/1     Running   0          111s    10.244.2.3   k8s-node02   <none>           <none>
nginx-deployment-66d47c769d-mqqkd   1/1     Running   0          111s    10.244.1.3   k8s-node01   <none>           <none>

随便删除一个

[root@k8s-master01 ~]# kubectl  delete pod nginx-deployment-66d47c769d-dzgg2
pod "nginx-deployment-66d47c769d-dzgg2" deleted


发现又新创建了一个
[root@k8s-master01 ~]# kubectl  get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-66d47c769d-blptv   1/1     Running   0          40s
nginx-deployment-66d47c769d-kffbh   1/1     Running   0          4m12s
nginx-deployment-66d47c769d-mqqkd   1/1     Running   0          4m12s

创建出来怎么访问?
查看帮助信息

[root@k8s-master01 ~]# kubectl  expose --help

kubectl expose deployment nginx --port=80 --target-port=8000

[root@k8s-master01 ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 53m

修改svc的端口

[root@k8s-master01 ~]# kubectl  expose deployment nginx-deployment --port=3000 --target-port=80
service/nginx-deployment exposed

查看

[root@k8s-master01 ~]# kubectl  get svc
NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes         ClusterIP   10.96.0.1       <none>        443/TCP    19h
nginx-deployment   ClusterIP   10.104.129.35   <none>        3000/TCP   49s

访问是一个轮询的机制

[root@k8s-master01 ~]# ipvsadm -Ln |grep 10.104.129.35
TCP  10.104.129.35:3000 rr
[root@k8s-master01 ~]# ipvsadm -Ln 
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.96.0.1:443 rr
  -> 10.14.2.150:6443             Masq    1      3          0         
TCP  10.96.0.10:53 rr
  -> 10.244.0.19:53               Masq    1      0          0         
  -> 10.244.0.20:53               Masq    1      0          0         
TCP  10.96.0.10:9153 rr
  -> 10.244.0.19:9153             Masq    1      0          0         
  -> 10.244.0.20:9153             Masq    1      0          0         
TCP  10.104.129.35:3000 rr
  -> 10.244.1.3:80                Masq    1      0          2         
  -> 10.244.1.4:80                Masq    1      0          2         
  -> 10.244.2.3:80                Masq    1      0          3         
UDP  10.96.0.10:53 rr
  -> 10.244.0.19:53               Masq    1      0          0         
  -> 10.244.0.20:53               Masq    1      0          0      
[root@k8s-master01 ~]# kubectl get pod -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP           NODE         NOMINATED NODE   READINESS GATES
nginx-deployment-66d47c769d-blptv   1/1     Running   0          44m   10.244.1.4   k8s-node01   <none>           <none>
nginx-deployment-66d47c769d-kffbh   1/1     Running   0          47m   10.244.2.3   k8s-node02   <none>           <none>
nginx-deployment-66d47c769d-mqqkd   1/1     Running   0          47m   10.244.1.3   k8s-node01   <none>           <none>

当然这个是内部的IP,外部是不能访问

[root@k8s-master01 ~]# kubectl  get svc
NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes         ClusterIP   10.96.0.1       <none>        443/TCP    19h
nginx-deployment   ClusterIP   10.104.129.35   <none>        3000/TCP   8m3s

可以看到TEPE是ClusterIP

修改svc的类型

[root@k8s-master01 ~]# kubectl  edit svc nginx-deployment
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2021-04-18T03:24:59Z"
  labels:
    run: nginx-deployment
  name: nginx-deployment
  namespace: default
  resourceVersion: "102531"
  selfLink: /api/v1/namespaces/default/services/nginx-deployment
  uid: 5e82bf32-bd9e-405e-8ac9-08fbb42af79e
spec:
  clusterIP: 10.104.129.35
  ports:
  - port: 3000
    protocol: TCP
    targetPort: 80
  selector:
    run: nginx-deployment
  sessionAffinity: None
  type: ClusterIP 改为 NodePort
status:
  loadBalancer: {}

再重新查看svc

[root@k8s-master01 ~]# kubectl  get svc
NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes         ClusterIP   10.96.0.1       <none>        443/TCP          19h
nginx-deployment   NodePort    10.104.129.35   <none>        3000:31626/TCP   11m

访问http://10.14.2.150:31626/
只要改成nodeport,所有的节点上都会暴露31626的端口

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

Logo

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

更多推荐