Kubernetes相关操作记录

本文详细介绍了在三台Ubuntu虚拟机上部署Kubernetes集群的过程,包括Docker的安装、Kubernetes组件的配置、Pod与Deployment的管理、服务的暴露与访问、kube-proxy的作用及kube-dns的解析机制。通过实例演示了Kubernetes的扩缩容、镜像更新、服务配置与访问等关键操作。
摘要由CSDN通过智能技术生成

跟着学习操作了一遍,还不是很了解,先粗糙记录下
参考并感谢
三台ubuntu虚拟机并安装好了docker,后续的所有操作都是使用root账户。
虚拟机具体信息如下表:

系统类型IP地址节点角色Hostname
ubuntu16.04168.174.135workerserver01
ubuntu16.04168.174.136masterserver02
ubuntu16.04168.174.137workerserver03

kubectl version

kubectl get pods

kubectl run kubernetes-bootcamp --image=jocatalin/kubernetes-bootcamp:v1 --port=8080

kubectl get deployments

kubectl get pods -o wide

kubectl describe pods kubernetes-bootcamp-6b7849c495-z6mxx

kubectl proxy
curl http://localhost:8001/api/v1/proxy/namespaces/default/pods/kubernetes-bootcamp-6b7849c495-z6mxx

root@server02:~# curl http://localhost:8001/api/v1/proxy/namespaces/default/pods/kubernetes-bootcamp-6b7849c495-z6mxx/
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6b7849c495-z6mxx | v=1

扩缩容

root@server02:~# kubectl get deploy
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   1         1         1            1           20m

扩容命令

kubectl scale deploy kubernetes-bootcamp --replicas=4

root@server02:~# kubectl scale deploy kubernetes-bootcamp --replicas=4
deployment "kubernetes-bootcamp" scaled

root@server02:~# kubectl get deploy
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   4         4         4            2           21m

ContainerCreating中

root@server02:~# kubectl get pods
NAME                                   READY     STATUS              RESTARTS   AGE
kubernetes-bootcamp-6b7849c495-4n9gd   1/1       Running             0          1m
kubernetes-bootcamp-6b7849c495-c9d6q   0/1       ContainerCreating   0          1m
kubernetes-bootcamp-6b7849c495-txpfz   0/1       ContainerCreating   0          1m
kubernetes-bootcamp-6b7849c495-z6mxx   1/1       Running             0          22m
root@server02:~# kubectl get pods -o wide
NAME                                   READY     STATUS              RESTARTS   AGE       IP              NODE
kubernetes-bootcamp-6b7849c495-4n9gd   1/1       Running             0          2m        172.20.40.193   192.168.174.137
kubernetes-bootcamp-6b7849c495-c9d6q   0/1       ContainerCreating   0          2m        <none>          192.168.174.135
kubernetes-bootcamp-6b7849c495-txpfz   0/1       ContainerCreating   0          2m        <none>          192.168.174.136
kubernetes-bootcamp-6b7849c495-z6mxx   1/1       Running             0          22m       172.20.40.192   192.168.174.137
root@server02:~# kubectl get pods -o wide
NAME                                   READY     STATUS    RESTARTS   AGE       IP              NODE
kubernetes-bootcamp-6b7849c495-4n9gd   1/1       Running   0          8m        172.20.40.193   192.168.174.137
kubernetes-bootcamp-6b7849c495-c9d6q   1/1       Running   0          8m        172.20.188.0    192.168.174.135
kubernetes-bootcamp-6b7849c495-txpfz   1/1       Running   0          8m        172.20.6.0      192.168.174.136
kubernetes-bootcamp-6b7849c495-z6mxx   1/1       Running   0          29m       172.20.40.192   192.168.174.137

缩容,则将replica减小即可

kubectl scale deploy kubernetes-bootcamp --replicas=2

两个pods在Terminating中

root@server02:~# kubectl get pods -o wide
NAME                                   READY     STATUS        RESTARTS   AGE       IP              NODE
kubernetes-bootcamp-7689dc585d-6glz5   1/1       Terminating   0          7m        172.20.40.194   192.168.174.137
kubernetes-bootcamp-7689dc585d-8sb2m   1/1       Terminating   0          7m        172.20.188.2    192.168.174.135
kubernetes-bootcamp-7689dc585d-ltcrd   1/1       Running       0          7m        172.20.6.1      192.168.174.136
kubernetes-bootcamp-7689dc585d-psrsf   1/1       Running       0          7m        172.20.188.1    192.168.174.135

完成缩容

root@server02:~# kubectl get pods -o wide
NAME                                   READY     STATUS    RESTARTS   AGE       IP             NODE
kubernetes-bootcamp-7689dc585d-ltcrd   1/1       Running   0          8m        172.20.6.1     192.168.174.136
kubernetes-bootcamp-7689dc585d-psrsf   1/1       Running   0          8m        172.20.188.1   192.168.174.135

更新镜像

kubectl set image deploy kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2

