K8s使用方法

1 查看k8s的版本信息

[root@k8s-master ~]# kubectl version

Client Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"269f928217957e7126dc87e6adfa82242bfe5b1e", GitTreeState:"clean", BuildDate:"2017-07-03T15:31:10Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}

Server Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"269f928217957e7126dc87e6adfa82242bfe5b1e", GitTreeState:"clean", BuildDate:"2017-07-03T15:31:10Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}

[root@k8s-master ~]#  kubectl api-versions

apps/v1beta1

authentication.k8s.io/v1beta1

authorization.k8s.io/v1beta1

autoscaling/v1

batch/v1

certificates.k8s.io/v1alpha1

extensions/v1beta1

policy/v1beta1

rbac.authorization.k8s.io/v1alpha1

storage.k8s.io/v1beta1

v1

2 查看各组件状态

[root@k8s-master yamls]# kubectl -s http://localhost:8080 get componentstatuses

NAME                 STATUS    MESSAGE              ERROR

etcd-0               Healthy   {"health": "true"}  

scheduler            Healthy   ok                  

controller-manager   Healthy   ok

3 查看k8s的组成节点(该节点指的是对应的服务器节点)

[root@k8s-master yamls]# kubectl get node

NAME              STATUS    AGE

192.168.248.142   Ready     18h

192.168.248.143   Ready     20s

4 确认Deployment

[root@k8s-master ~]# kubectl get deployment

NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE

foo        1         1         1            0           15h

my-nginx   2         2         2            2           14h

5 确认pod(该pod指的是对应的容器)

[root@k8s-master ~]# kubectl get pods

NAME                       READY     STATUS             RESTARTS   AGE

foo-1189255365-3j6fr       0/1       CrashLoopBackOff   17         15h

my-nginx-379829228-pw1wj   1/1       Running            1          14h

my-nginx-379829228-wmlrt   1/1       Running            1          14h

6 删除pod

[root@k8s-master ~]# kubectl delete pods foo-1189255365-3j6fr

pod "foo-1189255365-3j6fr" deleted

确认结果看到foo-1189255365-3j6f为状态为CrashLoopBackOff这是确保replicas为1的动作,执行如下指令再次确认

[root@k8s-master ~]# kubectl get deployments

NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE

foo        1         1         1            0           15h

my-nginx   2         2         2            2           14h

删除deployment,直接删除pod触发replicas的确保机制,则可以删除deployment

[root@k8s-master ~]# kubectl delete deployment foo

deployment "foo" deleted

[root@k8s-master ~]# kubectl get pods

NAME                       READY     STATUS    RESTARTS   AGE

my-nginx-379829228-pw1wj   1/1       Running   1          14h

my-nginx-379829228-wmlrt   1/1       Running   1          14h

7 通过yaml文件创建pod

[root@k8s-master yamls]# cat mysql.yaml

---

kind: ReplicationController

apiVersion: v1

metadata:

  name: mysql

spec:

  replicas: 1

  selector:

    name: mysql

  template:

    metadata:

      labels:

        name: mysql

    spec:

      containers:

      - name: mysql

        image: mysql:5.7.16

        ports:

        - containerPort: 3306

          protocol: TCP

        env:

          - name: MYSQL_ROOT_PASSWORD

            value: "hello123"

[root@k8s-master yamls]# cat sonar.yaml

---

kind: ReplicationController

apiVersion: v1

metadata:

  name: sonarqube

spec:

  replicas: 1

  selector:

    name: sonarqube

  template:

    metadata:

      labels:

        name: sonarqube

    spec:

      containers:

      - name: sonarqube

        image: sonarqube:5.6.5

        ports:

        - containerPort: 9000

          protocol: TCP

8 创建mysql及sonarqube

创建mysql

[root@k8s-master yamls]# kubectl create -f mysql.yaml

replicationcontroller "mysql" created

[root@k8s-master yamls]# kubectl get pod

NAME                       READY     STATUS    RESTARTS   AGE

my-nginx-379829228-pw1wj   1/1       Running   1          14h

my-nginx-379829228-wmlrt   1/1       Running   1          14h

