k8s 1. 先跑起来

参考:每天5分钟玩转Kubernetes, Coldman著,第一章,先把kubernetes跑起来

1. 在线练习:

https://kubernetes.io/docs/tutorials/kubernetes-basics/create-cluster/cluster-interactive/

2. 

Kubernetes Bootcamp Terminal

$
$ minikube version
minikube version: v1.6.2
commit: 54f28ac5d3a815d1196cd5d57d707439ee4bb392
$ minikube start
* minikube v1.6.2 on Ubuntu 18.04
* Selecting 'none' driver from user configuration (alternates: [])
* Running on localhost (CPUs=2, Memory=2461MB, Disk=47990MB) ...
* OS release is Ubuntu 18.04.3 LTS
* Preparing Kubernetes v1.17.0 on Docker '18.09.7' ...
  - kubelet.resolv-conf=/run/systemd/resolve/resolv.conf
* Pulling images ...
* Launching Kubernetes ...
* Configuring local host environment ...
* Waiting for cluster to come online ...
* Done! kubectl is now configured to use "minikube"
$ kubectl get nodes
NAME       STATUS   ROLES    AGE     VERSION
minikube   Ready    master   2m32s   v1.17.0
$ hostname
minikube
$ kubectl cluster-info
Kubernetes master is running at https://172.17.0.56:8443
KubeDNS is running at https://172.17.0.56:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

 

[root@k8s-master ~]# docker pull docker.io/jocatalin/kubernetes-bootcamp:v1
v1: Pulling from jocatalin/kubernetes-bootcamp
5c90d4a2d1a8: Pull complete 
ab30c63719b1: Pull complete 
29d0bc1e8c52: Pull complete 
d4fe0dc68927: Pull complete 
dfa9e924f957: Pull complete 
Digest: sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Status: Downloaded newer image for jocatalin/kubernetes-bootcamp:v1
docker.io/jocatalin/kubernetes-bootcamp:v1
[root@k8s-master ~]# kubectl run kubernetes-bootcamp --image=docker.io/jocatalin/kubernetes-bootcamp:v1 --port=8082
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/kubernetes-bootcamp created
[root@k8s-master ~]# kubectl get pods -o wide
NAME                                   READY   STATUS    RESTARTS   AGE     IP       NODE     NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-755c59d578-6hj7g   0/1     Pending   0          10s     <none>   <none>   <none>           <none>
nginx-6db489d4b7-hlgwm                 0/1     Pending   0          6d18h   <none>   <none>   <none>           <none>
[root@k8s-master ~]# kubectl get nodes
NAME         STATUS   ROLES    AGE     VERSION
k8s-master   Ready    master   6d22h   v1.16.0
[root@k8s-master ~]# kubectl get pods -o wide
NAME                                   READY   STATUS    RESTARTS   AGE     IP       NODE     NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-755c59d578-6hj7g   0/1     Pending   0          58s     <none>   <none>   <none>           <none>
nginx-6db489d4b7-hlgwm                 0/1     Pending   0          6d18h   <none>   <none>   <none>           <none>
[root@k8s-master ~]# kubectl get po -n kube-system
NAME                                 READY   STATUS    RESTARTS   AGE
coredns-58cc8c89f4-pwfvx             1/1     Running   2          6d22h
coredns-58cc8c89f4-tdv25             1/1     Running   2          6d22h
etcd-k8s-master                      1/1     Running   2          6d22h
kube-apiserver-k8s-master            1/1     Running   2          6d22h
kube-controller-manager-k8s-master   1/1     Running   2          6d22h
kube-flannel-ds-amd64-crgkj          1/1     Running   3          6d22h
kube-proxy-lsh2m                     1/1     Running   2          6d22h
kube-scheduler-k8s-master            1/1     Running   2          6d22h
[root@k8s-master ~]# kubectl taint node k8s-master node-role.kubernetes.io/master-

node/k8s-master untainted
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get po -o wide
NAME                                   READY   STATUS              RESTARTS   AGE     IP           NODE         NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-755c59d578-6hj7g   1/1     Running             0          2m18s   10.244.0.9   k8s-master   <none>           <none>
nginx-6db489d4b7-hlgwm                 0/1     ContainerCreating   0          6d18h   <none>       k8s-master   <none>           <none>
[root@k8s-master ~]# kubectl get po -o wide
NAME                                   READY   STATUS    RESTARTS   AGE         IP           NODE         NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-755c59d578-6hj7g   1/1     Running   0          <invalid>   10.244.0.9   k8s-master   <none>           <none>
nginx-6db489d4b7-hlgwm                 1/1     Running   0          6d18h       10.244.0.8   k8s-master   <none>           <none>

