技术流ken

运维拯救世界

课堂记录:k8s控制器job、cj及service讲解(三)–技术流ken

控制器讲解job

 

第一步:编写job的yml文件

apiVersion: batch/v1
kind: Job
metadata:
  name: job
spec:
  template:
    spec:
      containers:
      - name: job
        image: busybox
        command: ["echo","Hello World"]
        imagePullPolicy: IfNotPresent
      restartPolicy: Never

 

第二步:执行yml文件

[root@ken1 ~]# kubectl apply -f job.yml 

 

第三步:查看job

[root@ken1 ~]# kubectl get job
NAME   COMPLETIONS   DURATION   AGE
job    1/1           2s         22s

 

第四步:查看日志输出

[root@ken1 ~]# kubectl logs job-twctr 
Hello World

 

 

演示:如果job失败会发生什么?

会发现会自动生成很多的pod

为什么会出现这样的情况那?

因为在job中期待完成数是1.但是因为job内部有错误,无法完成相应的任务,而且重启策略是Never,表示容器从来不会重启,所以系统为了完成工作,就会一直开启新的容器,试图去完成自己的工作。

 

 

如果重启策略为OnFailure的话,pod会一直重启!

 

 

注意:

restartPolicy 表示pod重启策略

 

Always: 这个是默认值,pod退出就会自动重启

Never: 这个表示容器退出不会重启

OnFailure: 表示容器再失败退出的时候才会重启,容器正常退出不会重启

 

 

imagePullPolicy 表示镜像拉取的策略

 

Always: 这个是默认值,表示每次执行yml文件都会去拉取dockerhub中的镜像

Never: 总是不拉取镜像

IfNotPresent: 本地不存在才会去拉取镜像

 

 

cronjob演示

 

第一步:编写cj的yml文件

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cj
spec:
  schedule: "* * * * *"
  jobTemplate:
    metadata:
     name: job
    spec:
      template:
        metadata:
            name: pod
        spec:
          containers:
          - name: busybox
            image: busybox
            command: ["echo","hello"]
           # args:
           #  - /bin/sh
           #  - c
           #  - echo "hello wordld"
            imagePullPolicy: IfNotPresent
          restartPolicy: OnFailure

 

第二步:执行yml文件

[root@ken1 ~]# kubectl apply -f cj.yml 

 

第三步:查看计划任务

[root@ken1 ~]# kubectl get cj
NAME   SCHEDULE    SUSPEND   ACTIVE   LAST SCHEDULE   AGE
cj     * * * * *   False     0        24s             33s

 

第四步:查看job

[root@ken1 ~]# kubectl get job
NAME            COMPLETIONS   DURATION   AGE
cj-1566353280   1/1           3s         39s

 

第五步:查看pod

[root@ken1 ~]# kubectl get po
NAME                  READY   STATUS      RESTARTS   AGE
cj-1566353280-m7gpj   0/1     Completed   0          79s
cj-1566353340-j6fc4   0/1     Completed   0          19s

 

第六步:查看pod日志输出

[root@ken1 ~]# kubectl logs cj-1566353340-j6fc4
hello

 

 

第二版:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cj
spec:
  schedule: "* * * * *"
  jobTemplate:
    metadata:
     name: job
    spec:
      template:
        metadata:
            name: pod
        spec:
          restartPolicy: OnFailure
          containers:
          - name: busybox
            imagePullPolicy: IfNotPresent
            image: busybox
           # command: ["echo","hello"]
            args:
             - /bin/sh
             - c
             - echo "hello wordld"

 

 

 

演示service

 

第一步:编写部署nginx的yml文件

[root@ken1 ~]# cat nginx-svc.yml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  template:
   metadata:
    labels:
     run: nginx
   spec:
     restartPolicy: OnFailure
     containers:
     - name: nginx
       image: nginx
       imagePullPolicy: IfNotPresent

 

第二步:执行yml文件

[root@ken1 ~]# kubectl apply -f nginx-svc.yml 

 

第三步:查看标签

[root@ken1 ~]# kubectl get po --show-labels
NAME                     READY   STATUS    RESTARTS   AGE   LABELS
nginx-5c6666d499-hpg65   1/1     Running   0          37s   pod-template-hash=5c6666d499,run=nginx
nginx-5c6666d499-s9txs   1/1     Running   0          37s   pod-template-hash=5c6666d499,run=nginx

 

 

第四步:外部访问nginx服务

无法访问!

 

编写svc

 

第一步:编写svc的yaml文件

apiVersion: v1
kind: Service
metadata:
  name: my-svc