mysql-vn2k3                1/1       Running   0          1m

[root@k8s-master yamls]# kubectl get rc

NAME      DESIRED   CURRENT   READY     AGE

mysql     1         1         1         2m

创建sonarqube

[root@k8s-master yamls]# kubectl create -f sonar.yaml

replicationcontroller "sonarqube" created

[root@k8s-master yamls]# kubectl get pod

NAME                       READY     STATUS    RESTARTS   AGE

my-nginx-379829228-pw1wj   1/1       Running   1          15h

my-nginx-379829228-wmlrt   1/1       Running   1          15h

mysql-vn2k3                1/1       Running   0          18m

sonarqube-438sd            1/1       Running   0          2m

[root@k8s-master yamls]# kubectl delete -f mysql.yaml

[root@k8s-master yamls]# kubectl delete -f sonar.yaml

9 描述某一个pod的详细信息

[root@k8s-master yamls]# kubectl describe pod mysql-vn2k3

Name:      mysql-vn2k3

Namespace: default

Node:      192.168.248.142/192.168.248.142

Start Time:   Thu, 11 Oct 2018 18:28:04 -0700

Labels:       name=mysql

Status:       Running

IP:    172.17.0.3

Controllers:  ReplicationController/mysql

Containers:

  mysql:

    Container ID:    docker://4cdbb18e4a4d863a6f777b7738125f42a80eb6c071bc62573d045b984df5744b

    Image:    mysql:5.7.16

    Image ID:     docker-pullable://docker.io/mysql@sha256:89cc6ff6a7ac9916c3384e864fb04b8ee9415b572f872a2a4cf5b909dbbca81b

    Port:     3306/TCP

    State:    Running

      Started:       Thu, 11 Oct 2018 18:29:44 -0700

    Ready:    True

    Restart Count:   0

    Volume Mounts:   <none>

    Environment Variables:

      MYSQL_ROOT_PASSWORD:  hello123

Conditions:

  Type     Status

  Initialized     True

  Ready    True

  PodScheduled    True

No volumes.

QoS Class: BestEffort

Tolerations:  <none>

Events:

  FirstSeen   LastSeen   Count  From              SubObjectPath     Type       Reason         Message

  ---------   --------   -----  ----              -------------     --------   ------         -------

  26m      26m    1   {default-scheduler }               Normal     Scheduled      Successfully assigned mysql-vn2k3 to 192.168.248.142

  26m      26m    1   {kubelet 192.168.248.142}   spec.containers{mysql}   Normal     Pulling           pulling image "mysql:5.7.16"

  26m      25m    2   {kubelet 192.168.248.142}              Warning        MissingClusterDNS kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.

  25m      25m    1   {kubelet 192.168.248.142}   spec.containers{mysql}   Normal     Pulled        Successfully pulled image "mysql:5.7.16"

  24m      24m    1   {kubelet 192.168.248.142}   spec.containers{mysql}   Normal     Created           Created container with docker id 4cdbb18e4a4d; Security:[seccomp=unconfined]

  24m      24m    1   {kubelet 192.168.248.142}   spec.containers{mysql}   Normal     Started           Started container with docker id 4cdbb18e4a4d

10 scale

scale用于程序在负载加重或缩小时副本进行扩容或缩小,如前面创建的nginx有两个副本,可以轻松的使用scale命令对副本数进行扩展或缩小。
扩展副本数到4:

[root@k8s-master yamls]# kubectl scale rc mysql --replicas=4 

replicationcontroller "mysql" scaled

[root@k8s-master yamls]# kubectl get rc

[root@k8s-master ~]# kubectl get rc

NAME        DESIRED   CURRENT   READY     AGE

mysql       2         2         2         1h

sonarqube   1         1         1         1h

重新缩减副本数到2:

[root@k8s-master yamls]# kubectl scale rc mysql --replicas=2

replicationcontroller "mysql" scaled

[root@k8s-master yamls]# kubectl get pod

NAME                       READY     STATUS    RESTARTS   AGE

my-nginx-379829228-pw1wj   1/1       Running   1          15h

