K8s入门

一、部署一个tomcat

kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8

Kubectl get pods -o wide 可以获取到tomcat 信息

 Master节点

[root@k8s-node1 k8s]# kubectl get nodes
NAME        STATUS   ROLES    AGE     VERSION
k8s-node1   Ready    master   24m     v1.17.3
k8s-node2   Ready    <none>   5m3s    v1.17.3
k8s-node3   Ready    <none>   4m16s   v1.17.3
[root@k8s-node1 k8s]# watch kubectl get pod -n kube-system -o wide
[root@k8s-node1 k8s]# clear
[root@k8s-node1 k8s]# kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8
deployment.apps/tomcat6 created
[root@k8s-node1 k8s]# kubectl get all
NAME                           READY   STATUS              RESTARTS   AGE
pod/tomcat6-5f7ccf4cb9-krbrq   0/1     ContainerCreating   0          8s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   34m

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/tomcat6   0/1     1            0           8s

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/tomcat6-5f7ccf4cb9   1         1         0       8s
[root@k8s-node1 k8s]# kubectl get all -o wide
NAME                           READY   STATUS    RESTARTS   AGE   IP           NODE        NOMINATED NODE   READINESS GATES
pod/tomcat6-5f7ccf4cb9-krbrq   1/1     Running   0          58s   10.244.2.2   k8s-node3   <none>           <none>

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE   SELECTOR
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   35m   <none>

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES               SELECTOR
deployment.apps/tomcat6   1/1     1            1           58s   tomcat       tomcat:6.0.53-jre8   app=tomcat6

NAME                                 DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES               SELECTOR
replicaset.apps/tomcat6-5f7ccf4cb9   1         1         1       58s   tomcat       tomcat:6.0.53-jre8   app=tomcat6,pod-template-hash=5f7ccf4cb9
[root@k8s-node1 k8s]# kubectl get all
NAME                           READY   STATUS    RESTARTS   AGE
pod/tomcat6-5f7ccf4cb9-krbrq   1/1     Running   0          2m12s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   36m

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/tomcat6   1/1     1            1           2m12s

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/tomcat6-5f7ccf4cb9   1         1         1       2m12s
[root@k8s-node1 k8s]# kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
tomcat6-5f7ccf4cb9-krbrq   1/1     Running   0          2m20s
[root@k8s-node1 k8s]# kubectl get pods --all-namespaces
NAMESPACE     NAME                                READY   STATUS    RESTARTS   AGE
default       tomcat6-5f7ccf4cb9-krbrq            1/1     Running   0          2m56s
kube-system   coredns-7f9c544f75-5v7dn            1/1     Running   0          37m
kube-system   coredns-7f9c544f75-6jshq            1/1     Running   0          37m
kube-system   etcd-k8s-node1                      1/1     Running   0          37m
kube-system   kube-apiserver-k8s-node1            1/1     Running   0          37m
kube-system   kube-controller-manager-k8s-node1   1/1     Running   0          37m
kube-system   kube-flannel-ds-amd64-m64kw         1/1     Running   0          26m
kube-system   kube-flannel-ds-amd64-qdbgp         1/1     Running   0          17m
kube-system   kube-flannel-ds-amd64-rxp6p         1/1     Running   0          18m
kube-system   kube-proxy-jssns                    1/1     Running   0          18m
kube-system   kube-proxy-smq7v                    1/1     Running   0          17m
kube-system   kube-proxy-xc5dv                    1/1     Running   0          37m
kube-system   kube-scheduler-k8s-node1            1/1     Running   0          37m
[root@k8s-node1 k8s]# kubectl get pods -o wide
NAME                       READY   STATUS    RESTARTS   AGE     IP           NODE        NOMINATED NODE   READINESS GATES
tomcat6-5f7ccf4cb9-krbrq   1/1     Running   0          3m27s   10.244.2.2   k8s-node3   <none>           <none>

Node3节点

[root@k8s-node3 ~]# docker images
REPOSITORY                                                       TAG             IMAGE ID       CREATED         SIZE
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy   v1.17.3         ae853e93800d   13 months ago   116MB
quay.io/coreos/flannel                                           v0.11.0-amd64   ff281650a721   2 years ago     52.6MB
registry.cn-hangzhou.aliyuncs.com/google_containers/pause        3.1             da86e6ba6ca1   3 years ago     742kB
tomcat                                                           6.0.53-jre8     49ab0583115a   3 years ago     290MB
[root@k8s-node3 ~]# docker ps
CONTAINER ID   IMAGE                                                            COMMAND                  CREATED              STATUS              PORTS     NAMES
9e464c0ceafe   tomcat                                                           "catalina.sh run"        About a minute ago   Up About a minute             k8s_tomcat_tomcat6-5f7ccf4cb9-krbrq_default_d5fb7ba9-7dc4-4670-9744-b60179fcebb9_0
a70124c3f8c8   registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1    "/pause"                 About a minute ago   Up About a minute             k8s_POD_tomcat6-5f7ccf4cb9-krbrq_default_d5fb7ba9-7dc4-4670-9744-b60179fcebb9_0
f059af31e3aa   ff281650a721                                                     "/opt/bin/flanneld -…"   15 minutes ago       Up 15 minutes                 k8s_kube-flannel_kube-flannel-ds-amd64-qdbgp_kube-system_98e8e290-493a-4d2f-8243-26c93252fa03_0
2817862e4196   registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy   "/usr/local/bin/kube…"   15 minutes ago       Up 15 minutes                 k8s_kube-proxy_kube-proxy-smq7v_kube-system_287e2601-d349-496f-a8db-148ba156cb9c_0
d8aefe1016d3   registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1    "/pause"                 15 minutes ago       Up 15 minutes                 k8s_POD_kube-flannel-ds-amd64-qdbgp_kube-system_98e8e290-493a-4d2f-8243-26c93252fa03_0
59ab4a9b0d76   registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1    "/pause"                 15 minutes ago       Up 15 minutes                 k8s_POD_kube-proxy-smq7v_kube-system_287e2601-d349-496f-a8db-148ba156cb9c_0
[root@k8s-node3 ~]# docker stop 9e464c0ceafe
9e464c0ceafe
[root@k8s-node3 ~]# 