[root@k8s-master ~]# kubectl expose deployment/kubernetes-bootcamp   --type="NodePort"   --port 8082
service/kubernetes-bootcamp exposed
[root@k8s-master ~]# kubectl get svc
NAME                  TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes            ClusterIP   10.1.0.1       <none>        443/TCP          6d22h
kubernetes-bootcamp   NodePort    10.1.105.157   <none>        8082:30742/TCP   13s
[root@k8s-master ~]# curl k8s-master:30742
curl: (7) Failed connect to k8s-master:30742; 拒绝连接
[root@k8s-master ~]# curl http://k8s-master:30742
curl: (7) Failed connect to k8s-master:30742; 拒绝连接
[root@k8s-master ~]# kubectl get po -o wide
NAME                                   READY   STATUS    RESTARTS   AGE         IP           NODE         NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-755c59d578-6hj7g   1/1     Running   0          <invalid>   10.244.0.9   k8s-master   <none>           <none>
nginx-6db489d4b7-hlgwm                 1/1     Running   0          6d18h       10.244.0.8   k8s-master   <none>           <none>
[root@k8s-master ~]# 
[root@k8s-master ~]# curl http://10.244.0.8
<!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>



[root@k8s-master ~]# kubectl get deployments
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   1/1     1            1           2m18s
nginx                 1/1     1            1           6d18h
[root@k8s-master ~]# kubectl scale deployments/kubernetes-bootcamp --replicas=3
deployment.apps/kubernetes-bootcamp scaled
[root@k8s-master ~]# kubectl get deployments
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   3/3     3            3           2m57s
nginx                 1/1     1            1           6d18h
[root@k8s-master ~]# ping k8s-master
PING k8s-master (192.168.159.135) 56(84) bytes of data.
64 bytes from k8s-master (192.168.159.135): icmp_seq=1 ttl=64 time=0.052 ms
64 bytes from k8s-master (192.168.159.135): icmp_seq=2 ttl=64 time=0.051 ms
^C
--- k8s-master ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.051/0.051/0.052/0.007 ms
[root@k8s-master ~]# curl k8s-master:8082
curl: (7) Failed connect to k8s-master:8082; 拒绝连接
[root@k8s-master ~]# curl k8s-master:30742
curl: (7) Failed connect to k8s-master:30742; 拒绝连接
[root@k8s-master ~]# kubectl scale deployments/kubernetes-bootcamp --replicas=2
deployment.apps/kubernetes-bootcamp scaled
[root@k8s-master ~]# kubectl get deployments                                   
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   2/2     2            2           3m57s
nginx                 1/1     1            1           6d18h
[root@k8s-master ~]# kubectl scale deployments/kubernetes-bootcamp --replicas=2
deployment.apps/kubernetes-bootcamp scaled
[root@k8s-master ~]# kubectl get deployments                                   
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   2/2     2            2           3m57s
nginx                 1/1     1            1           6d18h
[root@k8s-master ~]# kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment.apps/kubernetes-bootcamp image updated
[root@k8s-master ~]# kubectl get po
NAME                                   READY   STATUS              RESTARTS   AGE
kubernetes-bootcamp-755c59d578-6hj7g   1/1     Running             0          5m36s
kubernetes-bootcamp-755c59d578-c7q8h   1/1     Terminating         0          2m47s
kubernetes-bootcamp-fd5fbfd96-95nfg    1/1     Running             0          11s
kubernetes-bootcamp-fd5fbfd96-rs476    0/1     ContainerCreating   0          1s
nginx-6db489d4b7-hlgwm                 1/1     Running             0          6d19h
[root@k8s-master ~]# kubectl get po
NAME                                   READY   STATUS        RESTARTS   AGE
kubernetes-bootcamp-755c59d578-6hj7g   1/1     Terminating   0          5m54s
kubernetes-bootcamp-755c59d578-c7q8h   1/1     Terminating   0          3m5s
kubernetes-bootcamp-fd5fbfd96-95nfg    1/1     Running       0          29s
kubernetes-bootcamp-fd5fbfd96-rs476    1/1     Running       0          19s
nginx-6db489d4b7-hlgwm                 1/1     Running       0          6d19h
[root@k8s-master ~]# kubectl get po
NAME                                   READY   STATUS        RESTARTS   AGE
kubernetes-bootcamp-755c59d578-6hj7g   1/1     Terminating   0          6m4s
kubernetes-bootcamp-755c59d578-c7q8h   1/1     Terminating   0          3m15s
kubernetes-bootcamp-fd5fbfd96-95nfg    1/1     Running       0          39s
kubernetes-bootcamp-fd5fbfd96-rs476    1/1     Running       0          29s
nginx-6db489d4b7-hlgwm                 1/1     Running       0          6d19h
[root@k8s-master ~]# kubectl get po
NAME                                  READY   STATUS    RESTARTS   AGE
kubernetes-bootcamp-fd5fbfd96-95nfg   1/1     Running   0          77s
kubernetes-bootcamp-fd5fbfd96-rs476   1/1     Running   0          67s
nginx-6db489d4b7-hlgwm                1/1     Running   0          6d19h
[root@k8s-master ~]# kubectl get svc
NAME                  TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes            ClusterIP   10.1.0.1       <none>        443/TCP          6d22h
kubernetes-bootcamp   NodePort    10.1.105.157   <none>        8082:30742/TCP   12m
[root@k8s-master ~]# curl 10.1.105.157:8082
curl: (7) Failed connect to 10.1.105.157:8082; 拒绝连接
[root@k8s-master ~]# kubectl rollout undo deployments/kubernetes-bootcamp
deployment.apps/kubernetes-bootcamp rolled back
[root@k8s-master ~]# kubectl get po
NAME                                   READY   STATUS        RESTARTS   AGE
kubernetes-bootcamp-755c59d578-dbfkk   1/1     Running       0          6s
kubernetes-bootcamp-755c59d578-tqjkj   1/1     Running       0          4s
kubernetes-bootcamp-fd5fbfd96-95nfg    1/1     Terminating   0          2m21s
kubernetes-bootcamp-fd5fbfd96-rs476    1/1     Terminating   0          2m11s
nginx-6db489d4b7-hlgwm                 1/1     Running       0          6d19h
[root@k8s-master ~]# 