my-nginx-379829228-wmlrt   1/1       Running   1          15h

mysql-8wz8j                1/1       Running   0          1m

mysql-vn2k3                1/1       Running   0          50m

sonarqube-438sd            1/1       Running   0          33m

11获取指定json或ymal格式的KEY数据,custom-columns=XXXXX(自定义列名):.status.hostIP(以“点开始”,然后写路径就可以):

[root@k8s-master yamls]# kubectl get pod mysql-vn2k3  -o custom-columns=HOST-IP:.status.hostIP,POD-IP:.status.podIP

HOST-IP           POD-IP

192.168.248.142   172.17.0.3

12 查看某个pod重启次数

[root@k8s-master ~]# kubectl get pod my-nginx-379829228-pw1wj --template="{{range .status.containerStatuses}}{{.name}}:{{.restartCount}}{{end}}"

my-nginx:1

13 查看pod的生命周期

[root@k8s-master ~]# kubectl get pod sonarqube-438sd --template="{{.status.phase}}"

Running

14 容器内日志输出

logs命令用于显示pod运行中,容器内程序输出到标准输出的内容。跟docker的logs命令类似。如果要获得tail -f 的方式,也可以使用-f选项。

[root@k8s-master ~]# kubectl logs mysql-8wz8j

Initializing database

2018-10-12T02:16:34.904907Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2018-10-12T02:16:40.315744Z 0 [Warning] InnoDB: New log files created, LSN=45790

2018-10-12T02:16:44.441289Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2018-10-12T02:16:48.178465Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: df7fe121-cdc4-11e8-a664-0242ac110006.

2018-10-12T02:16:48.221676Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2018-10-12T02:16:48.239652Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

2018-10-12T02:16:53.319037Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.

2018-10-12T02:16:53.324377Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.

2018-10-12T02:16:53.324458Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.

2018-10-12T02:16:53.327143Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.

2018-10-12T02:16:53.327271Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.

Database initialized

MySQL init process in progress...

MySQL init process in progress...

MySQL init process in progress...

MySQL init process in progress...

MySQL init process in progress...

2018-10-12T02:17:22.381789Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2018-10-12T02:17:25.089076Z 0 [Note] mysqld (mysqld 5.7.16) starting as process 53 ...

2018-10-12T02:17:25.419875Z 0 [Note] InnoDB: PUNCH HOLE support available

2018-10-12T02:17:25.422563Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins

2018-10-12T02:17:25.424559Z 0 [Note] InnoDB: Uses event mutexes

2018-10-12T02:17:25.424598Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier

2018-10-12T02:17:25.425938Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3

2018-10-12T02:17:25.426742Z 0 [Note] InnoDB: Using Linux native AIO

2018-10-12T02:17:25.434586Z 0 [Note] InnoDB: Number of pools: 1

2018-10-12T02:17:25.446547Z 0 [Note] InnoDB: Using CPU crc32 instructions

15 autoscale

scale虽然能够很方便的对副本数进行扩展或缩小,但是仍然需要人工介入,不能实时自动的根据系统负载对副本数进行扩、缩。autoscale命令提供了自动根据pod负载对其副本进行扩缩的功能。

autoscale命令会给一个rc指定一个副本数的范围,在实际运行中根据pod中运行的程序的负载自动在指定的范围内对pod进行扩容或缩容。如前面创建的nginx,可以用如下命令指定副本范围在1~4

[root@k8s-master ~]# kubectl autoscale rc sonarqube --min=1 --max=4

replicationcontroller "sonarqube" autoscaled

16 查看容器运行在哪个node节点

[root@k8s-master ~]# kubectl get pod -o wide

[root@k8s-master ~]# kubectl get pod -o wide

NAME                        READY     STATUS    RESTARTS   AGE       IP           NODE

my-nginx-379829228-pw1wj    1/1       Running   1          17h       172.17.0.2   192.168.248.142

my-nginx-379829228-wmlrt    1/1       Running   1          17h       172.17.0.4   192.168.248.142

mysql-8wz8j                 1/1       Running   0          1h        172.17.0.6   192.168.248.142