二、暴露Tomcat访问

Pod 的80 映射容器的8080;service 会代理Pod 的80

kubectl expose deployment tomcat6 --port=80 --target-port=8080 --type=NodePort

kubectl get svc

kubectl get svc -o wide

三、动态扩容测试

kubectl get deployment

扩容,扩容了多份,所以无论访问哪个node 的指定端口,都可以访问到tomcat6
kubectl scale --replicas=3 deployment tomcat6

kubectl get pods -o wide

kubectl get svc -o wide

删除pod

Kubectl get all

kubectl delete deployment.apps/tomcat6

kubectl delete service/tomcat6

流程:创建deployment会管理replicas,replicas控制pod数量,有pod故障会自动拉起新的pod

四、yam基本使用

五、Pod和控制器

K8s文档

六、部署service

6.1 部署一个nginx

kubectl create deployment nginx --image=nginx

6.2 暴露nginx 访问

kubectl expose deployment nginx --port=80 --type=NodePort

统一应用访问入口;

Service 管理一组Pod

防止Pod 失联(服务发现)、定义一组Pod 的访问策略

现在Service 我们使用NodePort 的方式暴露,这样访问每个节点的端口,都可以访问到这个Pod,如果节点宕机,就会出现问题。

[root@k8s-node1 k8s]# kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8 --dry-run -o yaml > tomcat6-deployment.yaml
[root@k8s-node1 k8s]# vi tomcat6-deployment.yaml 
[root@k8s-node1 k8s]# kubectl expose deployment tomcat6 --port=80 --target-port=8080 --type=NodePort --dry-run -o yaml
Error from server (NotFound): deployments.apps "tomcat6" not found
[root@k8s-node1 k8s]# kubectl apply -f tomcat6-deployment.yaml 
deployment.apps/tomcat6 created
[root@k8s-node1 k8s]# kubectl expose deployment tomcat6 --port=80 --target-port=8080 --type=NodePort --dry-run -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: tomcat6
  name: tomcat6
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: tomcat6
  type: NodePort
status:
  loadBalancer: {}
[root@k8s-node1 k8s]# vi tomcat6-deployment.yaml 
[root@k8s-node1 k8s]# cat tomcat6-deployment.yaml 
#部署
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: tomcat6
  name: tomcat6
spec:
  replicas: 3
  selector:
    matchLabels:
      app: tomcat6
  template:
    metadata:
      labels:
        app: tomcat6
    spec:
      containers:
      - image: tomcat:6.0.53-jre8
        name: tomcat
---
#暴露
apiVersion: v1
kind: Service
metadata:
  labels:
    app: tomcat6
  name: tomcat6
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: tomcat6
  type: NodePort

[root@k8s-node1 k8s]# kubectl get all
NAME                           READY   STATUS    RESTARTS   AGE
pod/tomcat6-5f7ccf4cb9-5hpbn   1/1     Running   0          3m7s
pod/tomcat6-5f7ccf4cb9-jqmh6   1/1     Running   0          3m7s
pod/tomcat6-5f7ccf4cb9-pzvwn   1/1     Running   0          3m7s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   12h

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/tomcat6   3/3     3            3           3m7s

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/tomcat6-5f7ccf4cb9   3         3         3       3m7s
[root@k8s-node1 k8s]# kubectl delete deployment.apps/tomcat6
deployment.apps "tomcat6" deleted
[root@k8s-node1 k8s]# kubectl apply -f tomcat6-deployment.yaml 
deployment.apps/tomcat6 created
service/tomcat6 created
[root@k8s-node1 k8s]# kubectl get all
NAME                           READY   STATUS    RESTARTS   AGE
pod/tomcat6-5f7ccf4cb9-77j4m   1/1     Running   0          22s
pod/tomcat6-5f7ccf4cb9-hdb87   1/1     Running   0          22s
pod/tomcat6-5f7ccf4cb9-zfjft   1/1     Running   0          22s

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        12h
service/tomcat6      NodePort    10.96.102.232   <none>        80:30723/TCP   22s

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/tomcat6   3/3     3            3           23s

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/tomcat6-5f7ccf4cb9   3         3         3       23s

七、部署Ingress

通过Service 发现Pod 进行关联。基于域名访问。

通过Ingress Controller 实现Pod 负载均衡

支持TCP/UDP 4 层负载均衡和HTTP 7 层负载均衡

视频教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值