k8s之使用配置

创建App

创建yaml文件

[root@localhost ~]# vim nginx.yaml 

apiVersion: v1
kind: ReplicationController
metadata:
  name: my-nginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 80

启动nginx

[root@localhost ~]# kubectl create -f nginx.yaml 
replicationcontroller "my-nginx" created
[root@localhost ~]# kubectl get pods
NAME             READY     STATUS              RESTARTS   AGE
my-nginx-lwr99   0/1       ContainerCreating   0          7s

查看日志

[root@localhost ~]# kubectl describe pod my-nginx-lwr99
Name:       my-nginx-lwr99
Namespace:  default
Node:       192.168.56.202/192.168.56.202
Start Time: Mon, 23 Apr 2018 14:24:57 +0800
Labels:     app=nginx
Status:     Running
IP:     172.30.55.2
Controllers:    ReplicationController/my-nginx
Containers:
  nginx:
    Container ID:   docker://4a01698fd122030de55c5b1caf42b256e28fe7264d32f97b4c89fdd0ab34f83d
    Image:      nginx
    Image ID:       docker-pullable://docker.io/nginx@sha256:18156dcd747677b03968621b2729d46021ce83a5bc15118e5bcced925fb4ebb9
    Port:       80/TCP
    Limits:
      cpu:  500m
      memory:   128Mi
    Requests:
      cpu:          500m
      memory:           128Mi
    State:          Running
      Started:          Mon, 23 Apr 2018 14:25:18 +0800
    Ready:          True
    Restart Count:      0
    Volume Mounts:      <none>
    Environment Variables:  <none>
Conditions:
  Type      Status
  Initialized   True 
  Ready     True 
  PodScheduled  True 
No volumes.
QoS Class:  Guaranteed
Tolerations:    <none>
Events:
  FirstSeen LastSeen    Count   From                SubObjectPath       Type        Reason          Message
  --------- --------    -----   ----                -------------       --------    ------          -------
  54s       54s     1   {default-scheduler }                    Normal      Scheduled       Successfully assigned my-nginx-lwr99 to 192.168.56.202
  50s       50s     1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Pulling         pulling image "nginx"
  54s       34s     2   {kubelet 192.168.56.202}                Warning     MissingClusterDNS   kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
  34s       34s     1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Pulled          Successfully pulled image "nginx"
  34s       34s     1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Created         Created container with docker id 4a01698fd122; Security:[seccomp=unconfined]
  33s       33s     1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Started         Started container with docker id 4a01698fd122

查看结果

[root@localhost ~]# kubectl get pods
NAME             READY     STATUS    RESTARTS   AGE
my-nginx-lwr99   1/1       Running   0          1m

在node上查看结果

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                                                        COMMAND                  CREATED              STATUS              PORTS               NAMES
4a01698fd122        nginx                                                        "nginx -g 'daemon ..."   46 seconds ago       Up 44 seconds                           k8s_nginx.cf5bec70_my-nginx-lwr99_default_0b2a62a8-46bf-11e8-94fc-000c29619eeb_2d98d52a
ce5d302bc2ed        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           About a minute ago   Up About a minute                       k8s_POD.a8590b41_my-nginx-lwr99_default_0b2a62a8-46bf-11e8-94fc-000c29619eeb_cf234267

动态添加配置

对于上面创建的app是一个,下面我们动态的再添加一个
执行命令:

kubectl scale replicationcontroller --replicas=2 my-nginx
[root@localhost ~]# kubectl scale replicationcontroller --replicas=2 my-nginx
replicationcontroller "my-nginx" scaled
[root@localhost ~]# kubectl get pods
NAME             READY     STATUS              RESTARTS   AGE
my-nginx-8m40c   0/1       ContainerCreating   0          12s
my-nginx-lwr99   1/1       Running             0          22m
[root@localhost ~]# kubectl get rc
NAME       DESIRED   CURRENT   READY     AGE
my-nginx   2         2         2         25m

查看结果

[root@localhost ~]# kubectl describe pod my-nginx
Name:       my-nginx-8m40c
Namespace:  default
Node:       192.168.56.201/192.168.56.201
Start Time: Mon, 23 Apr 2018 14:47:31 +0800
Labels:     app=nginx
Status:     Running
IP:     172.30.31.2
Controllers:    ReplicationController/my-nginx
Containers:
  nginx:
    Container ID:   docker://925085542d0d105693f72f4b4f727367aa8df69505e2424a90a19df27928bd09
    Image:      nginx
    Image ID:       docker-pullable://docker.io/nginx@sha256:18156dcd747677b03968621b2729d46021ce83a5bc15118e5bcced925fb4ebb9
    Port:       80/TCP
    Limits:
      cpu:  500m
      memory:   128Mi
    Requests:
      cpu:          500m
      memory:           128Mi
    State:          Running
      Started:          Mon, 23 Apr 2018 14:47:50 +0800
    Ready:          True
    Restart Count:      0
    Volume Mounts:      <none>
    Environment Variables:  <none>