mysql-vn2k3                 1/1       Running   0          2h        172.17.0.3   192.168.248.142

sonarqube-438sd             1/1       Running   0          1h        172.17.0.5   192.168.248.142

testnginx-94870464-9nkxn    1/1       Running   0          1m        172.17.0.5   192.168.248.143

testnginx-94870464-k3hdg    1/1       Running   0          1m        172.17.0.4   192.168.248.143

testnginx-94870464-r208x    1/1       Running   0          1m        172.17.0.7   192.168.248.142

testnginx-94870464-s9c9x    1/1       Running   0          1m        172.17.0.8   192.168.248.142

testnginx-94870464-sz6pb    1/1       Running   0          1m        172.17.0.3   192.168.248.143

younginx-2413665018-q2nvq   1/1       Running   0          53m       172.17.0.2   192.168.248.143

16 cordon, drain, uncordon

这三个命令是正式release的1.2新加入的命令,三个命令一起介绍,是因为三个命令配合使用可以实现节点的维护。在1.2之前,因为没有相应的命令支持,如果要维护一个节点,只能stop该节点上的kubelet将该节点退出集群,是集群不在将新的pod调度到该节点上。如果该节点上本生就没有pod在运行,则不会对业务有任何影响。如果该节点上有pod正在运行,kubelet停止后,master会发现该节点不可达,而将该节点标记为notReady状态,不会将新的节点调度到该节点上。同时,会在其他节点上创建新的pod替换该节点上的pod。这种方式虽然能够保证集群的健壮性,但是任然有些暴力,如果业务只有一个副本,而且该副本正好运行在被维护节点上的话,可能仍然会造成业务的短暂中断。

1.2中新加入的这3个命令可以保证维护节点时,平滑的将被维护节点上的业务迁移到其他节点上,保证业务不受影响。如下图所示是一个整个的节点维护的流程(为了方便demo增加了一些查看节点信息的操作):1)首先查看当前集群所有节点状态,可以看到共四个节点都处于ready状态;2)查看当前nginx两个副本分别运行在k8s-node和k8s-node1两个节点上;3)使用cordon命令将k8s-node1标记为不可调度;4)再使用kubectl get nodes查看节点状态,发现k8s-node1虽然还处于Ready状态,但是同时还被禁能了调度,这意味着新的pod将不会被调度到k8s-node1上。4)再查看nginx状态,没有任何变化,两个副本仍运行在k8s-node和k8s-node1上;5)执行drain命令,将运行在k8s-node1上运行的pod平滑的赶到其他节点上;6)再查看nginx的状态发现,k8s-node1上的副本已经被迁移到k8s-node上;这时候就可以对k8s-node1进行一些节点维护的操作,如升级内核,升级Docker等;7)节点维护完后,使用uncordon命令解锁k8s-node1,使其重新变得可调度;8)检查节点状态,发现k8s-node1重新变回Ready状态

[root@k8s-master ~]# kubectl get node

NAME              STATUS    AGE

192.168.248.142   Ready     19h

192.168.248.143   Ready     1h

[root@k8s-master ~]# kubectl cordon 192.168.248.143

node "192.168.248.143" cordoned

[root@k8s-master ~]# kubectl get node

NAME              STATUS                     AGE

192.168.248.142   Ready                      19h

192.168.248.143   Ready,SchedulingDisabled   1h

[root@k8s-master ~]# kubectl get pod -o wide

NAME                        READY     STATUS    RESTARTS   AGE       IP           NODE

my-nginx-379829228-pw1wj    1/1       Running   1          17h       172.17.0.2   192.168.248.142

my-nginx-379829228-wmlrt    1/1       Running   1          17h       172.17.0.4   192.168.248.142

mysql-8wz8j                 1/1       Running   0          1h        172.17.0.6   192.168.248.142

mysql-vn2k3                 1/1       Running   0          2h        172.17.0.3   192.168.248.142

sonarqube-438sd             1/1       Running   0          2h        172.17.0.5   192.168.248.142

testnginx-94870464-9nkxn    1/1       Running   0          12m       172.17.0.5   192.168.248.143