root@server02:~# kubectl set image deploy kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment "kubernetes-bootcamp" image updated

查看更新结果
kubectl rollout status deploy kubernetes-bootcamp

root@server02:~# kubectl rollout status deploy kubernetes-bootcamp
Waiting for rollout to finish: 3 of 4 updated replicas are available...
deployment "kubernetes-bootcamp" successfully rolled out

kubectl describe deploy
该命令可以查看image的版本变为v2

回退更新

kubectl rollout undo deploy kubernetes-bootcamp

通过配置文件来管理
/root/services/nginx-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
    - name: nginx
      image: nginx:1.7.9
      ports:
      - containerPort: 80

执行命令创建nginx pod
kubectl create -f /root/services/nginx-pod.yaml

root@server02:~/services# kubectl create -f /root/services/nginx-pod.yaml 
pod "nginx" created

查看;nginx在创建中ContainerCreating

root@server02:~/services# kubectl get pods
NAME                                   READY     STATUS              RESTARTS   AGE
kubernetes-bootcamp-7689dc585d-ltcrd   1/1       Running             0          19m
kubernetes-bootcamp-7689dc585d-psrsf   1/1       Running             0          19m
nginx                                  0/1       ContainerCreating   0          15s

访问验证
开启一个kubectl proxy来访问:验证返回nginx的欢迎页,验证成功

root@server02:~# curl http://localhost:8001/api/v1/proxy/namespaces/default/pods/nginx/
<!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>

创建一个nginx的deployment
/root/services/nginx-deployment.yaml

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  template:
    metadata: 
      labels: 
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
          - containerPort: 80
root@server02:~/services# kubectl create -f /root/services/nginx-deployment.yaml 
deployment "nginx-deployment" created
root@server02:~/services# 
root@server02:~/services# kubectl get deploy
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   2         2         2            2           1h
nginx-deployment      2         2         2            1           15s

ContainerCreating中

root@server02:~/services# kubectl get pods
NAME                                   READY     STATUS              RESTARTS   AGE
kubernetes-bootcamp-7689dc585d-ltcrd   1/1       Running             0          34m
kubernetes-bootcamp-7689dc585d-psrsf   1/1       Running             0          34m
nginx                                  1/1       Running             0          15m
nginx-deployment-6c54bd5869-44ndp      1/1       Running             0          1m
nginx-deployment-6c54bd5869-6nxww      0/1       ContainerCreating   0          1m
root@server02:~/services# 
root@server02:~/services# kubectl get pods -l app=nginx
NAME                                READY     STATUS              RESTARTS   AGE
nginx-deployment-6c54bd5869-44ndp   1/1       Running             0          2m
nginx-deployment-6c54bd5869-6nxww   0/1       ContainerCreating   0          2m

两个都运行起来了

root@server02:~/services# kubectl get pods -l app=nginx
NAME                                READY     STATUS    RESTARTS   AGE
nginx-deployment-6c54bd5869-44ndp   1/1       Running   0          4m
nginx-deployment-6c54bd5869-6nxww   1/1       Running   0          4m

kube-proxy
安装完成后,kubectl get services查看有哪些服务

root@server01:~# kubectl get services
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.68.0.1    <none>        443/TCP   3h

查看这个service的详细说明

kubectl describe service kubernetes
root@server01:~# kubectl describe service kubernetes
Name:              kubernetes
Namespace:         default
Labels:            component=apiserver
                   provider=kubernetes
Annotations:       <none>
Selector:          <none>
Type:              ClusterIP
IP:                10.68.0.1
Port:              https  443/TCP
TargetPort:        6443/TCP
Endpoints:         192.168.174.136:6443
Session Affinity:  ClientIP
Events:            <none>

kube-proxy有什么方便
kubectl expose deploy kubernetes-bootcamp --type="NodePort" --target-port=8080 --port=80

root@server01:~# kubectl expose deploy kubernetes-bootcamp --type="NodePort" --target-port=8080 --port=80
service "kubernetes-bootcamp" exposed

此时,多了一个NodePort类型的service

root@server01:~# kubectl get services
NAME                  TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
kubernetes            ClusterIP   10.68.0.1     <none>        443/TCP        3h
kubernetes-bootcamp   NodePort    10.68.70.33   <none>        80:34824/TCP   37s

创建这个service的时候有3个端口:8080/80/34824
34824这个端口是kube-proxy在node上实际启动的一个端口,node节点可以通过这个端口来访问服务
target-port 8080是当前这个容器提供服务的端口
port 80 这个端口是虚拟ip也就是10.68.70.33这个cluster-ip上访问的时候需要的端口

1 如,在node节点135和和137上通过34824访问;因136节点没有安装kube-proxy所以没有启动这个端口

root@server01:~# curl 192.168.174.135:34824
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7689dc585d-ltcrd | v=2
root@server01:~# 
root@server01:~# 
root@server01:~# curl 192.168.174.137:34824
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7689dc585d-psrsf | v=2
root@server01:~# 
root@server01:~# curl 192.168.174.136:34824
curl: (7) Failed to connect to 192.168.174.136 port 34824: Connection refused