kubernetes-bootcamp需暴露端口8080:

[root@k8s-master ~]# kubectl get po -o wide
NAME                                   READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-755c59d578-dbfkk   1/1     Running   0          8m1s    10.244.0.14   k8s-master   <none>           <none>
kubernetes-bootcamp-755c59d578-tqjkj   1/1     Running   0          7m59s   10.244.0.15   k8s-master   <none>           <none>
nginx-6db489d4b7-hlgwm                 1/1     Running   0          6d19h   10.244.0.8    k8s-master   <none>           <none>
[root@k8s-master ~]# kubectl delete deploy/kubernetes-bootcamp
deployment.apps "kubernetes-bootcamp" deleted
[root@k8s-master ~]# kubectl delete svc/kubernetes-bootcamp
service "kubernetes-bootcamp" deleted
[root@k8s-master ~]# kubectl run kubernetes-bootcamp --image=docker.io/jocatalin/kubernetes-bootcamp:v1 --port=8080
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/kubernetes-bootcamp created
[root@k8s-master ~]# kubectl expose deployment/kubernetes-bootcamp   --type="NodePort"   --port 8080
service/kubernetes-bootcamp exposed
[root@k8s-master ~]# kubectl get po -o wide
NAME                                 READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-dd9784f6-xrr2x   1/1     Running   0          84s     10.244.0.16   k8s-master   <none>           <none>
nginx-6db489d4b7-hlgwm               1/1     Running   0          6d19h   10.244.0.8    k8s-master   <none>           <none>
[root@k8s-master ~]# kubectl exec -it kubernetes-bootcamp-dd9784f6-xrr2x curl localhost:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-dd9784f6-xrr2x | v=1
[root@k8s-master ~]# curl 10.244.0.16:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-dd9784f6-xrr2x | v=1
[root@k8s-master ~]# kubectl get svc
NAME                  TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes            ClusterIP   10.1.0.1       <none>        443/TCP          6d22h
kubernetes-bootcamp   NodePort    10.1.120.140   <none>        8080:30991/TCP   90s
[root@k8s-master ~]# curl 10.1.120.140:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-dd9784f6-xrr2x | v=1
[root@k8s-master ~]# curl k8s-master:30991
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-dd9784f6-xrr2x | v=1
[root@k8s-master ~]# 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值