testnginx-94870464-k3hdg    1/1       Running   0          12m       172.17.0.4   192.168.248.143

testnginx-94870464-r208x    1/1       Running   0          12m       172.17.0.7   192.168.248.142

testnginx-94870464-s9c9x    1/1       Running   0          12m       172.17.0.8   192.168.248.142

testnginx-94870464-sz6pb    1/1       Running   0          12m       172.17.0.3   192.168.248.143

younginx-2413665018-q2nvq   1/1       Running   0          1h        172.17.0.2   192.168.248.143

[root@k8s-master ~]# kubectl drain 192.168.248.143

node "192.168.248.143" already cordoned

pod "testnginx-94870464-sz6pb" evicted

pod "testnginx-94870464-k3hdg" evicted

pod "younginx-2413665018-q2nvq" evicted

pod "testnginx-94870464-9nkxn" evicted

node "192.168.248.143" drained

[root@k8s-master ~]# kubectl uncordon 192.168.248.143

node "192.168.248.143" uncordoned

[root@k8s-master ~]# kubectl get pod -o wide

NAME                        READY     STATUS    RESTARTS   AGE       IP            NODE

my-nginx-379829228-pw1wj    1/1       Running   1          18h       172.17.0.2    192.168.248.142

my-nginx-379829228-wmlrt    1/1       Running   1          18h       172.17.0.4    192.168.248.142

mysql-8wz8j                 1/1       Running   0          2h        172.17.0.6    192.168.248.142

mysql-vn2k3                 1/1       Running   0          3h        172.17.0.3    192.168.248.142

sonarqube-438sd             1/1       Running   0          3h        172.17.0.5    192.168.248.142

testnginx-94870464-57pkj    1/1       Running   0          55m       172.17.0.11   192.168.248.142

testnginx-94870464-dgqdg    1/1       Running   0          55m       172.17.0.9    192.168.248.142

testnginx-94870464-r208x    1/1       Running   0          1h        172.17.0.7    192.168.248.142

testnginx-94870464-s9c9x    1/1       Running   0          1h        172.17.0.8    192.168.248.142

testnginx-94870464-zhq8g    1/1       Running   0          55m       172.17.0.10   192.168.248.142

younginx-2413665018-djpr4   1/1       Running   0          55m       172.17.0.12   192.168.248.142

17 rolling-update

rolling-update是一个非常重要的命令,对于已经部署并且正在运行的业务,rolling-update提供了不中断业务的更新方式。rolling-update每次起一个新的pod,等新pod完全起来后删除一个旧的pod,然后再起一个新的pod替换旧的pod,直到替换掉所有的pod。

rolling-update需要确保新的版本有不同的name,Version和label,否则会报错 。

18 edit

edit提供了另一种更新resource源的操作,通过edit能够灵活的在一个common的resource基础上,发展出更过的significant resource。

[root@k8s-master yamls]# kubectl get pod mysql-vn2k3 -o yaml >> /tmp/mysql-tmp.yaml

[root@k8s-master yamls]# cd /tmp

[root@k8s-master tmp]# ls

firefox_wzf       mysql-tmp.yaml                                                           systemd-private-d1eff1b44a034169825ab8953453f7b1-rtkit-daemon.service-mZYosX  vmware-config0

hsperfdata_root   ssh-oGW5Z3EQV1MZ                                                         Temp-06a78ebe-5e0b-4213-9d60-db80b8bb4c47                                     vmware-root

ks-script-5YmpDP  systemd-private-d1eff1b44a034169825ab8953453f7b1-chronyd.service-n8j5ru  tmp.nLlvBLCtsm                                                                yum.log

ks-script-IH2Z_2  systemd-private-d1eff1b44a034169825ab8953453f7b1-colord.service-HAlGh3   tmp.tzkmKHEunU                                                                yum_save_tx.2018-10-11.00-34.fI_o2U.yumtx

lua_a0C7G7        systemd-private-d1eff1b44a034169825ab8953453f7b1-cups.service-bsrX2b     tracker-extract-files.1000