2 通过cluster-ip访问

root@server01:~# curl 10.68.70.33
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7689dc585d-pk4qm | v=2
root@server01:~#

3 在容器中通过容器本身启动的端口8080访问
3.1 查出这个pod在137节点

root@server02:~# kubectl get pod -owide
NAME                                   READY     STATUS    RESTARTS   AGE       IP              NODE
kubernetes-bootcamp-7689dc585d-pk4qm   1/1       Running   0          1h        172.20.40.244   192.168.174.137

3.2 docker ps查出容器id

root@server03:~# docker ps  | grep kubernetes-bootcamp-7689dc585d-pk4qm
cef9f1540009        jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5   "/bin/sh -c 'node ..."   About an hour ago   Up About an hour                             k8s_kubernetes-bootcamp_kubernetes-bootcamp-7689dc585d-pk4qm_default_d7646026-7a0c-11e9-b4a4-000c2949bdee_0

3.3 在137节点通过docker exec进入容器,并通过cluster-ip访问pod服务

root@server03:~# docker exec -it cef9f1540009 bash
root@kubernetes-bootcamp-7689dc585d-pk4qm:/#
root@kubernetes-bootcamp-7689dc585d-pk4qm:/# curl 10.68.70.33
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7689dc585d-pk4qm | v=2
root@kubernetes-bootcamp-7689dc585d-pk4qm:/#

指定NodePort
nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  ports:
  - port: 8080
    targetPort: 80
    nodePort: 20000
  selector:
    app: nginx
  type: NodePort
root@server02:~/services# kubectl create -f /root/services/nginx-service.yaml 
service "nginx-service" created
root@server02:~/services# kubectl get services
NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes            ClusterIP   10.68.0.1       <none>        443/TCP          3h
kubernetes-bootcamp   NodePort    10.68.70.33     <none>        80:34824/TCP     11m
nginx-service         NodePort    10.68.121.152   <none>        8080:20000/TCP   1m

service简写svc

root@server02:~/services# kubectl get svc
NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes            ClusterIP   10.68.0.1       <none>        443/TCP          3h
kubernetes-bootcamp   NodePort    10.68.70.33     <none>        80:34824/TCP     12m
nginx-service         NodePort    10.68.121.152   <none>        8080:20000/TCP   1m

访问验证

root@server01:~# curl 192.168.174.135:20000
<!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>

kube-dns
kube-dns是在kube-system这个namespace中
在master节点执行

root@server02:~# kubectl -n kube-system get svc
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)         AGE
kube-dns   ClusterIP   10.68.0.2    <none>        53/UDP,53/TCP   237d

root@server02:~# kubectl -n kube-system get pod -owide
NAME                        READY     STATUS    RESTARTS   AGE       IP            NODE
kube-dns-64b968b6d9-8ftks   3/3       Running   27         231d      172.20.6.63   192.168.174.136

root@server02:~# kubectl -n kube-system get deploy
NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kube-dns   1         1         1            1           237d

通过名字来访问pod服务
获取service

root@server02:~# kubectl get svc
NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes-bootcamp   NodePort    10.68.70.33     <none>        80:34824/TCP     237d

获取pod与运行的node节点192.168.174.137

root@server02:~# kubectl get pod -owide
NAME                                   READY     STATUS    RESTARTS   AGE       IP              NODE
kubernetes-bootcamp-7689dc585d-pk4qm   1/1       Running   0          2h        172.20.40.244   192.168.174.137

在192.168.174.137节点进入容器中并通过service name访问


root@server03:~# docker ps  | grep kubernetes-bootcamp-7689dc585d-pk4qm
cef9f1540009        jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5   "/bin/sh -c 'node ..."   About an hour ago   Up About an hour                             k8s_kubernetes-bootcamp_kubernetes-bootcamp-7689dc585d-pk4qm_default_d7646026-7a0c-11e9-b4a4-000c2949bdee_0

root@server03:~# docker exec -it cef9f1540009 bash
root@kubernetes-bootcamp-7689dc585d-pk4qm:/#  curl kubernetes-bootcamp
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7689dc585d-pk4qm | v=2
root@kubernetes-bootcamp-7689dc585d-pk4qm:/#

这个容器的dns配置
通过10.68.0.2这个nameserver解析的

root@server03:~# docker exec -it cef9 bash
root@kubernetes-bootcamp-7689dc585d-pk4qm:/# cat /etc/resolv.conf
nameserver 10.68.0.2
search default.svc.cluster.local. svc.cluster.local. cluster.local. localdomain
options ndots:5

10.68.0.2这个地址解释kube-dns的cluster-ip

root@server02:~# kubectl -n kube-system get svc
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)         AGE
kube-dns   ClusterIP   10.68.0.2    <none>        53/UDP,53/TCP   237d
root@server03:~# cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 192.168.174.2
search localdomain

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值