Conditions:
  Type      Status
  Initialized   True 
  Ready     True 
  PodScheduled  True 
No volumes.
QoS Class:  Guaranteed
Tolerations:    <none>
Events:
  FirstSeen LastSeen    Count   From                SubObjectPath       Type        Reason          Message
  --------- --------    -----   ----                -------------       --------    ------          -------
  12m       12m     1   {default-scheduler }                    Normal      Scheduled       Successfully assigned my-nginx-8m40c to 192.168.56.201
  12m       12m     1   {kubelet 192.168.56.201}    spec.containers{nginx}  Normal      Pulling         pulling image "nginx"
  12m       12m     2   {kubelet 192.168.56.201}                Warning     MissingClusterDNS   kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
  12m       12m     1   {kubelet 192.168.56.201}    spec.containers{nginx}  Normal      Pulled          Successfully pulled image "nginx"
  12m       12m     1   {kubelet 192.168.56.201}    spec.containers{nginx}  Normal      Created         Created container with docker id 925085542d0d; Security:[seccomp=unconfined]
  12m       12m     1   {kubelet 192.168.56.201}    spec.containers{nginx}  Normal      Started         Started container with docker id 925085542d0d

Name:       my-nginx-lwr99
Namespace:  default
Node:       192.168.56.202/192.168.56.202
Start Time: Mon, 23 Apr 2018 14:24:57 +0800
Labels:     app=nginx
Status:     Running
IP:     172.30.55.2
Controllers:    ReplicationController/my-nginx
Containers:
  nginx:
    Container ID:   docker://4a01698fd122030de55c5b1caf42b256e28fe7264d32f97b4c89fdd0ab34f83d
    Image:      nginx
    Image ID:       docker-pullable://docker.io/nginx@sha256:18156dcd747677b03968621b2729d46021ce83a5bc15118e5bcced925fb4ebb9
    Port:       80/TCP
    Limits:
      cpu:  500m
      memory:   128Mi
    Requests:
      cpu:          500m
      memory:           128Mi
    State:          Running
      Started:          Mon, 23 Apr 2018 14:25:18 +0800
    Ready:          True
    Restart Count:      0
    Volume Mounts:      <none>
    Environment Variables:  <none>
Conditions:
  Type      Status
  Initialized   True 
  Ready     True 
  PodScheduled  True 
No volumes.
QoS Class:  Guaranteed
Tolerations:    <none>
Events:
  FirstSeen LastSeen    Count   From                SubObjectPath       Type        Reason          Message
  --------- --------    -----   ----                -------------       --------    ------          -------
  35m       35m     1   {default-scheduler }                    Normal      Scheduled       Successfully assigned my-nginx-lwr99 to 192.168.56.202
  35m       35m     1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Pulling         pulling image "nginx"
  35m       35m     2   {kubelet 192.168.56.202}                Warning     MissingClusterDNS   kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
  35m       35m     1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Pulled          Successfully pulled image "nginx"
  35m       35m     1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Created         Created container with docker id 4a01698fd122; Security:[seccomp=unconfined]
  35m       35m     1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Started         Started container with docker id 4a01698fd122

[root@localhost ~]# 

Node节点删除

[root@localhost ~]# kubectl get node
NAME             STATUS    AGE
192.168.56.201   Ready     4d
192.168.56.202   Ready     4d
[root@localhost ~]# kubectl delete node 192.168.56.201
node "192.168.56.201" deleted
[root@localhost ~]# kubectl get node
NAME             STATUS    AGE
192.168.56.202   Ready     4d

从上面的执行命令结束后,发现现在k8s就只有一个节点了。但是我们发现服务器还是2个

[root@localhost ~]# kubectl get pods
NAME             READY     STATUS    RESTARTS   AGE
my-nginx-lwr99   1/1       Running   0          38m
my-nginx-wwv4j   1/1       Running   0          47s

再次describe发现ip端都是来自同一个网段,如下:

[root@localhost ~]# kubectl describe pod my-nginx
Name:       my-nginx-lwr99
Namespace:  default
Node:       192.168.56.202/192.168.56.202
Start Time: Mon, 23 Apr 2018 14:24:57 +0800
Labels:     app=nginx
Status:     Running
IP:     172.30.55.2
Controllers:    ReplicationController/my-nginx
Containers:
  nginx:
    Container ID:   docker://4a01698fd122030de55c5b1caf42b256e28fe7264d32f97b4c89fdd0ab34f83d
    Image:      nginx
    Image ID:       docker-pullable://docker.io/nginx@sha256:18156dcd747677b03968621b2729d46021ce83a5bc15118e5bcced925fb4ebb9
    Port:       80/TCP
    Limits:
      cpu:  500m
      memory:   128Mi
    Requests:
      cpu:          500m
      memory:           128Mi
    State:          Running
      Started:          Mon, 23 Apr 2018 14:25:18 +0800
    Ready:          True
    Restart Count:      0
    Volume Mounts:      <none>
    Environment Variables:  <none>
Conditions:
  Type      Status
  Initialized   True 
  Ready     True 
  PodScheduled  True 
No volumes.
QoS Class:  Guaranteed
Tolerations:    <none>
Events:
  FirstSeen LastSeen    Count   From                SubObjectPath       Type        Reason          Message
  --------- --------    -----   ----                -------------       --------    ------          -------
  40m       40m     1   {default-scheduler }                    Normal      Scheduled       Successfully assigned my-nginx-lwr99 to 192.168.56.202
  40m       40m     1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Pulling         pulling image "nginx"
  40m       40m     2   {kubelet 192.168.56.202}                Warning     MissingClusterDNS   kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
  40m       40m     1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Pulled          Successfully pulled image "nginx"
  40m       40m     1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Created         Created container with docker id 4a01698fd122; Security:[seccomp=unconfined]
  40m       40m     1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Started         Started container with docker id 4a01698fd122

Name:       my-nginx-wwv4j
Namespace:  default
Node:       192.168.56.202/192.168.56.202
Start Time: Mon, 23 Apr 2018 15:02:36 +0800
Labels:     app=nginx
Status:     Running
IP:     172.30.55.3
Controllers:    ReplicationController/my-nginx
Containers:
  nginx:
    Container ID:   docker://b3446831953f87524167b134e863be44d1d55093344df41bb2d8c11b4a348966
    Image:      nginx
    Image ID:       docker-pullable://docker.io/nginx@sha256:18156dcd747677b03968621b2729d46021ce83a5bc15118e5bcced925fb4ebb9
    Port:       80/TCP
    Limits:
      cpu:  500m
      memory:   128Mi
    Requests:
      cpu:          500m
      memory:           128Mi
    State:          Running
      Started:          Mon, 23 Apr 2018 15:02:51 +0800
    Ready:          True
    Restart Count:      0
    Volume Mounts:      <none>
    Environment Variables:  <none>
Conditions:
  Type      Status
  Initialized   True 
  Ready     True 
  PodScheduled  True 
No volumes.
QoS Class:  Guaranteed
Tolerations:    <none>
Events:
  FirstSeen LastSeen    Count   From                SubObjectPath       Type        Reason          Message
  --------- --------    -----   ----                -------------       --------    ------          -------
  2m        2m      1   {default-scheduler }                    Normal      Scheduled       Successfully assigned my-nginx-wwv4j to 192.168.56.202
  2m        2m      1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Pulling         pulling image "nginx"
  2m        2m      2   {kubelet 192.168.56.202}                Warning     MissingClusterDNS   kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
  2m        2m      1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Pulled          Successfully pulled image "nginx"
  2m        2m      1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Created         Created container with docker id b3446831953f; Security:[seccomp=unconfined]
  2m        2m      1   {kubelet 192.168.56.202}    spec.containers{nginx}  Normal      Started         Started container with docker id b3446831953f

[root@localhost ~]# 

添加新的node

在刚刚删除的node上从新启动节点,执行下面的命令

[root@localhost ~]# systemctl start kubelet
[root@localhost ~]# systemctl restart kubelet

从M上查看结果

[root@localhost ~]# kubectl get node
NAME             STATUS    AGE
192.168.56.201   Ready     12s
192.168.56.202   Ready     4d

添加可以访问外网

上面的app是不可以访问外网的,如果想要访问外网需要创建一个service服务。

[root@localhost ~]# cat nginxsrc.yaml 
apiVersion: v1 
kind: Service 
metadata: 
  name: nginx-service 
spec: 
  externalIPs: 
    - 192.168.56.200 
  ports: 
  - port: 8000 
    targetPort: 80 
    protocol: TCP 
  selector: 
    app: nginx

https://www.58jb.com/html/180.html
https://www.cnblogs.com/junneyang/p/6274689.html

猜你喜欢

转载自blog.51cto.com/sgk2011/2106862