[root@k8s-master tmp]# vim mysql-tmp.yaml

/做一些配置文件的修改/

kubectl replace –f mysql-tmp.yaml

19如果一个容器已经在运行,这时需要对一些容器属性进行修改,又不想删除容器,或不方便通过replace的方式进行更新。kubernetes还提供了一种在容器运行时,直接对容器进行修改的方式,就是patch命令。

如前面创建pod的label是app=nginx-2,如果在运行过程中,需要把其label改为app=nginx-3。

[root@k8s-master ~]# kubectl patch pod my-nginx-379829228-pw1wj -p '{"metadata":{"labels":{"app":"nginx-3"}}}'

20 replace更新替换资源

replace命令用于对已有资源进行更新、替换。如前面create中创建的mysql,当我们需要更新resource的一些属性的时候,如果修改副本数量,增加、修改label,更改image版本,修改端口等。都可以直接修改原yaml文件,然后执行replace命令。

注:名字不能被更更新。另外,如果是更新label,原有标签的pod将会与更新label后的rc断开联系,有新label的rc将会创建指定副本数的新的pod,但是默认并不会删除原来的pod。所以此时如果使用get po将会发现pod数翻倍,进一步check会发现原来的pod已经不会被新rc控制

 kubectl replace -f mysql.yaml

21测试稳定性

在node上通过docker stop $(docker ps -a -q)停止容器,发现k8s会自动重新生成新容器

[root@k8s-node ~]# docker stop $(docker ps -a -q)

d6245b7cd653

82c2032630cf

6c63dd03bd40

f2ed49c519d4

b3e1d844a4bd

bcde1cbffb77

4dad2b30e835

2b2c3c3bd62e

72535e112570

e04d5be2b2ca

8f49e0d5dcf4

9ec557300ec5

3469ee4b3fa6

cb8c74f84229

8d5a48ea55a7

f8f68f273f3a

911281e9ff32

6efd0d3ff39c

e893bceabce6

4cdbb18e4a4d

bf6c67772f1d

350faeb2a846

562c22f0f72c

3eb1470101f0

aaae22de2e1f

[root@k8s-node ~]# docker ps -a

CONTAINER ID        IMAGE                                                        COMMAND                  CREATED             STATUS                          PORTS               NAMES

d7de526077a9        nginx                                                        "nginx -g 'daemon ..."   1 second ago        Up Less than a second                               k8s_testnginx.affadd93_testnginx-94870464-dgqdg_default_3f639a8e-cdd2-11e8-94c7-000c29ee98c6_429fea38

9ef0ed5e1452        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           3 seconds ago       Up Less than a second                               k8s_POD.1d520ba5_mysql-vn2k3_default_110cf8c4-cdbe-11e8-94c7-000c29ee98c6_c7954e7c

86d6620c4b61        mysql:5.7.16                                                 "docker-entrypoint..."   3 seconds ago       Up 2 seconds                                        k8s_mysql.f4f82081_mysql-8wz8j_default_cfab4bb8-cdc4-11e8-94c7-000c29ee98c6_a9707fc2

20c1db52c18a        sonarqube:5.6.5                                              "./bin/run.sh"           4 seconds ago       Up 2 seconds                                        k8s_sonarqube.5a6904a7_sonarqube-438sd_default_4eeb9745-cdc0-11e8-94c7-000c29ee98c6_3d66d17e

ca136f13a08d        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           6 seconds ago       Up 3 seconds                                        k8s_POD.1d520ba5_mysql-8wz8j_default_cfab4bb8-cdc4-11e8-94c7-000c29ee98c6_bd4258af

49c191c3debe        nginx                                                        "nginx -g 'daemon ..."   7 seconds ago       Up 3 seconds                                        k8s_my-nginx.a65fe6c_my-nginx-379829228-pw1wj_default_75a3218b-cd41-11e8-993b-000c29ee98c6_d78f4ca0

2ca27f74d8cc        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           7 seconds ago       Up 4 seconds                                        k8s_POD.17af0ba2_sonarqube-438sd_default_4eeb9745-cdc0-11e8-94c7-000c29ee98c6_4bc68f43