spec:
  selector:
   run: nginx
  ports:
   - port: 80  #svc的port
     targetPort: 80  #是pod的port

 

第二步:运行svcyml文件

[root@ken1 ~]# kubectl apply -f svc.yml

 

第三步:查看svc

[root@ken1 ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   5d17h
my-svc       ClusterIP   10.106.124.174   <none>        80/TCP    19s

 

第四步:查看svc详细信息

[root@ken1 ~]# kubectl describe svc my-svc
Name:              my-svc
Namespace:         default
Labels:            <none>
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"my-svc","namespace":"default"},"spec":{"ports":[{"port":80,"targe...
Selector:          run=nginx
Type:              ClusterIP
IP:                10.106.124.174
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.1.13:80,10.244.2.79:80
Session Affinity:  None
Events:            <none>

 

 

补充:在deploy当中的pod中暴露端口

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  template:
   metadata:
    labels:
     run: nginx
   spec:
     restartPolicy: Always
     containers:
     - name: nginx
       image: nginx
       imagePullPolicy: IfNotPresent
       ports:
       - containerPort: 80

 

 

 

yml文件合并

 

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  template:
   metadata:
    labels:
     run: nginx
   spec:
     restartPolicy: Always
     containers:
     - name: nginx
       image: nginx
       imagePullPolicy: IfNotPresent
       ports:
       - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: my-svc
spec:
  selector:
   run: nginx
  ports:
   - port: 80  #svc的port
     targetPort: 80  #是pod的port

 

 

DNS访问

 

第一步:启动busyboxpod

[root@ken1 ~]# kubectl run busybox -it --rm --image=busybox /bin/sh

 

第二步:通过名称访问svc

/ # wget my-svc:80
Connecting to my-svc:80 (10.99.81.124:80)
saving to 'index.html'
index.html           100% |*************************************************************************|   612  0:00:00 ETA
'index.html' saved
/ # cat index.html 
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body 

 

 

注意:

1.通过名称访问,只能是在同一名称空间的!

2.如果不是在同一名称空间,需要指定namespace

 

 

第一步:修改yml文件

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
  namespace: kube-system
spec:
  replicas: 2
  template:
   metadata:
    labels:
     run: nginx
   spec:
     restartPolicy: Always
     containers:
     - name: nginx
       image: nginx
       imagePullPolicy: IfNotPresent
       ports:
       - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: my-svc
  namespace: kube-system
spec:
  selector:
   run: nginx
  ports:
   - port: 80  #svc的port
     targetPort: 80  #是pod的port

 

第二步:查看

[root@ken1 ~]# kubectl get svc -n kube-system
NAME       TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP,9153/TCP   5d17h
my-svc     ClusterIP   10.100.60.114   <none>        80/TCP                   2m6s

 

第三步:访问

[root@ken1 ~]# kubectl run busybox -it --rm --image=busybox /bin/sh
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.
If you don't see a command prompt, try pressing enter.
/ # wget my-svc:80
wget: bad address 'my-svc:80'

 

第四步:指定名称空间进行访问

/ # wget my-svc.kube-system:80
Connecting to my-svc.kube-system:80 (10.100.60.114:80)
saving to 'index.html'
index.html           100% |*************************************************************************|   612  0:00:00 ETA
'index.html' saved

 

第五步:解决办法也可以在kube-system名称空间进行运行busybox

[root@ken1 ~]# kubectl run busybox1 -it --rm --image=busybox -n kube-system /bin/sh 
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.
If you don't see a command prompt, try pressing enter.
/ # wget my-svc:80
Connecting to my-svc:80 (10.100.60.114:80)
saving to 'index.html'
index.html           100% |*************************************************************************|   612  0:00:00 ETA
'index.html' saved

 

 

外网如何访问pod?

 

 

外部如何访问service?

 

  1. clusterIP
  2. NodePort
  3. LoadBalance
  4. ExternalName

 

第一步:编写svc的yml文件

apiVersion: v1
kind: Service
metadata:
  name: my-svc
spec:
  selector:
   run: nginx
  type: NodePort
  ports:
   - port: 80  #svc的port
     targetPort: 80  #是pod的port

 

第二步:执行yml文件

[root@ken1 ~]# kubectl apply -f nginx-svc.yml 

 

第三步:查看svc

[root@ken1 ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        5d17h
my-svc       NodePort    10.96.194.243   <none>        80:32247/TCP   4s

 

第四步:查看宿主机端口

[root@ken1 ~]# ss -tnl | grep 32247
LISTEN     0      128         :::32247                   :::*                  

 

第五步:外部访问

可以访问!

 

固定宿主机端口

端口需要在30000-32767

第一步:修改svc的yml文件

apiVersion: v1
kind: Service
metadata:
  name: my-svc
spec:
  selector:
   run: nginx
  type: NodePort
  ports:
   - port: 80  #svc的port
     targetPort: 80  #是pod的port
     nodePort: 30007

 

第二步:查看svc

[root@ken1 ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        5d17h
my-svc       NodePort    10.107.106.116   <none>        80:30007/TCP   43s

 

第三步:外部访问

http://192.168.64.11:30007/

 

几个端口表示的含义

port: 表示svc的端口

targetPort: pod的端口

nodePort: 需要映射到宿主机的端口

 

 

10 thoughts on “课堂记录:k8s控制器job、cj及service讲解(三)–技术流ken

  1. I simply wished to thank you very much once again. I am not sure the things I could possibly have accomplished without the recommendations documented by you regarding that topic. It was actually a real hard scenario in my position, however , seeing a skilled way you handled it forced me to cry with fulfillment. I will be thankful for the assistance and even hope that you really know what a great job you happen to be doing training many people all through your web blog. I am certain you haven’t encountered all of us.

  2. I have to convey my admiration for your kind-heartedness for those who require help with that study. Your special dedication to passing the solution throughout had been wonderfully productive and has truly enabled associates much like me to achieve their goals. Your new informative guidelines entails a lot a person like me and even further to my peers. Thanks a ton; from all of us.

  3. I intended to create you the little bit of word to say thank you yet again with the great pointers you have provided on this site. This is really open-handed of you to deliver freely what most of us would’ve advertised as an e book to help make some money on their own, mostly considering the fact that you might well have tried it if you ever desired. These advice in addition worked like the good way to know that other people online have a similar desire just like mine to find out whole lot more with reference to this issue. I’m sure there are many more pleasurable situations ahead for people who take a look at your blog.

  4. I actually wanted to write a quick message in order to express gratitude to you for the awesome information you are giving out here. My considerable internet research has at the end been rewarded with wonderful strategies to write about with my pals. I ‘d declare that we visitors are undoubtedly fortunate to exist in a useful community with so many special people with great points. I feel quite fortunate to have seen your entire web page and look forward to some more pleasurable times reading here. Thanks once again for all the details.

  5. I needed to write you a very small word just to give many thanks once again on your precious tactics you have discussed above. It’s really pretty open-handed of you to convey freely precisely what many of us might have advertised for an ebook to generate some cash for their own end, specifically given that you could possibly have tried it in the event you desired. The basics as well served as a great way to know that other people have the same eagerness just like my very own to know great deal more with reference to this problem. I know there are many more enjoyable sessions up front for many who read your site.

  6. I have to show some appreciation to this writer for bailing me out of this type of challenge. Just after surfing throughout the world wide web and finding solutions that were not powerful, I thought my entire life was over. Being alive minus the approaches to the problems you’ve sorted out by way of your short article is a critical case, as well as the ones that could have in a negative way affected my career if I hadn’t encountered your website. Your own competence and kindness in controlling all the details was important. I don’t know what I would’ve done if I had not discovered such a point like this. I can also at this time look forward to my future. Thanks for your time very much for the impressive and result oriented help. I will not be reluctant to propose your blog to anybody who wants and needs care about this topic.

  7. I simply wished to thank you very much all over again. I do not know the things that I would have done in the absence of the actual solutions shared by you concerning this problem. This was the distressing circumstance in my opinion, but viewing a new expert avenue you treated it made me to weep over contentment. I’m just happier for the advice and in addition sincerely hope you recognize what a powerful job you have been undertaking educating the mediocre ones through the use of a web site. Probably you have never got to know any of us.

  8. I must show appreciation to the writer for rescuing me from this incident. As a result of exploring through the search engines and meeting notions which are not helpful, I thought my entire life was done. Being alive without the approaches to the difficulties you’ve resolved all through your good blog post is a serious case, and the ones which could have badly affected my career if I had not encountered your web page. Your primary mastery and kindness in controlling all the details was tremendous. I’m not sure what I would’ve done if I had not encountered such a thing like this. I am able to at this time look ahead to my future. Thanks a lot very much for this skilled and sensible help. I will not think twice to propose your web blog to anybody who ought to have support about this situation.

  9. I am only writing to let you understand what a helpful discovery my friend’s princess went through going through the blog. She even learned several issues, including how it is like to possess an excellent teaching nature to get other individuals without problems know a variety of complex matters. You undoubtedly did more than people’s expectations. I appreciate you for imparting these essential, trustworthy, revealing not to mention fun guidance on that topic to Jane.

发表评论

邮箱地址不会被公开。