720ca258413e        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           8 seconds ago       Up 4 seconds                                        k8s_POD.ae8ee9ac_testnginx-94870464-r208x_default_4d8fdaed-cdd0-11e8-94c7-000c29ee98c6_c1d37620

95dfffa18d88        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           10 seconds ago      Up 8 seconds                                        k8s_POD.ae8ee9ac_testnginx-94870464-57pkj_default_3f6368c2-cdd2-11e8-94c7-000c29ee98c6_2b0a6b54

5557753c36c8        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           12 seconds ago      Up 10 seconds                                       k8s_POD.a8590b41_my-nginx-379829228-wmlrt_default_75a284fc-cd41-11e8-993b-000c29ee98c6_5174be7e

9b99654e2f98        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           12 seconds ago      Up 10 seconds                                       k8s_POD.ae8ee9ac_testnginx-94870464-zhq8g_default_3f51dbae-cdd2-11e8-94c7-000c29ee98c6_b422e753

c76dcef68f3b        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           14 seconds ago      Up 11 seconds                                       k8s_POD.ae8ee9ac_testnginx-94870464-s9c9x_default_4d8f42b0-cdd0-11e8-94c7-000c29ee98c6_a07a56a6

bdee5d0dcf94        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           15 seconds ago      Up 13 seconds                                       k8s_POD.ae8ee9ac_younginx-2413665018-djpr4_default_3f52038e-cdd2-11e8-94c7-000c29ee98c6_581d115a

3e45d88946e4        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           19 seconds ago      Up 15 seconds                                       k8s_POD.ae8ee9ac_testnginx-94870464-dgqdg_default_3f639a8e-cdd2-11e8-94c7-000c29ee98c6_06050950

42d8b7f25787        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           19 seconds ago      Up 15 seconds                                       k8s_POD.a8590b41_my-nginx-379829228-pw1wj_default_75a3218b-cd41-11e8-993b-000c29ee98c6_3b93844c

8f017385d1f8        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           22 seconds ago      Created                                             k8s_POD.ae8ee9ac_testnginx-94870464-r208x_default_4d8fdaed-cdd0-11e8-94c7-000c29ee98c6_db46c731

730a53238394        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           22 seconds ago      Created                                             k8s_POD.1d520ba5_mysql-vn2k3_default_110cf8c4-cdbe-11e8-94c7-000c29ee98c6_23252339

06234eb7bc47        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           23 seconds ago      Created                                             k8s_POD.1d520ba5_mysql-8wz8j_default_cfab4bb8-cdc4-11e8-94c7-000c29ee98c6_9f5893a2

59258eb463ad        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           44 seconds ago      Created                                             k8s_POD.1d520ba5_mysql-vn2k3_default_110cf8c4-cdbe-11e8-94c7-000c29ee98c6_9fdc5868

8203403a4b26        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           48 seconds ago      Created                                             k8s_POD.1d520ba5_mysql-8wz8j_default_cfab4bb8-cdc4-11e8-94c7-000c29ee98c6_89d3d41b

7fec434356b3        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           51 seconds ago      Created                                             k8s_POD.a8590b41_my-nginx-379829228-wmlrt_default_75a284fc-cd41-11e8-993b-000c29ee98c6_72d9e2e8

d562725bb32a        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           55 seconds ago      Created                                             k8s_POD.a8590b41_my-nginx-379829228-pw1wj_default_75a3218b-cd41-11e8-993b-000c29ee98c6_957a8f06

aed15bcffdad        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           58 seconds ago      Created                                             k8s_POD.ae8ee9ac_testnginx-94870464-dgqdg_default_3f639a8e-cdd2-11e8-94c7-000c29ee98c6_f720e

备注:RC(Replication Controller)

    Replication Controller确保任何时候Kubernetes集群中有指定数量的pod副本(replicas)在运行, 如果少于指定数量的pod副本(replicas),Replication Controller会启动新的Container,反之会杀死多余的以保证数量不变。Replication Controller使用预先定义的pod模板创建pods,一旦创建成功,pod 模板和创建的pods没有任何关联,可以修改pod 模板而不会对已创建pods有任何影响,也可以直接更新通过Replication Controller创建的pods。对于利用pod 模板创建的pods,Replication Controller根据label selector来关联,通过修改pods的label可以删除对应的pods。

22 进入创建的pod内

[root@k8s-master ~]# kubectl get pod

NAME                        READY     STATUS    RESTARTS   AGE

my-nginx-379829228-pw1wj    1/1       Running   2          19h

my-nginx-379829228-wmlrt    1/1       Running   2          19h

mysql-8wz8j                 1/1       Running   1          4h

mysql-vn2k3                 1/1       Running   1          4h

sonarqube-438sd             1/1       Running   1          4h

testnginx-94870464-57pkj    1/1       Running   1          2h

testnginx-94870464-dgqdg    1/1       Running   1          2h

testnginx-94870464-r208x    1/1       Running   1          2h

testnginx-94870464-s9c9x    1/1       Running   1          2h

testnginx-94870464-zhq8g    1/1       Running   1          2h

younginx-2413665018-djpr4   1/1       Running   1          2h

[root@k8s-master ~]# kubectl exec my-nginx-379829228-pw1wj  date

Fri Oct 12 06:27:59 UTC 2018

[root@k8s-master ~]# kubectl exec my-nginx-379829228-pw1wj  -it bash

root@my-nginx-379829228-pw1wj:/# ifconfig

23 attach

     attach命令类似于docker的attach命令,可以直接查看容器中以daemon形式运行的进程的输出,效果类似于logs -f,退出查看使用ctrl-c。

[root@k8s-master ~]# kubectl get pod

NAME                        READY     STATUS    RESTARTS   AGE

my-nginx-379829228-pw1wj    1/1       Running   2          21h

my-nginx-379829228-wmlrt    1/1       Running   2          21h

mysql-8wz8j                 1/1       Running   1          5h

mysql-vn2k3                 1/1       Running   1          6h

sonarqube-438sd             1/1       Running   1          6h

testnginx-94870464-57pkj    1/1       Running   1          4h

testnginx-94870464-dgqdg    1/1       Running   1          4h

testnginx-94870464-r208x    1/1       Running   1          4h

testnginx-94870464-s9c9x    1/1       Running   1          4h

testnginx-94870464-zhq8g    1/1       Running   1          4h

younginx-2413665018-djpr4   1/1       Running   1          4h

[root@k8s-master ~]# kubectl attach mysql-vn2k3

If you don't see a command prompt, try pressing enter.

Initializing database

2018-10-12T06:08:05.686626Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2018-10-12T06:08:08.351170Z 0 [Warning] InnoDB: New log files created, LSN=45790

2018-10-12T06:08:08.865590Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2018-10-12T06:08:08.976485Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 311992bb-cde5-11e8-beb9-0242ac11000c.

2018-10-12T06:08:08.979892Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2018-10-12T06:08:08.990000Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

2018-10-12T06:08:13.469651Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.

2018-10-12T06:08:13.473797Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.

2018-10-12T06:08:13.473872Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.

2018-10-12T06:08:13.473904Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.

2018-10-12T06:08:13.473994Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.

Database initialized

MySQL init process in progress...

MySQL init process in progress...

2018-10-12T06:08:18.765789Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2018-10-12T06:08:18.853393Z 0 [Note] mysqld (mysqld 5.7.16) starting as process 53 ...

MySQL init process in progress...

MySQL init process in progress...

2018-10-12T06:08:42.181567Z 0 [Note] InnoDB: PUNCH HOLE support available

2018-10-12T06:08:42.376104Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins

2018-10-12T06:08:42.376825Z 0 [Note] InnoDB: Uses event mutexes

2018-10-12T06:08:42.376866Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier

2018-10-12T06:08:42.376879Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3

2018-10-12T06:08:42.376890Z 0 [Note] InnoDB: Using Linux native AIO

2018-10-12T06:08:42.384160Z 0 [Note] InnoDB: Number of pools: 1

 

Logo

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

更多推荐